Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Merger.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (C) 2008-2013, Google Inc.
  3. * Copyright (C) 2016, Laurent Delaigue <laurent.delaigue@obeo.fr>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.merge;
  45. import java.io.IOException;
  46. import java.text.MessageFormat;
  47. import org.eclipse.jgit.annotations.Nullable;
  48. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  49. import org.eclipse.jgit.errors.NoMergeBaseException;
  50. import org.eclipse.jgit.errors.NoMergeBaseException.MergeBaseFailureReason;
  51. import org.eclipse.jgit.internal.JGitText;
  52. import org.eclipse.jgit.lib.AnyObjectId;
  53. import org.eclipse.jgit.lib.NullProgressMonitor;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.ObjectInserter;
  56. import org.eclipse.jgit.lib.ObjectReader;
  57. import org.eclipse.jgit.lib.ProgressMonitor;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.revwalk.RevCommit;
  60. import org.eclipse.jgit.revwalk.RevObject;
  61. import org.eclipse.jgit.revwalk.RevTree;
  62. import org.eclipse.jgit.revwalk.RevWalk;
  63. import org.eclipse.jgit.revwalk.filter.RevFilter;
  64. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  65. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  66. /**
  67. * Instance of a specific {@link MergeStrategy} for a single {@link Repository}.
  68. */
  69. public abstract class Merger {
  70. /**
  71. * The repository this merger operates on.
  72. * <p>
  73. * Null if and only if the merger was constructed with {@link
  74. * #Merger(ObjectInserter)}. Callers that want to assume the repo is not null
  75. * (e.g. because of a previous check that the merger is not in-core) may use
  76. * {@link #nonNullRepo()}.
  77. */
  78. @Nullable
  79. protected final Repository db;
  80. /** Reader to support {@link #walk} and other object loading. */
  81. protected ObjectReader reader;
  82. /** A RevWalk for computing merge bases, or listing incoming commits. */
  83. protected RevWalk walk;
  84. private ObjectInserter inserter;
  85. /** The original objects supplied in the merge; this can be any tree-ish. */
  86. protected RevObject[] sourceObjects;
  87. /** If {@link #sourceObjects}[i] is a commit, this is the commit. */
  88. protected RevCommit[] sourceCommits;
  89. /** The trees matching every entry in {@link #sourceObjects}. */
  90. protected RevTree[] sourceTrees;
  91. /**
  92. * A progress monitor.
  93. *
  94. * @since 4.2
  95. */
  96. protected ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  97. /**
  98. * Create a new merge instance for a repository.
  99. *
  100. * @param local
  101. * the repository this merger will read and write data on.
  102. */
  103. protected Merger(final Repository local) {
  104. if (local == null) {
  105. throw new NullPointerException(JGitText.get().repositoryIsRequired);
  106. }
  107. db = local;
  108. inserter = local.newObjectInserter();
  109. reader = inserter.newReader();
  110. walk = new RevWalk(reader);
  111. }
  112. /**
  113. * Create a new in-core merge instance from an inserter.
  114. *
  115. * @param oi
  116. * the inserter to write objects to. Will be closed at the
  117. * conclusion of {@code merge}, unless {@code flush} is false.
  118. * @since 4.8
  119. */
  120. protected Merger(ObjectInserter oi) {
  121. db = null;
  122. inserter = oi;
  123. reader = oi.newReader();
  124. walk = new RevWalk(reader);
  125. }
  126. /**
  127. * @return the repository this merger operates on.
  128. */
  129. @Nullable
  130. public Repository getRepository() {
  131. return db;
  132. }
  133. /**
  134. * @return non-null repository instance
  135. * @throws NullPointerException
  136. * if the merger was constructed without a repository.
  137. * @since 4.8
  138. */
  139. protected Repository nonNullRepo() {
  140. if (db == null) {
  141. throw new NullPointerException(JGitText.get().repositoryIsRequired);
  142. }
  143. return db;
  144. }
  145. /**
  146. * @return an object writer to create objects, writing objects to {@link
  147. * #getRepository()} (if a repository was provided).
  148. */
  149. public ObjectInserter getObjectInserter() {
  150. return inserter;
  151. }
  152. /**
  153. * Set the inserter this merger will use to create objects.
  154. * <p>
  155. * If an inserter was already set on this instance (such as by a prior set,
  156. * or a prior call to {@link #getObjectInserter()}), the prior inserter as
  157. * well as the in-progress walk will be released.
  158. *
  159. * @param oi
  160. * the inserter instance to use. Must be associated with the
  161. * repository instance returned by {@link #getRepository()} (if a
  162. * repository was provided). Will be closed at the conclusion of
  163. * {@code merge}, unless {@code flush} is false.
  164. */
  165. public void setObjectInserter(ObjectInserter oi) {
  166. walk.close();
  167. reader.close();
  168. inserter.close();
  169. inserter = oi;
  170. reader = oi.newReader();
  171. walk = new RevWalk(reader);
  172. }
  173. /**
  174. * Merge together two or more tree-ish objects.
  175. * <p>
  176. * Any tree-ish may be supplied as inputs. Commits and/or tags pointing at
  177. * trees or commits may be passed as input objects.
  178. *
  179. * @param tips
  180. * source trees to be combined together. The merge base is not
  181. * included in this set.
  182. * @return true if the merge was completed without conflicts; false if the
  183. * merge strategy cannot handle this merge or there were conflicts
  184. * preventing it from automatically resolving all paths.
  185. * @throws IncorrectObjectTypeException
  186. * one of the input objects is not a commit, but the strategy
  187. * requires it to be a commit.
  188. * @throws IOException
  189. * one or more sources could not be read, or outputs could not
  190. * be written to the Repository.
  191. */
  192. public boolean merge(final AnyObjectId... tips) throws IOException {
  193. return merge(true, tips);
  194. }
  195. /**
  196. * Merge together two or more tree-ish objects.
  197. * <p>
  198. * Any tree-ish may be supplied as inputs. Commits and/or tags pointing at
  199. * trees or commits may be passed as input objects.
  200. *
  201. * @since 3.5
  202. * @param flush
  203. * whether to flush and close the underlying object inserter when
  204. * finished to store any content-merged blobs and virtual merged
  205. * bases; if false, callers are responsible for flushing.
  206. * @param tips
  207. * source trees to be combined together. The merge base is not
  208. * included in this set.
  209. * @return true if the merge was completed without conflicts; false if the
  210. * merge strategy cannot handle this merge or there were conflicts
  211. * preventing it from automatically resolving all paths.
  212. * @throws IncorrectObjectTypeException
  213. * one of the input objects is not a commit, but the strategy
  214. * requires it to be a commit.
  215. * @throws IOException
  216. * one or more sources could not be read, or outputs could not
  217. * be written to the Repository.
  218. */
  219. public boolean merge(final boolean flush, final AnyObjectId... tips)
  220. throws IOException {
  221. sourceObjects = new RevObject[tips.length];
  222. for (int i = 0; i < tips.length; i++)
  223. sourceObjects[i] = walk.parseAny(tips[i]);
  224. sourceCommits = new RevCommit[sourceObjects.length];
  225. for (int i = 0; i < sourceObjects.length; i++) {
  226. try {
  227. sourceCommits[i] = walk.parseCommit(sourceObjects[i]);
  228. } catch (IncorrectObjectTypeException err) {
  229. sourceCommits[i] = null;
  230. }
  231. }
  232. sourceTrees = new RevTree[sourceObjects.length];
  233. for (int i = 0; i < sourceObjects.length; i++)
  234. sourceTrees[i] = walk.parseTree(sourceObjects[i]);
  235. try {
  236. boolean ok = mergeImpl();
  237. if (ok && flush)
  238. inserter.flush();
  239. return ok;
  240. } finally {
  241. if (flush)
  242. inserter.close();
  243. reader.close();
  244. }
  245. }
  246. /**
  247. * @return the ID of the commit that was used as merge base for merging, or
  248. * null if no merge base was used or it was set manually
  249. * @since 3.2
  250. */
  251. public abstract ObjectId getBaseCommitId();
  252. /**
  253. * Return the merge base of two commits.
  254. *
  255. * @param a
  256. * the first commit in {@link #sourceObjects}.
  257. * @param b
  258. * the second commit in {@link #sourceObjects}.
  259. * @return the merge base of two commits
  260. * @throws IncorrectObjectTypeException
  261. * one of the input objects is not a commit.
  262. * @throws IOException
  263. * objects are missing or multiple merge bases were found.
  264. * @since 3.0
  265. */
  266. protected RevCommit getBaseCommit(RevCommit a, RevCommit b)
  267. throws IncorrectObjectTypeException, IOException {
  268. walk.reset();
  269. walk.setRevFilter(RevFilter.MERGE_BASE);
  270. walk.markStart(a);
  271. walk.markStart(b);
  272. final RevCommit base = walk.next();
  273. if (base == null)
  274. return null;
  275. final RevCommit base2 = walk.next();
  276. if (base2 != null) {
  277. throw new NoMergeBaseException(
  278. MergeBaseFailureReason.MULTIPLE_MERGE_BASES_NOT_SUPPORTED,
  279. MessageFormat.format(
  280. JGitText.get().multipleMergeBasesFor, a.name(), b.name(),
  281. base.name(), base2.name()));
  282. }
  283. return base;
  284. }
  285. /**
  286. * Open an iterator over a tree.
  287. *
  288. * @param treeId
  289. * the tree to scan; must be a tree (not a treeish).
  290. * @return an iterator for the tree.
  291. * @throws IncorrectObjectTypeException
  292. * the input object is not a tree.
  293. * @throws IOException
  294. * the tree object is not found or cannot be read.
  295. */
  296. protected AbstractTreeIterator openTree(final AnyObjectId treeId)
  297. throws IncorrectObjectTypeException, IOException {
  298. return new CanonicalTreeParser(null, reader, treeId);
  299. }
  300. /**
  301. * Execute the merge.
  302. * <p>
  303. * This method is called from {@link #merge(AnyObjectId[])} after the
  304. * {@link #sourceObjects}, {@link #sourceCommits} and {@link #sourceTrees}
  305. * have been populated.
  306. *
  307. * @return true if the merge was completed without conflicts; false if the
  308. * merge strategy cannot handle this merge or there were conflicts
  309. * preventing it from automatically resolving all paths.
  310. * @throws IncorrectObjectTypeException
  311. * one of the input objects is not a commit, but the strategy
  312. * requires it to be a commit.
  313. * @throws IOException
  314. * one or more sources could not be read, or outputs could not
  315. * be written to the Repository.
  316. */
  317. protected abstract boolean mergeImpl() throws IOException;
  318. /**
  319. * @return resulting tree, if {@link #merge(AnyObjectId[])} returned true.
  320. */
  321. public abstract ObjectId getResultTreeId();
  322. /**
  323. * Set a progress monitor.
  324. *
  325. * @param monitor
  326. * Monitor to use, can be null to indicate no progress reporting
  327. * is desired.
  328. * @since 4.2
  329. */
  330. public void setProgressMonitor(ProgressMonitor monitor) {
  331. if (monitor == null) {
  332. this.monitor = NullProgressMonitor.INSTANCE;
  333. } else {
  334. this.monitor = monitor;
  335. }
  336. }
  337. }