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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.revwalk;
45
46 import java.io.IOException;
47 import java.text.MessageFormat;
48 import java.util.LinkedList;
49
50 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
51 import org.eclipse.jgit.errors.MissingObjectException;
52 import org.eclipse.jgit.internal.JGitText;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 class MergeBaseGenerator extends Generator {
71 private static final int PARSED = RevWalk.PARSED;
72 private static final int IN_PENDING = RevWalk.SEEN;
73 private static final int POPPED = RevWalk.TEMP_MARK;
74 private static final int MERGE_BASE = RevWalk.REWRITE;
75
76 private final RevWalk walker;
77 private final DateRevQueue pending;
78
79 private int branchMask;
80 private int recarryTest;
81 private int recarryMask;
82 private int mergeBaseAncestor = -1;
83 private LinkedList<RevCommit> ret = new LinkedList<>();
84
85 private CarryStack stack;
86
87 MergeBaseGenerator(final RevWalk w) {
88 walker = w;
89 pending = new DateRevQueue();
90 }
91
92 void init(final AbstractRevQueue p) throws IOException {
93 try {
94 for (;;) {
95 final RevCommit c = p.next();
96 if (c == null)
97 break;
98 add(c);
99 }
100
101
102
103 recarryTest = branchMask | POPPED;
104 recarryMask = branchMask | POPPED | MERGE_BASE;
105 mergeBaseAncestor = walker.allocFlag();
106
107 for (;;) {
108 RevCommit c = _next();
109 if (c == null) {
110 break;
111 }
112 ret.add(c);
113 }
114 } finally {
115
116
117
118 walker.freeFlag(branchMask | mergeBaseAncestor);
119 }
120 }
121
122 private void add(final RevCommit c) {
123 final int flag = walker.allocFlag();
124 branchMask |= flag;
125 if ((c.flags & branchMask) != 0) {
126
127
128
129
130 throw new IllegalStateException(MessageFormat.format(JGitText.get().staleRevFlagsOn, c.name()));
131 }
132 c.flags |= flag;
133 pending.add(c);
134 }
135
136 @Override
137 int outputType() {
138 return 0;
139 }
140
141 private RevCommit _next() throws MissingObjectException,
142 IncorrectObjectTypeException, IOException {
143 for (;;) {
144 final RevCommit c = pending.next();
145 if (c == null) {
146 return null;
147 }
148
149 for (final RevCommit p : c.parents) {
150 if ((p.flags & IN_PENDING) != 0)
151 continue;
152 if ((p.flags & PARSED) == 0)
153 p.parseHeaders(walker);
154 p.flags |= IN_PENDING;
155 pending.add(p);
156 }
157
158 int carry = c.flags & branchMask;
159 boolean mb = carry == branchMask;
160 if (mb) {
161
162
163
164
165 carry |= MERGE_BASE | mergeBaseAncestor;
166 }
167 carryOntoHistory(c, carry);
168
169 if ((c.flags & MERGE_BASE) != 0) {
170
171
172
173
174
175 if (pending.everbodyHasFlag(MERGE_BASE))
176 return null;
177 continue;
178 }
179 c.flags |= POPPED;
180
181 if (mb) {
182 c.flags |= MERGE_BASE;
183 return c;
184 }
185 }
186 }
187
188 @Override
189 RevCommit next() throws MissingObjectException,
190 IncorrectObjectTypeException, IOException {
191 while (!ret.isEmpty()) {
192 RevCommit commit = ret.remove();
193 if ((commit.flags & mergeBaseAncestor) == 0) {
194 return commit;
195 }
196 }
197 return null;
198 }
199
200 private void carryOntoHistory(RevCommit c, int carry) {
201 stack = null;
202 for (;;) {
203 carryOntoHistoryInnerLoop(c, carry);
204 if (stack == null) {
205 break;
206 }
207 c = stack.c;
208 carry = stack.carry;
209 stack = stack.prev;
210 }
211 }
212
213 private void carryOntoHistoryInnerLoop(RevCommit c, int carry) {
214 for (;;) {
215 RevCommit[] parents = c.parents;
216 if (parents == null || parents.length == 0) {
217 break;
218 }
219
220 int e = parents.length - 1;
221 for (int i = 0; i < e; i++) {
222 RevCommit p = parents[i];
223 if (carryOntoOne(p, carry) == CONTINUE) {
224
225 stack = new CarryStack(stack, p, carry);
226 }
227
228
229
230 }
231
232 c = parents[e];
233 if (carryOntoOne(c, carry) != CONTINUE) {
234 break;
235 }
236 }
237 }
238
239 private static final int CONTINUE = 0;
240 private static final int HAVE_ALL = 1;
241 private static final int CONTINUE_ON_STACK = 2;
242
243 private int carryOntoOne(RevCommit p, int carry) {
244
245
246
247
248
249 int rc = (p.flags & carry) == carry ? HAVE_ALL : CONTINUE;
250 p.flags |= carry;
251
252 if ((p.flags & recarryMask) == recarryTest) {
253
254
255
256
257 p.flags &= ~POPPED;
258 pending.add(p);
259 stack = new CarryStack(stack, p, branchMask | MERGE_BASE);
260 return CONTINUE_ON_STACK;
261 }
262 return rc;
263 }
264
265 private static class CarryStack {
266 final CarryStack prev;
267 final RevCommit c;
268 final int carry;
269
270 CarryStack(CarryStack prev, RevCommit c, int carry) {
271 this.prev = prev;
272 this.c = c;
273 this.carry = carry;
274 }
275 }
276 }