comparison src/org/tmatesoft/hg/core/HgRevertCommand.java @ 565:78a9e26e670d

Refactor common code to initialize changelog revision for a command into standalone class
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 09 Apr 2013 17:15:30 +0200
parents 2f9ed6bcefa2
children 0890628ed51e
comparison
equal deleted inserted replaced
564:e6407313bab7 565:78a9e26e670d
19 import java.io.File; 19 import java.io.File;
20 import java.util.Arrays; 20 import java.util.Arrays;
21 import java.util.LinkedHashSet; 21 import java.util.LinkedHashSet;
22 import java.util.Set; 22 import java.util.Set;
23 23
24 import org.tmatesoft.hg.internal.CsetParamKeeper;
24 import org.tmatesoft.hg.internal.DirstateBuilder; 25 import org.tmatesoft.hg.internal.DirstateBuilder;
25 import org.tmatesoft.hg.internal.DirstateReader; 26 import org.tmatesoft.hg.internal.DirstateReader;
26 import org.tmatesoft.hg.internal.Experimental; 27 import org.tmatesoft.hg.internal.Experimental;
27 import org.tmatesoft.hg.internal.Internals; 28 import org.tmatesoft.hg.internal.Internals;
28 import org.tmatesoft.hg.repo.HgInvalidRevisionException;
29 import org.tmatesoft.hg.repo.HgManifest; 29 import org.tmatesoft.hg.repo.HgManifest;
30 import org.tmatesoft.hg.repo.HgManifest.Flags;
30 import org.tmatesoft.hg.repo.HgRepository; 31 import org.tmatesoft.hg.repo.HgRepository;
31 import org.tmatesoft.hg.repo.HgRuntimeException; 32 import org.tmatesoft.hg.repo.HgRuntimeException;
32 import org.tmatesoft.hg.repo.HgManifest.Flags;
33 import org.tmatesoft.hg.util.CancelledException; 33 import org.tmatesoft.hg.util.CancelledException;
34 import org.tmatesoft.hg.util.Path; 34 import org.tmatesoft.hg.util.Path;
35 35
36 /** 36 /**
37 * WORK IN PROGRESS. 37 * WORK IN PROGRESS.
44 @Experimental(reason="Work in progress") 44 @Experimental(reason="Work in progress")
45 public class HgRevertCommand extends HgAbstractCommand<HgRevertCommand> { 45 public class HgRevertCommand extends HgAbstractCommand<HgRevertCommand> {
46 46
47 private final HgRepository repo; 47 private final HgRepository repo;
48 private final Set<Path> files = new LinkedHashSet<Path>(); 48 private final Set<Path> files = new LinkedHashSet<Path>();
49 private int changesetToCheckout = HgRepository.WORKING_COPY; // XXX WORKING_COPY_PARENT, in fact 49 private CsetParamKeeper changesetToCheckout;
50 private boolean keepOriginal = true; 50 private boolean keepOriginal = true;
51 51
52 public HgRevertCommand(HgRepository hgRepo) { 52 public HgRevertCommand(HgRepository hgRepo) {
53 repo = hgRepo; 53 repo = hgRepo;
54 changesetToCheckout = new CsetParamKeeper(hgRepo);
55 changesetToCheckout.doSet(HgRepository.WORKING_COPY); // XXX WORKING_COPY_PARENT, in fact
54 } 56 }
55 57
56 /** 58 /**
57 * Additive 59 * Additive
58 * 60 *
70 * @param changesetRevIndex 72 * @param changesetRevIndex
71 * @return <code>this</code> for convenience 73 * @return <code>this</code> for convenience
72 * @throws HgBadArgumentException 74 * @throws HgBadArgumentException
73 */ 75 */
74 public HgRevertCommand changeset(int changesetRevIndex) throws HgBadArgumentException { 76 public HgRevertCommand changeset(int changesetRevIndex) throws HgBadArgumentException {
75 int lastCsetIndex = repo.getChangelog().getLastRevision(); 77 changesetToCheckout.set(changesetRevIndex);
76 if (changesetRevIndex < 0 || changesetRevIndex > lastCsetIndex) {
77 throw new HgBadArgumentException(String.format("Bad revision index %d, value from [0..%d] expected", changesetRevIndex, lastCsetIndex), null).setRevisionIndex(changesetRevIndex);
78 }
79 changesetToCheckout = changesetRevIndex;
80 return this; 78 return this;
81 } 79 }
82 80
83 /** 81 /**
84 * Handy supplement to {@link #changeset(int)} 82 * Handy supplement to {@link #changeset(int)}
86 * @param revision 84 * @param revision
87 * @return <code>this</code> for convenience 85 * @return <code>this</code> for convenience
88 * @throws HgBadArgumentException 86 * @throws HgBadArgumentException
89 */ 87 */
90 public HgRevertCommand changeset(Nodeid revision) throws HgBadArgumentException { 88 public HgRevertCommand changeset(Nodeid revision) throws HgBadArgumentException {
91 try { 89 changesetToCheckout.set(revision);
92 return changeset(repo.getChangelog().getRevisionIndex(revision)); 90 return this;
93 } catch (HgInvalidRevisionException ex) {
94 throw new HgBadArgumentException("Can't find revision", ex).setRevision(revision);
95 }
96 } 91 }
97 92
98 // TODO keepOriginal() to save .orig 93 // TODO keepOriginal() to save .orig (with tests!)
99 94
100 /** 95 /**
101 * Perform the back out for the given files 96 * Perform the back out for the given files
102 * 97 *
103 * @throws HgIOException 98 * @throws HgIOException
105 * @throws CancelledException 100 * @throws CancelledException
106 */ 101 */
107 public void execute() throws HgException, CancelledException { 102 public void execute() throws HgException, CancelledException {
108 try { 103 try {
109 final int csetRevision; 104 final int csetRevision;
110 if (changesetToCheckout == HgRepository.WORKING_COPY) { 105 if (changesetToCheckout.get() == HgRepository.WORKING_COPY) {
111 csetRevision = repo.getChangelog().getRevisionIndex(repo.getWorkingCopyParents().first()); 106 csetRevision = repo.getChangelog().getRevisionIndex(repo.getWorkingCopyParents().first());
112 } else { 107 } else {
113 csetRevision = changesetToCheckout; 108 csetRevision = changesetToCheckout.get();
114 } 109 }
115 Internals implRepo = Internals.getInstance(repo); 110 Internals implRepo = Internals.getInstance(repo);
116 final DirstateBuilder dirstateBuilder = new DirstateBuilder(implRepo); 111 final DirstateBuilder dirstateBuilder = new DirstateBuilder(implRepo);
117 dirstateBuilder.fillFrom(new DirstateReader(implRepo, new Path.SimpleSource())); 112 dirstateBuilder.fillFrom(new DirstateReader(implRepo, new Path.SimpleSource()));
118 final HgCheckoutCommand.CheckoutWorker worker = new HgCheckoutCommand.CheckoutWorker(implRepo); 113 final HgCheckoutCommand.CheckoutWorker worker = new HgCheckoutCommand.CheckoutWorker(implRepo);