annotate src/org/tmatesoft/hg/core/HgCheckoutCommand.java @ 563:ca56a36c2eea

HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 28 Feb 2013 16:34:33 +0100
parents 47b7bedf0569
children 78a9e26e670d
rev   line source
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2012-2013 TMate Software Ltd
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.core;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
527
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
19 import static org.tmatesoft.hg.repo.HgRepositoryFiles.Branch;
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
20 import static org.tmatesoft.hg.repo.HgRepositoryFiles.Dirstate;
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
21
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.io.File;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.io.FileOutputStream;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.io.IOException;
527
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
25 import java.io.OutputStreamWriter;
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import java.nio.channels.FileChannel;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import org.tmatesoft.hg.internal.DirstateBuilder;
527
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
29 import org.tmatesoft.hg.internal.EncodingHelper;
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 import org.tmatesoft.hg.internal.Experimental;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 import org.tmatesoft.hg.internal.Internals;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 import org.tmatesoft.hg.internal.WorkingDirFileWriter;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 import org.tmatesoft.hg.repo.HgDataFile;
563
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
34 import org.tmatesoft.hg.repo.HgDirstate;
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
35 import org.tmatesoft.hg.repo.HgInternals;
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 import org.tmatesoft.hg.repo.HgInvalidRevisionException;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 import org.tmatesoft.hg.repo.HgManifest;
563
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
38 import org.tmatesoft.hg.repo.HgDirstate.EntryKind;
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
39 import org.tmatesoft.hg.repo.HgDirstate.Record;
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
40 import org.tmatesoft.hg.repo.HgManifest.Flags;
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 import org.tmatesoft.hg.repo.HgRepository;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 import org.tmatesoft.hg.repo.HgRuntimeException;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 import org.tmatesoft.hg.util.CancelledException;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 import org.tmatesoft.hg.util.Path;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 /**
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 * WORK IN PROGRESS.
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 * Update working directory to specific state, 'hg checkout' counterpart.
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 * For the time being, only 'clean' checkout is supported ('hg co --clean')
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 * @since 1.1
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 * @author Artem Tikhomirov
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 * @author TMate Software Ltd.
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 */
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 @Experimental(reason="Work in progress")
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 public class HgCheckoutCommand extends HgAbstractCommand<HgCheckoutCommand>{
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 private final HgRepository repo;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 private int revisionToCheckout = HgRepository.BAD_REVISION;
563
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
61 private boolean cleanCheckout;
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 public HgCheckoutCommand(HgRepository hgRepo) {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 repo = hgRepo;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 /**
563
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
68 * Whether to discard all uncommited changes prior to check-out.
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
69 *
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
70 * NOTE, at the moment, only clean checkout is supported!
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
71 *
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
72 * @param clean <code>true</code> to discard any change
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
73 * @return <code>this</code> for convenience
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
74 */
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
75 public HgCheckoutCommand clean(boolean clean) {
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
76 cleanCheckout = clean;
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
77 return this;
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
78 }
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
79
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
80 /**
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 * Select revision to check out
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 * @param nodeid revision
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 * @return <code>this</code> for convenience
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 * @throws HgBadArgumentException if failed to find supplied changeset
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 */
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 public HgCheckoutCommand changeset(Nodeid nodeid) throws HgBadArgumentException {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 try {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 return changeset(repo.getChangelog().getRevisionIndex(nodeid));
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 } catch (HgInvalidRevisionException ex) {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 throw new HgBadArgumentException("Can't find revision", ex).setRevision(nodeid);
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 /**
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 * Select revision to check out using local revision index
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 *
563
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
98 * @param changesetIndex local revision index, or {@link HgRepository#TIP}
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 * @return <code>this</code> for convenience
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 * @throws HgBadArgumentException if failed to find supplied changeset
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 */
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 public HgCheckoutCommand changeset(int changesetIndex) throws HgBadArgumentException {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 int lastCsetIndex = repo.getChangelog().getLastRevision();
563
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
104 if (changesetIndex == HgRepository.TIP) {
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
105 changesetIndex = lastCsetIndex;
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
106 }
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 if (changesetIndex < 0 || changesetIndex > lastCsetIndex) {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 throw new HgBadArgumentException(String.format("Bad revision index %d, value from [0..%d] expected", changesetIndex, lastCsetIndex), null).setRevisionIndex(changesetIndex);
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 revisionToCheckout = changesetIndex;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 return this;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 /**
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 * @throws HgIOException to indicate troubles updating files in working copy
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 * @throws HgException
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 * @throws CancelledException
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 */
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 public void execute() throws HgException, CancelledException {
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
121 try {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
122 Internals internalRepo = Internals.getInstance(repo);
563
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
123 if (cleanCheckout) {
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
124 // remove tracked files from wd (perhaps, just forget 'Added'?)
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
125 // for now, just delete each and every tracked file
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
126 // TODO WorkingCopy container with getFile(HgDataFile/Path) to access files in WD
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
127 HgDirstate dirstate = new HgInternals(repo).getDirstate();
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
128 dirstate.walk(new HgDirstate.Inspector() {
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
129
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
130 public boolean next(EntryKind kind, Record entry) {
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
131 File f = new File(repo.getWorkingDir(), entry.name().toString());
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
132 if (f.exists()) {
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
133 f.delete();
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
134 }
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
135 return true;
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
136 }
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
137 });
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
138 } else {
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
139 throw new HgBadArgumentException("Sorry, only clean checkout is supported now, use #clean(true)", null);
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
140 }
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
141 final DirstateBuilder dirstateBuilder = new DirstateBuilder(internalRepo);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
142 final CheckoutWorker worker = new CheckoutWorker(internalRepo);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
143 HgManifest.Inspector insp = new HgManifest.Inspector() {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
144
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
145 public boolean next(Nodeid nid, Path fname, Flags flags) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
146 if (worker.next(nid, fname, flags)) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
147 // new dirstate based on manifest
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
148 dirstateBuilder.recordNormal(fname, flags, worker.getLastWrittenFileSize());
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
149 return true;
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
150 }
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
151 return false;
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
152 }
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
153
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
154 public boolean end(int manifestRevision) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
155 return false;
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
156 }
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
157
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
158 public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) {
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
159 return true;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
160 }
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
161 };
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
162 dirstateBuilder.parents(repo.getChangelog().getRevision(revisionToCheckout), null);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
163 repo.getManifest().walk(revisionToCheckout, revisionToCheckout, insp);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
164 worker.checkFailed();
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
165 File dirstateFile = internalRepo.getRepositoryFile(Dirstate);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
166 try {
563
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
167 FileChannel dirstateFileChannel = new FileOutputStream(dirstateFile).getChannel();
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
168 dirstateBuilder.serialize(dirstateFileChannel);
ca56a36c2eea HgCheckoutCommand: clean parameter, discard changes in WD, test for clean checkout
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 527
diff changeset
169 dirstateFileChannel.close();
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
170 } catch (IOException ex) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
171 throw new HgIOException("Can't write down new directory state", ex, dirstateFile);
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
172 }
527
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
173 String branchName = repo.getChangelog().range(revisionToCheckout, revisionToCheckout).get(0).branch();
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
174 assert branchName != null;
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
175 if (!HgRepository.DEFAULT_BRANCH_NAME.equals(branchName)) {
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
176 File branchFile = internalRepo.getRepositoryFile(Branch);
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
177 try {
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
178 // branch file is UTF-8, see http://mercurial.selenic.com/wiki/EncodingStrategy#UTF-8_strings
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
179 OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(branchFile), EncodingHelper.getUTF8());
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
180 ow.write(branchName);
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
181 ow.close();
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
182 } catch (IOException ex) {
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
183 throw new HgIOException("Can't write down branch information", ex, branchFile);
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
184 }
47b7bedf0569 Tests for present HgCheckoutCommand functionality. Update branch information on checkout. Use UTF8 encoding for the branch file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
185 }
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
186 } catch (HgRuntimeException ex) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
187 throw new HgLibraryFailureException(ex);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
188 }
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
189 }
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
190
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
191 static class CheckoutWorker {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
192 private final Internals hgRepo;
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
193 private HgException failure;
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
194 private int lastWrittenFileSize;
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
195
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
196 CheckoutWorker(Internals implRepo) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
197 hgRepo = implRepo;
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
198 }
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
199
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
200 public boolean next(Nodeid nid, Path fname, Flags flags) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
201 try {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
202 HgDataFile df = hgRepo.getRepo().getFileNode(fname);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
203 int fileRevIndex = df.getRevisionIndex(nid);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
204 // check out files based on manifest
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
205 // FIXME links!
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
206 WorkingDirFileWriter workingDirWriter = new WorkingDirFileWriter(hgRepo);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
207 workingDirWriter.processFile(df, fileRevIndex);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
208 lastWrittenFileSize = workingDirWriter.bytesWritten();
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
209 return true;
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
210 } catch (IOException ex) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
211 failure = new HgIOException("Failed to write down file revision", ex, /*FIXME file*/null);
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
212 } catch (HgRuntimeException ex) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
213 failure = new HgLibraryFailureException(ex);
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
214 }
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
215 return false;
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
216 }
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
217
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
218 public int getLastWrittenFileSize() {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
219 return lastWrittenFileSize;
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
220 }
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
221
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
222 public void checkFailed() throws HgException {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
223 if (failure != null) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
224 throw failure;
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
225 }
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
226 }
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
227 };
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
228 }