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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.List;
  54. import org.eclipse.jgit.dircache.DirCache;
  55. import org.eclipse.jgit.dircache.DirCacheBuilder;
  56. import org.eclipse.jgit.dircache.DirCacheEntry;
  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.ObjectId;
  62. import org.eclipse.jgit.lib.ObjectInserter;
  63. import org.eclipse.jgit.lib.PersonIdent;
  64. import org.eclipse.jgit.lib.Repository;
  65. import org.eclipse.jgit.revwalk.RevCommit;
  66. import org.eclipse.jgit.revwalk.filter.RevFilter;
  67. import org.eclipse.jgit.treewalk.TreeWalk;
  68. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  69. /**
  70. * A three-way merger performing a content-merge if necessary across multiple
  71. * bases using recursion
  72. *
  73. * This merger extends the resolve merger and does several things differently:
  74. *
  75. * - allow more than one merge base, up to a maximum
  76. *
  77. * - uses "Lists" instead of Arrays for chained types
  78. *
  79. * - recursively merges the merge bases together to compute a usable base
  80. * @since 3.0
  81. */
  82. public class RecursiveMerger extends ResolveMerger {
  83. /**
  84. * The maximum number of merge bases. This merge will stop when the number
  85. * of merge bases exceeds this value
  86. */
  87. public final int MAX_BASES = 200;
  88. private PersonIdent ident = new PersonIdent(db);
  89. /**
  90. * Normal recursive merge when you want a choice of DirCache placement
  91. * inCore
  92. *
  93. * @param local
  94. * @param inCore
  95. */
  96. protected RecursiveMerger(Repository local, boolean inCore) {
  97. super(local, inCore);
  98. }
  99. /**
  100. * Normal recursive merge, implies not inCore
  101. *
  102. * @param local
  103. */
  104. protected RecursiveMerger(Repository local) {
  105. this(local, false);
  106. }
  107. /**
  108. * Get a single base commit for two given commits. If the two source commits
  109. * have more than one base commit recursively merge the base commits
  110. * together until you end up with a single base commit.
  111. *
  112. * @throws IOException
  113. * @throws IncorrectObjectTypeException
  114. */
  115. @Override
  116. protected RevCommit getBaseCommit(RevCommit a, RevCommit b)
  117. throws IncorrectObjectTypeException, IOException {
  118. return getBaseCommit(a, b, 0);
  119. }
  120. /**
  121. * Get a single base commit for two given commits. If the two source commits
  122. * have more than one base commit recursively merge the base commits
  123. * together until a virtual common base commit has been found.
  124. *
  125. * @param a
  126. * the first commit to be merged
  127. * @param b
  128. * the second commit to be merged
  129. * @param callDepth
  130. * the callDepth when this method is called recursively
  131. * @return the merge base of two commits
  132. * @throws IOException
  133. * @throws IncorrectObjectTypeException
  134. * one of the input objects is not a commit.
  135. * @throws NoMergeBaseException
  136. * too many merge bases are found or the computation of a common
  137. * merge base failed (e.g. because of a conflict).
  138. */
  139. protected RevCommit getBaseCommit(RevCommit a, RevCommit b, int callDepth)
  140. throws IOException {
  141. ArrayList<RevCommit> baseCommits = new ArrayList<RevCommit>();
  142. walk.reset();
  143. walk.setRevFilter(RevFilter.MERGE_BASE);
  144. walk.markStart(a);
  145. walk.markStart(b);
  146. RevCommit c;
  147. while ((c = walk.next()) != null)
  148. baseCommits.add(c);
  149. if (baseCommits.isEmpty())
  150. return null;
  151. if (baseCommits.size() == 1)
  152. return baseCommits.get(0);
  153. if (baseCommits.size() >= MAX_BASES)
  154. throw new NoMergeBaseException(NoMergeBaseException.MergeBaseFailureReason.TOO_MANY_MERGE_BASES, MessageFormat.format(
  155. JGitText.get().mergeRecursiveTooManyMergeBasesFor,
  156. Integer.valueOf(MAX_BASES), a.name(), b.name(),
  157. Integer.valueOf(baseCommits.size())));
  158. // We know we have more than one base commit. We have to do merges now
  159. // to determine a single base commit. We don't want to spoil the current
  160. // dircache and working tree with the results of this intermediate
  161. // merges. Therefore set the dircache to a new in-memory dircache and
  162. // disable that we update the working-tree. We set this back to the
  163. // original values once a single base commit is created.
  164. RevCommit currentBase = baseCommits.get(0);
  165. DirCache oldDircache = dircache;
  166. boolean oldIncore = inCore;
  167. WorkingTreeIterator oldWTreeIt = workingTreeIterator;
  168. workingTreeIterator = null;
  169. try {
  170. dircache = dircacheFromTree(currentBase.getTree());
  171. inCore = true;
  172. List<RevCommit> parents = new ArrayList<RevCommit>();
  173. parents.add(currentBase);
  174. for (int commitIdx = 1; commitIdx < baseCommits.size(); commitIdx++) {
  175. RevCommit nextBase = baseCommits.get(commitIdx);
  176. if (commitIdx >= MAX_BASES)
  177. throw new NoMergeBaseException(
  178. NoMergeBaseException.MergeBaseFailureReason.TOO_MANY_MERGE_BASES,
  179. MessageFormat.format(
  180. JGitText.get().mergeRecursiveTooManyMergeBasesFor,
  181. Integer.valueOf(MAX_BASES), a.name(), b.name(),
  182. Integer.valueOf(baseCommits.size())));
  183. parents.add(nextBase);
  184. if (mergeTrees(
  185. openTree(getBaseCommit(currentBase, nextBase,
  186. callDepth + 1).getTree()),
  187. currentBase.getTree(),
  188. nextBase.getTree()))
  189. currentBase = createCommitForTree(resultTree, parents);
  190. else
  191. throw new NoMergeBaseException(
  192. NoMergeBaseException.MergeBaseFailureReason.CONFLICTS_DURING_MERGE_BASE_CALCULATION,
  193. MessageFormat.format(
  194. JGitText.get().mergeRecursiveTooManyMergeBasesFor,
  195. Integer.valueOf(MAX_BASES), a.name(),
  196. b.name(),
  197. Integer.valueOf(baseCommits.size())));
  198. }
  199. } finally {
  200. inCore = oldIncore;
  201. dircache = oldDircache;
  202. workingTreeIterator = oldWTreeIt;
  203. }
  204. return currentBase;
  205. }
  206. /**
  207. * Create a new commit by explicitly specifying the content tree and the
  208. * parents. The commit message is not set and author/committer are set to
  209. * the current user.
  210. *
  211. * @param tree
  212. * the tree this commit should capture
  213. * @param parents
  214. * the list of parent commits
  215. * @return a new (persisted) commit
  216. * @throws IOException
  217. */
  218. private RevCommit createCommitForTree(ObjectId tree, List<RevCommit> parents)
  219. throws IOException {
  220. CommitBuilder c = new CommitBuilder();
  221. c.setParentIds(parents);
  222. c.setTreeId(tree);
  223. c.setAuthor(ident);
  224. c.setCommitter(ident);
  225. ObjectInserter odi = db.newObjectInserter();
  226. ObjectId newCommitId = odi.insert(c);
  227. odi.flush();
  228. RevCommit ret = walk.lookupCommit(newCommitId);
  229. walk.parseHeaders(ret);
  230. return ret;
  231. }
  232. /**
  233. * Create a new in memory dircache which has the same content as a given
  234. * tree.
  235. *
  236. * @param treeId
  237. * the tree which should be used to fill the dircache
  238. * @return a new in memory dircache
  239. * @throws IOException
  240. */
  241. private DirCache dircacheFromTree(ObjectId treeId) throws IOException {
  242. DirCache ret = DirCache.newInCore();
  243. DirCacheBuilder builder = ret.builder();
  244. TreeWalk tw = new TreeWalk(db);
  245. tw.addTree(treeId);
  246. tw.setRecursive(true);
  247. while (tw.next()) {
  248. DirCacheEntry e = new DirCacheEntry(tw.getRawPath());
  249. e.setFileMode(tw.getFileMode(0));
  250. e.setObjectId(tw.getObjectId(0));
  251. builder.add(e);
  252. }
  253. builder.finish();
  254. return ret;
  255. }
  256. }