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 com.google.gerrit.common.Nullable; 18 : import com.google.gerrit.entities.Patch.ChangeType; 19 : import java.util.Optional; 20 : 21 : /** 22 : * Adapter for old/new paths of the new diff cache to the old diff cache representation. This is 23 : * needed for backward compatibility with all old diff cache callers. 24 : * 25 : * <p>TODO(ghareeb): It's better to revisit this logic and update all diff cache callers to use the 26 : * new diff cache output directly. 27 : */ 28 : public class FilePathAdapter { 29 : private FilePathAdapter() {} 30 : 31 : /** 32 : * Converts the old file path of the new diff cache output to the old diff cache representation. 33 : */ 34 : @Nullable 35 : public static String getOldPath(Optional<String> oldName, ChangeType changeType) { 36 103 : switch (changeType) { 37 : case DELETED: 38 : case ADDED: 39 : case MODIFIED: 40 103 : return null; 41 : case COPIED: 42 : case RENAMED: 43 11 : return oldName.get(); 44 : case REWRITE: 45 2 : return oldName.isPresent() ? oldName.get() : null; 46 : default: 47 0 : throw new IllegalArgumentException("Unsupported type " + changeType); 48 : } 49 : } 50 : 51 : /** 52 : * Converts the new file path of the new diff cache output to the old diff cache representation. 53 : */ 54 : public static String getNewPath( 55 : Optional<String> oldName, Optional<String> newName, ChangeType changeType) { 56 90 : switch (changeType) { 57 : case DELETED: 58 22 : return oldName.get(); 59 : case ADDED: 60 : case MODIFIED: 61 : case REWRITE: 62 : case COPIED: 63 : case RENAMED: 64 90 : return newName.get(); 65 : default: 66 0 : throw new IllegalArgumentException("Unsupported type " + changeType); 67 : } 68 : } 69 : }