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

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