Line data Source code
1 : // Copyright (C) 2014 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.git; 16 : 17 : import static com.google.common.base.Preconditions.checkArgument; 18 : import static java.util.Objects.requireNonNull; 19 : 20 : import com.google.gerrit.common.Nullable; 21 : import java.util.Collection; 22 : import java.util.HashMap; 23 : import java.util.Map; 24 : import org.eclipse.jgit.lib.ObjectId; 25 : 26 : /** 27 : * Class describing a merge tip during merge operation. 28 : * 29 : * <p>The current tip of a {@link MergeTip} may be null if the merge operation is against an unborn 30 : * branch, and has not yet been attempted. This is distinct from a null {@link MergeTip} instance, 31 : * which may be used to indicate that a merge failed or another error state. 32 : */ 33 : public class MergeTip { 34 : private CodeReviewCommit initialTip; 35 : private CodeReviewCommit branchTip; 36 : private Map<ObjectId, ObjectId> mergeResults; 37 : 38 : /** 39 : * @param initialTip tip before the merge operation; may be null, indicating an unborn branch. 40 : * @param toMerge list of commits to be merged in merge operation; may not be null or empty. 41 : */ 42 53 : public MergeTip(@Nullable CodeReviewCommit initialTip, Collection<CodeReviewCommit> toMerge) { 43 53 : requireNonNull(toMerge, "toMerge may not be null"); 44 53 : checkArgument(!toMerge.isEmpty(), "toMerge may not be empty"); 45 53 : this.initialTip = initialTip; 46 53 : this.branchTip = initialTip; 47 53 : this.mergeResults = new HashMap<>(); 48 : // Assume fast-forward merge until opposite is proven. 49 53 : for (CodeReviewCommit commit : toMerge) { 50 53 : mergeResults.put(commit.copy(), commit.copy()); 51 53 : } 52 53 : } 53 : 54 : /** 55 : * Returns the initial tip of the branch before the merge operation started; may be null, 56 : * indicating a previously unborn branch. 57 : */ 58 : public CodeReviewCommit getInitialTip() { 59 53 : return initialTip; 60 : } 61 : 62 : /** 63 : * Moves this MergeTip to newTip and appends mergeResult. 64 : * 65 : * @param newTip The new tip; may not be null. 66 : * @param mergedFrom The result of the merge of {@code newTip}. 67 : */ 68 : public void moveTipTo(CodeReviewCommit newTip, ObjectId mergedFrom) { 69 53 : checkArgument(newTip != null); 70 53 : branchTip = newTip; 71 53 : mergeResults.put(mergedFrom, newTip.copy()); 72 53 : } 73 : 74 : /** 75 : * The merge results of all the merges of this merge operation. 76 : * 77 : * @return The merge results of the merge operation as a map of SHA-1 to be merged to SHA-1 of the 78 : * merge result. 79 : */ 80 : public Map<ObjectId, ObjectId> getMergeResults() { 81 53 : return mergeResults; 82 : } 83 : 84 : /** 85 : * Returns The current tip of the current merge operation; may be null, indicating an unborn 86 : * branch. 87 : */ 88 : @Nullable 89 : public CodeReviewCommit getCurrentTip() { 90 53 : return branchTip; 91 : } 92 : }