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.entities; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.common.collect.ImmutableList; 21 : import java.util.Collection; 22 : 23 : /** 24 : * An ordering of branches by stability. 25 : * 26 : * <p>The REST API supports automatically checking if changes on development branches can be merged 27 : * into stable branches. This is configured by the {@code branchOrder.branch} project setting. This 28 : * class represents the ordered list of branches, by increasing stability. 29 : */ 30 : @AutoValue 31 3 : public abstract class BranchOrderSection { 32 : 33 : /** 34 : * Branch names ordered from least to the most stable. 35 : * 36 : * <p>Typically the order will be like: master, stable-M.N, stable-M.N-1, ... 37 : * 38 : * <p>Ref names in this list are exactly as they appear in {@code project.config} 39 : */ 40 : public abstract ImmutableList<String> order(); 41 : 42 : public static BranchOrderSection create(Collection<String> order) { 43 : // Do not mutate the given list as this will be written back to disk when ProjectConfig is 44 : // stored. 45 3 : return new AutoValue_BranchOrderSection(ImmutableList.copyOf(order)); 46 : } 47 : 48 : /** 49 : * Returns the tail list of branches that are more stable - so lower in the entire list ordered by 50 : * priority compared to the provided branch. Always returns a fully qualified ref name (including 51 : * the refs/heads/ prefix). 52 : */ 53 : public ImmutableList<String> getMoreStable(String branch) { 54 1 : ImmutableList<String> fullyQualifiedOrder = 55 1 : order().stream().map(RefNames::fullName).collect(toImmutableList()); 56 1 : int i = fullyQualifiedOrder.indexOf(RefNames.fullName(branch)); 57 1 : if (0 <= i) { 58 1 : return fullyQualifiedOrder.subList(i + 1, fullyQualifiedOrder.size()); 59 : } 60 0 : return ImmutableList.of(); 61 : } 62 : }