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.api;
45
46 import java.text.MessageFormat;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50
51 import org.eclipse.jgit.internal.JGitText;
52 import org.eclipse.jgit.lib.ObjectId;
53 import org.eclipse.jgit.merge.MergeChunk;
54 import org.eclipse.jgit.merge.MergeChunk.ConflictState;
55 import org.eclipse.jgit.merge.MergeStrategy;
56 import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
57
58
59
60
61 public class MergeResult {
62
63
64
65
66 public enum MergeStatus {
67
68 FAST_FORWARD {
69 @Override
70 public String toString() {
71 return "Fast-forward";
72 }
73
74 @Override
75 public boolean isSuccessful() {
76 return true;
77 }
78 },
79
80
81
82 FAST_FORWARD_SQUASHED {
83 @Override
84 public String toString() {
85 return "Fast-forward-squashed";
86 }
87
88 @Override
89 public boolean isSuccessful() {
90 return true;
91 }
92 },
93
94 ALREADY_UP_TO_DATE {
95 @Override
96 public String toString() {
97 return "Already-up-to-date";
98 }
99
100 @Override
101 public boolean isSuccessful() {
102 return true;
103 }
104 },
105
106 FAILED {
107 @Override
108 public String toString() {
109 return "Failed";
110 }
111
112 @Override
113 public boolean isSuccessful() {
114 return false;
115 }
116 },
117
118 MERGED {
119 @Override
120 public String toString() {
121 return "Merged";
122 }
123
124 @Override
125 public boolean isSuccessful() {
126 return true;
127 }
128 },
129
130
131
132 MERGED_SQUASHED {
133 @Override
134 public String toString() {
135 return "Merged-squashed";
136 }
137
138 @Override
139 public boolean isSuccessful() {
140 return true;
141 }
142 },
143
144
145
146 MERGED_SQUASHED_NOT_COMMITTED {
147 @Override
148 public String toString() {
149 return "Merged-squashed-not-committed";
150 }
151
152 @Override
153 public boolean isSuccessful() {
154 return true;
155 }
156 },
157
158 CONFLICTING {
159 @Override
160 public String toString() {
161 return "Conflicting";
162 }
163
164 @Override
165 public boolean isSuccessful() {
166 return false;
167 }
168 },
169
170
171
172 ABORTED {
173 @Override
174 public String toString() {
175 return "Aborted";
176 }
177
178 @Override
179 public boolean isSuccessful() {
180 return false;
181 }
182 },
183
184
185
186 MERGED_NOT_COMMITTED {
187 @Override
188 public String toString() {
189 return "Merged-not-committed";
190 }
191
192 @Override
193 public boolean isSuccessful() {
194 return true;
195 }
196 },
197
198 NOT_SUPPORTED {
199 @Override
200 public String toString() {
201 return "Not-yet-supported";
202 }
203
204 @Override
205 public boolean isSuccessful() {
206 return false;
207 }
208 },
209
210
211
212
213
214 CHECKOUT_CONFLICT {
215 @Override
216 public String toString() {
217 return "Checkout Conflict";
218 }
219
220 @Override
221 public boolean isSuccessful() {
222 return false;
223 }
224 };
225
226
227
228
229 public abstract boolean isSuccessful();
230 }
231
232 private ObjectId[] mergedCommits;
233
234 private ObjectId base;
235
236 private ObjectId newHead;
237
238 private Map<String, int[][]> conflicts;
239
240 private MergeStatus mergeStatus;
241
242 private String description;
243
244 private MergeStrategy mergeStrategy;
245
246 private Map<String, MergeFailureReason> failingPaths;
247
248 private List<String> checkoutConflicts;
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 public MergeResult(ObjectId newHead, ObjectId base,
271 ObjectId[] mergedCommits, MergeStatus mergeStatus,
272 MergeStrategy mergeStrategy,
273 Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults) {
274 this(newHead, base, mergedCommits, mergeStatus, mergeStrategy,
275 lowLevelResults, null);
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 public MergeResult(ObjectId newHead, ObjectId base,
300 ObjectId[] mergedCommits, MergeStatus mergeStatus,
301 MergeStrategy mergeStrategy,
302 Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults,
303 String description) {
304 this(newHead, base, mergedCommits, mergeStatus, mergeStrategy,
305 lowLevelResults, null, description);
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332 public MergeResult(ObjectId newHead, ObjectId base,
333 ObjectId[] mergedCommits, MergeStatus mergeStatus,
334 MergeStrategy mergeStrategy,
335 Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults,
336 Map<String, MergeFailureReason> failingPaths, String description) {
337 this.newHead = newHead;
338 this.mergedCommits = mergedCommits;
339 this.base = base;
340 this.mergeStatus = mergeStatus;
341 this.mergeStrategy = mergeStrategy;
342 this.description = description;
343 this.failingPaths = failingPaths;
344 if (lowLevelResults != null)
345 for (Map.Entry<String, org.eclipse.jgit.merge.MergeResult<?>> result : lowLevelResults
346 .entrySet())
347 addConflict(result.getKey(), result.getValue());
348 }
349
350
351
352
353
354
355
356
357 public MergeResult(List<String> checkoutConflicts) {
358 this.checkoutConflicts = checkoutConflicts;
359 this.mergeStatus = MergeStatus.CHECKOUT_CONFLICT;
360 }
361
362
363
364
365
366
367 public ObjectId getNewHead() {
368 return newHead;
369 }
370
371
372
373
374
375
376 public MergeStatus getMergeStatus() {
377 return mergeStatus;
378 }
379
380
381
382
383
384
385 public ObjectId[] getMergedCommits() {
386 return mergedCommits;
387 }
388
389
390
391
392
393
394
395
396 public ObjectId getBase() {
397 return base;
398 }
399
400
401 @SuppressWarnings("nls")
402 @Override
403 public String toString() {
404 boolean first = true;
405 StringBuilder commits = new StringBuilder();
406 for (ObjectId commit : mergedCommits) {
407 if (!first)
408 commits.append(", ");
409 else
410 first = false;
411 commits.append(ObjectId.toString(commit));
412 }
413 return MessageFormat.format(
414 JGitText.get().mergeUsingStrategyResultedInDescription,
415 commits, ObjectId.toString(base), mergeStrategy.getName(),
416 mergeStatus, (description == null ? "" : ", " + description));
417 }
418
419
420
421
422
423
424
425 public void setConflicts(Map<String, int[][]> conflicts) {
426 this.conflicts = conflicts;
427 }
428
429
430
431
432
433
434
435
436
437 public void addConflict(String path, int[][] conflictingRanges) {
438 if (conflicts == null)
439 conflicts = new HashMap<>();
440 conflicts.put(path, conflictingRanges);
441 }
442
443
444
445
446
447
448
449
450
451 public void addConflict(String path, org.eclipse.jgit.merge.MergeResult<?> lowLevelResult) {
452 if (!lowLevelResult.containsConflicts())
453 return;
454 if (conflicts == null)
455 conflicts = new HashMap<>();
456 int nrOfConflicts = 0;
457
458 for (MergeChunk mergeChunk : lowLevelResult) {
459 if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
460 nrOfConflicts++;
461 }
462 }
463 int currentConflict = -1;
464 int[][] ret=new int[nrOfConflicts][mergedCommits.length+1];
465 for (MergeChunk mergeChunk : lowLevelResult) {
466
467 int endOfChunk = 0;
468 if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
469 if (currentConflict > -1) {
470
471
472 ret[currentConflict][mergedCommits.length] = endOfChunk;
473 }
474 currentConflict++;
475 endOfChunk = mergeChunk.getEnd();
476 ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
477 }
478 if (mergeChunk.getConflictState().equals(ConflictState.NEXT_CONFLICTING_RANGE)) {
479 if (mergeChunk.getEnd() > endOfChunk)
480 endOfChunk = mergeChunk.getEnd();
481 ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
482 }
483 }
484 conflicts.put(path, ret);
485 }
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527 public Map<String, int[][]> getConflicts() {
528 return conflicts;
529 }
530
531
532
533
534
535
536
537
538 public Map<String, MergeFailureReason> getFailingPaths() {
539 return failingPaths;
540 }
541
542
543
544
545
546
547
548 public List<String> getCheckoutConflicts() {
549 return checkoutConflicts;
550 }
551 }