Line data Source code
1 : // Copyright (C) 2012 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.submit; 16 : 17 : import com.google.common.collect.ImmutableList; 18 : import com.google.gerrit.entities.BranchNameKey; 19 : import com.google.gerrit.server.git.CodeReviewCommit; 20 : import com.google.gerrit.server.update.RepoContext; 21 : import java.util.Collection; 22 : import java.util.HashMap; 23 : import java.util.List; 24 : import java.util.Map; 25 : 26 : public class FastForwardOnly extends SubmitStrategy { 27 : FastForwardOnly(SubmitStrategy.Arguments args) { 28 3 : super(args); 29 3 : } 30 : 31 : @Override 32 : public ImmutableList<SubmitStrategyOp> buildOps(Collection<CodeReviewCommit> toMerge) { 33 3 : List<CodeReviewCommit> sorted = args.mergeUtil.reduceToMinimalMerge(args.mergeSorter, toMerge); 34 : 35 3 : Map<BranchNameKey, CodeReviewCommit> branchToCommit = new HashMap<>(); 36 3 : for (CodeReviewCommit codeReviewCommit : sorted) { 37 3 : BranchNameKey branchNameKey = codeReviewCommit.change().getDest(); 38 3 : CodeReviewCommit otherCommitInBranch = branchToCommit.get(branchNameKey); 39 3 : if (otherCommitInBranch == null) { 40 3 : branchToCommit.put(branchNameKey, codeReviewCommit); 41 : } else { 42 : // we found another change with the same destination branch. 43 1 : codeReviewCommit.setStatusCode(CommitMergeStatus.FAST_FORWARD_INDEPENDENT_CHANGES); 44 1 : otherCommitInBranch.setStatusCode(CommitMergeStatus.FAST_FORWARD_INDEPENDENT_CHANGES); 45 1 : return ImmutableList.of(); 46 : } 47 3 : } 48 : 49 3 : ImmutableList.Builder<SubmitStrategyOp> ops = 50 3 : ImmutableList.builderWithExpectedSize(sorted.size()); 51 3 : CodeReviewCommit newTipCommit = 52 3 : args.mergeUtil.getFirstFastForward(args.mergeTip.getInitialTip(), args.rw, sorted); 53 3 : if (!newTipCommit.equals(args.mergeTip.getInitialTip())) { 54 3 : ops.add(new FastForwardOp(args, newTipCommit)); 55 : } else { 56 1 : for (CodeReviewCommit c : toMerge) { 57 1 : ops.add(new NotFastForwardOp(c)); 58 1 : } 59 : } 60 3 : return ops.build(); 61 : } 62 : 63 : private class NotFastForwardOp extends SubmitStrategyOp { 64 1 : private NotFastForwardOp(CodeReviewCommit toMerge) { 65 1 : super(FastForwardOnly.this.args, toMerge); 66 1 : } 67 : 68 : @Override 69 : public void updateRepoImpl(RepoContext ctx) { 70 1 : toMerge.setStatusCode(CommitMergeStatus.NOT_FAST_FORWARD); 71 1 : } 72 : } 73 : 74 : static boolean dryRun( 75 : SubmitDryRun.Arguments args, CodeReviewCommit mergeTip, CodeReviewCommit toMerge) { 76 2 : return args.mergeUtil.canFastForward(args.mergeSorter, mergeTip, args.rw, toMerge); 77 : } 78 : }