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.treewalk;
45
46 import java.io.IOException;
47
48 import org.eclipse.jgit.annotations.Nullable;
49 import org.eclipse.jgit.errors.CorruptObjectException;
50 import org.eclipse.jgit.lib.FileMode;
51 import org.eclipse.jgit.lib.ObjectReader;
52 import org.eclipse.jgit.lib.Repository;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public class NameConflictTreeWalk extends TreeWalk {
90 private static final int TREE_MODE = FileMode.TREE.getBits();
91
92 private boolean fastMinHasMatch;
93
94 private AbstractTreeIterator dfConflict;
95
96
97
98
99
100
101
102 public NameConflictTreeWalk(Repository repo) {
103 super(repo);
104 }
105
106
107
108
109
110
111
112
113
114
115 public NameConflictTreeWalk(@Nullable Repository repo, ObjectReader or) {
116 super(repo, or);
117 }
118
119
120
121
122
123
124
125 public NameConflictTreeWalk(ObjectReader or) {
126 super(or);
127 }
128
129 @Override
130 AbstractTreeIterator min() throws CorruptObjectException {
131 for (;;) {
132 final AbstractTreeIterator minRef = fastMin();
133 if (fastMinHasMatch)
134 return minRef;
135
136 if (isTree(minRef)) {
137 if (skipEntry(minRef)) {
138 for (AbstractTreeIterator t : trees) {
139 if (t.matches == minRef) {
140 t.next(1);
141 t.matches = null;
142 }
143 }
144 continue;
145 }
146 return minRef;
147 }
148
149 return combineDF(minRef);
150 }
151 }
152
153 private AbstractTreeIterator fastMin() {
154 fastMinHasMatch = true;
155
156 int i = 0;
157 AbstractTreeIterator minRef = trees[i];
158 while (minRef.eof() && ++i < trees.length)
159 minRef = trees[i];
160 if (minRef.eof())
161 return minRef;
162
163 boolean hasConflict = false;
164 minRef.matches = minRef;
165 while (++i < trees.length) {
166 final AbstractTreeIterator t = trees[i];
167 if (t.eof())
168 continue;
169
170 final int cmp = t.pathCompare(minRef);
171 if (cmp < 0) {
172 if (fastMinHasMatch && isTree(minRef) && !isTree(t)
173 && nameEqual(minRef, t)) {
174
175
176
177
178 t.matches = minRef;
179 hasConflict = true;
180 } else {
181 fastMinHasMatch = false;
182 t.matches = t;
183 minRef = t;
184 }
185 } else if (cmp == 0) {
186
187
188 t.matches = minRef;
189 } else if (fastMinHasMatch && isTree(t) && !isTree(minRef)
190 && !isGitlink(minRef) && nameEqual(t, minRef)) {
191
192
193
194
195
196
197
198
199 for (int k = 0; k < i; k++) {
200 final AbstractTreeIterator p = trees[k];
201 if (p.matches == minRef)
202 p.matches = t;
203 }
204 t.matches = t;
205 minRef = t;
206 hasConflict = true;
207 } else
208 fastMinHasMatch = false;
209 }
210
211 if (hasConflict && fastMinHasMatch && dfConflict == null)
212 dfConflict = minRef;
213 return minRef;
214 }
215
216 private static boolean nameEqual(final AbstractTreeIterator a,
217 final AbstractTreeIterator b) {
218 return a.pathCompare(b, TREE_MODE) == 0;
219 }
220
221 private boolean isGitlink(AbstractTreeIterator p) {
222 return FileMode.GITLINK.equals(p.mode);
223 }
224
225 private static boolean isTree(AbstractTreeIterator p) {
226 return FileMode.TREE.equals(p.mode);
227 }
228
229 private boolean skipEntry(AbstractTreeIterator minRef)
230 throws CorruptObjectException {
231
232
233
234 for (AbstractTreeIterator t : trees) {
235 if (t.matches == minRef || t.first())
236 continue;
237
238 int stepsBack = 0;
239 for (;;) {
240 stepsBack++;
241 t.back(1);
242
243 final int cmp = t.pathCompare(minRef, 0);
244 if (cmp == 0) {
245
246
247 t.next(stepsBack);
248 return true;
249 } else if (cmp < 0 || t.first()) {
250
251
252 t.next(stepsBack);
253 break;
254 }
255 }
256 }
257
258
259
260 return false;
261 }
262
263 private AbstractTreeIteratorg/eclipse/jgit/treewalk/AbstractTreeIterator.html#AbstractTreeIterator">AbstractTreeIterator combineDF(AbstractTreeIterator minRef)
264 throws CorruptObjectException {
265
266
267
268
269 AbstractTreeIterator treeMatch = null;
270 for (AbstractTreeIterator t : trees) {
271 if (t.matches == minRef || t.eof())
272 continue;
273
274 for (;;) {
275 final int cmp = t.pathCompare(minRef, TREE_MODE);
276 if (cmp < 0) {
277
278
279 t.matchShift++;
280 t.next(1);
281 if (t.eof()) {
282 t.back(t.matchShift);
283 t.matchShift = 0;
284 break;
285 }
286 } else if (cmp == 0) {
287
288
289 t.matches = minRef;
290 treeMatch = t;
291 break;
292 } else {
293
294
295 if (t.matchShift != 0) {
296 t.back(t.matchShift);
297 t.matchShift = 0;
298 }
299 break;
300 }
301 }
302 }
303
304 if (treeMatch != null) {
305
306
307
308
309 for (AbstractTreeIterator t : trees)
310 if (t.matches == minRef)
311 t.matches = treeMatch;
312
313 if (dfConflict == null && !isGitlink(minRef)) {
314 dfConflict = treeMatch;
315 }
316
317 return treeMatch;
318 }
319
320 return minRef;
321 }
322
323 @Override
324 void popEntriesEqual() throws CorruptObjectException {
325 final AbstractTreeIterator ch = currentHead;
326 for (int i = 0; i < trees.length; i++) {
327 final AbstractTreeIterator t = trees[i];
328 if (t.matches == ch) {
329 if (t.matchShift == 0)
330 t.next(1);
331 else {
332 t.back(t.matchShift);
333 t.matchShift = 0;
334 }
335 t.matches = null;
336 }
337 }
338
339 if (ch == dfConflict)
340 dfConflict = null;
341 }
342
343 @Override
344 void skipEntriesEqual() throws CorruptObjectException {
345 final AbstractTreeIterator ch = currentHead;
346 for (int i = 0; i < trees.length; i++) {
347 final AbstractTreeIterator t = trees[i];
348 if (t.matches == ch) {
349 if (t.matchShift == 0)
350 t.skip();
351 else {
352 t.back(t.matchShift);
353 t.matchShift = 0;
354 }
355 t.matches = null;
356 }
357 }
358
359 if (ch == dfConflict)
360 dfConflict = null;
361 }
362
363 @Override
364 void stopWalk() throws IOException {
365 if (!needsStopWalk()) {
366 return;
367 }
368
369
370
371
372
373
374
375 for (;;) {
376 AbstractTreeIterator t = min();
377 if (t.eof()) {
378 if (depth > 0) {
379 exitSubtree();
380 popEntriesEqual();
381 continue;
382 }
383 return;
384 }
385 currentHead = t;
386 skipEntriesEqual();
387 }
388 }
389
390 private boolean needsStopWalk() {
391 for (AbstractTreeIterator t : trees) {
392 if (t.needsStopWalk()) {
393 return true;
394 }
395 }
396 return false;
397 }
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413 public boolean isDirectoryFileConflict() {
414 return dfConflict != null;
415 }
416 }