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