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

          Line data    Source code
       1             : // Copyright (C) 2021 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.patch;
      16             : 
      17             : import static java.nio.charset.StandardCharsets.UTF_8;
      18             : 
      19             : import java.io.IOException;
      20             : import org.eclipse.jgit.lib.Constants;
      21             : import org.eclipse.jgit.lib.FileMode;
      22             : import org.eclipse.jgit.lib.ObjectId;
      23             : import org.eclipse.jgit.lib.Repository;
      24             : 
      25             : /** Resolver of the source content of a specific file */
      26             : public class SrcContentResolver {
      27             : 
      28             :   private SrcContentResolver() {}
      29             : 
      30             :   /**
      31             :    * Return the source content of a specific file.
      32             :    *
      33             :    * @param repo Git repository.
      34             :    * @param id Git Object ID of the file blob.
      35             :    * @param fileMode File mode of the underlying file as recognized by Git.
      36             :    * @return byte[] source content of the underlying file if the {@code id} is of type blob, or a
      37             :    *     textual representation of the file if it is a git submodule.
      38             :    * @throws IOException the object ID does not exist in the repository or cannot be accessed.
      39             :    */
      40             :   public static byte[] getSourceContent(Repository repo, ObjectId id, FileMode fileMode)
      41             :       throws IOException {
      42          15 :     if (fileMode.getObjectType() == Constants.OBJ_BLOB) {
      43          14 :       return Text.asByteArray(repo.open(id, Constants.OBJ_BLOB));
      44             :     }
      45          11 :     if (fileMode.getObjectType() == Constants.OBJ_COMMIT) {
      46           1 :       String strContent = "Subproject commit " + ObjectId.toString(id);
      47           1 :       return strContent.getBytes(UTF_8);
      48             :     }
      49          10 :     return Text.NO_BYTES;
      50             :   }
      51             : }

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