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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 org.eclipse.jgit.errors.IncorrectObjectTypeException;
  46. import org.eclipse.jgit.lib.AnyObjectId;
  47. import org.eclipse.jgit.lib.Constants;
  48. import org.eclipse.jgit.lib.ObjectId;
  49. import org.eclipse.jgit.lib.ObjectWriter;
  50. import org.eclipse.jgit.lib.Repository;
  51. import org.eclipse.jgit.lib.WindowCursor;
  52. import org.eclipse.jgit.revwalk.RevCommit;
  53. import org.eclipse.jgit.revwalk.RevObject;
  54. import org.eclipse.jgit.revwalk.RevTree;
  55. import org.eclipse.jgit.revwalk.RevWalk;
  56. import org.eclipse.jgit.revwalk.filter.RevFilter;
  57. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  58. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  59. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  60. /**
  61. * Instance of a specific {@link MergeStrategy} for a single {@link Repository}.
  62. */
  63. public abstract class Merger {
  64. /** The repository this merger operates on. */
  65. protected final Repository db;
  66. /** A RevWalk for computing merge bases, or listing incoming commits. */
  67. protected final RevWalk walk;
  68. private ObjectWriter writer;
  69. /** The original objects supplied in the merge; this can be any tree-ish. */
  70. protected RevObject[] sourceObjects;
  71. /** If {@link #sourceObjects}[i] is a commit, this is the commit. */
  72. protected RevCommit[] sourceCommits;
  73. /** The trees matching every entry in {@link #sourceObjects}. */
  74. protected RevTree[] sourceTrees;
  75. /**
  76. * Create a new merge instance for a repository.
  77. *
  78. * @param local
  79. * the repository this merger will read and write data on.
  80. */
  81. protected Merger(final Repository local) {
  82. db = local;
  83. walk = new RevWalk(db);
  84. }
  85. /**
  86. * @return the repository this merger operates on.
  87. */
  88. public Repository getRepository() {
  89. return db;
  90. }
  91. /**
  92. * @return an object writer to create objects in {@link #getRepository()}.
  93. */
  94. public ObjectWriter getObjectWriter() {
  95. if (writer == null)
  96. writer = new ObjectWriter(getRepository());
  97. return writer;
  98. }
  99. /**
  100. * Merge together two or more tree-ish objects.
  101. * <p>
  102. * Any tree-ish may be supplied as inputs. Commits and/or tags pointing at
  103. * trees or commits may be passed as input objects.
  104. *
  105. * @param tips
  106. * source trees to be combined together. The merge base is not
  107. * included in this set.
  108. * @return true if the merge was completed without conflicts; false if the
  109. * merge strategy cannot handle this merge or there were conflicts
  110. * preventing it from automatically resolving all paths.
  111. * @throws IncorrectObjectTypeException
  112. * one of the input objects is not a commit, but the strategy
  113. * requires it to be a commit.
  114. * @throws IOException
  115. * one or more sources could not be read, or outputs could not
  116. * be written to the Repository.
  117. */
  118. public boolean merge(final AnyObjectId[] tips) throws IOException {
  119. sourceObjects = new RevObject[tips.length];
  120. for (int i = 0; i < tips.length; i++)
  121. sourceObjects[i] = walk.parseAny(tips[i]);
  122. sourceCommits = new RevCommit[sourceObjects.length];
  123. for (int i = 0; i < sourceObjects.length; i++) {
  124. try {
  125. sourceCommits[i] = walk.parseCommit(sourceObjects[i]);
  126. } catch (IncorrectObjectTypeException err) {
  127. sourceCommits[i] = null;
  128. }
  129. }
  130. sourceTrees = new RevTree[sourceObjects.length];
  131. for (int i = 0; i < sourceObjects.length; i++)
  132. sourceTrees[i] = walk.parseTree(sourceObjects[i]);
  133. return mergeImpl();
  134. }
  135. /**
  136. * Create an iterator to walk the merge base of two commits.
  137. *
  138. * @param aIdx
  139. * index of the first commit in {@link #sourceObjects}.
  140. * @param bIdx
  141. * index of the second commit in {@link #sourceObjects}.
  142. * @return the new iterator
  143. * @throws IncorrectObjectTypeException
  144. * one of the input objects is not a commit.
  145. * @throws IOException
  146. * objects are missing or multiple merge bases were found.
  147. */
  148. protected AbstractTreeIterator mergeBase(final int aIdx, final int bIdx)
  149. throws IOException {
  150. if (sourceCommits[aIdx] == null)
  151. throw new IncorrectObjectTypeException(sourceObjects[aIdx],
  152. Constants.TYPE_COMMIT);
  153. if (sourceCommits[bIdx] == null)
  154. throw new IncorrectObjectTypeException(sourceObjects[bIdx],
  155. Constants.TYPE_COMMIT);
  156. walk.reset();
  157. walk.setRevFilter(RevFilter.MERGE_BASE);
  158. walk.markStart(sourceCommits[aIdx]);
  159. walk.markStart(sourceCommits[bIdx]);
  160. final RevCommit base = walk.next();
  161. if (base == null)
  162. return new EmptyTreeIterator();
  163. final RevCommit base2 = walk.next();
  164. if (base2 != null) {
  165. throw new IOException("Multiple merge bases for:" + "\n "
  166. + sourceCommits[aIdx].name() + "\n "
  167. + sourceCommits[bIdx].name() + "found:" + "\n "
  168. + base.name() + "\n " + base2.name());
  169. }
  170. return openTree(base.getTree());
  171. }
  172. /**
  173. * Open an iterator over a tree.
  174. *
  175. * @param treeId
  176. * the tree to scan; must be a tree (not a treeish).
  177. * @return an iterator for the tree.
  178. * @throws IncorrectObjectTypeException
  179. * the input object is not a tree.
  180. * @throws IOException
  181. * the tree object is not found or cannot be read.
  182. */
  183. protected AbstractTreeIterator openTree(final AnyObjectId treeId)
  184. throws IncorrectObjectTypeException, IOException {
  185. final WindowCursor curs = new WindowCursor();
  186. try {
  187. return new CanonicalTreeParser(null, db, treeId, curs);
  188. } finally {
  189. curs.release();
  190. }
  191. }
  192. /**
  193. * Execute the merge.
  194. * <p>
  195. * This method is called from {@link #merge(AnyObjectId[])} after the
  196. * {@link #sourceObjects}, {@link #sourceCommits} and {@link #sourceTrees}
  197. * have been populated.
  198. *
  199. * @return true if the merge was completed without conflicts; false if the
  200. * merge strategy cannot handle this merge or there were conflicts
  201. * preventing it from automatically resolving all paths.
  202. * @throws IncorrectObjectTypeException
  203. * one of the input objects is not a commit, but the strategy
  204. * requires it to be a commit.
  205. * @throws IOException
  206. * one or more sources could not be read, or outputs could not
  207. * be written to the Repository.
  208. */
  209. protected abstract boolean mergeImpl() throws IOException;
  210. /**
  211. * @return resulting tree, if {@link #merge(AnyObjectId[])} returned true.
  212. */
  213. public abstract ObjectId getResultTreeId();
  214. }