LCOV - code coverage report
Current view: top level - server - ProjectUtil.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 17 18 94.4 %
Date: 2022-11-19 15:00:39 Functions: 4 5 80.0 %

          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;
      16             : 
      17             : import com.google.gerrit.entities.BranchNameKey;
      18             : import com.google.gerrit.entities.RefNames;
      19             : import com.google.gerrit.server.git.GitRepositoryManager;
      20             : import java.io.IOException;
      21             : import org.eclipse.jgit.errors.RepositoryNotFoundException;
      22             : import org.eclipse.jgit.lib.Repository;
      23             : 
      24           0 : public class ProjectUtil {
      25             : 
      26             :   /**
      27             :    * Checks whether the specified branch exists.
      28             :    *
      29             :    * @param repoManager Git repository manager to open the git repository
      30             :    * @param branch the branch for which it should be checked if it exists
      31             :    * @return {@code true} if the specified branch exists or if {@code HEAD} points to this branch,
      32             :    *     otherwise {@code false}
      33             :    * @throws RepositoryNotFoundException the repository of the branch's project does not exist.
      34             :    * @throws IOException error while retrieving the branch from the repository.
      35             :    */
      36             :   public static boolean branchExists(final GitRepositoryManager repoManager, BranchNameKey branch)
      37             :       throws RepositoryNotFoundException, IOException {
      38          46 :     try (Repository repo = repoManager.openRepository(branch.project())) {
      39          46 :       boolean exists = repo.getRefDatabase().exactRef(branch.branch()) != null;
      40          46 :       if (!exists) {
      41          16 :         exists =
      42          16 :             repo.getFullBranch().equals(branch.branch())
      43          16 :                 || RefNames.REFS_CONFIG.equals(branch.branch());
      44             :       }
      45          46 :       return exists;
      46             :     }
      47             :   }
      48             : 
      49             :   public static String sanitizeProjectName(String name) {
      50         144 :     name = stripGitSuffix(name);
      51         144 :     name = stripTrailingSlash(name);
      52         144 :     return name;
      53             :   }
      54             : 
      55             :   public static String stripGitSuffix(String name) {
      56         144 :     if (name.endsWith(".git")) {
      57             :       // Be nice and drop the trailing ".git" suffix, which we never keep
      58             :       // in our database, but clients might mistakenly provide anyway.
      59             :       //
      60           3 :       name = name.substring(0, name.length() - 4);
      61           3 :       name = stripTrailingSlash(name);
      62             :     }
      63         144 :     return name;
      64             :   }
      65             : 
      66             :   private static String stripTrailingSlash(String name) {
      67         144 :     while (name.endsWith("/")) {
      68           2 :       name = name.substring(0, name.length() - 1);
      69             :     }
      70         144 :     return name;
      71             :   }
      72             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750