LCOV - code coverage report
Current view: top level - server/patch - ComparisonType.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 17 25 68.0 %
Date: 2022-11-19 15:00:39 Functions: 12 14 85.7 %

          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.patch;
      16             : 
      17             : import static com.google.gerrit.server.ioutil.BasicSerialization.readVarInt32;
      18             : import static com.google.gerrit.server.ioutil.BasicSerialization.writeVarInt32;
      19             : 
      20             : import com.google.auto.value.AutoValue;
      21             : import com.google.gerrit.server.cache.proto.Cache.FileDiffOutputProto;
      22             : import java.io.IOException;
      23             : import java.io.InputStream;
      24             : import java.io.OutputStream;
      25             : import java.util.Optional;
      26             : 
      27             : /** Relation between the old and new commits used in the diff. */
      28             : @AutoValue
      29         105 : public abstract class ComparisonType {
      30             : 
      31             :   /**
      32             :    * 1-based parent. Available if the old commit is the parent of the new commit and old commit is
      33             :    * not the auto-merge. If set to 0, then comparison is for a root commit.
      34             :    */
      35             :   abstract Optional<Integer> parentNum();
      36             : 
      37             :   abstract boolean autoMerge();
      38             : 
      39             :   public static ComparisonType againstOtherPatchSet() {
      40          37 :     return new AutoValue_ComparisonType(Optional.empty(), false);
      41             :   }
      42             : 
      43             :   public static ComparisonType againstParent(int parentNum) {
      44         100 :     return new AutoValue_ComparisonType(Optional.of(parentNum), false);
      45             :   }
      46             : 
      47             :   public static ComparisonType againstAutoMerge() {
      48          31 :     return new AutoValue_ComparisonType(Optional.empty(), true);
      49             :   }
      50             : 
      51             :   public static ComparisonType againstRoot() {
      52          32 :     return new AutoValue_ComparisonType(Optional.of(0), false);
      53             :   }
      54             : 
      55             :   private static ComparisonType create(Optional<Integer> parent, boolean automerge) {
      56           2 :     return new AutoValue_ComparisonType(parent, automerge);
      57             :   }
      58             : 
      59             :   public boolean isAgainstParentOrAutoMerge() {
      60         104 :     return isAgainstParent() || isAgainstAutoMerge();
      61             :   }
      62             : 
      63             :   public boolean isAgainstParent() {
      64         104 :     return parentNum().isPresent();
      65             :   }
      66             : 
      67             :   public boolean isAgainstAutoMerge() {
      68         104 :     return autoMerge();
      69             :   }
      70             : 
      71             :   public Optional<Integer> getParentNum() {
      72          11 :     return parentNum();
      73             :   }
      74             : 
      75             :   void writeTo(OutputStream out) throws IOException {
      76           0 :     writeVarInt32(out, isAgainstParent() ? parentNum().get() : 0);
      77           0 :     writeVarInt32(out, autoMerge() ? 1 : 0);
      78           0 :   }
      79             : 
      80             :   static ComparisonType readFrom(InputStream in) throws IOException {
      81           0 :     int p = readVarInt32(in);
      82           0 :     Optional<Integer> parentNum = p > 0 ? Optional.of(p) : Optional.empty();
      83           0 :     boolean autoMerge = readVarInt32(in) != 0;
      84           0 :     return create(parentNum, autoMerge);
      85             :   }
      86             : 
      87             :   public FileDiffOutputProto.ComparisonType toProto() {
      88             :     FileDiffOutputProto.ComparisonType.Builder builder =
      89           7 :         FileDiffOutputProto.ComparisonType.newBuilder().setAutoMerge(autoMerge());
      90           7 :     if (parentNum().isPresent()) {
      91           6 :       builder.setParentNum(parentNum().get());
      92             :     }
      93           7 :     return builder.build();
      94             :   }
      95             : 
      96             :   public static ComparisonType fromProto(FileDiffOutputProto.ComparisonType proto) {
      97           2 :     Optional<Integer> parentNum = Optional.empty();
      98           2 :     if (proto.hasField(FileDiffOutputProto.ComparisonType.getDescriptor().findFieldByNumber(1))) {
      99           0 :       parentNum = Optional.of(proto.getParentNum());
     100             :     }
     101           2 :     return create(parentNum, proto.getAutoMerge());
     102             :   }
     103             : }

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