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.query.approval; 16 : 17 : import com.google.gerrit.entities.Patch; 18 : import com.google.gerrit.entities.Patch.ChangeType; 19 : import com.google.gerrit.entities.PatchSet; 20 : import com.google.gerrit.entities.Project; 21 : import com.google.gerrit.exceptions.StorageException; 22 : import com.google.gerrit.index.query.Predicate; 23 : import com.google.gerrit.server.git.GitRepositoryManager; 24 : import com.google.gerrit.server.patch.DiffNotAvailableException; 25 : import com.google.gerrit.server.patch.DiffOperations; 26 : import com.google.gerrit.server.patch.DiffOptions; 27 : import com.google.gerrit.server.patch.gitdiff.ModifiedFile; 28 : import com.google.inject.Inject; 29 : import com.google.inject.Singleton; 30 : import java.io.IOException; 31 : import java.util.Collection; 32 : import java.util.HashSet; 33 : import java.util.Map; 34 : import java.util.Objects; 35 : import java.util.Set; 36 : import org.eclipse.jgit.lib.ObjectId; 37 : import org.eclipse.jgit.lib.Repository; 38 : import org.eclipse.jgit.revwalk.RevWalk; 39 : 40 : /** Predicate that matches when the new patch-set includes the same files as the old patch-set. */ 41 : @Singleton 42 : public class ListOfFilesUnchangedPredicate extends ApprovalPredicate { 43 : private final DiffOperations diffOperations; 44 : private final GitRepositoryManager repositoryManager; 45 : 46 : @Inject 47 : public ListOfFilesUnchangedPredicate( 48 148 : DiffOperations diffOperations, GitRepositoryManager repositoryManager) { 49 148 : this.diffOperations = diffOperations; 50 148 : this.repositoryManager = repositoryManager; 51 148 : } 52 : 53 : @Override 54 : public boolean match(ApprovalContext ctx) { 55 2 : PatchSet targetPatchSet = ctx.targetPatchSet(); 56 2 : PatchSet sourcePatchSet = ctx.changeNotes().getPatchSets().get(ctx.sourcePatchSetId()); 57 : 58 : Integer parentNum = 59 2 : isInitialCommit(ctx.changeNotes().getProjectName(), targetPatchSet.commitId()) ? 0 : 1; 60 : try { 61 2 : Map<String, ModifiedFile> baseVsCurrent = 62 2 : diffOperations.loadModifiedFilesAgainstParent( 63 2 : ctx.changeNotes().getProjectName(), 64 2 : targetPatchSet.commitId(), 65 2 : parentNum, 66 : DiffOptions.DEFAULTS, 67 2 : ctx.revWalk(), 68 2 : ctx.repoConfig()); 69 2 : Map<String, ModifiedFile> baseVsPrior = 70 2 : diffOperations.loadModifiedFilesAgainstParent( 71 2 : ctx.changeNotes().getProjectName(), 72 2 : sourcePatchSet.commitId(), 73 2 : parentNum, 74 : DiffOptions.DEFAULTS, 75 2 : ctx.revWalk(), 76 2 : ctx.repoConfig()); 77 2 : Map<String, ModifiedFile> priorVsCurrent = 78 2 : diffOperations.loadModifiedFiles( 79 2 : ctx.changeNotes().getProjectName(), 80 2 : sourcePatchSet.commitId(), 81 2 : targetPatchSet.commitId(), 82 : DiffOptions.DEFAULTS, 83 2 : ctx.revWalk(), 84 2 : ctx.repoConfig()); 85 2 : return match(baseVsCurrent, baseVsPrior, priorVsCurrent); 86 0 : } catch (DiffNotAvailableException ex) { 87 0 : throw new StorageException( 88 : "failed to compute difference in files, so won't copy" 89 : + " votes on labels even if list of files is the same", 90 : ex); 91 : } 92 : } 93 : 94 : /** 95 : * returns {@code true} if the files that were modified are the same in both inputs, and the 96 : * {@link ChangeType} matches for each modified file. 97 : */ 98 : public boolean match( 99 : Map<String, ModifiedFile> baseVsCurrent, 100 : Map<String, ModifiedFile> baseVsPrior, 101 : Map<String, ModifiedFile> priorVsCurrent) { 102 2 : Set<String> allFiles = new HashSet<>(); 103 2 : allFiles.addAll(baseVsCurrent.keySet()); 104 2 : allFiles.addAll(baseVsPrior.keySet()); 105 2 : for (String file : allFiles) { 106 2 : if (Patch.isMagic(file)) { 107 0 : continue; 108 : } 109 2 : ModifiedFile modifiedFile1 = baseVsCurrent.get(file); 110 2 : ModifiedFile modifiedFile2 = baseVsPrior.get(file); 111 2 : if (!priorVsCurrent.containsKey(file)) { 112 : // If the file is not modified between prior and current patchsets, then scan safely skip 113 : // it. The file might have been modified due to rebase. 114 1 : continue; 115 : } 116 2 : if (modifiedFile1 == null || modifiedFile2 == null) { 117 2 : return false; 118 : } 119 2 : if (!modifiedFile2.changeType().equals(modifiedFile1.changeType())) { 120 0 : return false; 121 : } 122 2 : } 123 2 : return true; 124 : } 125 : 126 : public boolean isInitialCommit(Project.NameKey project, ObjectId objectId) { 127 2 : try (Repository repo = repositoryManager.openRepository(project); 128 2 : RevWalk revWalk = new RevWalk(repo)) { 129 2 : return revWalk.parseCommit(objectId).getParentCount() == 0; 130 0 : } catch (IOException ex) { 131 0 : throw new StorageException(ex); 132 : } 133 : } 134 : 135 : @Override 136 : public Predicate<ApprovalContext> copy( 137 : Collection<? extends Predicate<ApprovalContext>> children) { 138 0 : return new ListOfFilesUnchangedPredicate(diffOperations, repositoryManager); 139 : } 140 : 141 : @Override 142 : public int hashCode() { 143 0 : return Objects.hash(diffOperations, repositoryManager); 144 : } 145 : 146 : @Override 147 : public boolean equals(Object other) { 148 0 : if (!(other instanceof ListOfFilesUnchangedPredicate)) { 149 0 : return false; 150 : } 151 0 : ListOfFilesUnchangedPredicate o = (ListOfFilesUnchangedPredicate) other; 152 0 : return Objects.equals(o.diffOperations, diffOperations) 153 0 : && Objects.equals(o.repositoryManager, repositoryManager); 154 : } 155 : }