Line data Source code
1 : // Copyright (C) 2011 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.util; 16 : 17 : import com.google.common.flogger.FluentLogger; 18 : import com.google.gerrit.common.Nullable; 19 : import com.google.gerrit.common.data.Capable; 20 : import com.google.gerrit.entities.Project; 21 : import java.io.IOException; 22 : import java.util.List; 23 : import org.eclipse.jgit.lib.Ref; 24 : import org.eclipse.jgit.lib.Repository; 25 : 26 : public final class MagicBranch { 27 113 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 28 : 29 : public static final String NEW_CHANGE = "refs/for/"; 30 : 31 : /** Extracts the destination from a ref name */ 32 : public static String getDestBranchName(String refName) { 33 89 : return refName.substring(NEW_CHANGE.length()); 34 : } 35 : 36 : /** Checks if the supplied ref name is a magic branch */ 37 : public static boolean isMagicBranch(String refName) { 38 113 : return refName.startsWith(NEW_CHANGE); 39 : } 40 : 41 : /** Returns the ref name prefix for a magic branch, {@code null} if the branch is not magic */ 42 : @Nullable 43 : public static String getMagicRefNamePrefix(String refName) { 44 1 : if (refName.startsWith(NEW_CHANGE)) { 45 1 : return NEW_CHANGE; 46 : } 47 0 : return null; 48 : } 49 : 50 : /** 51 : * Checks if a (magic branch)/branch_name reference exists in the destination repository and only 52 : * returns Capable.OK if it does not match any. 53 : * 54 : * <p>These block the client from being able to even send us a pack file, as it is very unlikely 55 : * the user passed the --force flag and the new commit is probably not going to fast-forward the 56 : * branch. 57 : */ 58 : public static Capable checkMagicBranchRefs(Repository repo, Project project) { 59 97 : return checkMagicBranchRef(NEW_CHANGE, repo, project); 60 : } 61 : 62 : private static Capable checkMagicBranchRef(String branchName, Repository repo, Project project) { 63 : List<Ref> blockingFors; 64 : try { 65 97 : blockingFors = repo.getRefDatabase().getRefsByPrefix(branchName); 66 0 : } catch (IOException err) { 67 0 : String projName = project.getName(); 68 0 : logger.atWarning().withCause(err).log("Cannot scan refs in '%s'", projName); 69 0 : return new Capable("Server process cannot read '" + projName + "'"); 70 97 : } 71 97 : if (!blockingFors.isEmpty()) { 72 0 : String projName = project.getName(); 73 0 : logger.atSevere().log( 74 : "Repository '%s' needs the following refs removed to receive changes: %s", 75 : projName, blockingFors); 76 0 : return new Capable("One or more " + branchName + " names blocks change upload"); 77 : } 78 : 79 97 : return Capable.OK; 80 : } 81 : 82 : private MagicBranch() {} 83 : }