comparison src/org/tmatesoft/hg/core/HgLogCommand.java @ 427:31a89587eb04

FIXMEs: consistent names, throws for commands and their handlers. Use of checked exceptions in hi-level api
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 29 Mar 2012 17:14:35 +0200
parents 6437d261048a
children 12f668401613
comparison
equal deleted inserted replaced
426:063b0663495a 427:31a89587eb04
1 /* 1 /*
2 * Copyright (c) 2011-2012 TMate Software Ltd 2 s * Copyright (c) 2011-2012 TMate Software Ltd
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License. 6 * the Free Software Foundation; version 2 of the License.
7 * 7 *
34 import org.tmatesoft.hg.repo.HgChangelog; 34 import org.tmatesoft.hg.repo.HgChangelog;
35 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; 35 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
36 import org.tmatesoft.hg.repo.HgDataFile; 36 import org.tmatesoft.hg.repo.HgDataFile;
37 import org.tmatesoft.hg.repo.HgInternals; 37 import org.tmatesoft.hg.repo.HgInternals;
38 import org.tmatesoft.hg.repo.HgInvalidControlFileException; 38 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
39 import org.tmatesoft.hg.repo.HgInvalidRevisionException;
40 import org.tmatesoft.hg.repo.HgInvalidStateException; 39 import org.tmatesoft.hg.repo.HgInvalidStateException;
41 import org.tmatesoft.hg.repo.HgRepository; 40 import org.tmatesoft.hg.repo.HgRepository;
42 import org.tmatesoft.hg.repo.HgRuntimeException; 41 import org.tmatesoft.hg.repo.HgRuntimeException;
43 import org.tmatesoft.hg.repo.HgStatusCollector; 42 import org.tmatesoft.hg.repo.HgStatusCollector;
44 import org.tmatesoft.hg.util.CancelSupport; 43 import org.tmatesoft.hg.util.CancelSupport;
134 } 133 }
135 134
136 /** 135 /**
137 * Limit to specified subset of Changelog, [min(rev1,rev2), max(rev1,rev2)], inclusive. 136 * Limit to specified subset of Changelog, [min(rev1,rev2), max(rev1,rev2)], inclusive.
138 * Revision may be specified with {@link HgRepository#TIP} 137 * Revision may be specified with {@link HgRepository#TIP}
139 * @param rev1 - revision local index 138 *
140 * @param rev2 - revision local index 139 * @param rev1 - local index of start changeset revision
140 * @param rev2 - index of end changeset revision
141 * @return <code>this</code> instance for convenience 141 * @return <code>this</code> instance for convenience
142 */ 142 */
143 public HgLogCommand range(int rev1, int rev2) { 143 public HgLogCommand range(int rev1, int rev2) {
144 if (rev1 != TIP && rev2 != TIP) { 144 if (rev1 != TIP && rev2 != TIP) {
145 startRev = rev2 < rev1 ? rev2 : rev1; 145 startRev = rev2 < rev1 ? rev2 : rev1;
157 /** 157 /**
158 * Select specific changeset 158 * Select specific changeset
159 * 159 *
160 * @param nid changeset revision 160 * @param nid changeset revision
161 * @return <code>this</code> for convenience 161 * @return <code>this</code> for convenience
162 * @throws HgInvalidRevisionException if supplied nodeid doesn't identify any revision from this revlog 162 * @throws HgBadArgumentException if failed to find supplied changeset revision
163 * @throws HgInvalidControlFileException if access to revlog index/data entry failed 163 */
164 */ 164 public HgLogCommand changeset(Nodeid nid) throws HgBadArgumentException {
165 public HgLogCommand changeset(Nodeid nid) throws HgInvalidControlFileException, HgInvalidRevisionException {
166 // XXX perhaps, shall support multiple (...) arguments and extend #execute to handle not only range, but also set of revisions. 165 // XXX perhaps, shall support multiple (...) arguments and extend #execute to handle not only range, but also set of revisions.
167 final int csetRevIndex = repo.getChangelog().getRevisionIndex(nid); 166 try {
168 return range(csetRevIndex, csetRevIndex); 167 final int csetRevIndex = repo.getChangelog().getRevisionIndex(nid);
168 return range(csetRevIndex, csetRevIndex);
169 } catch (HgRuntimeException ex) {
170 throw new HgBadArgumentException("Can't find revision", ex).setRevision(nid);
171 }
169 } 172 }
170 173
171 /** 174 /**
172 * Visit history of a given file only. 175 * Visit history of a given file only.
173 * @param file path relative to repository root. Pass <code>null</code> to reset. 176 * @param file path relative to repository root. Pass <code>null</code> to reset.
187 return file(Path.create(repo.getToRepoPathHelper().rewrite(file)), followCopyRename); 190 return file(Path.create(repo.getToRepoPathHelper().rewrite(file)), followCopyRename);
188 } 191 }
189 192
190 /** 193 /**
191 * Similar to {@link #execute(HgChangesetHandler)}, collects and return result as a list. 194 * Similar to {@link #execute(HgChangesetHandler)}, collects and return result as a list.
192 * @throws HgException FIXME EXCEPTIONS 195 *
196 * @see #execute(HgChangesetHandler)
197 * @throws HgException subclass thereof to indicate specific issue with the command arguments or repository state
193 */ 198 */
194 public List<HgChangeset> execute() throws HgException { 199 public List<HgChangeset> execute() throws HgException {
195 CollectHandler collector = new CollectHandler(); 200 CollectHandler collector = new CollectHandler();
196 try { 201 try {
197 execute(collector); 202 execute(collector);
211 216
212 /** 217 /**
213 * Iterate over range of changesets configured in the command. 218 * Iterate over range of changesets configured in the command.
214 * 219 *
215 * @param handler callback to process changesets. 220 * @param handler callback to process changesets.
216 * @throws HgCallbackTargetException wrapper for any exception user callback code may produce 221 * @throws HgCallbackTargetException propagated exception from the handler
217 * @throws HgInvalidControlFileException if access to revlog index/data entry failed 222 * @throws HgException subclass thereof to indicate specific issue with the command arguments or repository state
218 * @throws HgException in case of some other library issue
219 * @throws CancelledException if execution of the command was cancelled 223 * @throws CancelledException if execution of the command was cancelled
220 * @throws IllegalArgumentException when inspector argument is null 224 * @throws IllegalArgumentException when inspector argument is null
221 * @throws ConcurrentModificationException if this log command instance is already running 225 * @throws ConcurrentModificationException if this log command instance is already running
222 */ 226 */
223 public void execute(HgChangesetHandler handler) throws HgCallbackTargetException, HgException, CancelledException { 227 public void execute(HgChangesetHandler handler) throws HgCallbackTargetException, HgException, CancelledException {
239 repo.getChangelog().range(startRev, endRev, this); 243 repo.getChangelog().range(startRev, endRev, this);
240 csetTransform.checkFailure(); 244 csetTransform.checkFailure();
241 } else { 245 } else {
242 progressHelper.start(-1/*XXX enum const, or a dedicated method startUnspecified(). How about startAtLeast(int)?*/); 246 progressHelper.start(-1/*XXX enum const, or a dedicated method startUnspecified(). How about startAtLeast(int)?*/);
243 HgDataFile fileNode = repo.getFileNode(file); 247 HgDataFile fileNode = repo.getFileNode(file);
248 if (!fileNode.exists()) {
249 throw new HgPathNotFoundException(String.format("File %s not found in the repository", file), file);
250 }
244 fileNode.history(startRev, endRev, this); 251 fileNode.history(startRev, endRev, this);
245 csetTransform.checkFailure(); 252 csetTransform.checkFailure();
246 if (fileNode.isCopy()) { 253 if (fileNode.isCopy()) {
247 // even if we do not follow history, report file rename 254 // even if we do not follow history, report file rename
248 do { 255 do {
249 if (handler instanceof FileHistoryHandler) { 256 if (handler instanceof HgChangesetHandler.WithCopyHistory) {
250 HgFileRevision src = new HgFileRevision(repo, fileNode.getCopySourceRevision(), null, fileNode.getCopySourceName()); 257 HgFileRevision src = new HgFileRevision(repo, fileNode.getCopySourceRevision(), null, fileNode.getCopySourceName());
251 HgFileRevision dst = new HgFileRevision(repo, fileNode.getRevision(0), null, fileNode.getPath(), src.getPath()); 258 HgFileRevision dst = new HgFileRevision(repo, fileNode.getRevision(0), null, fileNode.getPath(), src.getPath());
252 ((FileHistoryHandler) handler).copy(src, dst); 259 ((HgChangesetHandler.WithCopyHistory) handler).copy(src, dst);
253 } 260 }
254 if (limit > 0 && count >= limit) { 261 if (limit > 0 && count >= limit) {
255 // if limit reach, follow is useless. 262 // if limit reach, follow is useless.
256 break; 263 break;
257 } 264 }
261 csetTransform.checkFailure(); 268 csetTransform.checkFailure();
262 } 269 }
263 } while (followHistory && fileNode.isCopy()); 270 } while (followHistory && fileNode.isCopy());
264 } 271 }
265 } 272 }
266 // } catch (HgRuntimeException ex) { 273 } catch (HgRuntimeException ex) {
267 // FIXME wrap with checked HgRuntime subclass 274 throw new HgLibraryFailureException(ex);
268 } finally { 275 } finally {
269 csetTransform = null; 276 csetTransform = null;
270 progressHelper.done(); 277 progressHelper.done();
271 } 278 }
272 } 279 }
273 280
274 /** 281 /**
275 * Tree-wise iteration of a file history, with handy access to parent-child relations between changesets. 282 * Tree-wise iteration of a file history, with handy access to parent-child relations between changesets.
276 * 283 *
277 * @param handler callback to process changesets. 284 * @param handler callback to process changesets.
278 * @throws HgCallbackTargetException to re-throw exception from the handler 285 * @throws HgCallbackTargetException propagated exception from the handler
279 * @throws HgInvalidControlFileException if access to revlog index/data entry failed 286 * @throws HgException subclass thereof to indicate specific issue with the command arguments or repository state
280 * @throws HgException in case of some other library issue
281 * @throws CancelledException if execution of the command was cancelled 287 * @throws CancelledException if execution of the command was cancelled
282 * @throws IllegalArgumentException if command is not satisfied with its arguments 288 * @throws IllegalArgumentException if command is not satisfied with its arguments
283 * @throws ConcurrentModificationException if this log command instance is already running 289 * @throws ConcurrentModificationException if this log command instance is already running
284 */ 290 */
285 public void execute(HgChangesetTreeHandler handler) throws HgCallbackTargetException, HgException, CancelledException { 291 public void execute(HgChangesetTreeHandler handler) throws HgCallbackTargetException, HgException, CancelledException {
343 } 349 }
344 ph2.start(completeHistory.length); 350 ph2.start(completeHistory.length);
345 // XXX shall sort completeHistory according to changeset numbers? 351 // XXX shall sort completeHistory according to changeset numbers?
346 for (int i = 0; i < completeHistory.length; i++ ) { 352 for (int i = 0; i < completeHistory.length; i++ ) {
347 final HistoryNode n = completeHistory[i]; 353 final HistoryNode n = completeHistory[i];
348 handler.next(ei.init(n)); 354 handler.treeElement(ei.init(n));
349 ph2.worked(1); 355 ph2.worked(1);
350 cancelHelper.checkCancelled(); 356 cancelHelper.checkCancelled();
351 } 357 }
352 progressHelper.done(); 358 progressHelper.done();
353 } 359 }
388 } 394 }
389 return parentHelper; 395 return parentHelper;
390 } 396 }
391 397
392 398
393 /**
394 * When {@link HgLogCommand} is executed against file, handler passed to {@link HgLogCommand#execute(HgChangesetHandler)} may optionally
395 * implement this interface to get information about file renames. Method {@link #copy(HgFileRevision, HgFileRevision)} would
396 * get invoked prior any changeset of the original file (if file history being followed) is reported via {@link #next(HgChangeset)}.
397 *
398 * For {@link HgLogCommand#file(Path, boolean)} with renamed file path and follow argument set to false,
399 * {@link #copy(HgFileRevision, HgFileRevision)} would be invoked for the first copy/rename in the history of the file, but not
400 * followed by any changesets.
401 *
402 * @author Artem Tikhomirov
403 * @author TMate Software Ltd.
404 */
405 public interface FileHistoryHandler extends HgChangesetHandler { // FIXME move to stanalone class file, perhaps?
406 // XXX perhaps, should distinguish copy from rename? And what about merged revisions and following them?
407 /**
408 * @throws HgCallbackTargetException wrapper object for any exception user code may produce
409 */
410 void copy(HgFileRevision from, HgFileRevision to) throws HgCallbackTargetException;
411 }
412
413 public static class CollectHandler implements HgChangesetHandler { 399 public static class CollectHandler implements HgChangesetHandler {
414 private final List<HgChangeset> result = new LinkedList<HgChangeset>(); 400 private final List<HgChangeset> result = new LinkedList<HgChangeset>();
415 401
416 public List<HgChangeset> getChanges() { 402 public List<HgChangeset> getChanges() {
417 return Collections.unmodifiableList(result); 403 return Collections.unmodifiableList(result);
418 } 404 }
419 405
420 public void next(HgChangeset changeset) { 406 public void cset(HgChangeset changeset) {
421 result.add(changeset.clone()); 407 result.add(changeset.clone());
422 } 408 }
423 } 409 }
424 410
425 private static class HistoryNode { 411 private static class HistoryNode {