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.

Merger.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.merge;
  44. import java.io.IOException;
  45. import java.text.MessageFormat;
  46. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  47. import org.eclipse.jgit.errors.NoMergeBaseException;
  48. import org.eclipse.jgit.errors.NoMergeBaseException.MergeBaseFailureReason;
  49. import org.eclipse.jgit.internal.JGitText;
  50. import org.eclipse.jgit.lib.AnyObjectId;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import org.eclipse.jgit.lib.ObjectInserter;
  54. import org.eclipse.jgit.lib.Repository;
  55. import org.eclipse.jgit.lib.ObjectReader;
  56. import org.eclipse.jgit.revwalk.RevCommit;
  57. import org.eclipse.jgit.revwalk.RevObject;
  58. import org.eclipse.jgit.revwalk.RevTree;
  59. import org.eclipse.jgit.revwalk.RevWalk;
  60. import org.eclipse.jgit.revwalk.filter.RevFilter;
  61. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  62. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  63. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  64. /**
  65. * Instance of a specific {@link MergeStrategy} for a single {@link Repository}.
  66. */
  67. public abstract class Merger {
  68. /** The repository this merger operates on. */
  69. protected final Repository db;
  70. /** Reader to support {@link #walk} and other object loading. */
  71. protected final ObjectReader reader;
  72. /** A RevWalk for computing merge bases, or listing incoming commits. */
  73. protected final RevWalk walk;
  74. private ObjectInserter inserter;
  75. /** The original objects supplied in the merge; this can be any tree-ish. */
  76. protected RevObject[] sourceObjects;
  77. /** If {@link #sourceObjects}[i] is a commit, this is the commit. */
  78. protected RevCommit[] sourceCommits;
  79. /** The trees matching every entry in {@link #sourceObjects}. */
  80. protected RevTree[] sourceTrees;
  81. /**
  82. * Create a new merge instance for a repository.
  83. *
  84. * @param local
  85. * the repository this merger will read and write data on.
  86. */
  87. protected Merger(final Repository local) {
  88. db = local;
  89. reader = db.newObjectReader();
  90. walk = new RevWalk(reader);
  91. }
  92. /**
  93. * @return the repository this merger operates on.
  94. */
  95. public Repository getRepository() {
  96. return db;
  97. }
  98. /**
  99. * @return an object writer to create objects in {@link #getRepository()}.
  100. * If no inserter has been set on this instance, one will be created
  101. * and returned by all future calls.
  102. */
  103. public ObjectInserter getObjectInserter() {
  104. if (inserter == null)
  105. inserter = getRepository().newObjectInserter();
  106. return inserter;
  107. }
  108. /**
  109. * Set the inserter this merger will use to create objects.
  110. * <p>
  111. * If an inserter was already set on this instance (such as by a prior set,
  112. * or a prior call to {@link #getObjectInserter()}), the prior inserter will
  113. * be released first.
  114. *
  115. * @param oi
  116. * the inserter instance to use. Must be associated with the
  117. * repository instance returned by {@link #getRepository()}.
  118. */
  119. public void setObjectInserter(ObjectInserter oi) {
  120. if (inserter != null)
  121. inserter.release();
  122. inserter = oi;
  123. }
  124. /**
  125. * Merge together two or more tree-ish objects.
  126. * <p>
  127. * Any tree-ish may be supplied as inputs. Commits and/or tags pointing at
  128. * trees or commits may be passed as input objects.
  129. *
  130. * @param tips
  131. * source trees to be combined together. The merge base is not
  132. * included in this set.
  133. * @return true if the merge was completed without conflicts; false if the
  134. * merge strategy cannot handle this merge or there were conflicts
  135. * preventing it from automatically resolving all paths.
  136. * @throws IncorrectObjectTypeException
  137. * one of the input objects is not a commit, but the strategy
  138. * requires it to be a commit.
  139. * @throws IOException
  140. * one or more sources could not be read, or outputs could not
  141. * be written to the Repository.
  142. */
  143. public boolean merge(final AnyObjectId... tips) throws IOException {
  144. sourceObjects = new RevObject[tips.length];
  145. for (int i = 0; i < tips.length; i++)
  146. sourceObjects[i] = walk.parseAny(tips[i]);
  147. sourceCommits = new RevCommit[sourceObjects.length];
  148. for (int i = 0; i < sourceObjects.length; i++) {
  149. try {
  150. sourceCommits[i] = walk.parseCommit(sourceObjects[i]);
  151. } catch (IncorrectObjectTypeException err) {
  152. sourceCommits[i] = null;
  153. }
  154. }
  155. sourceTrees = new RevTree[sourceObjects.length];
  156. for (int i = 0; i < sourceObjects.length; i++)
  157. sourceTrees[i] = walk.parseTree(sourceObjects[i]);
  158. try {
  159. boolean ok = mergeImpl();
  160. if (ok && inserter != null)
  161. inserter.flush();
  162. return ok;
  163. } finally {
  164. if (inserter != null)
  165. inserter.release();
  166. reader.release();
  167. }
  168. }
  169. /**
  170. * Create an iterator to walk the merge base of two commits.
  171. *
  172. * @param a
  173. * the first commit in {@link #sourceObjects}.
  174. * @param b
  175. * the second commit in {@link #sourceObjects}.
  176. * @return the new iterator
  177. * @throws IncorrectObjectTypeException
  178. * one of the input objects is not a commit.
  179. * @throws IOException
  180. * objects are missing or multiple merge bases were found.
  181. */
  182. protected AbstractTreeIterator mergeBase(RevCommit a, RevCommit b)
  183. throws IOException {
  184. RevCommit base = getBaseCommit(a, b);
  185. return (base == null) ? new EmptyTreeIterator() : openTree(base.getTree());
  186. }
  187. /**
  188. * Return the merge base of two commits.
  189. *
  190. * @param aIdx
  191. * index of the first commit in {@link #sourceObjects}.
  192. * @param bIdx
  193. * index of the second commit in {@link #sourceObjects}.
  194. * @return the merge base of two commits
  195. * @throws IncorrectObjectTypeException
  196. * one of the input objects is not a commit.
  197. * @throws IOException
  198. * objects are missing or multiple merge bases were found.
  199. */
  200. public RevCommit getBaseCommit(final int aIdx, final int bIdx)
  201. throws IncorrectObjectTypeException,
  202. IOException {
  203. if (sourceCommits[aIdx] == null)
  204. throw new IncorrectObjectTypeException(sourceObjects[aIdx],
  205. Constants.TYPE_COMMIT);
  206. if (sourceCommits[bIdx] == null)
  207. throw new IncorrectObjectTypeException(sourceObjects[bIdx],
  208. Constants.TYPE_COMMIT);
  209. return getBaseCommit(sourceCommits[aIdx], sourceCommits[bIdx]);
  210. }
  211. /**
  212. * Return the merge base of two commits.
  213. *
  214. * @param a
  215. * the first commit in {@link #sourceObjects}.
  216. * @param b
  217. * the second commit in {@link #sourceObjects}.
  218. * @return the merge base of two commits
  219. * @throws IncorrectObjectTypeException
  220. * one of the input objects is not a commit.
  221. * @throws IOException
  222. * objects are missing or multiple merge bases were found.
  223. */
  224. protected RevCommit getBaseCommit(RevCommit a, RevCommit b)
  225. throws IncorrectObjectTypeException, IOException {
  226. walk.reset();
  227. walk.setRevFilter(RevFilter.MERGE_BASE);
  228. walk.markStart(a);
  229. walk.markStart(b);
  230. final RevCommit base = walk.next();
  231. if (base == null)
  232. return null;
  233. final RevCommit base2 = walk.next();
  234. if (base2 != null) {
  235. throw new NoMergeBaseException(
  236. MergeBaseFailureReason.MULTIPLE_MERGE_BASES_NOT_SUPPORTED,
  237. MessageFormat.format(
  238. JGitText.get().multipleMergeBasesFor, a.name(), b.name(),
  239. base.name(), base2.name()));
  240. }
  241. return base;
  242. }
  243. /**
  244. * Open an iterator over a tree.
  245. *
  246. * @param treeId
  247. * the tree to scan; must be a tree (not a treeish).
  248. * @return an iterator for the tree.
  249. * @throws IncorrectObjectTypeException
  250. * the input object is not a tree.
  251. * @throws IOException
  252. * the tree object is not found or cannot be read.
  253. */
  254. protected AbstractTreeIterator openTree(final AnyObjectId treeId)
  255. throws IncorrectObjectTypeException, IOException {
  256. return new CanonicalTreeParser(null, reader, treeId);
  257. }
  258. /**
  259. * Execute the merge.
  260. * <p>
  261. * This method is called from {@link #merge(AnyObjectId[])} after the
  262. * {@link #sourceObjects}, {@link #sourceCommits} and {@link #sourceTrees}
  263. * have been populated.
  264. *
  265. * @return true if the merge was completed without conflicts; false if the
  266. * merge strategy cannot handle this merge or there were conflicts
  267. * preventing it from automatically resolving all paths.
  268. * @throws IncorrectObjectTypeException
  269. * one of the input objects is not a commit, but the strategy
  270. * requires it to be a commit.
  271. * @throws IOException
  272. * one or more sources could not be read, or outputs could not
  273. * be written to the Repository.
  274. */
  275. protected abstract boolean mergeImpl() throws IOException;
  276. /**
  277. * @return resulting tree, if {@link #merge(AnyObjectId[])} returned true.
  278. */
  279. public abstract ObjectId getResultTreeId();
  280. }