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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (C) 2008-2013, 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.ObjectReader;
  55. import org.eclipse.jgit.lib.Repository;
  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. /**
  64. * Instance of a specific {@link MergeStrategy} for a single {@link Repository}.
  65. */
  66. public abstract class Merger {
  67. /** The repository this merger operates on. */
  68. protected final Repository db;
  69. /** Reader to support {@link #walk} and other object loading. */
  70. protected final ObjectReader reader;
  71. /** A RevWalk for computing merge bases, or listing incoming commits. */
  72. protected final RevWalk walk;
  73. private ObjectInserter inserter;
  74. /** The original objects supplied in the merge; this can be any tree-ish. */
  75. protected RevObject[] sourceObjects;
  76. /** If {@link #sourceObjects}[i] is a commit, this is the commit. */
  77. protected RevCommit[] sourceCommits;
  78. /** The trees matching every entry in {@link #sourceObjects}. */
  79. protected RevTree[] sourceTrees;
  80. /**
  81. * Create a new merge instance for a repository.
  82. *
  83. * @param local
  84. * the repository this merger will read and write data on.
  85. */
  86. protected Merger(final Repository local) {
  87. db = local;
  88. reader = db.newObjectReader();
  89. walk = new RevWalk(reader);
  90. }
  91. /**
  92. * @return the repository this merger operates on.
  93. */
  94. public Repository getRepository() {
  95. return db;
  96. }
  97. /**
  98. * @return an object writer to create objects in {@link #getRepository()}.
  99. * If no inserter has been set on this instance, one will be created
  100. * and returned by all future calls.
  101. */
  102. public ObjectInserter getObjectInserter() {
  103. if (inserter == null)
  104. inserter = getRepository().newObjectInserter();
  105. return inserter;
  106. }
  107. /**
  108. * Set the inserter this merger will use to create objects.
  109. * <p>
  110. * If an inserter was already set on this instance (such as by a prior set,
  111. * or a prior call to {@link #getObjectInserter()}), the prior inserter will
  112. * be released first.
  113. *
  114. * @param oi
  115. * the inserter instance to use. Must be associated with the
  116. * repository instance returned by {@link #getRepository()}.
  117. */
  118. public void setObjectInserter(ObjectInserter oi) {
  119. if (inserter != null)
  120. inserter.release();
  121. inserter = oi;
  122. }
  123. /**
  124. * Merge together two or more tree-ish objects.
  125. * <p>
  126. * Any tree-ish may be supplied as inputs. Commits and/or tags pointing at
  127. * trees or commits may be passed as input objects.
  128. *
  129. * @param tips
  130. * source trees to be combined together. The merge base is not
  131. * included in this set.
  132. * @return true if the merge was completed without conflicts; false if the
  133. * merge strategy cannot handle this merge or there were conflicts
  134. * preventing it from automatically resolving all paths.
  135. * @throws IncorrectObjectTypeException
  136. * one of the input objects is not a commit, but the strategy
  137. * requires it to be a commit.
  138. * @throws IOException
  139. * one or more sources could not be read, or outputs could not
  140. * be written to the Repository.
  141. */
  142. public boolean merge(final AnyObjectId... tips) throws IOException {
  143. sourceObjects = new RevObject[tips.length];
  144. for (int i = 0; i < tips.length; i++)
  145. sourceObjects[i] = walk.parseAny(tips[i]);
  146. sourceCommits = new RevCommit[sourceObjects.length];
  147. for (int i = 0; i < sourceObjects.length; i++) {
  148. try {
  149. sourceCommits[i] = walk.parseCommit(sourceObjects[i]);
  150. } catch (IncorrectObjectTypeException err) {
  151. sourceCommits[i] = null;
  152. }
  153. }
  154. sourceTrees = new RevTree[sourceObjects.length];
  155. for (int i = 0; i < sourceObjects.length; i++)
  156. sourceTrees[i] = walk.parseTree(sourceObjects[i]);
  157. try {
  158. boolean ok = mergeImpl();
  159. if (ok && inserter != null)
  160. inserter.flush();
  161. return ok;
  162. } finally {
  163. if (inserter != null)
  164. inserter.release();
  165. reader.release();
  166. }
  167. }
  168. /**
  169. * @return the ID of the commit that was used as merge base for merging, or
  170. * null if no merge base was used or it was set manually
  171. * @since 3.2
  172. */
  173. public abstract ObjectId getBaseCommitId();
  174. /**
  175. * Return the merge base of two commits.
  176. *
  177. * @param aIdx
  178. * index of the first commit in {@link #sourceObjects}.
  179. * @param bIdx
  180. * index of the second commit in {@link #sourceObjects}.
  181. * @return the merge base of two commits
  182. * @throws IncorrectObjectTypeException
  183. * one of the input objects is not a commit.
  184. * @throws IOException
  185. * objects are missing or multiple merge bases were found.
  186. * @deprecated use {@link #getBaseCommitId()} instead, as that does not
  187. * require walking the commits again
  188. */
  189. @Deprecated
  190. public RevCommit getBaseCommit(final int aIdx, final int bIdx)
  191. throws IncorrectObjectTypeException,
  192. IOException {
  193. if (sourceCommits[aIdx] == null)
  194. throw new IncorrectObjectTypeException(sourceObjects[aIdx],
  195. Constants.TYPE_COMMIT);
  196. if (sourceCommits[bIdx] == null)
  197. throw new IncorrectObjectTypeException(sourceObjects[bIdx],
  198. Constants.TYPE_COMMIT);
  199. return getBaseCommit(sourceCommits[aIdx], sourceCommits[bIdx]);
  200. }
  201. /**
  202. * Return the merge base of two commits.
  203. *
  204. * @param a
  205. * the first commit in {@link #sourceObjects}.
  206. * @param b
  207. * the second commit in {@link #sourceObjects}.
  208. * @return the merge base of two commits
  209. * @throws IncorrectObjectTypeException
  210. * one of the input objects is not a commit.
  211. * @throws IOException
  212. * objects are missing or multiple merge bases were found.
  213. * @since 3.0
  214. */
  215. protected RevCommit getBaseCommit(RevCommit a, RevCommit b)
  216. throws IncorrectObjectTypeException, IOException {
  217. walk.reset();
  218. walk.setRevFilter(RevFilter.MERGE_BASE);
  219. walk.markStart(a);
  220. walk.markStart(b);
  221. final RevCommit base = walk.next();
  222. if (base == null)
  223. return null;
  224. final RevCommit base2 = walk.next();
  225. if (base2 != null) {
  226. throw new NoMergeBaseException(
  227. MergeBaseFailureReason.MULTIPLE_MERGE_BASES_NOT_SUPPORTED,
  228. MessageFormat.format(
  229. JGitText.get().multipleMergeBasesFor, a.name(), b.name(),
  230. base.name(), base2.name()));
  231. }
  232. return base;
  233. }
  234. /**
  235. * Open an iterator over a tree.
  236. *
  237. * @param treeId
  238. * the tree to scan; must be a tree (not a treeish).
  239. * @return an iterator for the tree.
  240. * @throws IncorrectObjectTypeException
  241. * the input object is not a tree.
  242. * @throws IOException
  243. * the tree object is not found or cannot be read.
  244. */
  245. protected AbstractTreeIterator openTree(final AnyObjectId treeId)
  246. throws IncorrectObjectTypeException, IOException {
  247. return new CanonicalTreeParser(null, reader, treeId);
  248. }
  249. /**
  250. * Execute the merge.
  251. * <p>
  252. * This method is called from {@link #merge(AnyObjectId[])} after the
  253. * {@link #sourceObjects}, {@link #sourceCommits} and {@link #sourceTrees}
  254. * have been populated.
  255. *
  256. * @return true if the merge was completed without conflicts; false if the
  257. * merge strategy cannot handle this merge or there were conflicts
  258. * preventing it from automatically resolving all paths.
  259. * @throws IncorrectObjectTypeException
  260. * one of the input objects is not a commit, but the strategy
  261. * requires it to be a commit.
  262. * @throws IOException
  263. * one or more sources could not be read, or outputs could not
  264. * be written to the Repository.
  265. */
  266. protected abstract boolean mergeImpl() throws IOException;
  267. /**
  268. * @return resulting tree, if {@link #merge(AnyObjectId[])} returned true.
  269. */
  270. public abstract ObjectId getResultTreeId();
  271. }