1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package com.aragost.javahg.commands;
27
28 import java.io.IOException;
29
30 import com.aragost.javahg.Changeset;
31 import com.aragost.javahg.Repository;
32 import com.aragost.javahg.commands.flags.GraftCommandFlags;
33 import com.aragost.javahg.internals.HgInputStream;
34 import com.aragost.javahg.internals.RuntimeIOException;
35 import com.aragost.javahg.merge.GraftContext;
36
37
38
39
40
41 public class GraftCommand extends GraftCommandFlags {
42
43 private boolean hasConflicts = false;
44 private boolean somethingSkipped = false;
45
46
47
48
49
50 public GraftCommand(Repository repository) {
51 super(repository);
52 cmdAppend("-y");
53 cmdAppend("--tool", "internal:fail");
54 }
55
56 @Override
57 public boolean isSuccessful() {
58 return super.isSuccessful() || this.hasConflicts || this.somethingSkipped;
59 }
60
61
62
63
64
65
66
67
68
69
70
71 public GraftContext execute(Changeset changeset) {
72 Repository repo = getRepository();
73 repo.lock();
74 HgInputStream stream = launchStream(changeset.getNode());
75 try {
76 if (stream.match("grafting revision ".getBytes())) {
77 int rev = stream.revisionUpTo('\n');
78 GraftContext ctx = new GraftContext(this, rev);
79 if (!stream.isEof()) {
80 ctx.processStream(stream, true);
81 }
82 stream.consumeAll();
83 boolean flagOrKeepDeleteConflicts = !ctx.getFlagConflicts().isEmpty()
84 || !ctx.getKeepDeleteConflicts().isEmpty();
85 if (getReturnCode() == -1 && this.hasConflicts || flagOrKeepDeleteConflicts) {
86 if (flagOrKeepDeleteConflicts && ctx.getMergeConflicts().isEmpty()) {
87
88
89
90
91
92 Changeset rollbackChangeset = repo.tip();
93 if (!changeset.getNode().equals(rollbackChangeset.getExtra().getString("source"))) {
94 throw new IllegalStateException("Current tip is not grafted from expected changeset");
95 }
96 RollbackCommand.on(repo).execute();
97 ctx.setRollbackChangeset(rollbackChangeset);
98 }
99 return ctx;
100 } else {
101 return null;
102 }
103 } else {
104 return null;
105 }
106 } catch (IOException e) {
107 throw new RuntimeIOException(e);
108 } finally {
109 repo.unlock();
110 }
111 }
112
113
114
115
116
117
118
119 public boolean executeContinue() {
120 launchString("--continue");
121 return getReturnCode() == 0;
122 }
123
124 @Override
125 protected void doneHook() {
126 String errorString = getErrorString();
127 this.hasConflicts = errorString.indexOf("abort: unresolved conflicts, can't continue") >= 0
128 || errorString.indexOf("abort: unresolved merge conflicts (see hg help resolve)") >= 0;
129 this.somethingSkipped = errorString.indexOf("skipping ancestor revision ") >= 0;
130 }
131
132 @Override
133 protected void clear() {
134 super.clear();
135 this.hasConflicts = false;
136 this.somethingSkipped = false;
137 }
138
139 }