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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.JGitText;
  47. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  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. */
  99. public ObjectInserter getObjectInserter() {
  100. if (inserter == null)
  101. inserter = getRepository().newObjectInserter();
  102. return inserter;
  103. }
  104. /**
  105. * Merge together two or more tree-ish objects.
  106. * <p>
  107. * Any tree-ish may be supplied as inputs. Commits and/or tags pointing at
  108. * trees or commits may be passed as input objects.
  109. *
  110. * @param tips
  111. * source trees to be combined together. The merge base is not
  112. * included in this set.
  113. * @return true if the merge was completed without conflicts; false if the
  114. * merge strategy cannot handle this merge or there were conflicts
  115. * preventing it from automatically resolving all paths.
  116. * @throws IncorrectObjectTypeException
  117. * one of the input objects is not a commit, but the strategy
  118. * requires it to be a commit.
  119. * @throws IOException
  120. * one or more sources could not be read, or outputs could not
  121. * be written to the Repository.
  122. */
  123. public boolean merge(final AnyObjectId[] tips) throws IOException {
  124. sourceObjects = new RevObject[tips.length];
  125. for (int i = 0; i < tips.length; i++)
  126. sourceObjects[i] = walk.parseAny(tips[i]);
  127. sourceCommits = new RevCommit[sourceObjects.length];
  128. for (int i = 0; i < sourceObjects.length; i++) {
  129. try {
  130. sourceCommits[i] = walk.parseCommit(sourceObjects[i]);
  131. } catch (IncorrectObjectTypeException err) {
  132. sourceCommits[i] = null;
  133. }
  134. }
  135. sourceTrees = new RevTree[sourceObjects.length];
  136. for (int i = 0; i < sourceObjects.length; i++)
  137. sourceTrees[i] = walk.parseTree(sourceObjects[i]);
  138. try {
  139. return mergeImpl();
  140. } finally {
  141. if (inserter != null)
  142. inserter.release();
  143. reader.release();
  144. }
  145. }
  146. /**
  147. * Create an iterator to walk the merge base of two commits.
  148. *
  149. * @param aIdx
  150. * index of the first commit in {@link #sourceObjects}.
  151. * @param bIdx
  152. * index of the second commit in {@link #sourceObjects}.
  153. * @return the new iterator
  154. * @throws IncorrectObjectTypeException
  155. * one of the input objects is not a commit.
  156. * @throws IOException
  157. * objects are missing or multiple merge bases were found.
  158. */
  159. protected AbstractTreeIterator mergeBase(final int aIdx, final int bIdx)
  160. throws IOException {
  161. RevCommit base = getBaseCommit(aIdx, bIdx);
  162. return (base == null) ? new EmptyTreeIterator() : openTree(base.getTree());
  163. }
  164. /**
  165. * Return the merge base of two commits.
  166. *
  167. * @param aIdx
  168. * index of the first commit in {@link #sourceObjects}.
  169. * @param bIdx
  170. * index of the second commit in {@link #sourceObjects}.
  171. * @return the merge base of two commits
  172. * @throws IncorrectObjectTypeException
  173. * one of the input objects is not a commit.
  174. * @throws IOException
  175. * objects are missing or multiple merge bases were found.
  176. */
  177. public RevCommit getBaseCommit(final int aIdx, final int bIdx)
  178. throws IncorrectObjectTypeException,
  179. IOException {
  180. if (sourceCommits[aIdx] == null)
  181. throw new IncorrectObjectTypeException(sourceObjects[aIdx],
  182. Constants.TYPE_COMMIT);
  183. if (sourceCommits[bIdx] == null)
  184. throw new IncorrectObjectTypeException(sourceObjects[bIdx],
  185. Constants.TYPE_COMMIT);
  186. walk.reset();
  187. walk.setRevFilter(RevFilter.MERGE_BASE);
  188. walk.markStart(sourceCommits[aIdx]);
  189. walk.markStart(sourceCommits[bIdx]);
  190. final RevCommit base = walk.next();
  191. if (base == null)
  192. return null;
  193. final RevCommit base2 = walk.next();
  194. if (base2 != null) {
  195. throw new IOException(MessageFormat.format(JGitText.get().multipleMergeBasesFor
  196. , sourceCommits[aIdx].name(), sourceCommits[bIdx].name()
  197. , base.name(), base2.name()));
  198. }
  199. return base;
  200. }
  201. /**
  202. * Open an iterator over a tree.
  203. *
  204. * @param treeId
  205. * the tree to scan; must be a tree (not a treeish).
  206. * @return an iterator for the tree.
  207. * @throws IncorrectObjectTypeException
  208. * the input object is not a tree.
  209. * @throws IOException
  210. * the tree object is not found or cannot be read.
  211. */
  212. protected AbstractTreeIterator openTree(final AnyObjectId treeId)
  213. throws IncorrectObjectTypeException, IOException {
  214. return new CanonicalTreeParser(null, reader, treeId);
  215. }
  216. /**
  217. * Execute the merge.
  218. * <p>
  219. * This method is called from {@link #merge(AnyObjectId[])} after the
  220. * {@link #sourceObjects}, {@link #sourceCommits} and {@link #sourceTrees}
  221. * have been populated.
  222. *
  223. * @return true if the merge was completed without conflicts; false if the
  224. * merge strategy cannot handle this merge or there were conflicts
  225. * preventing it from automatically resolving all paths.
  226. * @throws IncorrectObjectTypeException
  227. * one of the input objects is not a commit, but the strategy
  228. * requires it to be a commit.
  229. * @throws IOException
  230. * one or more sources could not be read, or outputs could not
  231. * be written to the Repository.
  232. */
  233. protected abstract boolean mergeImpl() throws IOException;
  234. /**
  235. * @return resulting tree, if {@link #merge(AnyObjectId[])} returned true.
  236. */
  237. public abstract ObjectId getResultTreeId();
  238. }