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