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.server.git.CodeReviewCommit; 19 : import java.util.Collection; 20 : import java.util.List; 21 : 22 : public class MergeIfNecessary extends SubmitStrategy { 23 : MergeIfNecessary(SubmitStrategy.Arguments args) { 24 53 : super(args); 25 53 : } 26 : 27 : @Override 28 : public ImmutableList<SubmitStrategyOp> buildOps(Collection<CodeReviewCommit> toMerge) { 29 53 : List<CodeReviewCommit> sorted = args.mergeUtil.reduceToMinimalMerge(args.mergeSorter, toMerge); 30 53 : ImmutableList.Builder<SubmitStrategyOp> ops = 31 53 : ImmutableList.builderWithExpectedSize(sorted.size()); 32 : 33 53 : if (args.mergeTip.getInitialTip() == null 34 53 : || !args.subscriptionGraph.hasSubscription(args.destBranch)) { 35 53 : CodeReviewCommit firstFastForward = 36 53 : args.mergeUtil.getFirstFastForward(args.mergeTip.getInitialTip(), args.rw, sorted); 37 53 : if (firstFastForward != null && !firstFastForward.equals(args.mergeTip.getInitialTip())) { 38 53 : ops.add(new FastForwardOp(args, firstFastForward)); 39 : } 40 : } 41 : 42 : // For every other commit do a pair-wise merge. 43 53 : while (!sorted.isEmpty()) { 44 15 : CodeReviewCommit n = sorted.remove(0); 45 15 : ops.add(new MergeOneOp(args, n)); 46 15 : } 47 53 : return ops.build(); 48 : } 49 : 50 : static boolean dryRun( 51 : SubmitDryRun.Arguments args, CodeReviewCommit mergeTip, CodeReviewCommit toMerge) { 52 21 : return args.mergeUtil.canFastForward(args.mergeSorter, mergeTip, args.rw, toMerge) 53 21 : || args.mergeUtil.canMerge(args.mergeSorter, args.repo, mergeTip, toMerge); 54 : } 55 : }