Open Repository in try-with-resource
Change-Id: I40009fb8c6886c1338c14d49bca2ac5987bc369b
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java b/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
index 49edc41..60810b8 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
@@ -235,27 +235,23 @@
e = pending.get(uri);
}
if (e == null) {
- Repository git;
- try {
- git = gitManager.openRepository(project);
- } catch (IOException err) {
- stateLog.error(String.format(
- "source project %s not available", project), err, state);
- return;
- }
- try {
- Ref head = git.getRef(Constants.HEAD);
- if (head != null
- && head.isSymbolic()
- && RefNames.REFS_CONFIG.equals(head.getLeaf().getName())) {
+ try (Repository git = gitManager.openRepository(project)) {
+ try {
+ Ref head = git.getRef(Constants.HEAD);
+ if (head != null
+ && head.isSymbolic()
+ && RefNames.REFS_CONFIG.equals(head.getLeaf().getName())) {
+ return;
+ }
+ } catch (IOException err) {
+ stateLog.error(String.format(
+ "cannot check type of project %s", project), err, state);
return;
}
} catch (IOException err) {
stateLog.error(String.format(
- "cannot check type of project %s", project), err, state);
+ "source project %s not available", project), err, state);
return;
- } finally {
- git.close();
}
}
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java
index 581b065..67d172f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java
@@ -260,18 +260,13 @@
}
private static void createLocally(URIish uri, String head) {
- try {
- Repository repo = new FileRepository(uri.getPath());
- try {
- repo.create(true /* bare */);
+ try (Repository repo = new FileRepository(uri.getPath())) {
+ repo.create(true /* bare */);
- if (head != null) {
- RefUpdate u = repo.updateRef(Constants.HEAD);
- u.disableRefLog();
- u.link(head);
- }
- } finally {
- repo.close();
+ if (head != null) {
+ RefUpdate u = repo.updateRef(Constants.HEAD);
+ u.disableRefLog();
+ u.link(head);
}
} catch (IOException e) {
repLog.error(String.format(
@@ -389,15 +384,10 @@
}
private static void updateHeadLocally(URIish uri, String newHead) {
- try {
- Repository repo = new FileRepository(uri.getPath());
- try {
- if (newHead != null) {
- RefUpdate u = repo.updateRef(Constants.HEAD);
- u.link(newHead);
- }
- } finally {
- repo.close();
+ try (Repository repo = new FileRepository(uri.getPath())) {
+ if (newHead != null) {
+ RefUpdate u = repo.updateRef(Constants.HEAD);
+ u.link(newHead);
}
} catch (IOException e) {
repLog.error(