comparison src/org/tmatesoft/hg/core/HgCheckoutCommand.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 ca56a36c2eea
children becd2a1310a2
comparison
equal deleted inserted replaced
564:e6407313bab7 565:78a9e26e670d
23 import java.io.FileOutputStream; 23 import java.io.FileOutputStream;
24 import java.io.IOException; 24 import java.io.IOException;
25 import java.io.OutputStreamWriter; 25 import java.io.OutputStreamWriter;
26 import java.nio.channels.FileChannel; 26 import java.nio.channels.FileChannel;
27 27
28 import org.tmatesoft.hg.internal.CsetParamKeeper;
28 import org.tmatesoft.hg.internal.DirstateBuilder; 29 import org.tmatesoft.hg.internal.DirstateBuilder;
29 import org.tmatesoft.hg.internal.EncodingHelper; 30 import org.tmatesoft.hg.internal.EncodingHelper;
30 import org.tmatesoft.hg.internal.Experimental; 31 import org.tmatesoft.hg.internal.Experimental;
31 import org.tmatesoft.hg.internal.Internals; 32 import org.tmatesoft.hg.internal.Internals;
32 import org.tmatesoft.hg.internal.WorkingDirFileWriter; 33 import org.tmatesoft.hg.internal.WorkingDirFileWriter;
33 import org.tmatesoft.hg.repo.HgDataFile; 34 import org.tmatesoft.hg.repo.HgDataFile;
34 import org.tmatesoft.hg.repo.HgDirstate; 35 import org.tmatesoft.hg.repo.HgDirstate;
35 import org.tmatesoft.hg.repo.HgInternals;
36 import org.tmatesoft.hg.repo.HgInvalidRevisionException;
37 import org.tmatesoft.hg.repo.HgManifest;
38 import org.tmatesoft.hg.repo.HgDirstate.EntryKind; 36 import org.tmatesoft.hg.repo.HgDirstate.EntryKind;
39 import org.tmatesoft.hg.repo.HgDirstate.Record; 37 import org.tmatesoft.hg.repo.HgDirstate.Record;
38 import org.tmatesoft.hg.repo.HgInternals;
39 import org.tmatesoft.hg.repo.HgManifest;
40 import org.tmatesoft.hg.repo.HgManifest.Flags; 40 import org.tmatesoft.hg.repo.HgManifest.Flags;
41 import org.tmatesoft.hg.repo.HgRepository; 41 import org.tmatesoft.hg.repo.HgRepository;
42 import org.tmatesoft.hg.repo.HgRuntimeException; 42 import org.tmatesoft.hg.repo.HgRuntimeException;
43 import org.tmatesoft.hg.util.CancelledException; 43 import org.tmatesoft.hg.util.CancelledException;
44 import org.tmatesoft.hg.util.Path; 44 import org.tmatesoft.hg.util.Path;
55 */ 55 */
56 @Experimental(reason="Work in progress") 56 @Experimental(reason="Work in progress")
57 public class HgCheckoutCommand extends HgAbstractCommand<HgCheckoutCommand>{ 57 public class HgCheckoutCommand extends HgAbstractCommand<HgCheckoutCommand>{
58 58
59 private final HgRepository repo; 59 private final HgRepository repo;
60 private int revisionToCheckout = HgRepository.BAD_REVISION; 60 private final CsetParamKeeper revisionToCheckout;
61 private boolean cleanCheckout; 61 private boolean cleanCheckout;
62 62
63 public HgCheckoutCommand(HgRepository hgRepo) { 63 public HgCheckoutCommand(HgRepository hgRepo) {
64 repo = hgRepo; 64 repo = hgRepo;
65 revisionToCheckout = new CsetParamKeeper(repo);
65 } 66 }
66 67
67 /** 68 /**
68 * Whether to discard all uncommited changes prior to check-out. 69 * Whether to discard all uncommited changes prior to check-out.
69 * 70 *
83 * @param nodeid revision 84 * @param nodeid revision
84 * @return <code>this</code> for convenience 85 * @return <code>this</code> for convenience
85 * @throws HgBadArgumentException if failed to find supplied changeset 86 * @throws HgBadArgumentException if failed to find supplied changeset
86 */ 87 */
87 public HgCheckoutCommand changeset(Nodeid nodeid) throws HgBadArgumentException { 88 public HgCheckoutCommand changeset(Nodeid nodeid) throws HgBadArgumentException {
88 try { 89 revisionToCheckout.set(nodeid);
89 return changeset(repo.getChangelog().getRevisionIndex(nodeid)); 90 return this;
90 } catch (HgInvalidRevisionException ex) {
91 throw new HgBadArgumentException("Can't find revision", ex).setRevision(nodeid);
92 }
93 } 91 }
94 92
95 /** 93 /**
96 * Select revision to check out using local revision index 94 * Select revision to check out using local revision index
97 * 95 *
98 * @param changesetIndex local revision index, or {@link HgRepository#TIP} 96 * @param changesetIndex local changelog revision index, or {@link HgRepository#TIP}
99 * @return <code>this</code> for convenience 97 * @return <code>this</code> for convenience
100 * @throws HgBadArgumentException if failed to find supplied changeset 98 * @throws HgBadArgumentException if failed to find supplied changeset
101 */ 99 */
102 public HgCheckoutCommand changeset(int changesetIndex) throws HgBadArgumentException { 100 public HgCheckoutCommand changeset(int changesetIndex) throws HgBadArgumentException {
103 int lastCsetIndex = repo.getChangelog().getLastRevision(); 101 revisionToCheckout.set(changesetIndex);
104 if (changesetIndex == HgRepository.TIP) {
105 changesetIndex = lastCsetIndex;
106 }
107 if (changesetIndex < 0 || changesetIndex > lastCsetIndex) {
108 throw new HgBadArgumentException(String.format("Bad revision index %d, value from [0..%d] expected", changesetIndex, lastCsetIndex), null).setRevisionIndex(changesetIndex);
109 }
110 revisionToCheckout = changesetIndex;
111 return this; 102 return this;
112 } 103 }
113 104
114 /** 105 /**
115 * 106 *
157 148
158 public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) { 149 public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) {
159 return true; 150 return true;
160 } 151 }
161 }; 152 };
162 dirstateBuilder.parents(repo.getChangelog().getRevision(revisionToCheckout), null); 153 // checkout tip if no revision set
163 repo.getManifest().walk(revisionToCheckout, revisionToCheckout, insp); 154 final int coRevision = revisionToCheckout.get(HgRepository.TIP);
155 dirstateBuilder.parents(repo.getChangelog().getRevision(coRevision), null);
156 repo.getManifest().walk(coRevision, coRevision, insp);
164 worker.checkFailed(); 157 worker.checkFailed();
165 File dirstateFile = internalRepo.getRepositoryFile(Dirstate); 158 File dirstateFile = internalRepo.getRepositoryFile(Dirstate);
166 try { 159 try {
167 FileChannel dirstateFileChannel = new FileOutputStream(dirstateFile).getChannel(); 160 FileChannel dirstateFileChannel = new FileOutputStream(dirstateFile).getChannel();
168 dirstateBuilder.serialize(dirstateFileChannel); 161 dirstateBuilder.serialize(dirstateFileChannel);
169 dirstateFileChannel.close(); 162 dirstateFileChannel.close();
170 } catch (IOException ex) { 163 } catch (IOException ex) {
171 throw new HgIOException("Can't write down new directory state", ex, dirstateFile); 164 throw new HgIOException("Can't write down new directory state", ex, dirstateFile);
172 } 165 }
173 String branchName = repo.getChangelog().range(revisionToCheckout, revisionToCheckout).get(0).branch(); 166 String branchName = repo.getChangelog().range(coRevision, coRevision).get(0).branch();
174 assert branchName != null; 167 assert branchName != null;
175 if (!HgRepository.DEFAULT_BRANCH_NAME.equals(branchName)) { 168 if (!HgRepository.DEFAULT_BRANCH_NAME.equals(branchName)) {
176 File branchFile = internalRepo.getRepositoryFile(Branch); 169 File branchFile = internalRepo.getRepositoryFile(Branch);
177 try { 170 try {
178 // branch file is UTF-8, see http://mercurial.selenic.com/wiki/EncodingStrategy#UTF-8_strings 171 // branch file is UTF-8, see http://mercurial.selenic.com/wiki/EncodingStrategy#UTF-8_strings