LCOV - code coverage report
Current view: top level - server/patch - DiffMappings.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 9 21 42.9 %
Date: 2022-11-19 15:00:39 Functions: 3 7 42.9 %

          Line data    Source code
       1             : // Copyright (C) 2020 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 com.google.common.collect.ImmutableSet.toImmutableSet;
      18             : import static java.util.stream.Collectors.toList;
      19             : 
      20             : import com.google.common.collect.ImmutableSet;
      21             : import com.google.gerrit.entities.Patch;
      22             : import com.google.gerrit.server.patch.GitPositionTransformer.FileMapping;
      23             : import com.google.gerrit.server.patch.GitPositionTransformer.Mapping;
      24             : import com.google.gerrit.server.patch.GitPositionTransformer.Range;
      25             : import com.google.gerrit.server.patch.GitPositionTransformer.RangeMapping;
      26             : import com.google.gerrit.server.patch.filediff.Edit;
      27             : import com.google.gerrit.server.patch.filediff.FileEdits;
      28             : import java.util.List;
      29             : 
      30             : /** Mappings derived from diffs. */
      31             : public class DiffMappings {
      32             : 
      33             :   private DiffMappings() {}
      34             : 
      35             :   public static Mapping toMapping(PatchListEntry patchListEntry) {
      36           0 :     FileMapping fileMapping = toFileMapping(patchListEntry);
      37           0 :     ImmutableSet<RangeMapping> rangeMappings = toRangeMappings(patchListEntry);
      38           0 :     return Mapping.create(fileMapping, rangeMappings);
      39             :   }
      40             : 
      41             :   public static Mapping toMapping(FileEdits fileEdits) {
      42           3 :     FileMapping fileMapping = FileMapping.forFile(fileEdits.oldPath(), fileEdits.newPath());
      43           3 :     ImmutableSet<RangeMapping> rangeMappings = toRangeMappings(fileEdits.edits());
      44           3 :     return Mapping.create(fileMapping, rangeMappings);
      45             :   }
      46             : 
      47             :   private static FileMapping toFileMapping(PatchListEntry ple) {
      48           0 :     return toFileMapping(ple.getChangeType(), ple.getOldName(), ple.getNewName());
      49             :   }
      50             : 
      51             :   private static FileMapping toFileMapping(
      52             :       Patch.ChangeType changeType, String oldName, String newName) {
      53           0 :     switch (changeType) {
      54             :       case ADDED:
      55           0 :         return FileMapping.forAddedFile(newName);
      56             :       case MODIFIED:
      57             :       case REWRITE:
      58           0 :         return FileMapping.forModifiedFile(newName);
      59             :       case DELETED:
      60             :         // Name of deleted file is mentioned as newName.
      61           0 :         return FileMapping.forDeletedFile(newName);
      62             :       case RENAMED:
      63             :       case COPIED:
      64           0 :         return FileMapping.forRenamedFile(oldName, newName);
      65             :       default:
      66           0 :         throw new IllegalStateException("Unmapped diff type: " + changeType);
      67             :     }
      68             :   }
      69             : 
      70             :   private static ImmutableSet<RangeMapping> toRangeMappings(PatchListEntry patchListEntry) {
      71           0 :     return toRangeMappings(
      72           0 :         patchListEntry.getEdits().stream().map(Edit::fromJGitEdit).collect(toList()));
      73             :   }
      74             : 
      75             :   private static ImmutableSet<RangeMapping> toRangeMappings(List<Edit> edits) {
      76           3 :     return edits.stream()
      77           3 :         .map(
      78             :             edit ->
      79           3 :                 RangeMapping.create(
      80           3 :                     Range.create(edit.beginA(), edit.endA()),
      81           3 :                     Range.create(edit.beginB(), edit.endB())))
      82           3 :         .collect(toImmutableSet());
      83             :   }
      84             : }

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