Merge branch stable-2.11

* stable-2.11:
  Adapt to the JGit v4.5.x

Change-Id: I9bca8da402097e8f54e477113299abb96c18c773
diff --git a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java
index 6c2f16d..72c9ec9 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java
@@ -92,16 +92,16 @@
   @Inject
   CreateReviewNotes(@GerritPersonIdent final PersonIdent gerritIdent,
       final AccountCache accountCache,
-      final @AnonymousCowardName String anonymousCowardName,
+      @AnonymousCowardName final String anonymousCowardName,
       final ProjectCache projectCache,
       final ApprovalsUtil approvalsUtil,
       final ChangeControl.GenericFactory changeControlFactory,
       final IdentifiedUser.GenericFactory userFactory,
       final NotesBranchUtil.Factory notesBranchUtilFactory,
-      final @Nullable @CanonicalWebUrl String canonicalWebUrl,
-      final @Assisted ReviewDb reviewDb,
-      final @Assisted Project.NameKey project,
-      final @Assisted Repository git) {
+      @Nullable @CanonicalWebUrl final String canonicalWebUrl,
+      @Assisted final ReviewDb reviewDb,
+      @Assisted final Project.NameKey project,
+      @Assisted final Repository git) {
     this.gerritServerIdent = gerritIdent;
     this.accountCache = accountCache;
     this.anonymousCowardName = anonymousCowardName;
@@ -129,25 +129,24 @@
       return;
     }
 
