Line data Source code
1 : // Copyright (C) 2016 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 com.google.common.collect.ImmutableSet; 22 : import com.google.common.flogger.FluentLogger; 23 : import java.util.Collection; 24 : import java.util.HashSet; 25 : import java.util.Set; 26 : import org.eclipse.jgit.lib.Ref; 27 : import org.eclipse.jgit.transport.RefSpec; 28 : 29 : /** Portion of a {@link Project} describing superproject subscription rules. */ 30 : @AutoValue 31 4 : public abstract class SubscribeSection { 32 4 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 33 : 34 : public abstract Project.NameKey project(); 35 : 36 : protected abstract ImmutableList<RefSpec> matchingRefSpecs(); 37 : 38 : protected abstract ImmutableList<RefSpec> multiMatchRefSpecs(); 39 : 40 : public static Builder builder(Project.NameKey project) { 41 4 : return new AutoValue_SubscribeSection.Builder().project(project); 42 : } 43 : 44 : public abstract Builder toBuilder(); 45 : 46 : @AutoValue.Builder 47 4 : public abstract static class Builder { 48 : public abstract Builder project(Project.NameKey project); 49 : 50 : abstract ImmutableList.Builder<RefSpec> matchingRefSpecsBuilder(); 51 : 52 : abstract ImmutableList.Builder<RefSpec> multiMatchRefSpecsBuilder(); 53 : 54 : public Builder addMatchingRefSpec(String matchingRefSpec) { 55 3 : matchingRefSpecsBuilder() 56 3 : .add(new RefSpec(matchingRefSpec, RefSpec.WildcardMode.REQUIRE_MATCH)); 57 3 : return this; 58 : } 59 : 60 : public Builder addMultiMatchRefSpec(String multiMatchRefSpec) { 61 3 : multiMatchRefSpecsBuilder() 62 3 : .add(new RefSpec(multiMatchRefSpec, RefSpec.WildcardMode.ALLOW_MISMATCH)); 63 3 : return this; 64 : } 65 : 66 : public abstract SubscribeSection build(); 67 : } 68 : 69 : /** 70 : * Determines if the <code>branch</code> could trigger a superproject update as allowed via this 71 : * subscribe section. 72 : * 73 : * @param branch the branch to check 74 : * @return if the branch could trigger a superproject update 75 : */ 76 : public boolean appliesTo(BranchNameKey branch) { 77 2 : for (RefSpec r : matchingRefSpecs()) { 78 2 : if (r.matchSource(branch.branch())) { 79 2 : return true; 80 : } 81 2 : } 82 2 : for (RefSpec r : multiMatchRefSpecs()) { 83 1 : if (r.matchSource(branch.branch())) { 84 1 : return true; 85 : } 86 1 : } 87 2 : return false; 88 : } 89 : 90 : public Collection<String> matchingRefSpecsAsString() { 91 3 : return matchingRefSpecs().stream().map(RefSpec::toString).collect(toImmutableList()); 92 : } 93 : 94 : public Collection<String> multiMatchRefSpecsAsString() { 95 3 : return multiMatchRefSpecs().stream().map(RefSpec::toString).collect(toImmutableList()); 96 : } 97 : 98 : /** Evaluates what the destination branches for the subscription are. */ 99 : public ImmutableSet<BranchNameKey> getDestinationBranches( 100 : BranchNameKey src, Collection<Ref> allRefsInRefsHeads) { 101 3 : Set<BranchNameKey> ret = new HashSet<>(); 102 : 103 3 : ImmutableList<RefSpec> matching = matchingRefSpecs(); 104 3 : ImmutableList<RefSpec> multiMatch = multiMatchRefSpecs(); 105 3 : for (RefSpec r : matching) { 106 2 : if (!r.matchSource(src.branch())) { 107 1 : continue; 108 : } 109 2 : if (r.isWildcard()) { 110 : // refs/heads/*[:refs/somewhere/*] 111 1 : ret.add(BranchNameKey.create(project(), r.expandFromSource(src.branch()).getDestination())); 112 : } else { 113 : // e.g. refs/heads/master[:refs/heads/stable] 114 2 : String dest = r.getDestination(); 115 2 : if (dest == null) { 116 1 : dest = r.getSource(); 117 : } 118 2 : ret.add(BranchNameKey.create(project(), dest)); 119 : } 120 2 : } 121 : 122 3 : for (RefSpec r : multiMatch) { 123 2 : if (!r.matchSource(src.branch())) { 124 0 : continue; 125 : } 126 2 : for (Ref ref : allRefsInRefsHeads) { 127 2 : if (r.getDestination() != null && !r.matchDestination(ref.getName())) { 128 0 : continue; 129 : } 130 2 : BranchNameKey b = BranchNameKey.create(project(), ref.getName()); 131 2 : if (!ret.contains(b)) { 132 2 : ret.add(b); 133 : } 134 2 : } 135 2 : } 136 3 : logger.atFine().log( 137 : "getDestinationBranches(%s): %s. matching refs: %s, multimatch refs: %s", 138 : this, ret, matching, multiMatch); 139 3 : return ImmutableSet.copyOf(ret); 140 : } 141 : 142 : @Override 143 : public final String toString() { 144 1 : StringBuilder ret = new StringBuilder(); 145 1 : ret.append("[SubscribeSection, project="); 146 1 : ret.append(project()); 147 1 : if (!matchingRefSpecs().isEmpty()) { 148 0 : ret.append(", matching=["); 149 0 : for (RefSpec r : matchingRefSpecs()) { 150 0 : ret.append(r.toString()); 151 0 : ret.append(", "); 152 0 : } 153 : } 154 1 : if (!multiMatchRefSpecs().isEmpty()) { 155 1 : ret.append(", all=["); 156 1 : for (RefSpec r : multiMatchRefSpecs()) { 157 1 : ret.append(r.toString()); 158 1 : ret.append(", "); 159 1 : } 160 : } 161 1 : ret.append("]"); 162 1 : return ret.toString(); 163 : } 164 : }