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