LCOV - code coverage report
Current view: top level - server/submit - GitModules.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 32 81.2 %
Date: 2022-11-19 15:00:39 Functions: 3 3 100.0 %

          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.server.submit;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.gerrit.common.Nullable;
      19             : import com.google.gerrit.entities.BranchNameKey;
      20             : import com.google.gerrit.entities.Project;
      21             : import com.google.gerrit.entities.SubmoduleSubscription;
      22             : import com.google.gerrit.server.config.CanonicalWebUrl;
      23             : import com.google.gerrit.server.project.NoSuchProjectException;
      24             : import com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo;
      25             : import com.google.gerrit.server.util.git.SubmoduleSectionParser;
      26             : import com.google.inject.Inject;
      27             : import com.google.inject.assistedinject.Assisted;
      28             : import java.io.IOException;
      29             : import java.util.ArrayList;
      30             : import java.util.Collection;
      31             : import java.util.Collections;
      32             : import java.util.Set;
      33             : import org.eclipse.jgit.errors.ConfigInvalidException;
      34             : import org.eclipse.jgit.lib.BlobBasedConfig;
      35             : import org.eclipse.jgit.lib.FileMode;
      36             : import org.eclipse.jgit.lib.ObjectId;
      37             : import org.eclipse.jgit.revwalk.RevCommit;
      38             : import org.eclipse.jgit.treewalk.TreeWalk;
      39             : 
      40             : /**
      41             :  * Loads the .gitmodules file of the specified project/branch. It can be queried which submodules
      42             :  * this branch is subscribed to.
      43             :  */
      44             : public class GitModules {
      45           3 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      46             : 
      47             :   public interface Factory {
      48             :     GitModules create(BranchNameKey project, MergeOpRepoManager m);
      49             :   }
      50             : 
      51             :   private static final String GIT_MODULES = ".gitmodules";
      52             : 
      53             :   private Set<SubmoduleSubscription> subscriptions;
      54             : 
      55             :   @Inject
      56             :   GitModules(
      57             :       @CanonicalWebUrl @Nullable String canonicalWebUrl,
      58             :       @Assisted BranchNameKey branch,
      59             :       @Assisted MergeOpRepoManager orm)
      60           2 :       throws IOException {
      61           2 :     Project.NameKey project = branch.project();
      62           2 :     logger.atFine().log("Loading .gitmodules of %s for project %s", branch, project);
      63             :     try {
      64           2 :       OpenRepo or = orm.getRepo(project);
      65           2 :       ObjectId id = or.repo.resolve(branch.branch());
      66           2 :       if (id == null) {
      67           0 :         throw new IOException("Cannot open branch " + branch.branch());
      68             :       }
      69           2 :       RevCommit commit = or.rw.parseCommit(id);
      70             : 
      71           2 :       try (TreeWalk tw = TreeWalk.forPath(or.repo, GIT_MODULES, commit.getTree())) {
      72           2 :         if (tw == null || (tw.getRawMode(0) & FileMode.TYPE_MASK) != FileMode.TYPE_FILE) {
      73           2 :           subscriptions = Collections.emptySet();
      74           2 :           logger.atFine().log("The .gitmodules file doesn't exist in %s", branch);
      75           2 :           return;
      76             :         }
      77           2 :       }
      78             :       BlobBasedConfig config;
      79             :       try {
      80           2 :         config = new BlobBasedConfig(null, or.repo, commit, GIT_MODULES);
      81           0 :       } catch (ConfigInvalidException e) {
      82           0 :         throw new IOException(
      83           0 :             "Could not read .gitmodules of super project: " + branch.project(), e);
      84           2 :       }
      85           2 :       subscriptions =
      86           2 :           new SubmoduleSectionParser(config, canonicalWebUrl, branch).parseAllSections();
      87           0 :     } catch (NoSuchProjectException e) {
      88           0 :       throw new IOException(e);
      89           2 :     }
      90           2 :   }
      91             : 
      92             :   Collection<SubmoduleSubscription> subscribedTo(BranchNameKey src) {
      93           2 :     Collection<SubmoduleSubscription> ret = new ArrayList<>();
      94           2 :     for (SubmoduleSubscription s : subscriptions) {
      95           2 :       if (s.getSubmodule().equals(src)) {
      96           2 :         ret.add(s);
      97             :       }
      98           2 :     }
      99           2 :     return ret;
     100             :   }
     101             : }

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