SkipCodeOwnerConfigValidationPushOption: Get changed files without rename detection

The code only needs to know if any OWNERS file has been added or
updated. For this is only looks at the new paths of the changed files.
Hence rename detection is not required here.

If the rename detection is enabled, renamed files are retured as a
single ChangedFile (with old and new path). If the rename detection is
disabled, renamed files are returned as two ChangeFiles (one with the
old path for the deletion and one with the new path for the addition).
Verify this behaviour in a test.

Change-Id: If42e8410045d38613e6292e8d6e0e87575214ab0
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/ChangedFiles.java b/java/com/google/gerrit/plugins/codeowners/backend/ChangedFiles.java
index f0723bc..ccbb5ba 100644
--- a/java/com/google/gerrit/plugins/codeowners/backend/ChangedFiles.java
+++ b/java/com/google/gerrit/plugins/codeowners/backend/ChangedFiles.java
@@ -86,6 +86,32 @@
   public ImmutableList<ChangedFile> get(
       Project.NameKey project, ObjectId revision, MergeCommitStrategy mergeCommitStrategy)
       throws IOException, DiffNotAvailableException {
+    return get(project, revision, mergeCommitStrategy, /* enableRenameDetection= */ true);
+  }
+
+  /**
+   * Gets the changed files.
+   *
+   * <p>Rename detection is disabled.
+   *
+   * @param project the project
+   * @param revision the revision for which the changed files should be retrieved
+   * @param mergeCommitStrategy the merge commit strategy that should be used to compute the changed
+   *     files for merge commits
+   * @return the files that have been changed in the given revision, sorted alphabetically by path
+   */
+  public ImmutableList<ChangedFile> getWithoutRenameDetection(
+      Project.NameKey project, ObjectId revision, MergeCommitStrategy mergeCommitStrategy)
+      throws IOException, DiffNotAvailableException {
+    return get(project, revision, mergeCommitStrategy, /* enableRenameDetection= */ false);
+  }
+
+  private ImmutableList<ChangedFile> get(
+      Project.NameKey project,
+      ObjectId revision,
+      MergeCommitStrategy mergeCommitStrategy,
+      boolean enableRenameDetection)
+      throws IOException, DiffNotAvailableException {
     requireNonNull(project, "project");
     requireNonNull(revision, "revision");
     requireNonNull(mergeCommitStrategy, "mergeCommitStrategy");
@@ -101,7 +127,7 @@
         // if the merge commit strategy is FILES_WITH_CONFLICT_RESOLUTION.
         modifiedFiles =
             diffOperations.getModifiedFiles(
-                project, revision, /* parentNum= */ 0, /* enableRenameDetection= */ true);
+                project, revision, /* parentNum= */ 0, enableRenameDetection);
       } else {
         checkState(mergeCommitStrategy.equals(MergeCommitStrategy.ALL_CHANGED_FILES));
         // Always use parent 1 to do the comparison.
@@ -111,7 +137,7 @@
         // ALL_CHANGED_FILES.
         modifiedFiles =
             diffOperations.getModifiedFiles(
-                project, revision, /* parentNum= */ 1, /* enableRenameDetection= */ true);
+                project, revision, /* parentNum= */ 1, enableRenameDetection);
       }
 
       return modifiedFilesToChangedFiles(filterOutMagicFilesFromModifiedFilesAndSort(modifiedFiles))
diff --git a/java/com/google/gerrit/plugins/codeowners/validation/SkipCodeOwnerConfigValidationPushOption.java b/java/com/google/gerrit/plugins/codeowners/validation/SkipCodeOwnerConfigValidationPushOption.java
index dafa555..222334a 100644
--- a/java/com/google/gerrit/plugins/codeowners/validation/SkipCodeOwnerConfigValidationPushOption.java
+++ b/java/com/google/gerrit/plugins/codeowners/validation/SkipCodeOwnerConfigValidationPushOption.java
@@ -98,7 +98,9 @@
     // (MergeCommitStrategy.ALL_CHANGED_FILES) as this is what CodeOwnerConfigValidator does.
     try {
       return changedFiles
-          .get(
+          // we are only interested in the new paths (see below), hence we do not need to detect
+          // renames
+          .getWithoutRenameDetection(
               changeNotes.getProjectName(),
               changeNotes.getCurrentPatchSet().commitId(),
               MergeCommitStrategy.ALL_CHANGED_FILES)
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/ChangedFilesTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/ChangedFilesTest.java
index 9c5e303..b225828 100644
--- a/javatests/com/google/gerrit/plugins/codeowners/backend/ChangedFilesTest.java
+++ b/javatests/com/google/gerrit/plugins/codeowners/backend/ChangedFilesTest.java
@@ -192,6 +192,23 @@
     changedFile.hasOldPath().value().isEqualTo(Path.of(oldPath));
     changedFile.isRename();
     changedFile.isNoDeletion();
+
+    ImmutableList<ChangedFile> changedFilesSetWithoutRenameDetection =
+        changedFiles.getWithoutRenameDetection(
+            project,
+            getRevisionResource(change.id()).getPatchSet().commitId(),
+            MergeCommitStrategy.ALL_CHANGED_FILES);
+    assertThatCollection(changedFilesSetWithoutRenameDetection).hasSize(2);
+    ChangedFileSubject addition = assertThat(changedFilesSetWithoutRenameDetection.get(0));
+    addition.hasNewPath().value().isEqualTo(Path.of(newPath));
+    addition.hasOldPath().isEmpty();
+    addition.isNoRename();
+    addition.isNoDeletion();
+    ChangedFileSubject deletion = assertThat(changedFilesSetWithoutRenameDetection.get(1));
+    deletion.hasNewPath().isEmpty();
+    deletion.hasOldPath().value().isEqualTo(Path.of(oldPath));
+    deletion.isNoRename();
+    deletion.isDeletion();
   }
 
   @Test