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 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.internal.JGitText;
  48. import org.eclipse.jgit.lib.AnyObjectId;
  49. import org.eclipse.jgit.lib.Constants;
  50. import org.eclipse.jgit.lib.ObjectId;
  51. import org.eclipse.jgit.lib.ObjectInserter;
  52. import org.eclipse.jgit.lib.Repository;
  53. import org.eclipse.jgit.lib.ObjectReader;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.eclipse.jgit.revwalk.RevObject;
  56. import org.eclipse.jgit.revwalk.RevTree;
  57. import org.eclipse.jgit.revwalk.RevWalk;
  58. import org.eclipse.jgit.revwalk.filter.RevFilter;
  59. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  60. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  61. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  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 final ObjectReader reader;
  70. /** A RevWalk for computing merge bases, or listing incoming commits. */
  71. protected final 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. reader = db.newObjectReader();
  88. walk = new RevWalk(reader);
  89. }
  90. /**
  91. * @return the repository this merger operates on.
  92. */
  93. public Repository getRepository() {
  94. return db;
  95. }
  96. /**
  97. * @return an object writer to create objects in {@link #getRepository()}.
  98. * If no inserter has been set on this instance, one will be created
  99. * and returned by all future calls.
  100. */
  101. public ObjectInserter getObjectInserter() {
  102. if (inserter == null)
  103. inserter = getRepository().newObjectInserter();
  104. return inserter;
  105. }
  106. /**
  107. * Set the inserter this merger will use to create objects.
  108. * <p>
  109. * If an inserter was already set on this instance (such as by a prior set,
  110. * or a prior call to {@link #getObjectInserter()}), the prior inserter will
  111. * be released first.
  112. *
  113. * @param oi
  114. * the inserter instance to use. Must be associated with the
  115. * repository instance returned by {@link #getRepository()}.
  116. */
  117. public void setObjectInserter(ObjectInserter oi) {
  118. if (inserter != null)
  119. inserter.release();
  120. inserter = oi;
  121. }
  122. /**
  123. * Merge together two or more tree-ish objects.
  124. * <p>
  125. * Any tree-ish may be supplied as inputs. Commits and/or tags pointing at
  126. * trees or commits may be passed as input objects.
  127. *
  128. * @param tips
  129. * source trees to be combined together. The merge base is not
  130. * included in this set.
  131. * @return true if the merge was completed without conflicts; false if the
  132. * merge strategy cannot handle this merge or there were conflicts
  133. * preventing it from automatically resolving all paths.
  134. * @throws IncorrectObjectTypeException
  135. * one of the input objects is not a commit, but the strategy
  136. * requires it to be a commit.
  137. * @throws IOException
  138. * one or more sources could not be read, or outputs could not
  139. * be written to the Repository.
  140. */
  141. public boolean merge(final AnyObjectId... tips) throws IOException {
  142. sourceObjects = new RevObject[tips.length];
  143. for (int i = 0; i < tips.length; i++)
  144. sourceObjects[i] = walk.parseAny(tips[i]);
  145. sourceCommits = new RevCommit[sourceObjects.length];
  146. for (int i = 0; i < sourceObjects.length; i++) {
  147. try {
  148. sourceCommits[i] = walk.parseCommit(sourceObjects[i]);
  149. } catch (IncorrectObjectTypeException err) {
  150. sourceCommits[i] = null;
  151. }
  152. }
  153. sourceTrees = new RevTree[sourceObjects.length];
  154. for (int i = 0; i < sourceObjects.length; i++)
  155. sourceTrees[i] = walk.parseTree(sourceObjects[i]);
  156. try {
  157. boolean ok = mergeImpl();
  158. if (ok && inserter != null)
  159. inserter.flush();
  160. return ok;
  161. } finally {
  162. if (inserter != null)
  163. inserter.release();
  164. reader.release();
  165. }
  166. }
  167. /**
  168. * Create an iterator to walk the merge base of two commits.
  169. *
  170. * @param aIdx
  171. * index of the first commit in {@link #sourceObjects}.
  172. * @param bIdx
  173. * index of the second commit in {@link #sourceObjects}.
  174. * @return the new iterator
  175. * @throws IncorrectObjectTypeException
  176. * one of the input objects is not a commit.
  177. * @throws IOException
  178. * objects are missing or multiple merge bases were found.
  179. */
  180. protected AbstractTreeIterator mergeBase(final int aIdx, final int bIdx)
  181. throws IOException {
  182. RevCommit base = getBaseCommit(aIdx, bIdx);
  183. return (base == null) ? new EmptyTreeIterator() : openTree(base.getTree());
  184. }
  185. /**
  186. * Return the merge base of two commits.
  187. *
  188. * @param aIdx
  189. * index of the first commit in {@link #sourceObjects}.
  190. * @param bIdx
  191. * index of the second commit in {@link #sourceObjects}.
  192. * @return the merge base of two commits
  193. * @throws IncorrectObjectTypeException
  194. * one of the input objects is not a commit.
  195. * @throws IOException
  196. * objects are missing or multiple merge bases were found.
  197. */
  198. public RevCommit getBaseCommit(final int aIdx, final int bIdx)
  199. throws IncorrectObjectTypeException,
  200. IOException {
  201. if (sourceCommits[aIdx] == null)
  202. throw new IncorrectObjectTypeException(sourceObjects[aIdx],
  203. Constants.TYPE_COMMIT);
  204. if (sourceCommits[bIdx] == null)
  205. throw new IncorrectObjectTypeException(sourceObjects[bIdx],
  206. Constants.TYPE_COMMIT);
  207. walk.reset();
  208. walk.setRevFilter(RevFilter.MERGE_BASE);
  209. walk.markStart(sourceCommits[aIdx]);
  210. walk.markStart(sourceCommits[bIdx]);
  211. final RevCommit base = walk.next();
  212. if (base == null)
  213. return null;
  214. final RevCommit base2 = walk.next();
  215. if (base2 != null) {
  216. throw new IOException(MessageFormat.format(JGitText.get().multipleMergeBasesFor
  217. , sourceCommits[aIdx].name(), sourceCommits[bIdx].name()
  218. , base.name(), base2.name()));
  219. }
  220. return base;
  221. }
  222. /**
  223. * Open an iterator over a tree.
  224. *
  225. * @param treeId
  226. * the tree to scan; must be a tree (not a treeish).
  227. * @return an iterator for the tree.
  228. * @throws IncorrectObjectTypeException
  229. * the input object is not a tree.
  230. * @throws IOException
  231. * the tree object is not found or cannot be read.
  232. */
  233. protected AbstractTreeIterator openTree(final AnyObjectId treeId)
  234. throws IncorrectObjectTypeException, IOException {
  235. return new CanonicalTreeParser(null, reader, treeId);
  236. }
  237. /**
  238. * Execute the merge.
  239. * <p>
  240. * This method is called from {@link #merge(AnyObjectId[])} after the
  241. * {@link #sourceObjects}, {@link #sourceCommits} and {@link #sourceTrees}
  242. * have been populated.
  243. *
  244. * @return true if the merge was completed without conflicts; false if the
  245. * merge strategy cannot handle this merge or there were conflicts
  246. * preventing it from automatically resolving all paths.
  247. * @throws IncorrectObjectTypeException
  248. * one of the input objects is not a commit, but the strategy
  249. * requires it to be a commit.
  250. * @throws IOException
  251. * one or more sources could not be read, or outputs could not
  252. * be written to the Repository.
  253. */
  254. protected abstract boolean mergeImpl() throws IOException;
  255. /**
  256. * @return resulting tree, if {@link #merge(AnyObjectId[])} returned true.
  257. */
  258. public abstract ObjectId getResultTreeId();
  259. }