summaryrefslogtreecommitdiffstats
path: root/java/com/google/gerrit/server/patch/diff/ModifiedFilesCacheImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/com/google/gerrit/server/patch/diff/ModifiedFilesCacheImpl.java')
-rw-r--r--java/com/google/gerrit/server/patch/diff/ModifiedFilesCacheImpl.java63
1 files changed, 14 insertions, 49 deletions
diff --git a/java/com/google/gerrit/server/patch/diff/ModifiedFilesCacheImpl.java b/java/com/google/gerrit/server/patch/diff/ModifiedFilesCacheImpl.java
index b4d8c04df0..41f2fedab7 100644
--- a/java/com/google/gerrit/server/patch/diff/ModifiedFilesCacheImpl.java
+++ b/java/com/google/gerrit/server/patch/diff/ModifiedFilesCacheImpl.java
@@ -1,16 +1,16 @@
-// Copyright (C) 2020 The Android Open Source Project
+// Copyright (C) 2020 The Android Open Source Project
//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
//
-// http://www.apache.org/licenses/LICENSE-2.0
+// http://www.apache.org/licenses/LICENSE-2.0
//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
package com.google.gerrit.server.patch.diff;
@@ -18,14 +18,11 @@ import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
-import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.ListMultimap;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import com.google.common.flogger.FluentLogger;
-import com.google.gerrit.entities.Patch.ChangeType;
import com.google.gerrit.server.cache.CacheModule;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.patch.DiffNotAvailableException;
@@ -40,7 +37,6 @@ import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import java.io.IOException;
-import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
@@ -143,14 +139,15 @@ public class ModifiedFilesCacheImpl implements ModifiedFilesCache {
.bTree(bTree)
.renameScore(key.renameScore())
.build();
- List<ModifiedFile> modifiedFiles = mergeRewrittenEntries(gitCache.get(gitKey));
+ ImmutableList<ModifiedFile> modifiedFiles =
+ DiffUtil.mergeRewrittenModifiedFiles(gitCache.get(gitKey));
if (key.aCommit().equals(ObjectId.zeroId())) {
- return ImmutableList.copyOf(modifiedFiles);
+ return modifiedFiles;
}
RevCommit revCommitA = DiffUtil.getRevCommit(rw, key.aCommit());
RevCommit revCommitB = DiffUtil.getRevCommit(rw, key.bCommit());
if (DiffUtil.areRelated(revCommitA, revCommitB)) {
- return ImmutableList.copyOf(modifiedFiles);
+ return modifiedFiles;
}
Set<String> touchedFiles =
getTouchedFilesWithParents(
@@ -206,37 +203,5 @@ public class ModifiedFilesCacheImpl implements ModifiedFilesCache {
// value as the set of file paths shouldn't contain it.
return touchedFilePaths.contains(oldFilePath) || touchedFilePaths.contains(newFilePath);
}
-
- /**
- * Return the {@code modifiedFiles} input list while merging rewritten entries.
- *
- * <p>Background: In some cases, JGit returns two diff entries (ADDED/DELETED, RENAMED/DELETED,
- * etc...) for the same file path. This happens e.g. when a file's mode is changed between
- * patchsets, for example converting a symlink file to a regular file. We identify this case and
- * return a single modified file with changeType = {@link ChangeType#REWRITE}.
- */
- private static List<ModifiedFile> mergeRewrittenEntries(List<ModifiedFile> modifiedFiles) {
- List<ModifiedFile> result = new ArrayList<>();
- ListMultimap<String, ModifiedFile> byPath = ArrayListMultimap.create();
- modifiedFiles.stream()
- .forEach(
- f -> {
- if (f.changeType() == ChangeType.DELETED) {
- byPath.get(f.oldPath().get()).add(f);
- } else {
- byPath.get(f.newPath().get()).add(f);
- }
- });
- for (String path : byPath.keySet()) {
- List<ModifiedFile> entries = byPath.get(path);
- if (entries.size() == 1) {
- result.add(entries.get(0));
- } else {
- // More than one. Return a single REWRITE entry.
- result.add(entries.get(0).toBuilder().changeType(ChangeType.REWRITE).build());
- }
- }
- return result;
- }
}
}