Require a RevWalk when building CommitData
All callers have a RevWalk conveniently available, so simplify by
assuming one is always present.
Change-Id: I9fc83122c3af2240c9c4e39c585af93496ea17aa
diff --git a/java/com/google/gitiles/CommitData.java b/java/com/google/gitiles/CommitData.java
index 576de87..ef2ff95 100644
--- a/java/com/google/gitiles/CommitData.java
+++ b/java/com/google/gitiles/CommitData.java
@@ -81,23 +81,19 @@
}
static class Builder {
- private RevWalk walk;
private ArchiveFormat archiveFormat;
private Map<AnyObjectId, Set<Ref>> refsById;
- Builder setRevWalk(@Nullable RevWalk walk) {
- this.walk = walk;
- return this;
- }
-
Builder setArchiveFormat(@Nullable ArchiveFormat archiveFormat) {
this.archiveFormat = archiveFormat;
return this;
}
- CommitData build(HttpServletRequest req, RevCommit c, Set<Field> fs) throws IOException {
+ CommitData build(HttpServletRequest req, RevWalk walk, RevCommit c, Set<Field> fs)
+ throws IOException {
checkFields(fs);
checkNotNull(req, "request");
+ checkNotNull(walk, "walk");
Repository repo = ServletUtils.getRepository(req);
GitilesView view = ViewFilter.getView(req);
@@ -165,14 +161,13 @@
result.shortMessage = msg;
}
if (fs.contains(Field.DIFF_TREE)) {
- result.diffEntries = computeDiffEntries(repo, view, c);
+ result.diffEntries = computeDiffEntries(repo, view, walk, c);
}
return result;
}
private void checkFields(Set<Field> fs) {
- checkState(!fs.contains(Field.DIFF_TREE) || walk != null, "RevWalk required for diffTree");
if (fs.contains(Field.ARCHIVE_URL) || fs.contains(Field.ARCHIVE_TYPE)) {
checkState(archiveFormat != null, "archive format required");
}
@@ -203,14 +198,15 @@
.collect(toList());
}
- private AbstractTreeIterator getTreeIterator(RevCommit commit) throws IOException {
+ private AbstractTreeIterator getTreeIterator(RevWalk walk, RevCommit commit)
+ throws IOException {
CanonicalTreeParser p = new CanonicalTreeParser();
p.reset(walk.getObjectReader(), walk.parseTree(walk.parseCommit(commit).getTree()));
return p;
}
- private DiffList computeDiffEntries(Repository repo, GitilesView view, RevCommit commit)
- throws IOException {
+ private DiffList computeDiffEntries(
+ Repository repo, GitilesView view, RevWalk walk, RevCommit commit) throws IOException {
DiffList result = new DiffList();
result.revision =
view.getRevision().matches(commit)
@@ -226,13 +222,13 @@
case 1:
result.oldRevision =
Revision.peeled(result.revision.getName() + "^", commit.getParent(0));
- oldTree = getTreeIterator(commit.getParent(0));
+ oldTree = getTreeIterator(walk, commit.getParent(0));
break;
default:
// TODO(dborowitz): handle merges
return result;
}
- AbstractTreeIterator newTree = getTreeIterator(commit);
+ AbstractTreeIterator newTree = getTreeIterator(walk, commit);
try (DiffFormatter diff = new DiffFormatter(NullOutputStream.INSTANCE)) {
diff.setRepository(repo);
diff --git a/java/com/google/gitiles/CommitJsonData.java b/java/com/google/gitiles/CommitJsonData.java
index 96f7f2f..953c19c 100644
--- a/java/com/google/gitiles/CommitJsonData.java
+++ b/java/com/google/gitiles/CommitJsonData.java
@@ -23,7 +23,6 @@
import java.io.IOException;
import java.util.List;
import java.util.Set;
-import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.lib.PersonIdent;
@@ -70,20 +69,15 @@
Integer score;
}
- private RevWalk walk;
-
- CommitJsonData setRevWalk(@Nullable RevWalk walk) {
- this.walk = walk;
- return this;
- }
-
- Commit toJsonData(HttpServletRequest req, RevCommit c, DateFormatter df) throws IOException {
- return toJsonData(req, c, DEFAULT_FIELDS, df);
- }
-
- Commit toJsonData(HttpServletRequest req, RevCommit c, Set<Field> fs, DateFormatter df)
+ Commit toJsonData(HttpServletRequest req, RevWalk walk, RevCommit c, DateFormatter df)
throws IOException {
- CommitData cd = new CommitData.Builder().setRevWalk(walk).build(req, c, fs);
+ return toJsonData(req, walk, c, DEFAULT_FIELDS, df);
+ }
+
+ Commit toJsonData(
+ HttpServletRequest req, RevWalk walk, RevCommit c, Set<Field> fs, DateFormatter df)
+ throws IOException {
+ CommitData cd = new CommitData.Builder().build(req, walk, c, fs);
Commit result = new Commit();
if (cd.sha != null) {
diff --git a/java/com/google/gitiles/CommitSoyData.java b/java/com/google/gitiles/CommitSoyData.java
index 40c5f74..c4087a6 100644
--- a/java/com/google/gitiles/CommitSoyData.java
+++ b/java/com/google/gitiles/CommitSoyData.java
@@ -61,7 +61,6 @@
Sets.immutableEnumSet(Field.PARENT_BLAME_URL);
private Linkifier linkifier;
- private RevWalk walk;
private CommitData.Builder cdb;
private ArchiveFormat archiveFormat;
@@ -70,24 +69,20 @@
return this;
}
- CommitSoyData setRevWalk(@Nullable RevWalk walk) {
- this.walk = walk;
- return this;
- }
-
CommitSoyData setArchiveFormat(@Nullable ArchiveFormat archiveFormat) {
this.archiveFormat = archiveFormat;
return this;
}
Map<String, Object> toSoyData(
- HttpServletRequest req, RevCommit c, Set<Field> fs, DateFormatter df) throws IOException {
+ HttpServletRequest req, RevWalk walk, RevCommit c, Set<Field> fs, DateFormatter df)
+ throws IOException {
GitilesView view = ViewFilter.getView(req);
if (cdb == null) {
cdb = new CommitData.Builder();
}
- CommitData cd = cdb.setRevWalk(walk).setArchiveFormat(archiveFormat).build(req, c, fs);
+ CommitData cd = cdb.setArchiveFormat(archiveFormat).build(req, walk, c, fs);
Map<String, Object> data = Maps.newHashMapWithExpectedSize(fs.size());
if (cd.author != null) {
@@ -150,9 +145,9 @@
return data;
}
- Map<String, Object> toSoyData(HttpServletRequest req, RevCommit commit, DateFormatter df)
- throws IOException {
- return toSoyData(req, commit, DEFAULT_FIELDS, df);
+ Map<String, Object> toSoyData(
+ HttpServletRequest req, RevWalk walk, RevCommit commit, DateFormatter df) throws IOException {
+ return toSoyData(req, walk, commit, DEFAULT_FIELDS, df);
}
// TODO(dborowitz): Extract this.
diff --git a/java/com/google/gitiles/DiffServlet.java b/java/com/google/gitiles/DiffServlet.java
index 657edf8..d0e4409 100644
--- a/java/com/google/gitiles/DiffServlet.java
+++ b/java/com/google/gitiles/DiffServlet.java
@@ -96,7 +96,7 @@
new CommitSoyData()
.setLinkifier(linkifier)
.setArchiveFormat(getArchiveFormat(access))
- .toSoyData(req, walk.parseCommit(view.getRevision().getId()), fs, df));
+ .toSoyData(req, walk, walk.parseCommit(view.getRevision().getId()), fs, df));
}
if (!data.containsKey("repositoryName") && (view.getRepositoryName() != null)) {
data.put("repositoryName", view.getRepositoryName());
diff --git a/java/com/google/gitiles/LogServlet.java b/java/com/google/gitiles/LogServlet.java
index 5d06ae5..0dd00f3 100644
--- a/java/com/google/gitiles/LogServlet.java
+++ b/java/com/google/gitiles/LogServlet.java
@@ -168,8 +168,7 @@
List<CommitJsonData.Commit> entries = Lists.newArrayListWithCapacity(paginator.getLimit());
for (RevCommit c : paginator) {
paginator.getWalk().parseBody(c);
- entries.add(
- new CommitJsonData().setRevWalk(paginator.getWalk()).toJsonData(req, c, fs, df));
+ entries.add(new CommitJsonData().toJsonData(req, paginator.getWalk(), c, fs, df));
}
result.log = entries;
if (paginator.getPreviousStart() != null) {
diff --git a/java/com/google/gitiles/LogSoyData.java b/java/com/google/gitiles/LogSoyData.java
index 6a27782..8bc243f 100644
--- a/java/com/google/gitiles/LogSoyData.java
+++ b/java/com/google/gitiles/LogSoyData.java
@@ -124,7 +124,7 @@
csd = new CommitSoyData();
}
- Map<String, Object> entry = csd.setRevWalk(paginator.getWalk()).toSoyData(req, c, fields, df);
+ Map<String, Object> entry = csd.toSoyData(req, paginator.getWalk(), c, fields, df);
DiffEntry rename = paginator.getRename(c);
if (rename != null) {
entry.put("rename", toRenameSoyData(rename));
diff --git a/java/com/google/gitiles/RevisionServlet.java b/java/com/google/gitiles/RevisionServlet.java
index a126c7e..17ad143 100644
--- a/java/com/google/gitiles/RevisionServlet.java
+++ b/java/com/google/gitiles/RevisionServlet.java
@@ -96,9 +96,8 @@
"data",
new CommitSoyData()
.setLinkifier(linkifier)
- .setRevWalk(walk)
.setArchiveFormat(getArchiveFormat(access))
- .toSoyData(req, (RevCommit) obj, COMMIT_SOY_FIELDS, df)));
+ .toSoyData(req, walk, (RevCommit) obj, COMMIT_SOY_FIELDS, df)));
break;
case OBJ_TREE:
Map<String, Object> tree =
@@ -184,9 +183,7 @@
renderJson(
req,
res,
- new CommitJsonData()
- .setRevWalk(walk)
- .toJsonData(req, (RevCommit) obj, COMMIT_JSON_FIELDS, df),
+ new CommitJsonData().toJsonData(req, walk, (RevCommit) obj, COMMIT_JSON_FIELDS, df),
Commit.class);
break;
default: