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.

RecursiveMerger.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2012, Research In Motion Limited
  3. * Copyright (C) 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. /*
  45. * Contributors:
  46. * George Young - initial API and implementation
  47. * Christian Halstrick - initial API and implementation
  48. */
  49. package org.eclipse.jgit.merge;
  50. import java.io.IOException;
  51. import java.text.MessageFormat;
  52. import java.util.ArrayList;
  53. import java.util.Date;
  54. import java.util.List;
  55. import java.util.TimeZone;
  56. import org.eclipse.jgit.dircache.DirCache;
  57. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  58. import org.eclipse.jgit.errors.NoMergeBaseException;
  59. import org.eclipse.jgit.internal.JGitText;
  60. import org.eclipse.jgit.lib.CommitBuilder;
  61. import org.eclipse.jgit.lib.Config;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.ObjectInserter;
  64. import org.eclipse.jgit.lib.PersonIdent;
  65. import org.eclipse.jgit.lib.Repository;
  66. import org.eclipse.jgit.revwalk.RevCommit;
  67. import org.eclipse.jgit.revwalk.filter.RevFilter;
  68. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  69. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  70. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  71. /**
  72. * A three-way merger performing a content-merge if necessary across multiple
  73. * bases using recursion
  74. *
  75. * This merger extends the resolve merger and does several things differently:
  76. *
  77. * - allow more than one merge base, up to a maximum
  78. *
  79. * - uses "Lists" instead of Arrays for chained types
  80. *
  81. * - recursively merges the merge bases together to compute a usable base
  82. *
  83. * @since 3.0
  84. */
  85. public class RecursiveMerger extends ResolveMerger {
  86. /**
  87. * The maximum number of merge bases. This merge will stop when the number
  88. * of merge bases exceeds this value
  89. */
  90. public final int MAX_BASES = 200;
  91. /**
  92. * Normal recursive merge when you want a choice of DirCache placement
  93. * inCore
  94. *
  95. * @param local
  96. * a {@link org.eclipse.jgit.lib.Repository} object.
  97. * @param inCore
  98. * a boolean.
  99. */
  100. protected RecursiveMerger(Repository local, boolean inCore) {
  101. super(local, inCore);
  102. }
  103. /**
  104. * Normal recursive merge, implies not inCore
  105. *
  106. * @param local a {@link org.eclipse.jgit.lib.Repository} object.
  107. */
  108. protected RecursiveMerger(Repository local) {
  109. this(local, false);
  110. }
  111. /**
  112. * Normal recursive merge, implies inCore.
  113. *
  114. * @param inserter
  115. * an {@link org.eclipse.jgit.lib.ObjectInserter} object.
  116. * @param config
  117. * the repository configuration
  118. * @since 4.8
  119. */
  120. protected RecursiveMerger(ObjectInserter inserter, Config config) {
  121. super(inserter, config);
  122. }
  123. /**
  124. * {@inheritDoc}
  125. * <p>
  126. * Get a single base commit for two given commits. If the two source commits
  127. * have more than one base commit recursively merge the base commits
  128. * together until you end up with a single base commit.
  129. */
  130. @Override
  131. protected RevCommit getBaseCommit(RevCommit a, RevCommit b)
  132. throws IncorrectObjectTypeException, IOException {
  133. return getBaseCommit(a, b, 0);
  134. }
  135. /**
  136. * Get a single base commit for two given commits. If the two source commits
  137. * have more than one base commit recursively merge the base commits
  138. * together until a virtual common base commit has been found.
  139. *
  140. * @param a
  141. * the first commit to be merged
  142. * @param b
  143. * the second commit to be merged
  144. * @param callDepth
  145. * the callDepth when this method is called recursively
  146. * @return the merge base of two commits. If a criss-cross merge required a
  147. * synthetic merge base this commit is visible only the merger's
  148. * RevWalk and will not be in the repository.
  149. * @throws java.io.IOException
  150. * @throws IncorrectObjectTypeException
  151. * one of the input objects is not a commit.
  152. * @throws NoMergeBaseException
  153. * too many merge bases are found or the computation of a common
  154. * merge base failed (e.g. because of a conflict).
  155. */
  156. protected RevCommit getBaseCommit(RevCommit a, RevCommit b, int callDepth)
  157. throws IOException {
  158. ArrayList<RevCommit> baseCommits = new ArrayList<>();
  159. walk.reset();
  160. walk.setRevFilter(RevFilter.MERGE_BASE);
  161. walk.markStart(a);
  162. walk.markStart(b);
  163. RevCommit c;
  164. while ((c = walk.next()) != null)
  165. baseCommits.add(c);
  166. if (baseCommits.isEmpty())
  167. return null;
  168. if (baseCommits.size() == 1)
  169. return baseCommits.get(0);
  170. if (baseCommits.size() >= MAX_BASES)
  171. throw new NoMergeBaseException(NoMergeBaseException.MergeBaseFailureReason.TOO_MANY_MERGE_BASES, MessageFormat.format(
  172. JGitText.get().mergeRecursiveTooManyMergeBasesFor,
  173. Integer.valueOf(MAX_BASES), a.name(), b.name(),
  174. Integer.valueOf(baseCommits.size())));
  175. // We know we have more than one base commit. We have to do merges now
  176. // to determine a single base commit. We don't want to spoil the current
  177. // dircache and working tree with the results of this intermediate
  178. // merges. Therefore set the dircache to a new in-memory dircache and
  179. // disable that we update the working-tree. We set this back to the
  180. // original values once a single base commit is created.
  181. RevCommit currentBase = baseCommits.get(0);
  182. DirCache oldDircache = dircache;
  183. boolean oldIncore = inCore;
  184. WorkingTreeIterator oldWTreeIt = workingTreeIterator;
  185. workingTreeIterator = null;
  186. try {
  187. dircache = DirCache.read(reader, currentBase.getTree());
  188. inCore = true;
  189. List<RevCommit> parents = new ArrayList<>();
  190. parents.add(currentBase);
  191. for (int commitIdx = 1; commitIdx < baseCommits.size(); commitIdx++) {
  192. RevCommit nextBase = baseCommits.get(commitIdx);
  193. if (commitIdx >= MAX_BASES)
  194. throw new NoMergeBaseException(
  195. NoMergeBaseException.MergeBaseFailureReason.TOO_MANY_MERGE_BASES,
  196. MessageFormat.format(
  197. JGitText.get().mergeRecursiveTooManyMergeBasesFor,
  198. Integer.valueOf(MAX_BASES), a.name(), b.name(),
  199. Integer.valueOf(baseCommits.size())));
  200. parents.add(nextBase);
  201. RevCommit bc = getBaseCommit(currentBase, nextBase,
  202. callDepth + 1);
  203. AbstractTreeIterator bcTree = (bc == null) ? new EmptyTreeIterator()
  204. : openTree(bc.getTree());
  205. if (mergeTrees(bcTree, currentBase.getTree(),
  206. nextBase.getTree(), true))
  207. currentBase = createCommitForTree(resultTree, parents);
  208. else
  209. throw new NoMergeBaseException(
  210. NoMergeBaseException.MergeBaseFailureReason.CONFLICTS_DURING_MERGE_BASE_CALCULATION,
  211. MessageFormat.format(
  212. JGitText.get().mergeRecursiveConflictsWhenMergingCommonAncestors,
  213. currentBase.getName(), nextBase.getName()));
  214. }
  215. } finally {
  216. inCore = oldIncore;
  217. dircache = oldDircache;
  218. workingTreeIterator = oldWTreeIt;
  219. toBeCheckedOut.clear();
  220. toBeDeleted.clear();
  221. modifiedFiles.clear();
  222. unmergedPaths.clear();
  223. mergeResults.clear();
  224. failingPaths.clear();
  225. }
  226. return currentBase;
  227. }
  228. /**
  229. * Create a new commit by explicitly specifying the content tree and the
  230. * parents. The commit message is not set and author/committer are set to
  231. * the current user.
  232. *
  233. * @param tree
  234. * the tree this commit should capture
  235. * @param parents
  236. * the list of parent commits
  237. * @return a new commit visible only within this merger's RevWalk.
  238. * @throws IOException
  239. */
  240. private RevCommit createCommitForTree(ObjectId tree, List<RevCommit> parents)
  241. throws IOException {
  242. CommitBuilder c = new CommitBuilder();
  243. c.setTreeId(tree);
  244. c.setParentIds(parents);
  245. c.setAuthor(mockAuthor(parents));
  246. c.setCommitter(c.getAuthor());
  247. return RevCommit.parse(walk, c.build());
  248. }
  249. private static PersonIdent mockAuthor(List<RevCommit> parents) {
  250. String name = RecursiveMerger.class.getSimpleName();
  251. int time = 0;
  252. for (RevCommit p : parents)
  253. time = Math.max(time, p.getCommitTime());
  254. return new PersonIdent(
  255. name, name + "@JGit", //$NON-NLS-1$
  256. new Date((time + 1) * 1000L),
  257. TimeZone.getTimeZone("GMT+0000")); //$NON-NLS-1$
  258. }
  259. }