LCOV - code coverage report
Current view: top level - sshd - AbstractGitCommand.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 28 30 93.3 %
Date: 2022-11-19 15:00:39 Functions: 8 8 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2008 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.sshd;
      16             : 
      17             : import com.google.gerrit.entities.Project;
      18             : import com.google.gerrit.server.AccessPath;
      19             : import com.google.gerrit.server.DynamicOptions;
      20             : import com.google.gerrit.server.IdentifiedUser;
      21             : import com.google.gerrit.server.git.GitRepositoryManager;
      22             : import com.google.gerrit.server.permissions.PermissionBackendException;
      23             : import com.google.gerrit.server.project.ProjectState;
      24             : import com.google.gerrit.sshd.SshScope.Context;
      25             : import com.google.inject.Inject;
      26             : import java.io.IOException;
      27             : import org.apache.sshd.server.Environment;
      28             : import org.apache.sshd.server.channel.ChannelSession;
      29             : import org.eclipse.jgit.errors.RepositoryNotFoundException;
      30             : import org.eclipse.jgit.lib.Repository;
      31             : import org.kohsuke.args4j.Argument;
      32             : 
      33           5 : public abstract class AbstractGitCommand extends BaseCommand {
      34             :   private static final String GIT_PROTOCOL = "GIT_PROTOCOL";
      35             : 
      36             :   @Argument(index = 0, metaVar = "PROJECT.git", required = true, usage = "project name")
      37             :   protected ProjectState projectState;
      38             : 
      39             :   @Inject private SshScope sshScope;
      40             : 
      41             :   @Inject private GitRepositoryManager repoManager;
      42             : 
      43             :   @Inject private SshScope.Context context;
      44             : 
      45             :   @Inject private IdentifiedUser.GenericFactory userFactory;
      46             : 
      47             :   @Inject protected SshSession session;
      48             : 
      49             :   protected Repository repo;
      50             :   protected Project.NameKey projectName;
      51             :   protected Project project;
      52             :   protected String[] extraParameters;
      53             : 
      54             :   @Override
      55             :   public void start(ChannelSession channel, Environment env) {
      56           5 :     enableGracefulStop();
      57           5 :     String gitProtocol = env.getEnv().get(GIT_PROTOCOL);
      58           5 :     if (gitProtocol != null) {
      59           4 :       extraParameters = gitProtocol.split(":");
      60             :     }
      61             : 
      62           5 :     Context ctx = context.subContext(newSession(), context.getCommandLine());
      63           5 :     final Context old = sshScope.set(ctx);
      64             :     try {
      65           5 :       startThread(
      66           5 :           new ProjectCommandRunnable() {
      67             :             @Override
      68             :             public void executeParseCommand(DynamicOptions pluginOptions) throws Exception {
      69           4 :               parseCommandLine(pluginOptions);
      70           4 :             }
      71             : 
      72             :             @Override
      73             :             public void run() throws Exception {
      74           4 :               AbstractGitCommand.this.service();
      75           4 :             }
      76             : 
      77             :             @Override
      78             :             public Project.NameKey getProjectName() {
      79           4 :               Project project = projectState.getProject();
      80           4 :               return project.getNameKey();
      81             :             }
      82             :           },
      83             :           AccessPath.GIT);
      84             :     } finally {
      85           5 :       sshScope.set(old);
      86             :     }
      87           5 :   }
      88             : 
      89             :   private SshSession newSession() {
      90           5 :     SshSession n =
      91             :         new SshSession(
      92             :             session,
      93           5 :             session.getRemoteAddress(),
      94           5 :             userFactory.create(session.getRemoteAddress(), user.getAccountId()));
      95           5 :     return n;
      96             :   }
      97             : 
      98             :   private void service() throws IOException, PermissionBackendException, Failure {
      99           4 :     project = projectState.getProject();
     100           4 :     projectName = project.getNameKey();
     101             : 
     102             :     try {
     103           4 :       repo = repoManager.openRepository(projectName);
     104           0 :     } catch (RepositoryNotFoundException e) {
     105           0 :       throw new Failure(1, "fatal: '" + project.getName() + "': not a git archive", e);
     106           4 :     }
     107             : 
     108             :     try {
     109           4 :       runImpl();
     110             :     } finally {
     111           4 :       repo.close();
     112             :     }
     113           4 :   }
     114             : 
     115             :   protected abstract void runImpl() throws IOException, PermissionBackendException, Failure;
     116             : }

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