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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. NOT_SUPPORTED {
  146. @Override
  147. public String toString() {
  148. return "Not-yet-supported";
  149. }
  150. @Override
  151. public boolean isSuccessful() {
  152. return false;
  153. }
  154. };
  155. /**
  156. * @return whether the status indicates a successful result
  157. */
  158. public abstract boolean isSuccessful();
  159. }
  160. private ObjectId[] mergedCommits;
  161. private ObjectId base;
  162. private ObjectId newHead;
  163. private Map<String, int[][]> conflicts;
  164. private MergeStatus mergeStatus;
  165. private String description;
  166. private MergeStrategy mergeStrategy;
  167. private Map<String, MergeFailureReason> failingPaths;
  168. /**
  169. * @param newHead
  170. * the object the head points at after the merge
  171. * @param base
  172. * the common base which was used to produce a content-merge. May
  173. * be <code>null</code> if the merge-result was produced without
  174. * computing a common base
  175. * @param mergedCommits
  176. * all the commits which have been merged together
  177. * @param mergeStatus
  178. * the status the merge resulted in
  179. * @param mergeStrategy
  180. * the used {@link MergeStrategy}
  181. * @param lowLevelResults
  182. * merge results as returned by
  183. * {@link ResolveMerger#getMergeResults()}
  184. * @since 2.0
  185. */
  186. public MergeResult(ObjectId newHead, ObjectId base,
  187. ObjectId[] mergedCommits, MergeStatus mergeStatus,
  188. MergeStrategy mergeStrategy,
  189. Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults) {
  190. this(newHead, base, mergedCommits, mergeStatus, mergeStrategy,
  191. lowLevelResults, null);
  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 {@link ResolveMerger#getMergeResults()}
  208. * @param description
  209. * a user friendly description of the merge result
  210. */
  211. public MergeResult(ObjectId newHead, ObjectId base,
  212. ObjectId[] mergedCommits, MergeStatus mergeStatus,
  213. MergeStrategy mergeStrategy,
  214. Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults,
  215. String description) {
  216. this(newHead, base, mergedCommits, mergeStatus, mergeStrategy,
  217. lowLevelResults, null, description);
  218. }
  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 MergeStrategy}
  232. * @param lowLevelResults
  233. * merge results as returned by
  234. * {@link ResolveMerger#getMergeResults()}
  235. * @param failingPaths
  236. * list of paths causing this merge to fail as returned by
  237. * {@link ResolveMerger#getFailingPaths()}
  238. * @param description
  239. * a user friendly description of the merge result
  240. */
  241. public MergeResult(ObjectId newHead, ObjectId base,
  242. ObjectId[] mergedCommits, MergeStatus mergeStatus,
  243. MergeStrategy mergeStrategy,
  244. Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults,
  245. Map<String, MergeFailureReason> failingPaths, String description) {
  246. this.newHead = newHead;
  247. this.mergedCommits = mergedCommits;
  248. this.base = base;
  249. this.mergeStatus = mergeStatus;
  250. this.mergeStrategy = mergeStrategy;
  251. this.description = description;
  252. this.failingPaths = failingPaths;
  253. if (lowLevelResults != null)
  254. for (Map.Entry<String, org.eclipse.jgit.merge.MergeResult<?>> result : lowLevelResults
  255. .entrySet())
  256. addConflict(result.getKey(), result.getValue());
  257. }
  258. /**
  259. * @return the object the head points at after the merge
  260. */
  261. public ObjectId getNewHead() {
  262. return newHead;
  263. }
  264. /**
  265. * @return the status the merge resulted in
  266. */
  267. public MergeStatus getMergeStatus() {
  268. return mergeStatus;
  269. }
  270. /**
  271. * @return all the commits which have been merged together
  272. */
  273. public ObjectId[] getMergedCommits() {
  274. return mergedCommits;
  275. }
  276. /**
  277. * @return base the common base which was used to produce a content-merge.
  278. * May be <code>null</code> if the merge-result was produced without
  279. * computing a common base
  280. */
  281. public ObjectId getBase() {
  282. return base;
  283. }
  284. @Override
  285. public String toString() {
  286. boolean first = true;
  287. StringBuilder commits = new StringBuilder();
  288. for (ObjectId commit : mergedCommits) {
  289. if (!first)
  290. commits.append(", ");
  291. else
  292. first = false;
  293. commits.append(ObjectId.toString(commit));
  294. }
  295. return MessageFormat.format(
  296. JGitText.get().mergeUsingStrategyResultedInDescription,
  297. commits, ObjectId.toString(base), mergeStrategy.getName(),
  298. mergeStatus, (description == null ? "" : ", " + description));
  299. }
  300. /**
  301. * @param conflicts
  302. * the conflicts to set
  303. */
  304. public void setConflicts(Map<String, int[][]> conflicts) {
  305. this.conflicts = conflicts;
  306. }
  307. /**
  308. * @param path
  309. * @param conflictingRanges
  310. * the conflicts to set
  311. */
  312. public void addConflict(String path, int[][] conflictingRanges) {
  313. if (conflicts == null)
  314. conflicts = new HashMap<String, int[][]>();
  315. conflicts.put(path, conflictingRanges);
  316. }
  317. /**
  318. * @param path
  319. * @param lowLevelResult
  320. */
  321. public void addConflict(String path, org.eclipse.jgit.merge.MergeResult<?> lowLevelResult) {
  322. if (!lowLevelResult.containsConflicts())
  323. return;
  324. if (conflicts == null)
  325. conflicts = new HashMap<String, int[][]>();
  326. int nrOfConflicts = 0;
  327. // just counting
  328. for (MergeChunk mergeChunk : lowLevelResult) {
  329. if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
  330. nrOfConflicts++;
  331. }
  332. }
  333. int currentConflict = -1;
  334. int[][] ret=new int[nrOfConflicts][mergedCommits.length+1];
  335. for (MergeChunk mergeChunk : lowLevelResult) {
  336. // to store the end of this chunk (end of the last conflicting range)
  337. int endOfChunk = 0;
  338. if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
  339. if (currentConflict > -1) {
  340. // there was a previous conflicting range for which the end
  341. // is not set yet - set it!
  342. ret[currentConflict][mergedCommits.length] = endOfChunk;
  343. }
  344. currentConflict++;
  345. endOfChunk = mergeChunk.getEnd();
  346. ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
  347. }
  348. if (mergeChunk.getConflictState().equals(ConflictState.NEXT_CONFLICTING_RANGE)) {
  349. if (mergeChunk.getEnd() > endOfChunk)
  350. endOfChunk = mergeChunk.getEnd();
  351. ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
  352. }
  353. }
  354. conflicts.put(path, ret);
  355. }
  356. /**
  357. * Returns information about the conflicts which occurred during a
  358. * {@link MergeCommand}. The returned value maps the path of a conflicting
  359. * file to a two-dimensional int-array of line-numbers telling where in the
  360. * file conflict markers for which merged commit can be found.
  361. * <p>
  362. * If the returned value contains a mapping "path"->[x][y]=z then this means
  363. * <ul>
  364. * <li>the file with path "path" contains conflicts</li>
  365. * <li>if y < "number of merged commits": for conflict number x in this file
  366. * the chunk which was copied from commit number y starts on line number z.
  367. * All numberings and line numbers start with 0.</li>
  368. * <li>if y == "number of merged commits": the first non-conflicting line
  369. * after conflict number x starts at line number z</li>
  370. * </ul>
  371. * <p>
  372. * Example code how to parse this data:
  373. * <pre> MergeResult m=...;
  374. * Map<String, int[][]> allConflicts = m.getConflicts();
  375. * for (String path : allConflicts.keySet()) {
  376. * int[][] c = allConflicts.get(path);
  377. * System.out.println("Conflicts in file " + path);
  378. * for (int i = 0; i < c.length; ++i) {
  379. * System.out.println(" Conflict #" + i);
  380. * for (int j = 0; j < (c[i].length) - 1; ++j) {
  381. * if (c[i][j] >= 0)
  382. * System.out.println(" Chunk for "
  383. * + m.getMergedCommits()[j] + " starts on line #"
  384. * + c[i][j]);
  385. * }
  386. * }
  387. * }</pre>
  388. *
  389. * @return the conflicts or <code>null</code> if no conflict occurred
  390. */
  391. public Map<String, int[][]> getConflicts() {
  392. return conflicts;
  393. }
  394. /**
  395. * Returns a list of paths causing this merge to fail as returned by
  396. * {@link ResolveMerger#getFailingPaths()}
  397. *
  398. * @return the list of paths causing this merge to fail or <code>null</code>
  399. * if no failure occurred
  400. */
  401. public Map<String, MergeFailureReason> getFailingPaths() {
  402. return failingPaths;
  403. }
  404. }