Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MergeResult.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. @Override
  298. public String toString() {
  299. boolean first = true;
  300. StringBuilder commits = new StringBuilder();
  301. for (ObjectId commit : mergedCommits) {
  302. if (!first)
  303. commits.append(", ");
  304. else
  305. first = false;
  306. commits.append(ObjectId.toString(commit));
  307. }
  308. return MessageFormat.format(
  309. JGitText.get().mergeUsingStrategyResultedInDescription,
  310. commits, ObjectId.toString(base), mergeStrategy.getName(),
  311. mergeStatus, (description == null ? "" : ", " + description));
  312. }
  313. /**
  314. * @param conflicts
  315. * the conflicts to set
  316. */
  317. public void setConflicts(Map<String, int[][]> conflicts) {
  318. this.conflicts = conflicts;
  319. }
  320. /**
  321. * @param path
  322. * @param conflictingRanges
  323. * the conflicts to set
  324. */
  325. public void addConflict(String path, int[][] conflictingRanges) {
  326. if (conflicts == null)
  327. conflicts = new HashMap<String, int[][]>();
  328. conflicts.put(path, conflictingRanges);
  329. }
  330. /**
  331. * @param path
  332. * @param lowLevelResult
  333. */
  334. public void addConflict(String path, org.eclipse.jgit.merge.MergeResult<?> lowLevelResult) {
  335. if (!lowLevelResult.containsConflicts())
  336. return;
  337. if (conflicts == null)
  338. conflicts = new HashMap<String, int[][]>();
  339. int nrOfConflicts = 0;
  340. // just counting
  341. for (MergeChunk mergeChunk : lowLevelResult) {
  342. if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
  343. nrOfConflicts++;
  344. }
  345. }
  346. int currentConflict = -1;
  347. int[][] ret=new int[nrOfConflicts][mergedCommits.length+1];
  348. for (MergeChunk mergeChunk : lowLevelResult) {
  349. // to store the end of this chunk (end of the last conflicting range)
  350. int endOfChunk = 0;
  351. if (mergeChunk.getConflictState().equals(ConflictState.FIRST_CONFLICTING_RANGE)) {
  352. if (currentConflict > -1) {
  353. // there was a previous conflicting range for which the end
  354. // is not set yet - set it!
  355. ret[currentConflict][mergedCommits.length] = endOfChunk;
  356. }
  357. currentConflict++;
  358. endOfChunk = mergeChunk.getEnd();
  359. ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
  360. }
  361. if (mergeChunk.getConflictState().equals(ConflictState.NEXT_CONFLICTING_RANGE)) {
  362. if (mergeChunk.getEnd() > endOfChunk)
  363. endOfChunk = mergeChunk.getEnd();
  364. ret[currentConflict][mergeChunk.getSequenceIndex()] = mergeChunk.getBegin();
  365. }
  366. }
  367. conflicts.put(path, ret);
  368. }
  369. /**
  370. * Returns information about the conflicts which occurred during a
  371. * {@link MergeCommand}. The returned value maps the path of a conflicting
  372. * file to a two-dimensional int-array of line-numbers telling where in the
  373. * file conflict markers for which merged commit can be found.
  374. * <p>
  375. * If the returned value contains a mapping "path"->[x][y]=z then this means
  376. * <ul>
  377. * <li>the file with path "path" contains conflicts</li>
  378. * <li>if y < "number of merged commits": for conflict number x in this file
  379. * the chunk which was copied from commit number y starts on line number z.
  380. * All numberings and line numbers start with 0.</li>
  381. * <li>if y == "number of merged commits": the first non-conflicting line
  382. * after conflict number x starts at line number z</li>
  383. * </ul>
  384. * <p>
  385. * Example code how to parse this data:
  386. * <pre> MergeResult m=...;
  387. * Map<String, int[][]> allConflicts = m.getConflicts();
  388. * for (String path : allConflicts.keySet()) {
  389. * int[][] c = allConflicts.get(path);
  390. * System.out.println("Conflicts in file " + path);
  391. * for (int i = 0; i < c.length; ++i) {
  392. * System.out.println(" Conflict #" + i);
  393. * for (int j = 0; j < (c[i].length) - 1; ++j) {
  394. * if (c[i][j] >= 0)
  395. * System.out.println(" Chunk for "
  396. * + m.getMergedCommits()[j] + " starts on line #"
  397. * + c[i][j]);
  398. * }
  399. * }
  400. * }</pre>
  401. *
  402. * @return the conflicts or <code>null</code> if no conflict occurred
  403. */
  404. public Map<String, int[][]> getConflicts() {
  405. return conflicts;
  406. }
  407. /**
  408. * Returns a list of paths causing this merge to fail as returned by
  409. * {@link ResolveMerger#getFailingPaths()}
  410. *
  411. * @return the list of paths causing this merge to fail or <code>null</code>
  412. * if no failure occurred
  413. */
  414. public Map<String, MergeFailureReason> getFailingPaths() {
  415. return failingPaths;
  416. }
  417. }