ChangedFiles: Pass in RevWalk from which the revision should be loaded

If a revision is newly inserted, but not committed to the repository
yet, it can only be seen from the RevWalk instance that created it, but
not by newly instantiated RevWalks. To support loading changed files
from newly inserted revisions that have not been committed yet, we now
allow to pass in a RevWalk instance that should be used to load it.

In addition we allow to pass in the repository config, since callers
that will need to pass in the RevWalk (CodeOwnerConfigValidator) will
have the repository config already available, and hence we will not need
to reopen the repository just to read the repository config.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: If80c7fcd48219b7217de5d517fcd817cf974e999
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/ChangedFiles.java b/java/com/google/gerrit/plugins/codeowners/backend/ChangedFiles.java
index b1248d0..1a3bb94 100644
--- a/java/com/google/gerrit/plugins/codeowners/backend/ChangedFiles.java
+++ b/java/com/google/gerrit/plugins/codeowners/backend/ChangedFiles.java
@@ -37,6 +37,7 @@
 import org.eclipse.jgit.diff.DiffEntry;
 import org.eclipse.jgit.diff.DiffFormatter;
 import org.eclipse.jgit.diff.RawTextComparator;
+import org.eclipse.jgit.lib.Config;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
@@ -102,23 +103,34 @@
       throws IOException, PatchListNotAvailableException {
     requireNonNull(project, "project");
     requireNonNull(revision, "revision");
-    logger.atFine().log(
-        "computing changed files for revision %s in project %s", revision.name(), project);
 
     try (Repository repository = repoManager.openRepository(project);
         RevWalk revWalk = new RevWalk(repository)) {
       RevCommit revCommit = revWalk.parseCommit(revision);
-
-      if (revCommit.getParentCount() > 1
-          && MergeCommitStrategy.FILES_WITH_CONFLICT_RESOLUTION.equals(
-              codeOwnersPluginConfiguration.getMergeCommitStrategy(project))) {
-        return computeByComparingAgainstAutoMerge(project, revCommit);
-      }
-
-      return computeByComparingAgainstFirstParent(repository, revWalk, revCommit);
+      return compute(project, repository.getConfig(), revWalk, revCommit);
     }
   }
 
+  public ImmutableSet<ChangedFile> compute(
+      Project.NameKey project, Config repoConfig, RevWalk revWalk, RevCommit revCommit)
+      throws IOException, PatchListNotAvailableException {
+    requireNonNull(project, "project");
+    requireNonNull(repoConfig, "repoConfig");
+    requireNonNull(revWalk, "revWalk");
+    requireNonNull(revCommit, "revCommit");
+
+    logger.atFine().log(
+        "computing changed files for revision %s in project %s", revCommit.name(), project);
+
+    if (revCommit.getParentCount() > 1
+        && MergeCommitStrategy.FILES_WITH_CONFLICT_RESOLUTION.equals(
+            codeOwnersPluginConfiguration.getMergeCommitStrategy(project))) {
+      return computeByComparingAgainstAutoMerge(project, revCommit);
+    }
+
+    return computeByComparingAgainstFirstParent(repoConfig, revWalk, revCommit);
+  }
+
   /**
    * Computes the changed files by comparing the given merge commit against the auto merge.
    *
@@ -151,18 +163,18 @@
    *
    * <p>The computation also works if the commit doesn't have any parent.
    *
-   * @param repository the repository
+   * @param repoConfig the repository configuration
    * @param revWalk the rev walk
    * @param revCommit the commit for which the changed files should be computed
    * @return the changed files for the given commit
    */
   private ImmutableSet<ChangedFile> computeByComparingAgainstFirstParent(
-      Repository repository, RevWalk revWalk, RevCommit revCommit) throws IOException {
+      Config repoConfig, RevWalk revWalk, RevCommit revCommit) throws IOException {
     RevCommit baseCommit = revCommit.getParentCount() > 0 ? revCommit.getParent(0) : null;
     logger.atFine().log("baseCommit = %s", baseCommit != null ? baseCommit.name() : "n/a");
 
     try (DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE)) {
-      diffFormatter.setReader(revWalk.getObjectReader(), repository.getConfig());
+      diffFormatter.setReader(revWalk.getObjectReader(), repoConfig);
       diffFormatter.setDiffComparator(RawTextComparator.DEFAULT);
       diffFormatter.setDetectRenames(true);
       List<DiffEntry> diffEntries = diffFormatter.scan(baseCommit, revCommit);