Use auto-closeable RevWalk
Change-Id: I36beaacb0dccdf5980e2e46431a049d3e6fdb836
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
index 88a32fd..d6a7213 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
@@ -114,8 +114,7 @@
projectControlFactory.controlFor(project.getNameKey()).controlForRef(
destinationBranch);
- try {
- RevWalk revWalk = new RevWalk(git);
+ try (RevWalk revWalk = new RevWalk(git)) {
try {
Ref destRef = git.getRef(destinationBranch);
if (destRef == null) {
@@ -181,7 +180,6 @@
doValidation));
}
} finally {
- revWalk.release();
if (newChange == null) {
db.rollback();
}
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
index b13cc13..174de06 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
@@ -173,34 +173,35 @@
ObjectId baseObjectId = ObjectId.fromString(pr.getBase().getSha());
ObjectId prHeadObjectId = ObjectId.fromString(pr.getHead().getSha());
- RevWalk walk = new RevWalk(gitRepo);
- walk.markUninteresting(walk.lookupCommit(baseObjectId));
- walk.markStart(walk.lookupCommit(prHeadObjectId));
- walk.sort(RevSort.REVERSE);
-
- int patchNr = 1;
- for (GHPullRequestCommitDetail ghCommitDetail : pr.listCommits()) {
- status.update(Code.SYNC, "Patch #" + patchNr, "Patch#" + patchNr
- + ": Inserting PullRequest into Gerrit");
- RevCommit revCommit =
- walk.parseCommit(ObjectId.fromString(ghCommitDetail.getSha()));
-
- GHUser prUser = pr.getUser();
- GitUser commitAuthor = ghCommitDetail.getCommit().getAuthor();
- GitHubUser gitHubUser = GitHubUser.from(prUser, commitAuthor);
-
- Account.Id pullRequestOwner = getOrRegisterAccount(db, gitHubUser);
- Id changeId =
- createChange.addCommitToChange(db, project, gitRepo,
- destinationBranch, pullRequestOwner, revCommit,
- getChangeMessage(pr),
- String.format(TOPIC_FORMAT, pr.getNumber()), false);
- if (changeId != null) {
- prChanges.add(changeId);
+ try (RevWalk walk = new RevWalk(gitRepo)) {
+ walk.markUninteresting(walk.lookupCommit(baseObjectId));
+ walk.markStart(walk.lookupCommit(prHeadObjectId));
+ walk.sort(RevSort.REVERSE);
+
+ int patchNr = 1;
+ for (GHPullRequestCommitDetail ghCommitDetail : pr.listCommits()) {
+ status.update(Code.SYNC, "Patch #" + patchNr, "Patch#" + patchNr
+ + ": Inserting PullRequest into Gerrit");
+ RevCommit revCommit =
+ walk.parseCommit(ObjectId.fromString(ghCommitDetail.getSha()));
+
+ GHUser prUser = pr.getUser();
+ GitUser commitAuthor = ghCommitDetail.getCommit().getAuthor();
+ GitHubUser gitHubUser = GitHubUser.from(prUser, commitAuthor);
+
+ Account.Id pullRequestOwner = getOrRegisterAccount(db, gitHubUser);
+ Id changeId =
+ createChange.addCommitToChange(db, project, gitRepo,
+ destinationBranch, pullRequestOwner, revCommit,
+ getChangeMessage(pr),
+ String.format(TOPIC_FORMAT, pr.getNumber()), false);
+ if (changeId != null) {
+ prChanges.add(changeId);
+ }
}
+
+ return prChanges;
}
-
- return prChanges;
}
private com.google.gerrit.reviewdb.client.Account.Id getOrRegisterAccount(
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
index c661242..8449edb 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
@@ -205,30 +205,31 @@
Repository gitRepo, GHPullRequest ghPullRequest)
throws IncorrectObjectTypeException, IOException {
boolean pullRequestToImport = false;
- RevWalk gitWalk = new RevWalk(gitRepo);
- for (GHPullRequestCommitDetail pullRequestCommit : ghPullRequest
- .listCommits()) {
- ObjectId pullRequestHeadObjectId =
- ObjectId.fromString(pullRequestCommit.getSha());
-
- try {
- gitWalk.parseCommit(pullRequestHeadObjectId);
-
- ResultSet<PatchSet> patchSets;
+ try (RevWalk gitWalk = new RevWalk(gitRepo)) {
+ for (GHPullRequestCommitDetail pullRequestCommit : ghPullRequest
+ .listCommits()) {
+ ObjectId pullRequestHeadObjectId =
+ ObjectId.fromString(pullRequestCommit.getSha());
+
try {
- patchSets =
- db.patchSets().byRevision(new RevId(pullRequestCommit.getSha()));
- } catch (OrmException e) {
- LOG.error("Error whilst fetching patch-sets from DB associated to commit "
- + pullRequestCommit.getSha());
- return false;
+ gitWalk.parseCommit(pullRequestHeadObjectId);
+
+ ResultSet<PatchSet> patchSets;
+ try {
+ patchSets =
+ db.patchSets().byRevision(new RevId(pullRequestCommit.getSha()));
+ } catch (OrmException e) {
+ LOG.error("Error whilst fetching patch-sets from DB associated to commit "
+ + pullRequestCommit.getSha());
+ return false;
+ }
+ pullRequestToImport = !patchSets.iterator().hasNext();
+ patchSets.close();
+ } catch (MissingObjectException e) {
+ pullRequestToImport = true;
}
- pullRequestToImport = !patchSets.iterator().hasNext();
- patchSets.close();
- } catch (MissingObjectException e) {
- pullRequestToImport = true;
}
+ return pullRequestToImport;
}
- return pullRequestToImport;
}
}