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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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.internal.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. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  55. /**
  56. * Encapsulates the result of a {@link MergeCommand}.
  57. */
  58. public class MergeResult {
  59. /**
  60. * The status the merge resulted in.
  61. */
  62. public enum MergeStatus {
  63. /** */
  64. FAST_FORWARD {
  65. @Override
  66. public String toString() {
  67. return "Fast-forward";
  68. }
  69. @Override
  70. public boolean isSuccessful() {
  71. return true;
  72. }
  73. },
  74. /** */
  75. ALREADY_UP_TO_DATE {
  76. @Override
  77. public String toString() {
  78. return "Already-up-to-date";
  79. }
  80. @Override
  81. public boolean isSuccessful() {
  82. return true;
  83. }
  84. },
  85. /** */
  86. FAILED {
  87. @Override
  88. public String toString() {
  89. return "Failed";
  90. }
  91. @Override
  92. public boolean isSuccessful() {
  93. return false;
  94. }
  95. },
  96. /** */
  97. MERGED {
  98. @Override
  99. public String toString() {
  100. return "Merged";
  101. }
  102. @Override
  103. public boolean isSuccessful() {
  104. return true;
  105. }
  106. },
  107. /** */
  108. CONFLICTING {
  109. @Override
  110. public String toString() {
  111. return "Conflicting";
  112. }
  113. @Override
  114. public boolean isSuccessful() {
  115. return false;
  116. }
  117. },
  118. /** */
  119. NOT_SUPPORTED {
  120. @Override
  121. public String toString() {
  122. return "Not-yet-supported";
  123. }
  124. @Override
  125. public boolean isSuccessful() {
  126. return false;
  127. }
  128. };
  129. /**
  130. * @return whether the status indicates a successful result
  131. */
  132. public abstract boolean isSuccessful();
  133. }
  134. private ObjectId[] mergedCommits;
  135. private ObjectId base;
  136. private ObjectId newHead;
  137. private Map<String, int[][]> conflicts;
  138. private MergeStatus mergeStatus;
  139. private String description;
  140. private MergeStrategy mergeStrategy;
  141. private Map<String, MergeFailureReason> failingPaths;
  142. /**
  143. * @param newHead
  144. * the object the head points at after the merge
  145. * @param base
  146. * the common base which was used to produce a content-merge. May
  147. * be <code>null</code> if the merge-result was produced without
  148. * computing a common base
  149. * @param mergedCommits
  150. * all the commits which have been merged together
  151. * @param mergeStatus
  152. * the status the merge resulted in
  153. * @param mergeStrategy
  154. * the used {@link MergeStrategy}
  155. * @param lowLevelResults
  156. * merge results as returned by
  157. * {@link ResolveMerger#getMergeResults()}
  158. * @since 2.0
  159. */
  160. public MergeResult(ObjectId newHead, ObjectId base,
  161. ObjectId[] mergedCommits, MergeStatus mergeStatus,
  162. MergeStrategy mergeStrategy,
  163. Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults) {
  164. this(newHead, base, mergedCommits, mergeStatus, mergeStrategy,
  165. lowLevelResults, null);
  166. }
  167. /**
  168. * @param newHead
  169. * the object the head points at after the merge
  170. * @param base
  171. * the common base which was used to produce a content-merge. May
  172. * be <code>null</code> if the merge-result was produced without
  173. * computing a common base
  174. * @param mergedCommits
  175. * all the commits which have been merged together
  176. * @param mergeStatus
  177. * the status the merge resulted in
  178. * @param mergeStrategy
  179. * the used {@link MergeStrategy}
  180. * @param lowLevelResults
  181. * merge results as returned by {@link ResolveMerger#getMergeResults()}
  182. * @param description
  183. * a user friendly description of the merge result
  184. */
  185. public MergeResult(ObjectId newHead, ObjectId base,
  186. ObjectId[] mergedCommits, MergeStatus mergeStatus,
  187. MergeStrategy mergeStrategy,
  188. Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults,
  189. String description) {
  190. this(newHead, base, mergedCommits, mergeStatus, mergeStrategy,
  191. lowLevelResults, null, description);
  192. }
  193. /**
  194. * @param newHead
  195. * the object the head points at after the merge
  196. * @param base
  197. * the common base which was used to produce a content-merge. May
  198. * be <code>null</code> if the merge-result was produced without
  199. * computing a common base
  200. * @param mergedCommits
  201. * all the commits which have been merged together
  202. * @param mergeStatus
  203. * the status the merge resulted in
  204. * @param mergeStrategy
  205. * the used {@link MergeStrategy}
  206. * @param lowLevelResults
  207. * merge results as returned by
  208. * {@link ResolveMerger#getMergeResults()}
  209. * @param failingPaths
  210. * list of paths causing this merge to fail as returned by
  211. * {@link ResolveMerger#getFailingPaths()}
  212. * @param description
  213. * a user friendly description of the merge result
  214. */
  215. public MergeResult(ObjectId newHead, ObjectId base,
  216. ObjectId[] mergedCommits, MergeStatus mergeStatus,
  217. MergeStrategy mergeStrategy,
  218. Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults,
  219. Map<String, MergeFailureReason> failingPaths, String description) {
  220. this.newHead = newHead;
  221. this.mergedCommits = mergedCommits;
  222. this.base = base;
  223. this.mergeStatus = mergeStatus;
  224. this.mergeStrategy = mergeStrategy;
  225. this.description = description;
  226. this.failingPaths = failingPaths;
  227. if (lowLevelResults != null)
  228. for (Map.Entry<String, org.eclipse.jgit.merge.MergeResult<?>> result : lowLevelResults
  229. .entrySet())
  230. addConflict(result.getKey(), result.getValue());
  231. }
  232. /**
  233. * @return the object the head points at after the merge
  234. */
  235. public ObjectId getNewHead() {
  236. return newHead;
  237. }
  238. /**
  239. * @return the status the merge resulted in
  240. */
  241. public MergeStatus getMergeStatus() {
  242. return mergeStatus;
  243. }
  244. /**
  245. * @return all the commits which have been merged together
  246. */
  247. public ObjectId[] getMergedCommits() {
  248. return mergedCommits;
  249. }
  250. /**
  251. * @return base the common base which was used to produce a content-merge.
  252. * May be <code>null</code> if the merge-result was produced without
  253. * computing a common base
  254. */
  255. public ObjectId getBase() {
  256. return base;
  257. }
  258. @Override
  259. public String toString() {
  260. boolean first = true;
  261. StringBuilder commits = new StringBuilder();
  262. for (ObjectId commit : mergedCommits) {
  263. if (!first)
  264. commits.append(", ");
  265. else
  266. first = false;
  267. commits.append(ObjectId.toString(commit));
  268. }
  269. return MessageFormat.format(
  270. JGitText.get().mergeUsingStrategyResultedInDescription,
  271. commits, ObjectId.toString(base), mergeStrategy.getName(),
  272. mergeStatus, (description == null ? "" : ", " + description));
  273. }
  274. /**
  275. * @param conflicts
  276. * the conflicts to set
  277. */
  278. public void setConflicts(Map<String, int[][]> conflicts) {
  279. this.conflicts = conflicts;
  280. }
  281. /**
  282. * @param path
  283. * @param conflictingRanges
  284. * the conflicts to set
  285. */
  286. public void addConflict(String path, int[][] conflictingRanges) {
  287. if (conflicts == null)
  288. conflicts = new HashMap<String, int[][]>();
  289. conflicts.put(path, conflictingRanges);
  290. }
  291. /**
  292. * @param path
  293. * @param lowLevelResult
  294. */
  295. public void addConflict(String path, org.eclipse.jgit.merge.MergeResult<?> lowLevelResult) {
  296. if (!lowLevelResult.containsConflicts())
  297. return;
  298. if (conflicts == null)
  299. conflicts = new HashMap<String, int[][]>();
  300. int nrOfConflicts = 0;
  301. // just counting
  302. for (MergeChunk mergeChunk : lowLevelResult) {
  303. if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
  304. nrOfConflicts++;
  305. }
  306. }
  307. int currentConflict = -1;
  308. int[][] ret=new int[nrOfConflicts][mergedCommits.length+1];
  309. for (MergeChunk mergeChunk : lowLevelResult) {
  310. // to store the end of this chunk (end of the last conflicting range)
  311. int endOfChunk = 0;
  312. if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
  313. if (currentConflict > -1) {
  314. // there was a previous conflicting range for which the end
  315. // is not set yet - set it!
  316. ret[currentConflict][mergedCommits.length] = endOfChunk;
  317. }
  318. currentConflict++;
  319. endOfChunk = mergeChunk.getEnd();
  320. ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
  321. }
  322. if (mergeChunk.getConflictState().equals(ConflictState.NEXT_CONFLICTING_RANGE)) {
  323. if (mergeChunk.getEnd() > endOfChunk)
  324. endOfChunk = mergeChunk.getEnd();
  325. ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
  326. }
  327. }
  328. conflicts.put(path, ret);
  329. }
  330. /**
  331. * Returns information about the conflicts which occurred during a
  332. * {@link MergeCommand}. The returned value maps the path of a conflicting
  333. * file to a two-dimensional int-array of line-numbers telling where in the
  334. * file conflict markers for which merged commit can be found.
  335. * <p>
  336. * If the returned value contains a mapping "path"->[x][y]=z then this means
  337. * <ul>
  338. * <li>the file with path "path" contains conflicts</li>
  339. * <li>if y < "number of merged commits": for conflict number x in this file
  340. * the chunk which was copied from commit number y starts on line number z.
  341. * All numberings and line numbers start with 0.</li>
  342. * <li>if y == "number of merged commits": the first non-conflicting line
  343. * after conflict number x starts at line number z</li>
  344. * </ul>
  345. * <p>
  346. * Example code how to parse this data:
  347. * <pre> MergeResult m=...;
  348. * Map<String, int[][]> allConflicts = m.getConflicts();
  349. * for (String path : allConflicts.keySet()) {
  350. * int[][] c = allConflicts.get(path);
  351. * System.out.println("Conflicts in file " + path);
  352. * for (int i = 0; i < c.length; ++i) {
  353. * System.out.println(" Conflict #" + i);
  354. * for (int j = 0; j < (c[i].length) - 1; ++j) {
  355. * if (c[i][j] >= 0)
  356. * System.out.println(" Chunk for "
  357. * + m.getMergedCommits()[j] + " starts on line #"
  358. * + c[i][j]);
  359. * }
  360. * }
  361. * }</pre>
  362. *
  363. * @return the conflicts or <code>null</code> if no conflict occurred
  364. */
  365. public Map<String, int[][]> getConflicts() {
  366. return conflicts;
  367. }
  368. /**
  369. * Returns a list of paths causing this merge to fail as returned by
  370. * {@link ResolveMerger#getFailingPaths()}
  371. *
  372. * @return the list of paths causing this merge to fail or <code>null</code>
  373. * if no failure occurred
  374. */
  375. public Map<String, MergeFailureReason> getFailingPaths() {
  376. return failingPaths;
  377. }
  378. }