You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MergeResult.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
  3. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.api;
  45. import java.text.MessageFormat;
  46. import java.util.HashMap;
  47. import java.util.Map;
  48. import org.eclipse.jgit.JGitText;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. import org.eclipse.jgit.merge.MergeChunk;
  51. import org.eclipse.jgit.merge.MergeChunk.ConflictState;
  52. import org.eclipse.jgit.merge.MergeStrategy;
  53. import org.eclipse.jgit.merge.ResolveMerger;
  54. /**
  55. * Encapsulates the result of a {@link MergeCommand}.
  56. */
  57. public class MergeResult {
  58. /**
  59. * The status the merge resulted in.
  60. */
  61. public enum MergeStatus {
  62. /** */
  63. FAST_FORWARD {
  64. @Override
  65. public String toString() {
  66. return "Fast-forward";
  67. }
  68. },
  69. /** */
  70. ALREADY_UP_TO_DATE {
  71. public String toString() {
  72. return "Already-up-to-date";
  73. }
  74. },
  75. /** */
  76. FAILED {
  77. public String toString() {
  78. return "Failed";
  79. }
  80. },
  81. /** */
  82. MERGED {
  83. public String toString() {
  84. return "Merged";
  85. }
  86. },
  87. /** */
  88. CONFLICTING {
  89. public String toString() {
  90. return "Conflicting";
  91. }
  92. },
  93. /** */
  94. NOT_SUPPORTED {
  95. public String toString() {
  96. return "Not-yet-supported";
  97. }
  98. }
  99. }
  100. private ObjectId[] mergedCommits;
  101. private ObjectId base;
  102. private ObjectId newHead;
  103. private Map<String, int[][]> conflicts;
  104. private MergeStatus mergeStatus;
  105. private String description;
  106. private MergeStrategy mergeStrategy;
  107. /**
  108. * @param newHead
  109. * the object the head points at after the merge
  110. * @param base
  111. * the common base which was used to produce a content-merge. May
  112. * be <code>null</code> if the merge-result was produced without
  113. * computing a common base
  114. * @param mergedCommits
  115. * all the commits which have been merged together
  116. * @param mergeStatus
  117. * the status the merge resulted in
  118. * @param mergeStrategy
  119. * the used {@link MergeStrategy}
  120. * @param lowLevelResults
  121. * merge results as returned by {@link ResolveMerger#getMergeResults()}
  122. */
  123. public MergeResult(ObjectId newHead, ObjectId base,
  124. ObjectId[] mergedCommits, MergeStatus mergeStatus,
  125. Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults,
  126. MergeStrategy mergeStrategy) {
  127. this(newHead, base, mergedCommits, mergeStatus, mergeStrategy, lowLevelResults, null);
  128. }
  129. /**
  130. * @param newHead
  131. * the object the head points at after the merge
  132. * @param base
  133. * the common base which was used to produce a content-merge. May
  134. * be <code>null</code> if the merge-result was produced without
  135. * computing a common base
  136. * @param mergedCommits
  137. * all the commits which have been merged together
  138. * @param mergeStatus
  139. * the status the merge resulted in
  140. * @param mergeStrategy
  141. * the used {@link MergeStrategy}
  142. * @param lowLevelResults
  143. * merge results as returned by {@link ResolveMerger#getMergeResults()}
  144. * @param description
  145. * a user friendly description of the merge result
  146. */
  147. public MergeResult(ObjectId newHead, ObjectId base,
  148. ObjectId[] mergedCommits, MergeStatus mergeStatus,
  149. MergeStrategy mergeStrategy,
  150. Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults,
  151. String description) {
  152. this.newHead = newHead;
  153. this.mergedCommits = mergedCommits;
  154. this.base = base;
  155. this.mergeStatus = mergeStatus;
  156. this.mergeStrategy = mergeStrategy;
  157. this.description = description;
  158. if (lowLevelResults != null)
  159. for (Map.Entry<String, org.eclipse.jgit.merge.MergeResult<?>> result : lowLevelResults
  160. .entrySet())
  161. addConflict(result.getKey(), result.getValue());
  162. }
  163. /**
  164. * @return the object the head points at after the merge
  165. */
  166. public ObjectId getNewHead() {
  167. return newHead;
  168. }
  169. /**
  170. * @return the status the merge resulted in
  171. */
  172. public MergeStatus getMergeStatus() {
  173. return mergeStatus;
  174. }
  175. /**
  176. * @return all the commits which have been merged together
  177. */
  178. public ObjectId[] getMergedCommits() {
  179. return mergedCommits;
  180. }
  181. /**
  182. * @return base the common base which was used to produce a content-merge.
  183. * May be <code>null</code> if the merge-result was produced without
  184. * computing a common base
  185. */
  186. public ObjectId getBase() {
  187. return base;
  188. }
  189. @Override
  190. public String toString() {
  191. boolean first = true;
  192. StringBuilder commits = new StringBuilder();
  193. for (ObjectId commit : mergedCommits) {
  194. if (!first)
  195. commits.append(", ");
  196. else
  197. first = false;
  198. commits.append(ObjectId.toString(commit));
  199. }
  200. return MessageFormat.format(
  201. JGitText.get().mergeUsingStrategyResultedInDescription,
  202. commits, ObjectId.toString(base), mergeStrategy.getName(),
  203. mergeStatus, (description == null ? "" : ", " + description));
  204. }
  205. /**
  206. * @param conflicts
  207. * the conflicts to set
  208. */
  209. public void setConflicts(Map<String, int[][]> conflicts) {
  210. this.conflicts = conflicts;
  211. }
  212. /**
  213. * @param path
  214. * @param conflictingRanges
  215. * the conflicts to set
  216. */
  217. public void addConflict(String path, int[][] conflictingRanges) {
  218. if (conflicts == null)
  219. conflicts = new HashMap<String, int[][]>();
  220. conflicts.put(path, conflictingRanges);
  221. }
  222. /**
  223. * @param path
  224. * @param lowLevelResult
  225. */
  226. public void addConflict(String path, org.eclipse.jgit.merge.MergeResult<?> lowLevelResult) {
  227. if (conflicts == null)
  228. conflicts = new HashMap<String, int[][]>();
  229. int nrOfConflicts = 0;
  230. // just counting
  231. for (MergeChunk mergeChunk : lowLevelResult) {
  232. if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
  233. nrOfConflicts++;
  234. }
  235. }
  236. int currentConflict = -1;
  237. int[][] ret=new int[nrOfConflicts][mergedCommits.length+1];
  238. for (MergeChunk mergeChunk : lowLevelResult) {
  239. // to store the end of this chunk (end of the last conflicting range)
  240. int endOfChunk = 0;
  241. if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
  242. if (currentConflict > -1) {
  243. // there was a previous conflicting range for which the end
  244. // is not set yet - set it!
  245. ret[currentConflict][mergedCommits.length] = endOfChunk;
  246. }
  247. currentConflict++;
  248. endOfChunk = mergeChunk.getEnd();
  249. ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
  250. }
  251. if (mergeChunk.getConflictState().equals(ConflictState.NEXT_CONFLICTING_RANGE)) {
  252. if (mergeChunk.getEnd() > endOfChunk)
  253. endOfChunk = mergeChunk.getEnd();
  254. ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
  255. }
  256. }
  257. conflicts.put(path, ret);
  258. }
  259. /**
  260. * Returns information about the conflicts which occurred during a
  261. * {@link MergeCommand}. The returned value maps the path of a conflicting
  262. * file to a two-dimensional int-array of line-numbers telling where in the
  263. * file conflict markers for which merged commit can be found.
  264. * <p>
  265. * If the returned value contains a mapping "path"->[x][y]=z then this means
  266. * <ul>
  267. * <li>the file with path "path" contains conflicts</li>
  268. * <li>if y < "number of merged commits": for conflict number x in this file
  269. * the chunk which was copied from commit number y starts on line number z.
  270. * All numberings and line numbers start with 0.</li>
  271. * <li>if y == "number of merged commits": the first non-conflicting line
  272. * after conflict number x starts at line number z</li>
  273. * </ul>
  274. * <p>
  275. * Example code how to parse this data:
  276. * <pre> MergeResult m=...;
  277. * Map<String, int[][]> allConflicts = m.getConflicts();
  278. * for (String path : allConflicts.keySet()) {
  279. * int[][] c = allConflicts.get(path);
  280. * System.out.println("Conflicts in file " + path);
  281. * for (int i = 0; i < c.length; ++i) {
  282. * System.out.println(" Conflict #" + i);
  283. * for (int j = 0; j < (c[i].length) - 1; ++j) {
  284. * if (c[i][j] >= 0)
  285. * System.out.println(" Chunk for "
  286. * + m.getMergedCommits()[j] + " starts on line #"
  287. * + c[i][j]);
  288. * }
  289. * }
  290. * }</pre>
  291. *
  292. * @return the conflicts or <code>null</code> if no conflict occured
  293. */
  294. public Map<String, int[][]> getConflicts() {
  295. return conflicts;
  296. }
  297. }