comparison src/org/tmatesoft/hg/core/HgCloneCommand.java @ 628:6526d8adbc0f

Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 22 May 2013 15:52:31 +0200
parents 4e6179bde4fc
children 4a0bab2c6da1
comparison
equal deleted inserted replaced
627:5153eb73b18d 628:6526d8adbc0f
88 * @throws HgBadArgumentException 88 * @throws HgBadArgumentException
89 * @throws HgRemoteConnectionException 89 * @throws HgRemoteConnectionException
90 * @throws HgRepositoryNotFoundException 90 * @throws HgRepositoryNotFoundException
91 * @throws HgException 91 * @throws HgException
92 * @throws CancelledException 92 * @throws CancelledException
93 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
94 */ 93 */
95 public HgRepository execute() throws HgException, CancelledException { 94 public HgRepository execute() throws HgException, CancelledException {
96 if (destination == null) { 95 if (destination == null) {
97 throw new IllegalArgumentException("Destination not set", null); 96 throw new IllegalArgumentException("Destination not set", null);
98 } 97 }
112 CancelSupport cancel = getCancelSupport(null, true); 111 CancelSupport cancel = getCancelSupport(null, true);
113 cancel.checkCancelled(); 112 cancel.checkCancelled();
114 // if cloning remote repo, which can stream and no revision is specified - 113 // if cloning remote repo, which can stream and no revision is specified -
115 // can use 'stream_out' wireproto 114 // can use 'stream_out' wireproto
116 // 115 //
117 // pull all changes from the very beginning
118 // XXX consult getContext() if by any chance has a bundle ready, if not, then read and register
119 HgBundle completeChanges = srcRepo.getChanges(Collections.singletonList(NULL));
120 cancel.checkCancelled();
121 WriteDownMate mate = new WriteDownMate(srcRepo.getSessionContext(), destination, progress, cancel);
122 try { 116 try {
123 // instantiate new repo in the destdir 117 // pull all changes from the very beginning
124 mate.initEmptyRepository(); 118 // XXX consult getContext() if by any chance has a bundle ready, if not, then read and register
125 // pull changes 119 HgBundle completeChanges = srcRepo.getChanges(Collections.singletonList(NULL));
126 completeChanges.inspectAll(mate); 120 cancel.checkCancelled();
127 mate.checkFailure(); 121 WriteDownMate mate = new WriteDownMate(srcRepo.getSessionContext(), destination, progress, cancel);
128 mate.complete(); 122 try {
129 } catch (IOException ex) { 123 // instantiate new repo in the destdir
130 throw new HgInvalidFileException(getClass().getName(), ex); 124 mate.initEmptyRepository();
131 } finally { 125 // pull changes
132 completeChanges.unlink(); 126 completeChanges.inspectAll(mate);
133 progress.done(); 127 mate.checkFailure();
128 mate.complete();
129 } catch (IOException ex) {
130 throw new HgInvalidFileException(getClass().getName(), ex);
131 } finally {
132 completeChanges.unlink();
133 progress.done();
134 }
135 } catch (HgRuntimeException ex) {
136 throw new HgLibraryFailureException(ex);
134 } 137 }
135 return new HgLookup().detect(destination); 138 return new HgLookup().detect(destination);
136 } 139 }
137 140
138 141
188 191
189 public void complete() throws IOException { 192 public void complete() throws IOException {
190 fncacheFile.write(); 193 fncacheFile.write();
191 } 194 }
192 195
193 public void changelogStart() { 196 public void changelogStart() throws HgInvalidControlFileException {
194 try { 197 try {
195 revlogHeader.offset(0).baseRevision(-1); 198 revlogHeader.offset(0).baseRevision(-1);
196 revisionSequence.clear(); 199 revisionSequence.clear();
197 indexFile = new FileOutputStream(currentFile = new File(hgDir, filename = "store/00changelog.i")); 200 indexFile = new FileOutputStream(currentFile = new File(hgDir, filename = "store/00changelog.i"));
198 collectChangelogIndexes = true; 201 collectChangelogIndexes = true;
200 throw new HgInvalidControlFileException("Failed to write changelog", ex, new File(hgDir, filename)); 203 throw new HgInvalidControlFileException("Failed to write changelog", ex, new File(hgDir, filename));
201 } 204 }
202 stopIfCancelled(); 205 stopIfCancelled();
203 } 206 }
204 207
205 public void changelogEnd() { 208 public void changelogEnd() throws HgInvalidControlFileException {
206 try { 209 try {
207 clearPreviousContent(); 210 clearPreviousContent();
208 collectChangelogIndexes = false; 211 collectChangelogIndexes = false;
209 closeIndexFile(); 212 closeIndexFile();
210 } catch (IOException ex) { 213 } catch (IOException ex) {
212 } 215 }
213 progressSupport.worked(1); 216 progressSupport.worked(1);
214 stopIfCancelled(); 217 stopIfCancelled();
215 } 218 }
216 219
217 public void manifestStart() { 220 public void manifestStart() throws HgInvalidControlFileException {
218 try { 221 try {
219 revlogHeader.offset(0).baseRevision(-1); 222 revlogHeader.offset(0).baseRevision(-1);
220 revisionSequence.clear(); 223 revisionSequence.clear();
221 indexFile = new FileOutputStream(currentFile = new File(hgDir, filename = "store/00manifest.i")); 224 indexFile = new FileOutputStream(currentFile = new File(hgDir, filename = "store/00manifest.i"));
222 } catch (IOException ex) { 225 } catch (IOException ex) {
223 throw new HgInvalidControlFileException("Failed to write manifest", ex, new File(hgDir, filename)); 226 throw new HgInvalidControlFileException("Failed to write manifest", ex, new File(hgDir, filename));
224 } 227 }
225 stopIfCancelled(); 228 stopIfCancelled();
226 } 229 }
227 230
228 public void manifestEnd() { 231 public void manifestEnd() throws HgInvalidControlFileException {
229 try { 232 try {
230 clearPreviousContent(); 233 clearPreviousContent();
231 closeIndexFile(); 234 closeIndexFile();
232 } catch (IOException ex) { 235 } catch (IOException ex) {
233 throw new HgInvalidControlFileException("Failed to write manifest", ex, new File(hgDir, filename)); 236 throw new HgInvalidControlFileException("Failed to write manifest", ex, new File(hgDir, filename));
234 } 237 }
235 progressSupport.worked(1); 238 progressSupport.worked(1);
236 stopIfCancelled(); 239 stopIfCancelled();
237 } 240 }
238 241
239 public void fileStart(String name) { 242 public void fileStart(String name) throws HgInvalidControlFileException {
240 try { 243 try {
241 revlogHeader.offset(0).baseRevision(-1); 244 revlogHeader.offset(0).baseRevision(-1);
242 revisionSequence.clear(); 245 revisionSequence.clear();
243 File file = new File(hgDir, filename = storagePathHelper.rewrite(name).toString()); 246 File file = new File(hgDir, filename = storagePathHelper.rewrite(name).toString());
244 file.getParentFile().mkdirs(); 247 file.getParentFile().mkdirs();
248 throw new HgInvalidControlFileException(m, ex, new File(filename)); 251 throw new HgInvalidControlFileException(m, ex, new File(filename));
249 } 252 }
250 stopIfCancelled(); 253 stopIfCancelled();
251 } 254 }
252 255
253 public void fileEnd(String name) { 256 public void fileEnd(String name) throws HgInvalidControlFileException {
254 try { 257 try {
255 fncacheFile.addIndex(pathFactory.path(name)); 258 fncacheFile.addIndex(pathFactory.path(name));
256 clearPreviousContent(); 259 clearPreviousContent();
257 closeIndexFile(); 260 closeIndexFile();
258 } catch (IOException ex) { 261 } catch (IOException ex) {
275 indexFile = null; 278 indexFile = null;
276 filename = null; 279 filename = null;
277 currentFile = null; 280 currentFile = null;
278 } 281 }
279 282
280 private int knownRevision(Nodeid p) { 283 private int knownRevision(Nodeid p) throws HgInvalidControlFileException {
281 if (p.isNull()) { 284 if (p.isNull()) {
282 return -1; 285 return -1;
283 } else { 286 } else {
284 for (int i = revisionSequence.size() - 1; i >= 0; i--) { 287 for (int i = revisionSequence.size() - 1; i >= 0; i--) {
285 if (revisionSequence.get(i).equals(p)) { 288 if (revisionSequence.get(i).equals(p)) {
289 } 292 }
290 String m = String.format("Can't find index of %s for file %s", p.shortNotation(), filename); 293 String m = String.format("Can't find index of %s for file %s", p.shortNotation(), filename);
291 throw new HgInvalidControlFileException(m, null, new File(hgDir, filename)).setRevision(p); 294 throw new HgInvalidControlFileException(m, null, new File(hgDir, filename)).setRevision(p);
292 } 295 }
293 296
294 public boolean element(GroupElement ge) { 297 public boolean element(GroupElement ge) throws HgRuntimeException {
295 try { 298 try {
296 assert indexFile != null; 299 assert indexFile != null;
297 boolean writeComplete = false; 300 boolean writeComplete = false;
298 Nodeid deltaBase = ge.patchBase(); 301 Nodeid deltaBase = ge.patchBase();
299 if (deltaBase.isNull()) { 302 if (deltaBase.isNull()) {