-    RevWalk rw = new RevWalk(git);
-    try {
-      RevCommit n = rw.parseCommit(newObjectId);
-      rw.markStart(n);
-      if (n.getParentCount() == 1 && n.getParent(0).equals(oldObjectId)) {
-        rw.markUninteresting(rw.parseCommit(oldObjectId));
-      } else {
-        markUninteresting(git, branch, rw, oldObjectId);
+    try (RevWalk rw = new RevWalk(git)) {
+      try {
+        RevCommit n = rw.parseCommit(newObjectId);
+        rw.markStart(n);
+        if (n.getParentCount() == 1 && n.getParent(0).equals(oldObjectId)) {
+          rw.markUninteresting(rw.parseCommit(oldObjectId));
+        } else {
+          markUninteresting(git, branch, rw, oldObjectId);
+        }
+      } catch (Exception e) {
+        log.error(e.getMessage(), e);
+        return;
       }
-    } catch (Exception e) {
-      log.error(e.getMessage(), e);
-      return;
-    }
 
-    if (monitor == null) {
-      monitor = NullProgressMonitor.INSTANCE;
-    }
+      if (monitor == null) {
+        monitor = NullProgressMonitor.INSTANCE;
+      }
 
-    try {
       for (RevCommit c : rw) {
         ObjectId content = createNoteContent(loadPatchSet(c, branch));
         if (content != null) {
@@ -156,8 +155,6 @@
           getMessage().append("* ").append(c.getShortMessage()).append("\n");
         }
       }
-    } finally {
-      rw.close();
     }
   }
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/ExportReviewNotes.java b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/ExportReviewNotes.java
index 379f923..2b1968c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/ExportReviewNotes.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/ExportReviewNotes.java
@@ -76,17 +76,11 @@
   }
 
   private List<Change> allChanges() {
-    ReviewDb db = null;
-    try {
-      db = database.open();
+    try (ReviewDb db = database.open()){
       return db.changes().all().toList();
     } catch (OrmException e) {
-      stderr.print("Cannot read changes from database " + e.getMessage());
+      stderr.println("Cannot read changes from database " + e.getMessage());
       return Collections.emptyList();
-    } finally {
-      if (db != null) {
-        db.close();
-      }
     }
   }
 
@@ -109,20 +103,14 @@
 
   private void export(ReviewDb db, Project.NameKey project, List<Change> changes)
       throws IOException, OrmException {
-    final Repository git;
-    try {
-      git = gitManager.openRepository(project);
-    } catch (RepositoryNotFoundException e) {
-      return;
-    }
-    try {
+    try (Repository git = gitManager.openRepository(project)) {
       CreateReviewNotes crn = reviewNotesFactory.create(db, project, git);
       crn.createNotes(changes, monitor);
       crn.commitNotes();
+    } catch (RepositoryNotFoundException e) {
+      stderr.println("Unable to open project: " + project.get());
     } catch (ConcurrentRefUpdateException e) {
-      stderr.print(e.getMessage());
-    } finally {
-      git.close();
+      stderr.println(e.getMessage());
     }
   }
 
@@ -156,29 +144,23 @@
   private class Worker extends Thread {
     @Override
     public void run() {
-      ReviewDb db;
-      try {
-        db = database.open();
-      } catch (OrmException e) {
-        stderr.print(e.getMessage());
-        return;
-      }
-      try {
+      try (ReviewDb db = database.open()){
         for (;;) {
           Entry<Project.NameKey, List<Change>> next = next();
           if (next != null) {
             try {
               export(db, next.getKey(), next.getValue());
             } catch (OrmException | IOException e) {
-              stderr.print(e.getMessage());
+              stderr.println(e.getMessage());
             }
           } else {
             break;
           }
         }
+      } catch (OrmException e) {
+        stderr.println(e.getMessage());
       } finally {
         monitor.endWorker();
-        db.close();
       }
     }
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/RefUpdateListener.java b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/RefUpdateListener.java
index c839c31..d798648 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/RefUpdateListener.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/RefUpdateListener.java
@@ -26,7 +26,6 @@
 import com.google.inject.Inject;
 
 import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
-import org.eclipse.jgit.errors.RepositoryNotFoundException;
 import org.eclipse.jgit.lib.Config;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Repository;
@@ -95,50 +94,19 @@
 
   private void createReviewNotes(Event e) {
     Project.NameKey projectName = new Project.NameKey(e.getProjectName());
-    Repository git;
-    try {
-      git = repoManager.openRepository(projectName);
-    } catch (RepositoryNotFoundException x) {
-      log.error(x.getMessage(), x);
-      return;
-    } catch (IOException x) {
-      log.error(x.getMessage(), x);
-      return;
-    }
-
-    ReviewDb reviewDb;
-    try {
-
-      try {
-        reviewDb = schema.open();
-      } catch (OrmException x) {
-        log.error(x.getMessage(), x);
-        return;
+    try (Repository git = repoManager.openRepository(projectName);
+        ReviewDb reviewDb = schema.open()) {
+      CreateReviewNotes crn = reviewNotesFactory.create(
+          reviewDb, projectName, git);
+      if (e.getRefName().startsWith("refs/heads/")) {
+        crn.createNotes(e.getRefName(),
+            ObjectId.fromString(e.getOldObjectId()),
+            ObjectId.fromString(e.getNewObjectId()),
+            null);
+        crn.commitNotes();
       }
-
-      try {
-        CreateReviewNotes crn = reviewNotesFactory.create(
-            reviewDb, projectName, git);
-        if (e.getRefName().startsWith("refs/heads/")) {
-          crn.createNotes(e.getRefName(),
-              ObjectId.fromString(e.getOldObjectId()),
-              ObjectId.fromString(e.getNewObjectId()),
-              null);
-          crn.commitNotes();
-        }
-      } catch (OrmException x) {
-        log.error(x.getMessage(), x);
-      } catch (IOException x) {
-        log.error(x.getMessage(), x);
-      } catch (ConcurrentRefUpdateException x) {
-        log.error(x.getMessage(), x);
-      } finally {
-        reviewDb.close();
-      }
-
-    } finally {
-      git.close();
+    } catch (OrmException | IOException | ConcurrentRefUpdateException x) {
+      log.error(x.getMessage(), x);
     }
-
   }
 }
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index ded934b..4e76dea 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -1,3 +1,2 @@
 Stores review information for Gerrit changes in the refs/notes/review
 branch.
-
diff --git a/src/main/resources/Documentation/refs-notes-review.md b/src/main/resources/Documentation/refs-notes-review.md
index a504025..20fd3c7 100644
--- a/src/main/resources/Documentation/refs-notes-review.md
+++ b/src/main/resources/Documentation/refs-notes-review.md
@@ -14,8 +14,7 @@
   $ git fetch origin refs/notes/review:refs/notes/review
 ```
 
-It is also possible to [configure git][1] to always to always fetch
-`refs/notes/review`:
+It is also possible to [configure git][1] to always fetch `refs/notes/review`:
 
 ```
   $ git config --add remote.origin.fetch refs/notes/review:refs/notes/review