Mercurial > hg4j
comparison src/org/tmatesoft/hg/core/HgCheckoutCommand.java @ 581:0890628ed51e
Progress/cancel support in new commands
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Mon, 22 Apr 2013 16:02:17 +0200 |
parents | bd5926e24aa3 |
children | f41dd9a3b8af |
comparison
equal
deleted
inserted
replaced
580:bd5926e24aa3 | 581:0890628ed51e |
---|---|
38 import org.tmatesoft.hg.repo.HgInternals; | 38 import org.tmatesoft.hg.repo.HgInternals; |
39 import org.tmatesoft.hg.repo.HgManifest; | 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.CancelSupport; | |
43 import org.tmatesoft.hg.util.CancelledException; | 44 import org.tmatesoft.hg.util.CancelledException; |
44 import org.tmatesoft.hg.util.Path; | 45 import org.tmatesoft.hg.util.Path; |
46 import org.tmatesoft.hg.util.ProgressSupport; | |
45 | 47 |
46 /** | 48 /** |
47 * WORK IN PROGRESS. | 49 * WORK IN PROGRESS. |
48 * | 50 * |
49 * Update working directory to specific state, 'hg checkout' counterpart. | 51 * Update working directory to specific state, 'hg checkout' counterpart. |
101 revisionToCheckout.set(changesetIndex); | 103 revisionToCheckout.set(changesetIndex); |
102 return this; | 104 return this; |
103 } | 105 } |
104 | 106 |
105 /** | 107 /** |
108 * Update working copy to match state of the selected revision. | |
106 * | 109 * |
107 * @throws HgIOException to indicate troubles updating files in working copy | 110 * @throws HgIOException to indicate troubles updating files in working copy |
108 * @throws HgException | 111 * @throws HgException subclass thereof to indicate specific issue with the command arguments or repository state |
109 * @throws CancelledException | 112 * @throws CancelledException if execution of the command was cancelled |
110 */ | 113 */ |
111 public void execute() throws HgException, CancelledException { | 114 public void execute() throws HgException, CancelledException { |
112 try { | 115 try { |
116 final ProgressSupport progress = getProgressSupport(null); | |
117 final CancelSupport cancellation = getCancelSupport(null, true); | |
118 cancellation.checkCancelled(); | |
119 progress.start(6); | |
113 Internals internalRepo = Internals.getInstance(repo); | 120 Internals internalRepo = Internals.getInstance(repo); |
114 if (cleanCheckout) { | 121 if (cleanCheckout) { |
115 // remove tracked files from wd (perhaps, just forget 'Added'?) | 122 // remove tracked files from wd (perhaps, just forget 'Added'?) |
116 // for now, just delete each and every tracked file | 123 // for now, just delete each and every tracked file |
117 // TODO WorkingCopy container with getFile(HgDataFile/Path) to access files in WD | 124 // TODO WorkingCopy container with getFile(HgDataFile/Path) to access files in WD |
127 } | 134 } |
128 }); | 135 }); |
129 } else { | 136 } else { |
130 throw new HgBadArgumentException("Sorry, only clean checkout is supported now, use #clean(true)", null); | 137 throw new HgBadArgumentException("Sorry, only clean checkout is supported now, use #clean(true)", null); |
131 } | 138 } |
139 progress.worked(1); | |
140 cancellation.checkCancelled(); | |
132 final DirstateBuilder dirstateBuilder = new DirstateBuilder(internalRepo); | 141 final DirstateBuilder dirstateBuilder = new DirstateBuilder(internalRepo); |
133 final CheckoutWorker worker = new CheckoutWorker(internalRepo); | 142 final CheckoutWorker worker = new CheckoutWorker(internalRepo); |
134 HgManifest.Inspector insp = new HgManifest.Inspector() { | 143 HgManifest.Inspector insp = new HgManifest.Inspector() { |
135 | 144 |
136 public boolean next(Nodeid nid, Path fname, Flags flags) { | 145 public boolean next(Nodeid nid, Path fname, Flags flags) { |
164 // checkout tip if no revision set | 173 // checkout tip if no revision set |
165 final int coRevision = revisionToCheckout.get(HgRepository.TIP); | 174 final int coRevision = revisionToCheckout.get(HgRepository.TIP); |
166 dirstateBuilder.parents(repo.getChangelog().getRevision(coRevision), null); | 175 dirstateBuilder.parents(repo.getChangelog().getRevision(coRevision), null); |
167 repo.getManifest().walk(coRevision, coRevision, insp); | 176 repo.getManifest().walk(coRevision, coRevision, insp); |
168 worker.checkFailed(); | 177 worker.checkFailed(); |
178 progress.worked(3); | |
179 cancellation.checkCancelled(); | |
169 File dirstateFile = internalRepo.getRepositoryFile(Dirstate); | 180 File dirstateFile = internalRepo.getRepositoryFile(Dirstate); |
170 try { | 181 try { |
171 FileChannel dirstateFileChannel = new FileOutputStream(dirstateFile).getChannel(); | 182 FileChannel dirstateFileChannel = new FileOutputStream(dirstateFile).getChannel(); |
172 dirstateBuilder.serialize(dirstateFileChannel); | 183 dirstateBuilder.serialize(dirstateFileChannel); |
173 dirstateFileChannel.close(); | 184 dirstateFileChannel.close(); |
174 } catch (IOException ex) { | 185 } catch (IOException ex) { |
175 throw new HgIOException("Can't write down new directory state", ex, dirstateFile); | 186 throw new HgIOException("Can't write down new directory state", ex, dirstateFile); |
176 } | 187 } |
188 progress.worked(1); | |
189 cancellation.checkCancelled(); | |
177 String branchName = repo.getChangelog().range(coRevision, coRevision).get(0).branch(); | 190 String branchName = repo.getChangelog().range(coRevision, coRevision).get(0).branch(); |
178 assert branchName != null; | 191 assert branchName != null; |
179 if (!HgRepository.DEFAULT_BRANCH_NAME.equals(branchName)) { | 192 File branchFile = internalRepo.getRepositoryFile(Branch); |
180 File branchFile = internalRepo.getRepositoryFile(Branch); | 193 if (HgRepository.DEFAULT_BRANCH_NAME.equals(branchName)) { |
194 // clean actual branch, if any | |
195 if (branchFile.isFile()) { | |
196 branchFile.delete(); | |
197 } | |
198 } else { | |
181 try { | 199 try { |
182 // branch file is UTF-8, see http://mercurial.selenic.com/wiki/EncodingStrategy#UTF-8_strings | 200 // branch file is UTF-8, see http://mercurial.selenic.com/wiki/EncodingStrategy#UTF-8_strings |
183 OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(branchFile), EncodingHelper.getUTF8()); | 201 OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(branchFile), EncodingHelper.getUTF8()); |
184 ow.write(branchName); | 202 ow.write(branchName); |
185 ow.close(); | 203 ow.close(); |
186 } catch (IOException ex) { | 204 } catch (IOException ex) { |
187 throw new HgIOException("Can't write down branch information", ex, branchFile); | 205 throw new HgIOException("Can't write down branch information", ex, branchFile); |
188 } | 206 } |
189 } | 207 } |
208 progress.worked(1); | |
209 progress.done(); | |
190 } catch (HgRuntimeException ex) { | 210 } catch (HgRuntimeException ex) { |
191 throw new HgLibraryFailureException(ex); | 211 throw new HgLibraryFailureException(ex); |
192 } | 212 } |
193 } | 213 } |
194 | 214 |