comparison src/org/tmatesoft/hg/core/HgCheckoutCommand.java @ 525:0be5be8d57e9

Repository checkout support, first iteration
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 11 Jan 2013 18:12:39 +0100
parents
children 2f9ed6bcefa2
comparison
equal deleted inserted replaced
524:57b2c9eb3c69 525:0be5be8d57e9
1 /*
2 * Copyright (c) 2012-2013 TMate Software Ltd
3 *
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
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.core;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.nio.channels.FileChannel;
23
24 import org.tmatesoft.hg.internal.DirstateBuilder;
25 import org.tmatesoft.hg.internal.Experimental;
26 import org.tmatesoft.hg.internal.Internals;
27 import org.tmatesoft.hg.internal.WorkingDirFileWriter;
28 import org.tmatesoft.hg.repo.HgDataFile;
29 import org.tmatesoft.hg.repo.HgInvalidRevisionException;
30 import org.tmatesoft.hg.repo.HgInvalidStateException;
31 import org.tmatesoft.hg.repo.HgManifest;
32 import org.tmatesoft.hg.repo.HgRepository;
33 import org.tmatesoft.hg.repo.HgRuntimeException;
34 import org.tmatesoft.hg.repo.HgManifest.Flags;
35 import org.tmatesoft.hg.util.CancelledException;
36 import org.tmatesoft.hg.util.Path;
37
38 /**
39 * WORK IN PROGRESS.
40 *
41 * Update working directory to specific state, 'hg checkout' counterpart.
42 * For the time being, only 'clean' checkout is supported ('hg co --clean')
43 *
44 * @since 1.1
45 * @author Artem Tikhomirov
46 * @author TMate Software Ltd.
47 */
48 @Experimental(reason="Work in progress")
49 public class HgCheckoutCommand extends HgAbstractCommand<HgCheckoutCommand>{
50
51 private final HgRepository repo;
52 private int revisionToCheckout = HgRepository.BAD_REVISION;
53
54 public HgCheckoutCommand(HgRepository hgRepo) {
55 repo = hgRepo;
56 }
57
58 /**
59 * Select revision to check out
60 *
61 * @param nodeid revision
62 * @return <code>this</code> for convenience
63 * @throws HgBadArgumentException if failed to find supplied changeset
64 */
65 public HgCheckoutCommand changeset(Nodeid nodeid) throws HgBadArgumentException {
66 try {
67 return changeset(repo.getChangelog().getRevisionIndex(nodeid));
68 } catch (HgInvalidRevisionException ex) {
69 throw new HgBadArgumentException("Can't find revision", ex).setRevision(nodeid);
70 }
71 }
72
73 /**
74 * Select revision to check out using local revision index
75 *
76 * @param changesetIndex local revision index
77 * @return <code>this</code> for convenience
78 * @throws HgBadArgumentException if failed to find supplied changeset
79 */
80 public HgCheckoutCommand changeset(int changesetIndex) throws HgBadArgumentException {
81 int lastCsetIndex = repo.getChangelog().getLastRevision();
82 if (changesetIndex < 0 || changesetIndex > lastCsetIndex) {
83 throw new HgBadArgumentException(String.format("Bad revision index %d, value from [0..%d] expected", changesetIndex, lastCsetIndex), null).setRevisionIndex(changesetIndex);
84 }
85 revisionToCheckout = changesetIndex;
86 return this;
87 }
88
89 /**
90 *
91 * @throws HgIOException to indicate troubles updating files in working copy
92 * @throws HgException
93 * @throws CancelledException
94 */
95 public void execute() throws HgException, CancelledException {
96 Internals internalRepo = Internals.getInstance(repo);
97 // remove tracked files from wd (perhaps, just forget 'Added'?)
98 // TODO
99 final DirstateBuilder dirstateBuilder = new DirstateBuilder(internalRepo.buildFileNameEncodingHelper());
100 final Exception[] failure = new Exception[1];
101 HgManifest.Inspector worker = new HgManifest.Inspector() {
102
103 public boolean next(Nodeid nid, Path fname, Flags flags) {
104 try {
105 HgDataFile df = repo.getFileNode(fname);
106 int fileRevIndex = df.getRevisionIndex(nid);
107 // check out files based on manifest
108 // FIXME links!
109 WorkingDirFileWriter workingDirWriter = new WorkingDirFileWriter(repo);
110 workingDirWriter.processFile(df, fileRevIndex);
111 // new dirstate based on manifest
112 dirstateBuilder.recordNormal(fname, flags, workingDirWriter.bytesWritten());
113 return true;
114 } catch (IOException ex) {
115 failure[0] = ex;
116 } catch (HgRuntimeException ex) {
117 failure[0] = ex;
118 }
119 return false;
120 }
121
122 public boolean end(int manifestRevision) {
123 return false;
124 }
125
126 public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) {
127 return true;
128 }
129 };
130 dirstateBuilder.parents(repo.getChangelog().getRevision(revisionToCheckout), null);
131 repo.getManifest().walk(revisionToCheckout, revisionToCheckout, worker);
132 if (failure[0] != null) {
133 if (failure[0] instanceof IOException) {
134 throw new HgIOException("Failed to write down file revision", failure[0], /*FIXME file*/null);
135 }
136 if (failure[0] instanceof HgRuntimeException) {
137 throw new HgLibraryFailureException((HgRuntimeException) failure[0]);
138 }
139 HgInvalidStateException e = new HgInvalidStateException("Unexpected exception");
140 e.initCause(failure[0]);
141 throw e;
142 }
143 File dirstateFile = internalRepo.getFileFromRepoDir("dirstate");
144 try {
145 FileChannel dirstate = new FileOutputStream(dirstateFile).getChannel();
146 dirstateBuilder.serialize(dirstate);
147 dirstate.close();
148 } catch (IOException ex) {
149 throw new HgIOException("Can't write down new directory state", ex, dirstateFile);
150 }
151 // FIXME write down branch file
152 }
153 }