From 7343c7a10f7cd180c77a985e793c27873918146d Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Thu, 2 Apr 2015 01:36:22 +0200 Subject: [PATCH] Use try-with-resource to close resources in CheckoutCommand Change-Id: Ia4d4f9bff03a03d116b80022d7691df67bf8b51b Signed-off-by: Matthias Sohn --- .../org/eclipse/jgit/api/CheckoutCommand.java | 54 +++++++++---------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java index 3787ac511..00c3e1f4b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java @@ -208,16 +208,17 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException, } if (createBranch) { - Git git = new Git(repo); - CreateBranchCommand command = git.branchCreate(); - command.setName(name); - if (startCommit != null) - command.setStartPoint(startCommit); - else - command.setStartPoint(startPoint); - if (upstreamMode != null) - command.setUpstreamMode(upstreamMode); - command.call(); + try (Git git = new Git(repo)) { + CreateBranchCommand command = git.branchCreate(); + command.setName(name); + if (startCommit != null) + command.setStartPoint(startCommit); + else + command.setStartPoint(startPoint); + if (upstreamMode != null) + command.setUpstreamMode(upstreamMode); + command.call(); + } } Ref headRef = repo.getRef(Constants.HEAD); @@ -243,11 +244,14 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException, JGitText.get().refNotResolved, name)); } - RevWalk revWalk = new RevWalk(repo); - AnyObjectId headId = headRef.getObjectId(); - RevCommit headCommit = headId == null ? null : revWalk - .parseCommit(headId); - RevCommit newCommit = revWalk.parseCommit(branch); + RevCommit headCommit = null; + RevCommit newCommit = null; + try (RevWalk revWalk = new RevWalk(repo)) { + AnyObjectId headId = headRef.getObjectId(); + headCommit = headId == null ? null + : revWalk.parseCommit(headId); + newCommit = revWalk.parseCommit(branch); + } RevTree headTree = headCommit == null ? null : headCommit.getTree(); DirCacheCheckout dco; DirCache dc = repo.lockDirCache(); @@ -376,26 +380,20 @@ public CheckoutCommand setAllPaths(boolean all) { */ protected CheckoutCommand checkoutPaths() throws IOException, RefNotFoundException { - RevWalk revWalk = new RevWalk(repo); DirCache dc = repo.lockDirCache(); - try { - TreeWalk treeWalk = new TreeWalk(revWalk.getObjectReader()); + try (RevWalk revWalk = new RevWalk(repo); + TreeWalk treeWalk = new TreeWalk(revWalk.getObjectReader())) { treeWalk.setRecursive(true); if (!checkoutAllPaths) treeWalk.setFilter(PathFilterGroup.createFromStrings(paths)); - try { - if (isCheckoutIndex()) - checkoutPathsFromIndex(treeWalk, dc); - else { - RevCommit commit = revWalk.parseCommit(getStartPointObjectId()); - checkoutPathsFromCommit(treeWalk, dc, commit); - } - } finally { - treeWalk.release(); + if (isCheckoutIndex()) + checkoutPathsFromIndex(treeWalk, dc); + else { + RevCommit commit = revWalk.parseCommit(getStartPointObjectId()); + checkoutPathsFromCommit(treeWalk, dc, commit); } } finally { dc.unlock(); - revWalk.release(); } return this; }