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