Let PatchListLoader always load parent commits

For comparing a commit against one of its parent commits,
PatchListLoader accepts PatchListKeys with a parentNum. If parentNum
in a PatchListKey is set, PatchListLoader takes care to load the
parent commit.

However some code paths did not make use of this functionality in
PatchListLoader. Instead they loaded the parent commit upfront and
then constructed a PatchListKey with 2 commits (the commit and its
parent commit) to load the PatchList.

Loading the parent commit upfront is unneeded since PatchListLoader
knows how to do this. If the parent commit is always loaded by
PatchListLoader, the code for loading the parent commit upfront can be
deleted. Also for listing files the repository is opened once less
now.

Change-Id: Ifca30529d25419aaf5af2252f1bcd45ab5dbb853
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-09-12 11:43:28 +02:00
parent da3a8d32ca
commit 7df4192760
3 changed files with 11 additions and 76 deletions

View File

@@ -14,8 +14,6 @@
package com.google.gerrit.server.change;
import static com.google.gerrit.server.util.GitUtil.getParent;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.extensions.client.DiffPreferencesInfo.Whitespace;
import com.google.gerrit.extensions.common.FileInfo;
@@ -23,7 +21,6 @@ import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Patch;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.patch.PatchList;
import com.google.gerrit.server.patch.PatchListCache;
import com.google.gerrit.server.patch.PatchListEntry;
@@ -32,24 +29,18 @@ import com.google.gerrit.server.patch.PatchListNotAvailableException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
@Singleton
public class FileInfoJson {
private final PatchListCache patchListCache;
private final GitRepositoryManager repoManager;
@Inject
FileInfoJson(
PatchListCache patchListCache,
GitRepositoryManager repoManager) {
this.repoManager = repoManager;
PatchListCache patchListCache) {
this.patchListCache = patchListCache;
}
@@ -64,24 +55,19 @@ public class FileInfoJson {
? null
: ObjectId.fromString(base.getRevision().get());
ObjectId b = ObjectId.fromString(revision.get());
return toFileInfoMap(change, a, b);
return toFileInfoMap(change, new PatchListKey(a, b, Whitespace.IGNORE_NONE));
}
Map<String, FileInfo> toFileInfoMap(Change change, RevId revision, int parent)
throws RepositoryNotFoundException, IOException,
PatchListNotAvailableException {
throws PatchListNotAvailableException {
ObjectId b = ObjectId.fromString(revision.get());
ObjectId a;
try (Repository git = repoManager.openRepository(change.getProject())) {
a = getParent(git, b, parent);
}
return toFileInfoMap(change, a, b);
return toFileInfoMap(change,
PatchListKey.againstParentNum(parent + 1, b, Whitespace.IGNORE_NONE));
}
private Map<String, FileInfo> toFileInfoMap(Change change,
ObjectId a, ObjectId b) throws PatchListNotAvailableException {
PatchList list = patchListCache.get(
new PatchListKey(a, b, Whitespace.IGNORE_NONE), change.getProject());
PatchListKey key) throws PatchListNotAvailableException {
PatchList list = patchListCache.get(key, change.getProject());
Map<String, FileInfo> files = new TreeMap<>();
for (PatchListEntry e : list.getPatches()) {