Use RevWalk from CommitReceivedEvent
When cherry-picking changes through the UI, we can be referencing
a tree SHA that hasn't been flushed yet. Use the RevWalk handed
down by CommitReceivedEvent so that we can parse this tree SHA.
Change-Id: Id64599f6d29732996efeaf3c7c1c15b70d61fc39
diff --git a/BUILD b/BUILD
index 0c83ab9..a5fa04e 100644
--- a/BUILD
+++ b/BUILD
@@ -6,7 +6,7 @@
srcs = glob(["src/main/java/**/*.java"]),
manifest_entries = [
"Gerrit-PluginName: uploadvalidator",
- "Gerrit-ApiVersion: 2.14-SNAPSHOT",
+ "Gerrit-ApiVersion: 2.15-SNAPSHOT",
"Gerrit-Module: com.googlesource.gerrit.plugins.uploadvalidator.Module",
],
resources = glob(["src/main/resources/**/*"]),
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java
index bd5420e..c8fa506 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java
@@ -43,6 +43,7 @@
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import java.io.BufferedReader;
import java.io.IOException;
@@ -123,8 +124,12 @@
try (Repository repo =
repoManager.openRepository(receiveEvent.project.getNameKey())) {
List<CommitValidationMessage> messages =
- performValidation(repo, receiveEvent.commit,
- blockedKeywordPatterns.values(), cfg);
+ performValidation(
+ repo,
+ receiveEvent.commit,
+ receiveEvent.revWalk,
+ blockedKeywordPatterns.values(),
+ cfg);
if (!messages.isEmpty()) {
throw new CommitValidationException(
"includes files containing blocked keywords", messages);
@@ -139,13 +144,18 @@
}
@VisibleForTesting
- List<CommitValidationMessage> performValidation(Repository repo, RevCommit c,
- ImmutableCollection<Pattern> blockedKeywordPartterns, PluginConfig cfg)
- throws IOException, ExecutionException {
+ List<CommitValidationMessage> performValidation(
+ Repository repo,
+ RevCommit c,
+ RevWalk revWalk,
+ ImmutableCollection<Pattern> blockedKeywordPartterns,
+ PluginConfig cfg)
+ throws IOException, ExecutionException {
List<CommitValidationMessage> messages = new LinkedList<>();
checkCommitMessageForBlockedKeywords(blockedKeywordPartterns, messages,
c.getFullMessage());
- Map<String, ObjectId> content = CommitUtils.getChangedContent(repo, c);
+ Map<String, ObjectId> content = CommitUtils.getChangedContent(
+ repo, c, revWalk);
for (String path : content.keySet()) {
ObjectLoader ol = repo.open(content.get(path));
if (contentTypeUtil.isBinary(ol, path, cfg)) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/CommitUtils.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/CommitUtils.java
index 8b622ab..0da4894 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/CommitUtils.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/CommitUtils.java
@@ -27,6 +27,7 @@
import java.util.Map;
import java.util.Set;
+/** Utility class for checking whether commits are different. */
public class CommitUtils {
/**
@@ -39,9 +40,12 @@
* passed commit and its parents.
* @throws IOException
*/
- public static Set<String> getChangedPaths(Repository repo, RevCommit c)
+ public static Set<String> getChangedPaths(
+ Repository repo,
+ RevCommit c,
+ RevWalk revWalk)
throws IOException {
- Map<String, ObjectId> content = getChangedContent(repo, c);
+ Map<String, ObjectId> content = getChangedContent(repo, c, revWalk);
return content.keySet();
}
@@ -61,10 +65,10 @@
* @throws IOException
*/
public static Map<String, ObjectId> getChangedContent(Repository repo,
- RevCommit c) throws IOException {
+ RevCommit c, RevWalk revWalk) throws IOException {
final Map<String, ObjectId> content = new HashMap<>();
- visitChangedEntries(repo, c, new TreeWalkVisitor() {
+ visitChangedEntries(repo, c, revWalk, new TreeWalkVisitor() {
@Override
public void onVisit(TreeWalk tw) {
if (isFile(tw)) {
@@ -90,29 +94,21 @@
* @param visitor A TreeWalkVisitor with the desired action
* @throws IOException
*/
- public static void visitChangedEntries(Repository repo, RevCommit c,
+ public static void visitChangedEntries(
+ Repository repo,
+ RevCommit c,
+ RevWalk revWalk,
TreeWalkVisitor visitor) throws IOException {
- try (TreeWalk tw = new TreeWalk(repo)) {
+ try (TreeWalk tw = new TreeWalk(revWalk.getObjectReader())) {
tw.setRecursive(true);
tw.setFilter(TreeFilter.ANY_DIFF);
tw.addTree(c.getTree());
if (c.getParentCount() > 0) {
- @SuppressWarnings("resource")
- RevWalk rw = null;
- try {
- for (RevCommit p : c.getParents()) {
- if (p.getTree() == null) {
- if (rw == null) {
- rw = new RevWalk(repo);
- }
- rw.parseHeaders(p);
- }
- tw.addTree(p.getTree());
+ for (RevCommit p : c.getParents()) {
+ if (p.getTree() == null) {
+ revWalk.parseHeaders(p);
}
- } finally {
- if (rw != null) {
- rw.close();
- }
+ tw.addTree(p.getTree());
}
while (tw.next()) {
if (isDifferentToAllParents(c, tw)) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidator.java
index ed236e1..f792cdb 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidator.java
@@ -36,6 +36,7 @@
import org.eclipse.jgit.lib.ObjectStream;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.util.Collections;
@@ -121,7 +122,7 @@
try (Repository repo =
repoManager.openRepository(receiveEvent.project.getNameKey())) {
List<CommitValidationMessage> messages =
- performValidation(repo, receiveEvent.commit, getBlockedTypes(cfg),
+ performValidation(repo, receiveEvent.commit, receiveEvent.revWalk, getBlockedTypes(cfg),
isWhitelist(cfg));
if (!messages.isEmpty()) {
throw new CommitValidationException("contains blocked content type",
@@ -137,10 +138,10 @@
@VisibleForTesting
List<CommitValidationMessage> performValidation(Repository repo,
- RevCommit c, String[] blockedTypes, boolean whitelist)
+ RevCommit c, RevWalk revWalk, String[] blockedTypes, boolean whitelist)
throws IOException, ExecutionException {
List<CommitValidationMessage> messages = new LinkedList<>();
- Map<String, ObjectId> content = CommitUtils.getChangedContent(repo, c);
+ Map<String, ObjectId> content = CommitUtils.getChangedContent(repo, c, revWalk);
for (String path : content.keySet()) {
ObjectLoader ol = repo.open(content.get(path));
try (ObjectStream os = ol.openStream()) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidator.java
index 72ae551..2a80d38 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidator.java
@@ -38,6 +38,7 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk;
+import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.util.Arrays;
@@ -179,7 +180,7 @@
try (Repository repo =
repoManager.openRepository(receiveEvent.project.getNameKey())) {
List<CommitValidationMessage> messages =
- performValidation(repo, receiveEvent.commit);
+ performValidation(repo, receiveEvent.commit, receiveEvent.revWalk);
if (!messages.isEmpty()) {
throw new CommitValidationException("contains duplicate pathnames",
messages);
@@ -194,11 +195,11 @@
}
@VisibleForTesting
- List<CommitValidationMessage> performValidation(Repository repo, RevCommit c)
+ List<CommitValidationMessage> performValidation(Repository repo, RevCommit c, RevWalk revWalk)
throws IOException {
List<CommitValidationMessage> messages = new LinkedList<>();
- Set<String> pathnames = CommitUtils.getChangedPaths(repo, c);
+ Set<String> pathnames = CommitUtils.getChangedPaths(repo, c, revWalk);
checkForDuplicatesInSet(pathnames, messages);
if (!messages.isEmpty() || c.getParentCount() == 0) {
return messages;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidator.java
index 79cac3f..3c0df3d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidator.java
@@ -32,6 +32,7 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.util.ArrayList;
@@ -103,7 +104,7 @@
try (Repository repo =
repoManager.openRepository(receiveEvent.project.getNameKey())) {
List<CommitValidationMessage> messages = performValidation(repo,
- receiveEvent.commit, getBlockedExtensions(cfg));
+ receiveEvent.commit, receiveEvent.revWalk, getBlockedExtensions(cfg));
if (!messages.isEmpty()) {
throw new CommitValidationException(
"contains files with blocked file extensions", messages);
@@ -117,9 +118,9 @@
}
static List<CommitValidationMessage> performValidation(Repository repo,
- RevCommit c, List<String> blockedFileExtensions) throws IOException {
+ RevCommit c, RevWalk revWalk, List<String> blockedFileExtensions) throws IOException {
List<CommitValidationMessage> messages = new LinkedList<>();
- for (String file : CommitUtils.getChangedPaths(repo, c)) {
+ for (String file : CommitUtils.getChangedPaths(repo, c, revWalk)) {
for (String blockedExtension : blockedFileExtensions) {
if (file.toLowerCase().endsWith(blockedExtension.toLowerCase())) {
messages.add(new CommitValidationMessage("blocked file: " + file, true));
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidator.java
index e2fd516..d0906ff 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidator.java
@@ -32,6 +32,7 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.util.ArrayList;
@@ -96,7 +97,7 @@
try (Repository repo = repoManager.openRepository(
receiveEvent.project.getNameKey())) {
List<CommitValidationMessage> messages =
- performValidation(repo, receiveEvent.commit,
+ performValidation(repo, receiveEvent.commit, receiveEvent.revWalk,
cfg.getStringList(KEY_INVALID_FILENAME_PATTERN));
if (!messages.isEmpty()) {
throw new CommitValidationException(
@@ -112,13 +113,13 @@
}
static List<CommitValidationMessage> performValidation(Repository repo,
- RevCommit c, String[] patterns) throws IOException {
+ RevCommit c, RevWalk revWalk, String[] patterns) throws IOException {
List<Pattern> invalidFilenamePatterns = new ArrayList<>();
for (String s : patterns) {
invalidFilenamePatterns.add(Pattern.compile(s));
}
List<CommitValidationMessage> messages = new LinkedList<>();
- for (String file : CommitUtils.getChangedPaths(repo, c)) {
+ for (String file : CommitUtils.getChangedPaths(repo, c, revWalk)) {
for (Pattern p : invalidFilenamePatterns) {
if (p.matcher(file).find()) {
messages.add(new CommitValidationMessage(
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidator.java
index 559b745..e3c44f1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidator.java
@@ -35,6 +35,7 @@
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.io.InputStreamReader;
@@ -106,7 +107,7 @@
try (Repository repo =
repoManager.openRepository(receiveEvent.project.getNameKey())) {
List<CommitValidationMessage> messages =
- performValidation(repo, receiveEvent.commit, cfg);
+ performValidation(repo, receiveEvent.commit, receiveEvent.revWalk, cfg);
if (!messages.isEmpty()) {
throw new CommitValidationException(
"contains files with a Windows line ending", messages);
@@ -121,10 +122,10 @@
}
@VisibleForTesting
- List<CommitValidationMessage> performValidation(Repository repo, RevCommit c,
+ List<CommitValidationMessage> performValidation(Repository repo, RevCommit c, RevWalk revWalk,
PluginConfig cfg) throws IOException, ExecutionException {
List<CommitValidationMessage> messages = new LinkedList<>();
- Map<String, ObjectId> content = CommitUtils.getChangedContent(repo, c);
+ Map<String, ObjectId> content = CommitUtils.getChangedContent(repo, c, revWalk);
for (String path : content.keySet()) {
ObjectLoader ol = repo.open(content.get(path));
if (contentTypeUtil.isBinary(ol, path, cfg)) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidator.java
index 53dc2e3..29aeba8 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidator.java
@@ -31,6 +31,7 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.util.Collections;
@@ -92,7 +93,7 @@
try (Repository repo =
repoManager.openRepository(receiveEvent.project.getNameKey())) {
List<CommitValidationMessage> messages =
- performValidation(repo, receiveEvent.commit, maxPathLength);
+ performValidation(repo, receiveEvent.commit, receiveEvent.revWalk, maxPathLength);
if (!messages.isEmpty()) {
throw new CommitValidationException(
"contains files with too long paths (max path length: "
@@ -108,9 +109,9 @@
}
static List<CommitValidationMessage> performValidation(Repository repo,
- RevCommit c, int maxPathLength) throws IOException {
+ RevCommit c, RevWalk revWalk, int maxPathLength) throws IOException {
List<CommitValidationMessage> messages = new LinkedList<>();
- for (String file : CommitUtils.getChangedPaths(repo, c)) {
+ for (String file : CommitUtils.getChangedPaths(repo, c, revWalk)) {
if (file.length() > maxPathLength) {
messages.add(new CommitValidationMessage("path too long: " + file, true));
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidator.java
index 5dd9ee5..88c8f62 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidator.java
@@ -33,6 +33,7 @@
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import java.io.IOException;
@@ -93,7 +94,7 @@
try (Repository repo =
repoManager.openRepository(receiveEvent.project.getNameKey())) {
List<CommitValidationMessage> messages =
- performValidation(repo, receiveEvent.commit);
+ performValidation(repo, receiveEvent.commit, receiveEvent.revWalk);
if (!messages.isEmpty()) {
throw new CommitValidationException("contains submodules", messages);
}
@@ -116,10 +117,10 @@
}
static List<CommitValidationMessage> performValidation(Repository repo,
- RevCommit c) throws IOException {
+ RevCommit c, RevWalk revWalk) throws IOException {
final List<CommitValidationMessage> messages = new LinkedList<>();
- CommitUtils.visitChangedEntries(repo, c, new TreeWalkVisitor() {
+ CommitUtils.visitChangedEntries(repo, c, revWalk, new TreeWalkVisitor() {
@Override
public void onVisit(TreeWalk tw) {
if (isSubmodule(tw)) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidator.java
index 572600c..381cc7b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidator.java
@@ -33,6 +33,7 @@
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import java.io.IOException;
@@ -94,7 +95,7 @@
try (Repository repo =
repoManager.openRepository(receiveEvent.project.getNameKey())) {
List<CommitValidationMessage> messages =
- performValidation(repo, receiveEvent.commit);
+ performValidation(repo, receiveEvent.commit, receiveEvent.revWalk);
if (!messages.isEmpty()) {
throw new CommitValidationException("contains symbolic links",
messages);
@@ -120,10 +121,10 @@
}
static List<CommitValidationMessage> performValidation(Repository repo,
- RevCommit c) throws IOException {
+ RevCommit c, RevWalk revWalk) throws IOException {
final List<CommitValidationMessage> messages = new LinkedList<>();
- CommitUtils.visitChangedEntries(repo, c, new TreeWalkVisitor() {
+ CommitUtils.visitChangedEntries(repo, c, revWalk, new TreeWalkVisitor() {
@Override
public void onVisit(TreeWalk tw) {
if (isSymLink(tw)) {
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java
index c2a92e5..2ec5dd7 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java
@@ -25,6 +25,7 @@
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Test;
import java.io.File;
@@ -46,7 +47,7 @@
.build();
}
- private RevCommit makeCommit() throws IOException, GitAPIException {
+ private RevCommit makeCommit(RevWalk rw) throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
// invalid files
String content = "http://foo.bar.tld/?pw=myp4ssw0rdTefoobarstline2\n";
@@ -68,25 +69,27 @@
+ "Testline4";
files.put(new File(repo.getDirectory().getParent(), "foobar.txt"),
content.getBytes(StandardCharsets.UTF_8));
- return TestUtils.makeCommit(repo, "Commit foobar with test files.", files);
+ return TestUtils.makeCommit(rw, repo, "Commit foobar with test files.", files);
}
@Test
public void testKeywords() throws Exception {
- RevCommit c = makeCommit();
- BlockedKeywordValidator validator = new BlockedKeywordValidator(null,
- new ContentTypeUtil(PATTERN_CACHE), PATTERN_CACHE, null, null, null);
- List<CommitValidationMessage> m = validator.performValidation(
- repo, c, getPatterns().values(), EMPTY_PLUGIN_CONFIG);
- Set<String> expected = ImmutableSet.of(
- "ERROR: blocked keyword(s) found in: foo.txt (Line: 1)"
- + " (found: myp4ssw0rd, foobar)",
- "ERROR: blocked keyword(s) found in: bar.txt (Line: 5)"
- + " (found: $Id: foo bar$)",
- "ERROR: blocked keyword(s) found in: " + Patch.COMMIT_MSG
- + " (Line: 1) (found: foobar)");
- assertThat(TestUtils.transformMessages(m))
- .containsExactlyElementsIn(expected);
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommit(rw);
+ BlockedKeywordValidator validator = new BlockedKeywordValidator(null,
+ new ContentTypeUtil(PATTERN_CACHE), PATTERN_CACHE, null, null, null);
+ List<CommitValidationMessage> m = validator.performValidation(
+ repo, c, rw, getPatterns().values(), EMPTY_PLUGIN_CONFIG);
+ Set<String> expected = ImmutableSet.of(
+ "ERROR: blocked keyword(s) found in: foo.txt (Line: 1)"
+ + " (found: myp4ssw0rd, foobar)",
+ "ERROR: blocked keyword(s) found in: bar.txt (Line: 5)"
+ + " (found: $Id: foo bar$)",
+ "ERROR: blocked keyword(s) found in: " + Patch.COMMIT_MSG
+ + " (Line: 1) (found: foobar)");
+ assertThat(TestUtils.transformMessages(m))
+ .containsExactlyElementsIn(expected);
+ }
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidatorTest.java
index edad751..75f1b07 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidatorTest.java
@@ -22,6 +22,7 @@
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Before;
import org.junit.Test;
@@ -65,22 +66,26 @@
String[] patterns =
new String[] {"application/pdf", "application/xml", "text/html"};
- List<CommitValidationMessage> m = validator.performValidation(
- repo, makeCommit(), patterns, false);
- assertThat(TestUtils.transformMessages(m)).containsExactly(
- "ERROR: found blocked content type (application/pdf) in file: foo.pdf",
- "ERROR: found blocked content type (application/xml) in file: foo.xml",
- "ERROR: found blocked content type (text/html) in file: foo.html");
+ try (RevWalk rw = new RevWalk(repo)) {
+ List<CommitValidationMessage> m = validator.performValidation(
+ repo, makeCommit(rw), rw, patterns, false);
+ assertThat(TestUtils.transformMessages(m)).containsExactly(
+ "ERROR: found blocked content type (application/pdf) in file: foo.pdf",
+ "ERROR: found blocked content type (application/xml) in file: foo.xml",
+ "ERROR: found blocked content type (text/html) in file: foo.html");
+ }
}
@Test
public void testWhitelist() throws Exception {
String[] patterns = new String[] {"application/pdf", "application/xml"};
- List<CommitValidationMessage> m = validator.performValidation(
- repo, makeCommit(), patterns, true);
- assertThat(TestUtils.transformMessages(m)).containsExactly(
- "ERROR: found blocked content type (text/html) in file: foo.html");
+ try (RevWalk rw = new RevWalk(repo)) {
+ List<CommitValidationMessage> m = validator.performValidation(
+ repo, makeCommit(rw), rw, patterns, true);
+ assertThat(TestUtils.transformMessages(m)).containsExactly(
+ "ERROR: found blocked content type (text/html) in file: foo.html");
+ }
}
@Test
@@ -89,7 +94,7 @@
assertThat(ContentTypeValidator.isWhitelist(EMPTY_PLUGIN_CONFIG)).isFalse();
}
- private RevCommit makeCommit() throws IOException, GitAPIException {
+ private RevCommit makeCommit(RevWalk rw) throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
String content = "<?xml version=\"1.0\"?><a><b>c</b></a>";
@@ -101,6 +106,6 @@
content.getBytes(StandardCharsets.UTF_8));
files.put(TestUtils.createEmptyFile("foo.pdf", repo), TEST_PDF);
- return TestUtils.makeCommit(repo, "Commit with test files.", files);
+ return TestUtils.makeCommit(rw, repo, "Commit with test files.", files);
}
}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidatorTest.java
index 2461bf6..cc0cee3 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidatorTest.java
@@ -33,6 +33,7 @@
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.junit.Before;
import org.junit.Test;
@@ -57,10 +58,10 @@
private DuplicatePathnameValidator validator;
private void runCheck(List<String> existingTreePaths, Set<String> testPaths,
- List<CommitValidationMessage> messages, List<String> visitedPaths)
- throws Exception {
+ List<CommitValidationMessage> messages, List<String> visitedPaths)
+ throws Exception {
RevCommit c = makeCommit(
- createEmptyDirCacheEntries(existingTreePaths, testRepo), testRepo);
+ testRepo.getRevWalk(), createEmptyDirCacheEntries(existingTreePaths, testRepo), testRepo);
try (TreeWalk tw = new TreeWalk(repo)) {
tw.setRecursive(false);
tw.addTree(c.getTree());
@@ -136,44 +137,48 @@
filenames.add("A");
filenames.add("F1/ab");
filenames.add("f2/sF1/aB");
- RevCommit c =
- makeCommit(createEmptyDirCacheEntries(filenames, testRepo), testRepo);
- List<CommitValidationMessage> m = validator.performValidation(repo, c);
- assertThat(m).hasSize(4);
- // During checking inside of the commit it's unknown which file is checked
- // first, because of that, both capabilities must be checked.
- assertThat(transformMessages(m)).containsAnyOf(
- transformMessage(conflict("A", "a")),
- transformMessage(conflict("a", "A")));
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c =
+ makeCommit(rw, createEmptyDirCacheEntries(filenames, testRepo), testRepo);
+ List<CommitValidationMessage> m = validator.performValidation(repo, c, rw);
+ assertThat(m).hasSize(4);
+ // During checking inside of the commit it's unknown which file is checked
+ // first, because of that, both capabilities must be checked.
+ assertThat(transformMessages(m)).containsAnyOf(
+ transformMessage(conflict("A", "a")),
+ transformMessage(conflict("a", "A")));
- assertThat(transformMessages(m)).containsAnyOf(
- transformMessage(conflict("F1", "f1")),
- transformMessage(conflict("f1", "F1")));
+ assertThat(transformMessages(m)).containsAnyOf(
+ transformMessage(conflict("F1", "f1")),
+ transformMessage(conflict("f1", "F1")));
- assertThat(transformMessages(m)).containsAnyOf(
- transformMessage(conflict("F1/ab", "f1/ab")),
- transformMessage(conflict("f1/ab", "F1/ab")));
+ assertThat(transformMessages(m)).containsAnyOf(
+ transformMessage(conflict("F1/ab", "f1/ab")),
+ transformMessage(conflict("f1/ab", "F1/ab")));
- assertThat(transformMessages(m)).containsAnyOf(
- transformMessage(
- conflict("f2/sF1/aB", "f2/sF1/ab")),
- transformMessage(
- conflict("f2/sF1/ab", "f2/sF1/aB")));
+ assertThat(transformMessages(m)).containsAnyOf(
+ transformMessage(
+ conflict("f2/sF1/aB", "f2/sF1/ab")),
+ transformMessage(
+ conflict("f2/sF1/ab", "f2/sF1/aB")));
+ }
}
@Test
public void testCheckRenaming() throws Exception {
- RevCommit c = makeCommit(
- createEmptyDirCacheEntries(INITIAL_PATHNAMES, testRepo), testRepo);
- DirCacheEntry[] entries = new DirCacheEntry[INITIAL_PATHNAMES.size()];
- for (int x = 0; x < INITIAL_PATHNAMES.size(); x++) {
- // Rename files
- entries[x] = createDirCacheEntry(INITIAL_PATHNAMES.get(x).toUpperCase(),
- EMPTY_CONTENT, testRepo);
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommit(
+ rw, createEmptyDirCacheEntries(INITIAL_PATHNAMES, testRepo), testRepo);
+ DirCacheEntry[] entries = new DirCacheEntry[INITIAL_PATHNAMES.size()];
+ for (int x = 0; x < INITIAL_PATHNAMES.size(); x++) {
+ // Rename files
+ entries[x] = createDirCacheEntry(INITIAL_PATHNAMES.get(x).toUpperCase(),
+ EMPTY_CONTENT, testRepo);
+ }
+ RevCommit c1 = makeCommit(rw, entries, testRepo, c);
+ List<CommitValidationMessage> m = validator.performValidation(repo, c1, rw);
+ assertThat(m).isEmpty();
}
- RevCommit c1 = makeCommit(entries, testRepo, c);
- List<CommitValidationMessage> m = validator.performValidation(repo, c1);
- assertThat(m).isEmpty();
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidatorTest.java
index 0b8af8a..5ee046c 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidatorTest.java
@@ -23,6 +23,7 @@
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Test;
import java.io.File;
@@ -38,7 +39,7 @@
private static final List<String> BLOCKED_EXTENSIONS_UC =
Lists.newArrayList("JPEG", "PDF", "ZIP", "EXE", "TAR.GZ");
- private RevCommit makeCommit(List<String> blockedExtensions)
+ private RevCommit makeCommit(RevWalk rw, List<String> blockedExtensions)
throws NoFilepatternException, IOException, GitAPIException {
Set<File> files = new HashSet<>();
for (String extension : blockedExtensions) {
@@ -49,33 +50,37 @@
files.add(new File(repo.getDirectory().getParent(), "foo.core.tmp"));
files.add(new File(repo.getDirectory().getParent(), "foo.c"));
files.add(new File(repo.getDirectory().getParent(), "foo.txt"));
- return TestUtils.makeCommit(repo, "Commit with empty test files.", files);
+ return TestUtils.makeCommit(rw, repo, "Commit with empty test files.", files);
}
@Test
public void testBlockedExtensions() throws Exception {
- RevCommit c = makeCommit(BLOCKED_EXTENSIONS_LC);
- List<CommitValidationMessage> m = FileExtensionValidator
- .performValidation(repo, c, BLOCKED_EXTENSIONS_LC);
- List<String> expected = new ArrayList<>();
- for (String extension : BLOCKED_EXTENSIONS_LC) {
- expected.add("ERROR: blocked file: foo." + extension);
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommit(rw, BLOCKED_EXTENSIONS_LC);
+ List<CommitValidationMessage> m = FileExtensionValidator
+ .performValidation(repo, c, rw, BLOCKED_EXTENSIONS_LC);
+ List<String> expected = new ArrayList<>();
+ for (String extension : BLOCKED_EXTENSIONS_LC) {
+ expected.add("ERROR: blocked file: foo." + extension);
+ }
+ assertThat(TestUtils.transformMessages(m))
+ .containsExactlyElementsIn(expected);
}
- assertThat(TestUtils.transformMessages(m))
- .containsExactlyElementsIn(expected);
}
@Test
public void testBlockedExtensionsCaseInsensitive() throws Exception {
- RevCommit c = makeCommit(BLOCKED_EXTENSIONS_UC);
- List<CommitValidationMessage> m = FileExtensionValidator
- .performValidation(repo, c, BLOCKED_EXTENSIONS_LC);
- List<String> expected = new ArrayList<>();
- for (String extension : BLOCKED_EXTENSIONS_UC) {
- expected.add("ERROR: blocked file: foo." + extension);
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommit(rw, BLOCKED_EXTENSIONS_UC);
+ List<CommitValidationMessage> m = FileExtensionValidator
+ .performValidation(repo, c, rw, BLOCKED_EXTENSIONS_LC);
+ List<String> expected = new ArrayList<>();
+ for (String extension : BLOCKED_EXTENSIONS_UC) {
+ expected.add("ERROR: blocked file: foo." + extension);
+ }
+ assertThat(TestUtils.transformMessages(m))
+ .containsExactlyElementsIn(expected);
}
- assertThat(TestUtils.transformMessages(m))
- .containsExactlyElementsIn(expected);
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidatorTest.java
index a620824..6b5191a 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidatorTest.java
@@ -21,6 +21,7 @@
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Test;
import java.io.File;
@@ -42,28 +43,30 @@
return filenames;
}
- private RevCommit makeCommit() throws IOException, GitAPIException {
+ private RevCommit makeCommit(RevWalk rw) throws IOException, GitAPIException {
Set<File> files = new HashSet<>();
for (String filenames : getInvalidFilenames()) {
files.add(new File(repo.getDirectory().getParent(), filenames));
}
// valid filename
files.add(new File(repo.getDirectory().getParent(), "test"));
- return TestUtils.makeCommit(repo, "Commit with empty test files.", files);
+ return TestUtils.makeCommit(rw, repo, "Commit with empty test files.", files);
}
@Test
public void test() throws Exception {
String[] invalidFilenamePattern = {"\\[|\\]|\\*|#", "[%:@]"};
- RevCommit c = makeCommit();
- List<CommitValidationMessage> m = InvalidFilenameValidator
- .performValidation(repo, c, invalidFilenamePattern);
- Set<String> expected = new HashSet<>();
- for (String filenames : getInvalidFilenames()) {
- expected.add("ERROR: invalid characters found in filename: " + filenames);
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommit(rw);
+ List<CommitValidationMessage> m = InvalidFilenameValidator
+ .performValidation(repo, c, rw, invalidFilenamePattern);
+ Set<String> expected = new HashSet<>();
+ for (String filenames : getInvalidFilenames()) {
+ expected.add("ERROR: invalid characters found in filename: " + filenames);
+ }
+ assertThat(TestUtils.transformMessages(m))
+ .containsExactlyElementsIn(expected);
}
- assertThat(TestUtils.transformMessages(m))
- .containsExactlyElementsIn(expected);
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidatorTest.java
index 9c3505e..664bcb8 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidatorTest.java
@@ -22,6 +22,7 @@
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Test;
import java.io.File;
@@ -32,7 +33,7 @@
import java.util.Map;
public class InvalidLineEndingValidatorTest extends ValidatorTestCase {
- private RevCommit makeCommit() throws IOException, GitAPIException {
+ private RevCommit makeCommit(RevWalk rw) throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
// invalid line endings
String content = "Testline1\r\n"
@@ -49,18 +50,20 @@
+ "Testline4";
files.put(new File(repo.getDirectory().getParent(), "bar.txt"),
content.getBytes(StandardCharsets.UTF_8));
- return TestUtils.makeCommit(repo, "Commit with test files.", files);
+ return TestUtils.makeCommit(rw, repo, "Commit with test files.", files);
}
@Test
public void testCarriageReturn() throws Exception {
- RevCommit c = makeCommit();
- InvalidLineEndingValidator validator = new InvalidLineEndingValidator(null,
- new ContentTypeUtil(PATTERN_CACHE), null, null, null);
- List<CommitValidationMessage> m = validator.performValidation(repo, c,
- EMPTY_PLUGIN_CONFIG);
- assertThat(TestUtils.transformMessages(m)).containsExactly(
- "ERROR: found carriage return (CR) character in file: foo.txt");
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommit(rw);
+ InvalidLineEndingValidator validator = new InvalidLineEndingValidator(null,
+ new ContentTypeUtil(PATTERN_CACHE), null, null, null);
+ List<CommitValidationMessage> m = validator.performValidation(repo, c,
+ rw, EMPTY_PLUGIN_CONFIG);
+ assertThat(TestUtils.transformMessages(m)).containsExactly(
+ "ERROR: found carriage return (CR) character in file: foo.txt");
+ }
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidatorTest.java
index 4ce9dba..16f73d6 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidatorTest.java
@@ -23,6 +23,7 @@
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Test;
import java.io.File;
@@ -39,36 +40,42 @@
return (TOO_LONG.length() + GOOD.length()) / 2;
}
- private RevCommit makeCommit() throws IOException, GitAPIException {
+ private RevCommit makeCommit(RevWalk rw) throws IOException, GitAPIException {
Set<File> files = new HashSet<>();
files.add(TestUtils.createEmptyFile(TOO_LONG, repo));
files.add(TestUtils.createEmptyFile(GOOD, repo));
- return TestUtils.makeCommit(repo, "Commit with empty test files.", files);
+ return TestUtils.makeCommit(rw, repo, "Commit with empty test files.", files);
}
@Test
public void testAddTooLongPath() throws Exception {
- RevCommit c = makeCommit();
- List<CommitValidationMessage> m =
- MaxPathLengthValidator.performValidation(repo, c, getMaxPathLength());
- Set<String> expected =
- ImmutableSet.of("ERROR: path too long: " + TOO_LONG);
- assertThat(TestUtils.transformMessages(m))
- .containsExactlyElementsIn(expected);
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommit(rw);
+ List<CommitValidationMessage> m =
+ MaxPathLengthValidator.performValidation(repo, c, rw,
+ getMaxPathLength());
+ Set<String> expected =
+ ImmutableSet.of("ERROR: path too long: " + TOO_LONG);
+ assertThat(TestUtils.transformMessages(m))
+ .containsExactlyElementsIn(expected);
+ }
}
@Test
public void testDeleteTooLongPath() throws Exception {
- RevCommit c = makeCommit();
- try(Git git = new Git(repo)) {
- Set<File> files = new HashSet<>();
- files.add(TestUtils.createEmptyFile(TOO_LONG, repo));
- TestUtils.removeFiles(git, files);
- c = git.commit().setMessage("Delete file which is too long").call();
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommit(rw);
+ try(Git git = new Git(repo)) {
+ Set<File> files = new HashSet<>();
+ files.add(TestUtils.createEmptyFile(TOO_LONG, repo));
+ TestUtils.removeFiles(git, files);
+ c = git.commit().setMessage("Delete file which is too long").call();
+ rw.parseCommit(c);
+ }
+ List<CommitValidationMessage> m = MaxPathLengthValidator
+ .performValidation(repo, c, rw, getMaxPathLength());
+ assertThat(m).isEmpty();
}
- List<CommitValidationMessage> m = MaxPathLengthValidator
- .performValidation(repo, c, getMaxPathLength());
- assertThat(m).isEmpty();
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidatorTest.java
index d24b3c2..4f867a7 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidatorTest.java
@@ -23,6 +23,7 @@
import org.eclipse.jgit.api.SubmoduleAddCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Test;
import java.io.File;
@@ -32,7 +33,7 @@
import java.util.Map;
public class SubmoduleValidatorTest extends ValidatorTestCase {
- private RevCommit makeCommitWithSubmodule()
+ private RevCommit makeCommitWithSubmodule(RevWalk rw)
throws IOException, GitAPIException {
try (Git git = new Git(repo)) {
SubmoduleAddCommand addCommand = git.submoduleAdd();
@@ -40,32 +41,36 @@
addCommand.setPath("modules/library");
addCommand.call().close();
git.add().addFilepattern(".").call();
- return git.commit().setMessage("Commit with submodule.").call();
+ return rw.parseCommit(git.commit().setMessage("Commit with submodule.").call());
}
}
@Test
public void testWithSubmodule() throws Exception {
- RevCommit c = makeCommitWithSubmodule();
- List<CommitValidationMessage> m =
- SubmoduleValidator.performValidation(repo, c);
- assertThat(TestUtils.transformMessages(m))
- .containsExactly("ERROR: submodules are not allowed: modules/library");
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommitWithSubmodule(rw);
+ List<CommitValidationMessage> m =
+ SubmoduleValidator.performValidation(repo, c, rw);
+ assertThat(TestUtils.transformMessages(m))
+ .containsExactly("ERROR: submodules are not allowed: modules/library");
+ }
}
- private RevCommit makeCommitWithoutSubmodule()
+ private RevCommit makeCommitWithoutSubmodule(RevWalk rw)
throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
files.put(new File(repo.getDirectory().getParent(), "foo.txt"), null);
- return TestUtils.makeCommit(repo, "Commit with empty test files.", files);
+ return TestUtils.makeCommit(rw, repo, "Commit with empty test files.", files);
}
@Test
public void testWithoutSubmodule() throws Exception {
- RevCommit c = makeCommitWithoutSubmodule();
- List<CommitValidationMessage> m =
- SubmoduleValidator.performValidation(repo, c);
- assertThat(m).isEmpty();
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommitWithoutSubmodule(rw);
+ List<CommitValidationMessage> m =
+ SubmoduleValidator.performValidation(repo, c, rw);
+ assertThat(m).isEmpty();
+ }
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidatorTest.java
index a4c2127..2e17c29 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidatorTest.java
@@ -22,6 +22,7 @@
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Test;
import java.io.File;
@@ -34,7 +35,7 @@
import java.util.Set;
public class SymlinkValidatorTest extends ValidatorTestCase {
- private RevCommit makeCommitWithSymlink()
+ private RevCommit makeCommitWithSymlink(RevWalk rw)
throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
File link = new File(repo.getDirectory().getParent(), "foo.txt");
@@ -45,34 +46,38 @@
Files.createSymbolicLink(link.toPath(), Paths.get("folder"));
files.put(link, null);
- return TestUtils.makeCommit(repo, "Commit with symlink.", files);
+ return TestUtils.makeCommit(rw, repo, "Commit with symlink.", files);
}
@Test
public void testWithSymlink() throws Exception {
- RevCommit c = makeCommitWithSymlink();
- List<CommitValidationMessage> m =
- SymlinkValidator.performValidation(repo, c);
- Set<String> expected = ImmutableSet.of(
- "ERROR: Symbolic links are not allowed: foo.txt",
- "ERROR: Symbolic links are not allowed: symbolicFolder");
- assertThat(TestUtils.transformMessages(m))
- .containsExactlyElementsIn(expected);
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommitWithSymlink(rw);
+ List<CommitValidationMessage> m =
+ SymlinkValidator.performValidation(repo, rw.parseCommit(c), rw);
+ Set<String> expected = ImmutableSet.of(
+ "ERROR: Symbolic links are not allowed: foo.txt",
+ "ERROR: Symbolic links are not allowed: symbolicFolder");
+ assertThat(TestUtils.transformMessages(m))
+ .containsExactlyElementsIn(expected);
+ }
}
- private RevCommit makeCommitWithoutSymlink()
+ private RevCommit makeCommitWithoutSymlink(RevWalk rw)
throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
files.put(new File(repo.getDirectory().getParent(), "foo.txt"), null);
- return TestUtils.makeCommit(repo, "Commit with empty test files.", files);
+ return TestUtils.makeCommit(rw, repo, "Commit with empty test files.", files);
}
@Test
public void testWithoutSymlink() throws Exception {
- RevCommit c = makeCommitWithoutSymlink();
- List<CommitValidationMessage> m =
- SymlinkValidator.performValidation(repo, c);
- assertThat(m).isEmpty();
+ try (RevWalk rw = new RevWalk(repo)) {
+ RevCommit c = makeCommitWithoutSymlink(rw);
+ List<CommitValidationMessage> m =
+ SymlinkValidator.performValidation(repo, rw.parseCommit(c), rw);
+ assertThat(m).isEmpty();
+ }
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/TestUtils.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/TestUtils.java
index ab17832..21d4f31 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/TestUtils.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/TestUtils.java
@@ -33,6 +33,7 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
+import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import java.io.File;
@@ -69,22 +70,22 @@
return repository;
}
- public static RevCommit makeCommit(Repository repo, String message,
+ public static RevCommit makeCommit(RevWalk rw, Repository repo, String message,
Set<File> files) throws IOException, GitAPIException {
Map<File, byte[]> tmp = new HashMap<>();
for (File f : files) {
tmp.put(f, null);
}
- return makeCommit(repo, message, tmp);
+ return makeCommit(rw, repo, message, tmp);
}
- public static RevCommit makeCommit(Repository repo, String message,
+ public static RevCommit makeCommit(RevWalk rw, Repository repo, String message,
Map<File, byte[]> files) throws IOException, GitAPIException {
try (Git git = new Git(repo)) {
if (files != null) {
addFiles(git, files);
}
- return git.commit().setMessage(message).call();
+ return rw.parseCommit(git.commit().setMessage(message).call());
}
}
@@ -148,18 +149,18 @@
return repo.file(pathname, repo.blob(content));
}
- public static RevCommit makeCommit(DirCacheEntry[] entries,
+ public static RevCommit makeCommit(RevWalk rw, DirCacheEntry[] entries,
TestRepository<Repository> repo) throws Exception {
- return makeCommit(entries, repo, (RevCommit[]) null);
+ return makeCommit(rw, entries, repo, (RevCommit[]) null);
}
- public static RevCommit makeCommit(DirCacheEntry[] entries,
+ public static RevCommit makeCommit(RevWalk rw, DirCacheEntry[] entries,
TestRepository<Repository> repo, RevCommit... parents)
throws Exception {
final RevTree ta = repo.tree(entries);
RevCommit c =
(parents == null) ? repo.commit(ta) : repo.commit(ta, parents);
repo.parseBody(c);
- return c;
+ return rw.parseCommit(c);
}
}