LCOV - code coverage report
Current view: top level - server/project - FileResource.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 16 16 100.0 %
Date: 2022-11-19 15:00:39 Functions: 7 7 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 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.project;
      16             : 
      17             : import com.google.gerrit.extensions.restapi.IdString;
      18             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      19             : import com.google.gerrit.extensions.restapi.RestResource;
      20             : import com.google.gerrit.extensions.restapi.RestView;
      21             : import com.google.gerrit.server.git.GitRepositoryManager;
      22             : import com.google.inject.TypeLiteral;
      23             : import java.io.IOException;
      24             : import org.eclipse.jgit.lib.ObjectId;
      25             : import org.eclipse.jgit.lib.Repository;
      26             : import org.eclipse.jgit.revwalk.RevTree;
      27             : import org.eclipse.jgit.revwalk.RevWalk;
      28             : import org.eclipse.jgit.treewalk.TreeWalk;
      29             : 
      30             : /**
      31             :  * A file.
      32             :  *
      33             :  * <p>This is in the project package because it is accessed through the project/branch/file path.
      34             :  */
      35             : public class FileResource implements RestResource {
      36         152 :   public static final TypeLiteral<RestView<FileResource>> FILE_KIND = new TypeLiteral<>() {};
      37             : 
      38             :   public static FileResource create(
      39             :       GitRepositoryManager repoManager, ProjectState projectState, ObjectId rev, String path)
      40             :       throws ResourceNotFoundException, IOException {
      41           5 :     try (Repository repo = repoManager.openRepository(projectState.getNameKey());
      42           5 :         RevWalk rw = new RevWalk(repo)) {
      43           5 :       RevTree tree = rw.parseTree(rev);
      44           5 :       if (TreeWalk.forPath(repo, path, tree) != null) {
      45           5 :         return new FileResource(projectState, rev, path);
      46             :       }
      47           5 :     }
      48           1 :     throw new ResourceNotFoundException(IdString.fromDecoded(path));
      49             :   }
      50             : 
      51             :   private final ProjectState projectState;
      52             :   private final ObjectId rev;
      53             :   private final String path;
      54             : 
      55           5 :   public FileResource(ProjectState projectState, ObjectId rev, String path) {
      56           5 :     this.projectState = projectState;
      57           5 :     this.rev = rev;
      58           5 :     this.path = path;
      59           5 :   }
      60             : 
      61             :   public ProjectState getProjectState() {
      62           5 :     return projectState;
      63             :   }
      64             : 
      65             :   public ObjectId getRev() {
      66           5 :     return rev;
      67             :   }
      68             : 
      69             :   public String getPath() {
      70           5 :     return path;
      71             :   }
      72             : }

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