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

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