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

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