View Javadoc
1   /*
2    * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
3    * Copyright (C) 2010-2012, Christian Halstrick <christian.halstrick@sap.com> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  package org.eclipse.jgit.api;
12  
13  import java.text.MessageFormat;
14  import java.util.HashMap;
15  import java.util.List;
16  import java.util.Map;
17  
18  import org.eclipse.jgit.internal.JGitText;
19  import org.eclipse.jgit.lib.ObjectId;
20  import org.eclipse.jgit.merge.MergeChunk;
21  import org.eclipse.jgit.merge.MergeChunk.ConflictState;
22  import org.eclipse.jgit.merge.MergeStrategy;
23  import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
24  
25  /**
26   * Encapsulates the result of a {@link org.eclipse.jgit.api.MergeCommand}.
27   */
28  public class MergeResult {
29  
30  	/**
31  	 * The status the merge resulted in.
32  	 */
33  	public enum MergeStatus {
34  		/** */
35  		FAST_FORWARD {
36  			@Override
37  			public String toString() {
38  				return "Fast-forward"; //$NON-NLS-1$
39  			}
40  
41  			@Override
42  			public boolean isSuccessful() {
43  				return true;
44  			}
45  		},
46  		/**
47  		 * @since 2.0
48  		 */
49  		FAST_FORWARD_SQUASHED {
50  			@Override
51  			public String toString() {
52  				return "Fast-forward-squashed"; //$NON-NLS-1$
53  			}
54  
55  			@Override
56  			public boolean isSuccessful() {
57  				return true;
58  			}
59  		},
60  		/** */
61  		ALREADY_UP_TO_DATE {
62  			@Override
63  			public String toString() {
64  				return "Already-up-to-date"; //$NON-NLS-1$
65  			}
66  
67  			@Override
68  			public boolean isSuccessful() {
69  				return true;
70  			}
71  		},
72  		/** */
73  		FAILED {
74  			@Override
75  			public String toString() {
76  				return "Failed"; //$NON-NLS-1$
77  			}
78  
79  			@Override
80  			public boolean isSuccessful() {
81  				return false;
82  			}
83  		},
84  		/** */
85  		MERGED {
86  			@Override
87  			public String toString() {
88  				return "Merged"; //$NON-NLS-1$
89  			}
90  
91  			@Override
92  			public boolean isSuccessful() {
93  				return true;
94  			}
95  		},
96  		/**
97  		 * @since 2.0
98  		 */
99  		MERGED_SQUASHED {
100 			@Override
101 			public String toString() {
102 				return "Merged-squashed"; //$NON-NLS-1$
103 			}
104 
105 			@Override
106 			public boolean isSuccessful() {
107 				return true;
108 			}
109 		},
110 		/**
111 		 * @since 3.0
112 		 */
113 		MERGED_SQUASHED_NOT_COMMITTED {
114 			@Override
115 			public String toString() {
116 				return "Merged-squashed-not-committed"; //$NON-NLS-1$
117 			}
118 
119 			@Override
120 			public boolean isSuccessful() {
121 				return true;
122 			}
123 		},
124 		/** */
125 		CONFLICTING {
126 			@Override
127 			public String toString() {
128 				return "Conflicting"; //$NON-NLS-1$
129 			}
130 
131 			@Override
132 			public boolean isSuccessful() {
133 				return false;
134 			}
135 		},
136 		/**
137 		 * @since 2.2
138 		 */
139 		ABORTED {
140 			@Override
141 			public String toString() {
142 				return "Aborted"; //$NON-NLS-1$
143 			}
144 
145 			@Override
146 			public boolean isSuccessful() {
147 				return false;
148 			}
149 		},
150 		/**
151 		 * @since 3.0
152 		 **/
153 		MERGED_NOT_COMMITTED {
154 			@Override
155 			public String toString() {
156 				return "Merged-not-committed"; //$NON-NLS-1$
157 			}
158 
159 			@Override
160 			public boolean isSuccessful() {
161 				return true;
162 			}
163 		},
164 		/** */
165 		NOT_SUPPORTED {
166 			@Override
167 			public String toString() {
168 				return "Not-yet-supported"; //$NON-NLS-1$
169 			}
170 
171 			@Override
172 			public boolean isSuccessful() {
173 				return false;
174 			}
175 		},
176 		/**
177 		 * Status representing a checkout conflict, meaning that nothing could
178 		 * be merged, as the pre-scan for the trees already failed for certain
179 		 * files (i.e. local modifications prevent checkout of files).
180 		 */
181 		CHECKOUT_CONFLICT {
182 			@Override
183 			public String toString() {
184 				return "Checkout Conflict"; //$NON-NLS-1$
185 			}
186 
187 			@Override
188 			public boolean isSuccessful() {
189 				return false;
190 			}
191 		};
192 
193 		/**
194 		 * @return whether the status indicates a successful result
195 		 */
196 		public abstract boolean isSuccessful();
197 	}
198 
199 	private ObjectId[] mergedCommits;
200 
201 	private ObjectId base;
202 
203 	private ObjectId newHead;
204 
205 	private Map<String, int[][]> conflicts;
206 
207 	private MergeStatus mergeStatus;
208 
209 	private String description;
210 
211 	private MergeStrategy mergeStrategy;
212 
213 	private Map<String, MergeFailureReason> failingPaths;
214 
215 	private List<String> checkoutConflicts;
216 
217 	/**
218 	 * Constructor for MergeResult.
219 	 *
220 	 * @param newHead
221 	 *            the object the head points at after the merge
222 	 * @param base
223 	 *            the common base which was used to produce a content-merge. May
224 	 *            be <code>null</code> if the merge-result was produced without
225 	 *            computing a common base
226 	 * @param mergedCommits
227 	 *            all the commits which have been merged together
228 	 * @param mergeStatus
229 	 *            the status the merge resulted in
230 	 * @param mergeStrategy
231 	 *            the used {@link org.eclipse.jgit.merge.MergeStrategy}
232 	 * @param lowLevelResults
233 	 *            merge results as returned by
234 	 *            {@link org.eclipse.jgit.merge.ResolveMerger#getMergeResults()}
235 	 * @since 2.0
236 	 */
237 	public MergeResult(ObjectId./../../../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId newHead, ObjectId base,
238 			ObjectId[] mergedCommits, MergeStatus mergeStatus,
239 			MergeStrategy mergeStrategy,
240 			Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults) {
241 		this(newHead, base, mergedCommits, mergeStatus, mergeStrategy,
242 				lowLevelResults, null);
243 	}
244 
245 	/**
246 	 * Constructor for MergeResult.
247 	 *
248 	 * @param newHead
249 	 *            the object the head points at after the merge
250 	 * @param base
251 	 *            the common base which was used to produce a content-merge. May
252 	 *            be <code>null</code> if the merge-result was produced without
253 	 *            computing a common base
254 	 * @param mergedCommits
255 	 *            all the commits which have been merged together
256 	 * @param mergeStatus
257 	 *            the status the merge resulted in
258 	 * @param mergeStrategy
259 	 *            the used {@link org.eclipse.jgit.merge.MergeStrategy}
260 	 * @param lowLevelResults
261 	 *            merge results as returned by
262 	 *            {@link org.eclipse.jgit.merge.ResolveMerger#getMergeResults()}
263 	 * @param description
264 	 *            a user friendly description of the merge result
265 	 */
266 	public MergeResult(ObjectId./../../../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId newHead, ObjectId base,
267 			ObjectId[] mergedCommits, MergeStatus mergeStatus,
268 			MergeStrategy mergeStrategy,
269 			Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults,
270 			String description) {
271 		this(newHead, base, mergedCommits, mergeStatus, mergeStrategy,
272 				lowLevelResults, null, description);
273 	}
274 
275 	/**
276 	 * Constructor for MergeResult.
277 	 *
278 	 * @param newHead
279 	 *            the object the head points at after the merge
280 	 * @param base
281 	 *            the common base which was used to produce a content-merge. May
282 	 *            be <code>null</code> if the merge-result was produced without
283 	 *            computing a common base
284 	 * @param mergedCommits
285 	 *            all the commits which have been merged together
286 	 * @param mergeStatus
287 	 *            the status the merge resulted in
288 	 * @param mergeStrategy
289 	 *            the used {@link org.eclipse.jgit.merge.MergeStrategy}
290 	 * @param lowLevelResults
291 	 *            merge results as returned by
292 	 *            {@link org.eclipse.jgit.merge.ResolveMerger#getMergeResults()}
293 	 * @param failingPaths
294 	 *            list of paths causing this merge to fail as returned by
295 	 *            {@link org.eclipse.jgit.merge.ResolveMerger#getFailingPaths()}
296 	 * @param description
297 	 *            a user friendly description of the merge result
298 	 */
299 	public MergeResult(ObjectId./../../../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId newHead, ObjectId base,
300 			ObjectId[] mergedCommits, MergeStatus mergeStatus,
301 			MergeStrategy mergeStrategy,
302 			Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults,
303 			Map<String, MergeFailureReason> failingPaths, String description) {
304 		this.newHead = newHead;
305 		this.mergedCommits = mergedCommits;
306 		this.base = base;
307 		this.mergeStatus = mergeStatus;
308 		this.mergeStrategy = mergeStrategy;
309 		this.description = description;
310 		this.failingPaths = failingPaths;
311 		if (lowLevelResults != null)
312 			for (Map.Entry<String, org.eclipse.jgit.merge.MergeResult<?>> result : lowLevelResults
313 					.entrySet())
314 				addConflict(result.getKey(), result.getValue());
315 	}
316 
317 	/**
318 	 * Creates a new result that represents a checkout conflict before the
319 	 * operation even started for real.
320 	 *
321 	 * @param checkoutConflicts
322 	 *            the conflicting files
323 	 */
324 	public MergeResult(List<String> checkoutConflicts) {
325 		this.checkoutConflicts = checkoutConflicts;
326 		this.mergeStatus = MergeStatus.CHECKOUT_CONFLICT;
327 	}
328 
329 	/**
330 	 * Get the object the head points at after the merge
331 	 *
332 	 * @return the object the head points at after the merge
333 	 */
334 	public ObjectId getNewHead() {
335 		return newHead;
336 	}
337 
338 	/**
339 	 * Get the merge status
340 	 *
341 	 * @return the status the merge resulted in
342 	 */
343 	public MergeStatus getMergeStatus() {
344 		return mergeStatus;
345 	}
346 
347 	/**
348 	 * Get the commits which have been merged
349 	 *
350 	 * @return all the commits which have been merged together
351 	 */
352 	public ObjectId[] getMergedCommits() {
353 		return mergedCommits;
354 	}
355 
356 	/**
357 	 * Get the common base
358 	 *
359 	 * @return base the common base which was used to produce a content-merge.
360 	 *         May be <code>null</code> if the merge-result was produced without
361 	 *         computing a common base
362 	 */
363 	public ObjectId getBase() {
364 		return base;
365 	}
366 
367 	/** {@inheritDoc} */
368 	@SuppressWarnings("nls")
369 	@Override
370 	public String toString() {
371 		boolean first = true;
372 		StringBuilder commits = new StringBuilder();
373 		for (ObjectId commit : mergedCommits) {
374 			if (!first)
375 				commits.append(", ");
376 			else
377 				first = false;
378 			commits.append(ObjectId.toString(commit));
379 		}
380 		return MessageFormat.format(
381 				JGitText.get().mergeUsingStrategyResultedInDescription,
382 				commits, ObjectId.toString(base), mergeStrategy.getName(),
383 				mergeStatus, (description == null ? "" : ", " + description));
384 	}
385 
386 	/**
387 	 * Set conflicts
388 	 *
389 	 * @param conflicts
390 	 *            the conflicts to set
391 	 */
392 	public void setConflicts(Map<String, int[][]> conflicts) {
393 		this.conflicts = conflicts;
394 	}
395 
396 	/**
397 	 * Add a conflict
398 	 *
399 	 * @param path
400 	 *            path of the file to add a conflict for
401 	 * @param conflictingRanges
402 	 *            the conflicts to set
403 	 */
404 	public void addConflict(String path, int[][] conflictingRanges) {
405 		if (conflicts == null)
406 			conflicts = new HashMap<>();
407 		conflicts.put(path, conflictingRanges);
408 	}
409 
410 	/**
411 	 * Add a conflict
412 	 *
413 	 * @param path
414 	 *            path of the file to add a conflict for
415 	 * @param lowLevelResult
416 	 *            a {@link org.eclipse.jgit.merge.MergeResult}
417 	 */
418 	public void addConflict(String path, org.eclipse.jgit.merge.MergeResult<?> lowLevelResult) {
419 		if (!lowLevelResult.containsConflicts())
420 			return;
421 		if (conflicts == null)
422 			conflicts = new HashMap<>();
423 		int nrOfConflicts = 0;
424 		// just counting
425 		for (MergeChunk mergeChunk : lowLevelResult) {
426 			if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
427 				nrOfConflicts++;
428 			}
429 		}
430 		int currentConflict = -1;
431 		int[][] ret=new int[nrOfConflicts][mergedCommits.length+1];
432 		for (MergeChunk mergeChunk : lowLevelResult) {
433 			// to store the end of this chunk (end of the last conflicting range)
434 			int endOfChunk = 0;
435 			if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
436 				if (currentConflict > -1) {
437 					// there was a previous conflicting range for which the end
438 					// is not set yet - set it!
439 					ret[currentConflict][mergedCommits.length] = endOfChunk;
440 				}
441 				currentConflict++;
442 				endOfChunk = mergeChunk.getEnd();
443 				ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
444 			}
445 			if (mergeChunk.getConflictState().equals(ConflictState.NEXT_CONFLICTING_RANGE)) {
446 				if (mergeChunk.getEnd() > endOfChunk)
447 					endOfChunk = mergeChunk.getEnd();
448 				ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
449 			}
450 		}
451 		conflicts.put(path, ret);
452 	}
453 
454 	/**
455 	 * Returns information about the conflicts which occurred during a
456 	 * {@link org.eclipse.jgit.api.MergeCommand}. The returned value maps the
457 	 * path of a conflicting file to a two-dimensional int-array of line-numbers
458 	 * telling where in the file conflict markers for which merged commit can be
459 	 * found.
460 	 * <p>
461 	 * If the returned value contains a mapping "path"-&gt;[x][y]=z then this
462 	 * means
463 	 * <ul>
464 	 * <li>the file with path "path" contains conflicts</li>
465 	 * <li>if y &lt; "number of merged commits": for conflict number x in this
466 	 * file the chunk which was copied from commit number y starts on line
467 	 * number z. All numberings and line numbers start with 0.</li>
468 	 * <li>if y == "number of merged commits": the first non-conflicting line
469 	 * after conflict number x starts at line number z</li>
470 	 * </ul>
471 	 * <p>
472 	 * Example code how to parse this data:
473 	 *
474 	 * <pre>
475 	 * MergeResult m=...;
476 	 * Map&lt;String, int[][]&gt; allConflicts = m.getConflicts();
477 	 * for (String path : allConflicts.keySet()) {
478 	 * 	int[][] c = allConflicts.get(path);
479 	 * 	System.out.println("Conflicts in file " + path);
480 	 * 	for (int i = 0; i &lt; c.length; ++i) {
481 	 * 		System.out.println("  Conflict #" + i);
482 	 * 		for (int j = 0; j &lt; (c[i].length) - 1; ++j) {
483 	 * 			if (c[i][j] &gt;= 0)
484 	 * 				System.out.println("    Chunk for "
485 	 * 						+ m.getMergedCommits()[j] + " starts on line #"
486 	 * 						+ c[i][j]);
487 	 * 		}
488 	 * 	}
489 	 * }
490 	 * </pre>
491 	 *
492 	 * @return the conflicts or <code>null</code> if no conflict occurred
493 	 */
494 	public Map<String, int[][]> getConflicts() {
495 		return conflicts;
496 	}
497 
498 	/**
499 	 * Returns a list of paths causing this merge to fail as returned by
500 	 * {@link org.eclipse.jgit.merge.ResolveMerger#getFailingPaths()}
501 	 *
502 	 * @return the list of paths causing this merge to fail or <code>null</code>
503 	 *         if no failure occurred
504 	 */
505 	public Map<String, MergeFailureReason> getFailingPaths() {
506 		return failingPaths;
507 	}
508 
509 	/**
510 	 * Returns a list of paths that cause a checkout conflict. These paths
511 	 * prevent the operation from even starting.
512 	 *
513 	 * @return the list of files that caused the checkout conflict.
514 	 */
515 	public List<String> getCheckoutConflicts() {
516 		return checkoutConflicts;
517 	}
518 }