comparison src/org/tmatesoft/hg/core/HgCommitCommand.java @ 586:73c20c648c1f

HgCommitCommand initial support
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 26 Apr 2013 18:38:41 +0200
parents
children e447384f3771
comparison
equal deleted inserted replaced
585:b47ef0d2777b 586:73c20c648c1f
1 /*
2 * Copyright (c) 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 static org.tmatesoft.hg.repo.HgRepository.NO_REVISION;
20
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.util.ArrayList;
24
25 import org.tmatesoft.hg.internal.ByteArrayChannel;
26 import org.tmatesoft.hg.internal.Experimental;
27 import org.tmatesoft.hg.internal.FileContentSupplier;
28 import org.tmatesoft.hg.repo.CommitFacility;
29 import org.tmatesoft.hg.repo.HgChangelog;
30 import org.tmatesoft.hg.repo.HgDataFile;
31 import org.tmatesoft.hg.repo.HgInternals;
32 import org.tmatesoft.hg.repo.HgRepository;
33 import org.tmatesoft.hg.repo.HgRuntimeException;
34 import org.tmatesoft.hg.repo.HgStatusCollector.Record;
35 import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector;
36 import org.tmatesoft.hg.util.CancelledException;
37 import org.tmatesoft.hg.util.Outcome;
38 import org.tmatesoft.hg.util.Outcome.Kind;
39 import org.tmatesoft.hg.util.Pair;
40 import org.tmatesoft.hg.util.Path;
41
42 /**
43 * WORK IN PROGRESS. UNSTABLE API
44 *
45 * 'hg commit' counterpart, commit changes
46 *
47 * @author Artem Tikhomirov
48 * @author TMate Software Ltd.
49 */
50 @Experimental(reason="Work in progress. Unstable API")
51 public class HgCommitCommand extends HgAbstractCommand<HgCommitCommand> {
52
53 private final HgRepository repo;
54 private String message;
55 private String user;
56 // nodeid of newly added revision
57 private Nodeid newRevision;
58
59 public HgCommitCommand(HgRepository hgRepo) {
60 repo = hgRepo;
61 }
62
63
64 public HgCommitCommand message(String msg) {
65 message = msg;
66 return this;
67 }
68
69 public HgCommitCommand user(String userName) {
70 user = userName;
71 return this;
72 }
73
74 /**
75 * Tell if changes in the working directory constitute merge commit. May be invoked prior to (and independently from) {@link #execute()}
76 *
77 * @return <code>true</code> if working directory changes are result of a merge
78 * @throws HgException subclass thereof to indicate specific issue with the repository
79 */
80 public boolean isMergeCommit() throws HgException {
81 int[] parents = new int[2];
82 detectParentFromDirstate(parents);
83 return parents[0] != NO_REVISION && parents[1] != NO_REVISION;
84 }
85
86 /**
87 * @throws HgException subclass thereof to indicate specific issue with the command arguments or repository state
88 * @throws IOException propagated IO errors from status walker over working directory
89 * @throws CancelledException if execution of the command was cancelled
90 */
91 public Outcome execute() throws HgException, IOException, CancelledException {
92 if (message == null) {
93 throw new HgBadArgumentException("Shall supply commit message", null);
94 }
95 try {
96 int[] parentRevs = new int[2];
97 detectParentFromDirstate(parentRevs);
98 if (parentRevs[0] != NO_REVISION && parentRevs[1] != NO_REVISION) {
99 throw new HgBadArgumentException("Sorry, I'm not yet smart enough to perform merge commits", null);
100 }
101 HgWorkingCopyStatusCollector sc = new HgWorkingCopyStatusCollector(repo);
102 Record status = sc.status(HgRepository.WORKING_COPY);
103 if (status.getModified().size() == 0 && status.getAdded().size() == 0 && status.getRemoved().size() == 0) {
104 newRevision = Nodeid.NULL;
105 return new Outcome(Kind.Failure, "nothing to add");
106 }
107 CommitFacility cf = new CommitFacility(repo, parentRevs[0], parentRevs[1]);
108 for (Path m : status.getModified()) {
109 HgDataFile df = repo.getFileNode(m);
110 cf.add(df, new WorkingCopyContent(df));
111 }
112 ArrayList<FileContentSupplier> toClear = new ArrayList<FileContentSupplier>();
113 for (Path a : status.getAdded()) {
114 HgDataFile df = repo.getFileNode(a); // TODO need smth explicit, like repo.createNewFileNode(Path) here
115 // XXX might be an interesting exercise not to demand a content supplier, but instead return a "DataRequester"
116 // object, that would indicate interest in data, and this code would "push" it to requester, so that any exception
117 // is handled here, right away, and won't need to travel supplier and CommitFacility. (although try/catch inside
118 // supplier.read (with empty throws declaration)
119 FileContentSupplier fcs = new FileContentSupplier(repo, a);
120 cf.add(df, fcs);
121 toClear.add(fcs);
122 }
123 for (Path r : status.getRemoved()) {
124 HgDataFile df = repo.getFileNode(r);
125 cf.forget(df);
126 }
127 cf.branch(detectBranch());
128 cf.user(detectUser());
129 newRevision = cf.commit(message);
130 // TODO toClear list is awful
131 for (FileContentSupplier fcs : toClear) {
132 fcs.done();
133 }
134 return new Outcome(Kind.Success, "Commit ok");
135 } catch (HgRuntimeException ex) {
136 throw new HgLibraryFailureException(ex);
137 }
138 }
139
140 public Nodeid getCommittedRevision() {
141 if (newRevision == null) {
142 throw new IllegalStateException("Call #execute() first!");
143 }
144 return newRevision;
145 }
146
147 private String detectBranch() {
148 return repo.getWorkingCopyBranchName();
149 }
150
151 private String detectUser() {
152 if (user != null) {
153 return user;
154 }
155 // TODO HgInternals is odd place for getNextCommitUsername()
156 return new HgInternals(repo).getNextCommitUsername();
157 }
158
159 private void detectParentFromDirstate(int[] parents) {
160 Pair<Nodeid, Nodeid> pn = repo.getWorkingCopyParents();
161 HgChangelog clog = repo.getChangelog();
162 parents[0] = pn.first().isNull() ? NO_REVISION : clog.getRevisionIndex(pn.first());
163 parents[1] = pn.second().isNull() ? NO_REVISION : clog.getRevisionIndex(pn.second());
164 }
165
166 private static class WorkingCopyContent implements CommitFacility.ByteDataSupplier {
167 private final HgDataFile file;
168 private ByteBuffer fileContent;
169
170 public WorkingCopyContent(HgDataFile dataFile) {
171 file = dataFile;
172 if (!dataFile.exists()) {
173 throw new IllegalArgumentException();
174 }
175 }
176
177 public int read(ByteBuffer dst) {
178 if (fileContent == null) {
179 try {
180 ByteArrayChannel sink = new ByteArrayChannel();
181 // TODO desperately need partial read here
182 file.workingCopy(sink);
183 fileContent = ByteBuffer.wrap(sink.toArray());
184 } catch (CancelledException ex) {
185 // ByteArrayChannel doesn't cancel, never happens
186 assert false;
187 }
188 }
189 if (fileContent.remaining() == 0) {
190 return -1;
191 }
192 int dstCap = dst.remaining();
193 if (fileContent.remaining() > dstCap) {
194 // save actual limit, and pretend we've got exactly desired amount of bytes
195 final int lim = fileContent.limit();
196 fileContent.limit(dstCap);
197 dst.put(fileContent);
198 fileContent.limit(lim);
199 } else {
200 dst.put(fileContent);
201 }
202 return dstCap - dst.remaining();
203 }
204 }
205 }