comparison src/org/tmatesoft/hg/internal/RepositoryComparator.java @ 423:9c9c442b5f2e

Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 23 Mar 2012 22:51:18 +0100
parents 994b5813a925
children 48f993aa2f41
comparison
equal deleted inserted replaced
422:5d1cc7366d04 423:9c9c442b5f2e
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import static org.tmatesoft.hg.core.Nodeid.NULL; 19 import static org.tmatesoft.hg.core.Nodeid.NULL;
20 20
21 import java.util.ArrayList; 21 import java.util.ArrayList;
22 import java.util.Arrays;
22 import java.util.Collections; 23 import java.util.Collections;
23 import java.util.HashMap; 24 import java.util.HashMap;
24 import java.util.HashSet; 25 import java.util.HashSet;
25 import java.util.LinkedList; 26 import java.util.LinkedList;
26 import java.util.List; 27 import java.util.List;
27 import java.util.ListIterator; 28 import java.util.ListIterator;
28 import java.util.Map; 29 import java.util.Map;
29 import java.util.Map.Entry; 30 import java.util.Map.Entry;
30 import java.util.Set; 31 import java.util.Set;
31 32
32 import org.tmatesoft.hg.core.HgBadStateException;
33 import org.tmatesoft.hg.core.HgInvalidControlFileException;
34 import org.tmatesoft.hg.core.HgRemoteConnectionException; 33 import org.tmatesoft.hg.core.HgRemoteConnectionException;
35 import org.tmatesoft.hg.core.Nodeid; 34 import org.tmatesoft.hg.core.Nodeid;
36 import org.tmatesoft.hg.repo.HgChangelog; 35 import org.tmatesoft.hg.repo.HgChangelog;
36 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
37 import org.tmatesoft.hg.repo.HgInvalidStateException;
37 import org.tmatesoft.hg.repo.HgRemoteRepository; 38 import org.tmatesoft.hg.repo.HgRemoteRepository;
38 import org.tmatesoft.hg.repo.HgRemoteRepository.Range; 39 import org.tmatesoft.hg.repo.HgRemoteRepository.Range;
39 import org.tmatesoft.hg.repo.HgRemoteRepository.RemoteBranch; 40 import org.tmatesoft.hg.repo.HgRemoteRepository.RemoteBranch;
40 import org.tmatesoft.hg.util.CancelSupport; 41 import org.tmatesoft.hg.util.CancelSupport;
41 import org.tmatesoft.hg.util.CancelledException; 42 import org.tmatesoft.hg.util.CancelledException;
63 progressSupport.start(10); 64 progressSupport.start(10);
64 common = Collections.unmodifiableList(findCommonWithRemote()); 65 common = Collections.unmodifiableList(findCommonWithRemote());
65 // sanity check 66 // sanity check
66 for (Nodeid n : common) { 67 for (Nodeid n : common) {
67 if (!localRepo.knownNode(n)) { 68 if (!localRepo.knownNode(n)) {
68 throw new HgBadStateException("Unknown node reported as common:" + n); 69 throw new HgInvalidStateException("Unknown node reported as common:" + n);
69 } 70 }
70 } 71 }
71 progressSupport.done(); 72 progressSupport.done();
72 return this; 73 return this;
73 } 74 }
74 75
75 public List<Nodeid> getCommon() { 76 public List<Nodeid> getCommon() {
76 if (common == null) { 77 if (common == null) {
77 throw new HgBadStateException("Call #compare(Object) first"); 78 throw new HgInvalidStateException("Call #compare(Object) first");
78 } 79 }
79 return common; 80 return common;
80 } 81 }
81 82
82 /** 83 /**
118 // but check for possible wrong repo comparison (hs says 'repository is unrelated' if I try to 119 // but check for possible wrong repo comparison (hs says 'repository is unrelated' if I try to
119 // check in/out for a repo that has no common nodes. 120 // check in/out for a repo that has no common nodes.
120 return; 121 return;
121 } 122 }
122 if (earliestRevision < 0 || earliestRevision >= changelog.getLastRevision()) { 123 if (earliestRevision < 0 || earliestRevision >= changelog.getLastRevision()) {
123 throw new HgBadStateException(String.format("Invalid index of common known revision: %d in total of %d", earliestRevision, 1+changelog.getLastRevision())); 124 throw new HgInvalidStateException(String.format("Invalid index of common known revision: %d in total of %d", earliestRevision, 1+changelog.getLastRevision()));
124 } 125 }
125 changelog.range(earliestRevision+1, changelog.getLastRevision(), inspector); 126 changelog.range(earliestRevision+1, changelog.getLastRevision(), inspector);
126 } 127 }
127 128
128 private List<Nodeid> findCommonWithRemote() throws HgRemoteConnectionException { 129 private List<Nodeid> findCommonWithRemote() throws HgRemoteConnectionException {
315 break; 316 break;
316 } 317 }
317 } 318 }
318 } while(--watchdog > 0); 319 } while(--watchdog > 0);
319 if (watchdog == 0) { 320 if (watchdog == 0) {
320 throw new HgBadStateException(String.format("Can't narrow down branch [%s, %s]", rb.head.shortNotation(), rb.root.shortNotation())); 321 throw new HgInvalidStateException(String.format("Can't narrow down branch [%s, %s]", rb.head.shortNotation(), rb.root.shortNotation()));
321 } 322 }
322 } 323 }
323 if (debug) { 324 if (debug) {
324 System.out.println("calculateMissingBranches:"); 325 System.out.println("calculateMissingBranches:");
325 for (BranchChain bc : branches2load) { 326 for (BranchChain bc : branches2load) {
484 rangeToEntry.clear(); 485 rangeToEntry.clear();
485 } 486 }
486 toQuery.clear(); 487 toQuery.clear();
487 } 488 }
488 if (rootIndex == -1) { 489 if (rootIndex == -1) {
489 throw new HgBadStateException("Shall not happen, provided between output is correct"); // FIXME EXCEPTIONS 490 throw new HgInvalidStateException("Shall not happen, provided between output is correct"); // FIXME EXCEPTIONS
490 } 491 }
491 result[rootIndex] = branchRoot; 492 result[rootIndex] = branchRoot;
492 boolean resultOk = true; 493 boolean resultOk = true;
493 LinkedList<Nodeid> fromRootToHead = new LinkedList<Nodeid>(); 494 LinkedList<Nodeid> fromRootToHead = new LinkedList<Nodeid>();
495 IntVector missing = new IntVector();
494 for (int i = 0; i <= rootIndex; i++) { 496 for (int i = 0; i <= rootIndex; i++) {
495 Nodeid n = result[i]; 497 Nodeid n = result[i];
496 if (n == null) { 498 if (n == null) {
497 System.out.printf("ERROR: element %d wasn't found\n",i); 499 missing.add(i);
498 resultOk = false; 500 resultOk = false;
499 } 501 }
500 fromRootToHead.addFirst(n); // reverse order 502 fromRootToHead.addFirst(n); // reverse order
501 } 503 }
502 if (debug) { 504 if (debug) {
503 System.out.println("Total queries:" + totalQueries); 505 System.out.println("Total queries:" + totalQueries);
504 } 506 }
505 if (!resultOk) { 507 if (!resultOk) {
506 throw new HgBadStateException("See console for details"); // FIXME EXCEPTIONS 508 assert missing.size() > 0;
509 // TODO post-1.0 perhaps, there's better alternative than HgInvalidStateException, e.g. HgDataFormatException?
510 throw new HgInvalidStateException(String.format("Missing elements with indexes: %s", Arrays.toString(missing.toArray())));
507 } 511 }
508 return fromRootToHead; 512 return fromRootToHead;
509 } 513 }
510 514
511 /** 515 /**