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

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