]> source.dussan.org Git - jgit.git/commitdiff
Use AutoClosable to close resources in bundle org.eclipse.jgit 03/45703/5
authorMatthias Sohn <matthias.sohn@sap.com>
Fri, 10 Apr 2015 22:24:27 +0000 (00:24 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Wed, 20 May 2015 22:01:36 +0000 (00:01 +0200)
- use try-with-resource where possible
- replace use of deprecated release() by close()

Change-Id: I0f139c3535679087b7fa09649166bca514750b81
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
40 files changed:
org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/DeleteBranchCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ListBranchCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ListNotesCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ListTagCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/RemoveNoteCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/RmCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ShowNoteCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/TagCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java
org.eclipse.jgit/src/org/eclipse/jgit/diff/RenameDetector.java
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackCompactor.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsRefDatabase.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/LargePackedWholeObject.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/PackInputStream.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectoryRename.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/DeltaTask.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackWriter.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexDiff.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/RefUpdate.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
org.eclipse.jgit/src/org/eclipse/jgit/merge/RecursiveMerger.java
org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java
org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/PushProcess.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkPushConnection.java

index a4ad2c9bfb74f4439678f556da514b1f926ad2d4..a6646602e9ff4aee340e3f5c8aa0abc4d7a78447 100644 (file)
@@ -124,8 +124,7 @@ public class CreateBranchCommand extends GitCommand<Ref> {
                        RefNotFoundException, InvalidRefNameException {
                checkCallable();
                processOptions();
-               RevWalk revWalk = new RevWalk(repo);
-               try {
+               try (RevWalk revWalk = new RevWalk(repo)) {
                        Ref refToCheck = repo.getRef(name);
                        boolean exists = refToCheck != null
                                        && refToCheck.getName().startsWith(Constants.R_HEADS);
@@ -270,8 +269,6 @@ public class CreateBranchCommand extends GitCommand<Ref> {
                        return result;
                } catch (IOException ioe) {
                        throw new JGitInternalException(ioe.getMessage(), ioe);
-               } finally {
-                       revWalk.release();
                }
        }
 
index 30b27f22c782c8abad1fce4170cce887f445fcba..61beb2f0d07fe2677b24b675dc2144c6a4c80349 100644 (file)
@@ -108,18 +108,21 @@ public class DeleteBranchCommand extends GitCommand<List<String>> {
                        if (!force) {
                                // check if the branches to be deleted
                                // are all merged into the current branch
-                               RevWalk walk = new RevWalk(repo);
-                               RevCommit tip = walk.parseCommit(repo.resolve(Constants.HEAD));
-                               for (String branchName : branchNames) {
-                                       if (branchName == null)
-                                               continue;
-                                       Ref currentRef = repo.getRef(branchName);
-                                       if (currentRef == null)
-                                               continue;
+                               try (RevWalk walk = new RevWalk(repo)) {
+                                       RevCommit tip = walk
+                                                       .parseCommit(repo.resolve(Constants.HEAD));
+                                       for (String branchName : branchNames) {
+                                               if (branchName == null)
+                                                       continue;
+                                               Ref currentRef = repo.getRef(branchName);
+                                               if (currentRef == null)
+                                                       continue;
 
-                                       RevCommit base = walk.parseCommit(repo.resolve(branchName));
-                                       if (!walk.isMergedInto(base, tip)) {
-                                               throw new NotMergedException();
+                                               RevCommit base = walk
+                                                               .parseCommit(repo.resolve(branchName));
+                                               if (!walk.isMergedInto(base, tip)) {
+                                                       throw new NotMergedException();
+                                               }
                                        }
                                }
                        }
index 10f33de7af4b1f4fe0d63165014dba5463f6ceff..904c74f6ded625231c75b269029dba333713251f 100644 (file)
@@ -139,8 +139,7 @@ public class ListBranchCommand extends GitCommand<List<Ref>> {
                if (containsCommitish == null)
                        return refs;
 
-               RevWalk walk = new RevWalk(repo);
-               try {
+               try (RevWalk walk = new RevWalk(repo)) {
                        ObjectId resolved = repo.resolve(containsCommitish);
                        if (resolved == null)
                                throw new RefNotFoundException(MessageFormat.format(
@@ -149,8 +148,6 @@ public class ListBranchCommand extends GitCommand<List<Ref>> {
                        RevCommit containsCommit = walk.parseCommit(resolved);
                        return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk,
                                        refs);
-               } finally {
-                       walk.release();
                }
        }
 
index 84fa355af5d10d0c0c1da0a6de8d53ed5cb93f6a..796ac798f0b849fbde7b46fd246f3a3288c1b849 100644 (file)
@@ -80,9 +80,8 @@ public class ListNotesCommand extends GitCommand<List<Note>> {
        public List<Note> call() throws GitAPIException {
                checkCallable();
                List<Note> notes = new ArrayList<Note>();
-               RevWalk walk = new RevWalk(repo);
                NoteMap map = NoteMap.newEmptyMap();
-               try {
+               try (RevWalk walk = new RevWalk(repo)) {
                        Ref ref = repo.getRef(notesRef);
                        // if we have a notes ref, use it
                        if (ref != null) {
@@ -95,8 +94,6 @@ public class ListNotesCommand extends GitCommand<List<Note>> {
                                notes.add(i.next());
                } catch (IOException e) {
                        throw new JGitInternalException(e.getMessage(), e);
-               } finally {
-                       walk.release();
                }
 
                return notes;
index a0a5d950cab243cedbc00019680f5735f8ad8078..a3b701be8ebb57aa18f0f92e8814be7b742521dc 100644 (file)
@@ -78,16 +78,13 @@ public class ListTagCommand extends GitCommand<List<Ref>> {
                checkCallable();
                Map<String, Ref> refList;
                List<Ref> tags = new ArrayList<Ref>();
-               RevWalk revWalk = new RevWalk(repo);
-               try {
+               try (RevWalk revWalk = new RevWalk(repo)) {
                        refList = repo.getRefDatabase().getRefs(Constants.R_TAGS);
                        for (Ref ref : refList.values()) {
                                tags.add(ref);
                        }
                } catch (IOException e) {
                        throw new JGitInternalException(e.getMessage(), e);
-               } finally {
-                       revWalk.release();
                }
                Collections.sort(tags, new Comparator<Ref>() {
                        public int compare(Ref o1, Ref o2) {
index 83026a06272e9fe7a6cedacc45efa103075e671b..d2075a70f2a95e72fdc726ce3ca57d3f7394df4c 100644 (file)
@@ -369,9 +369,11 @@ public class MergeCommand extends GitCommand<MergeResult> {
                                                mergeStatus = MergeStatus.MERGED_NOT_COMMITTED;
                                        }
                                        if (commit && !squash) {
-                                               newHeadId = new Git(getRepository()).commit()
-                                                               .setReflogComment(refLogMessage.toString())
-                                                               .call().getId();
+                                               try (Git git = new Git(getRepository())) {
+                                                       newHeadId = git.commit()
+                                                                       .setReflogComment(refLogMessage.toString())
+                                                                       .call().getId();
+                                               }
                                                mergeStatus = MergeStatus.MERGED;
                                        }
                                        if (commit && squash) {
@@ -416,7 +418,7 @@ public class MergeCommand extends GitCommand<MergeResult> {
                                                        e), e);
                } finally {
                        if (revWalk != null)
-                               revWalk.release();
+                               revWalk.close();
                }
        }
 
index 82444ba6094ea4e84c4b58ff3741063f04fb572d..a11822975ae45288b8212c438341d64aff5a222e 100644 (file)
@@ -405,11 +405,12 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                                                .call();
                        } catch (StashApplyFailureException e) {
                                conflicts = true;
-                               RevWalk rw = new RevWalk(repo);
-                               ObjectId stashId = repo.resolve(stash);
-                               RevCommit commit = rw.parseCommit(stashId);
-                               updateStashRef(commit, commit.getAuthorIdent(),
-                                               commit.getShortMessage());
+                               try (RevWalk rw = new RevWalk(repo)) {
+                                       ObjectId stashId = repo.resolve(stash);
+                                       RevCommit commit = rw.parseCommit(stashId);
+                                       updateStashRef(commit, commit.getAuthorIdent(),
+                                                       commit.getShortMessage());
+                               }
                        }
                }
                return conflicts;
@@ -518,21 +519,23 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                        // here we should skip this step in order to avoid
                        // confusing pseudo-changed
                        String ourCommitName = getOurCommitName();
-                       CherryPickResult cherryPickResult = new Git(repo).cherryPick()
+                       try (Git git = new Git(repo)) {
+                               CherryPickResult cherryPickResult = git.cherryPick()
                                        .include(commitToPick).setOurCommitName(ourCommitName)
                                        .setReflogPrefix(REFLOG_PREFIX).setStrategy(strategy)
                                        .call();
-                       switch (cherryPickResult.getStatus()) {
-                       case FAILED:
-                               if (operation == Operation.BEGIN)
-                                       return abort(RebaseResult.failed(cherryPickResult
-                                                       .getFailingPaths()));
-                               else
+                               switch (cherryPickResult.getStatus()) {
+                               case FAILED:
+                                       if (operation == Operation.BEGIN)
+                                               return abort(RebaseResult
+                                                               .failed(cherryPickResult.getFailingPaths()));
+                                       else
+                                               return stop(commitToPick, Status.STOPPED);
+                               case CONFLICTING:
                                        return stop(commitToPick, Status.STOPPED);
-                       case CONFLICTING:
-                               return stop(commitToPick, Status.STOPPED);
-                       case OK:
-                               newHead = cherryPickResult.getNewHead();
+                               case OK:
+                                       newHead = cherryPickResult.getNewHead();
+                               }
                        }
                }
                return null;
@@ -563,62 +566,67 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                        // Use the cherry-pick strategy if all non-first parents did not
                        // change. This is different from C Git, which always uses the merge
                        // strategy (see below).
-                       if (otherParentsUnchanged) {
-                               boolean isMerge = commitToPick.getParentCount() > 1;
-                               String ourCommitName = getOurCommitName();
-                               CherryPickCommand pickCommand = new Git(repo).cherryPick()
-                                               .include(commitToPick).setOurCommitName(ourCommitName)
-                                               .setReflogPrefix(REFLOG_PREFIX).setStrategy(strategy);
-                               if (isMerge) {
-                                       pickCommand.setMainlineParentNumber(1);
-                                       // We write a MERGE_HEAD and later commit explicitly
-                                       pickCommand.setNoCommit(true);
-                                       writeMergeInfo(commitToPick, newParents);
-                               }
-                               CherryPickResult cherryPickResult = pickCommand.call();
-                               switch (cherryPickResult.getStatus()) {
-                               case FAILED:
-                                       if (operation == Operation.BEGIN)
-                                               return abort(RebaseResult.failed(cherryPickResult
-                                                               .getFailingPaths()));
-                                       else
-                                               return stop(commitToPick, Status.STOPPED);
-                               case CONFLICTING:
-                                       return stop(commitToPick, Status.STOPPED);
-                               case OK:
+                       try (Git git = new Git(repo)) {
+                               if (otherParentsUnchanged) {
+                                       boolean isMerge = commitToPick.getParentCount() > 1;
+                                       String ourCommitName = getOurCommitName();
+                                       CherryPickCommand pickCommand = git.cherryPick()
+                                                       .include(commitToPick)
+                                                       .setOurCommitName(ourCommitName)
+                                                       .setReflogPrefix(REFLOG_PREFIX)
+                                                       .setStrategy(strategy);
                                        if (isMerge) {
-                                               // Commit the merge (setup above using writeMergeInfo())
-                                               CommitCommand commit = new Git(repo).commit();
+                                               pickCommand.setMainlineParentNumber(1);
+                                               // We write a MERGE_HEAD and later commit explicitly
+                                               pickCommand.setNoCommit(true);
+                                               writeMergeInfo(commitToPick, newParents);
+                                       }
+                                       CherryPickResult cherryPickResult = pickCommand.call();
+                                       switch (cherryPickResult.getStatus()) {
+                                       case FAILED:
+                                               if (operation == Operation.BEGIN)
+                                                       return abort(RebaseResult.failed(
+                                                                       cherryPickResult.getFailingPaths()));
+                                               else
+                                                       return stop(commitToPick, Status.STOPPED);
+                                       case CONFLICTING:
+                                               return stop(commitToPick, Status.STOPPED);
+                                       case OK:
+                                               if (isMerge) {
+                                                       // Commit the merge (setup above using
+                                                       // writeMergeInfo())
+                                                       CommitCommand commit = git.commit();
+                                                       commit.setAuthor(commitToPick.getAuthorIdent());
+                                                       commit.setReflogComment(REFLOG_PREFIX + " " //$NON-NLS-1$
+                                                                       + commitToPick.getShortMessage());
+                                                       newHead = commit.call();
+                                               } else
+                                                       newHead = cherryPickResult.getNewHead();
+                                               break;
+                                       }
+                               } else {
+                                       // Use the merge strategy to redo merges, which had some of
+                                       // their non-first parents rewritten
+                                       MergeCommand merge = git.merge()
+                                                       .setFastForward(MergeCommand.FastForwardMode.NO_FF)
+                                                       .setCommit(false);
+                                       for (int i = 1; i < commitToPick.getParentCount(); i++)
+                                               merge.include(newParents.get(i));
+                                       MergeResult mergeResult = merge.call();
+                                       if (mergeResult.getMergeStatus().isSuccessful()) {
+                                               CommitCommand commit = git.commit();
                                                commit.setAuthor(commitToPick.getAuthorIdent());
+                                               commit.setMessage(commitToPick.getFullMessage());
                                                commit.setReflogComment(REFLOG_PREFIX + " " //$NON-NLS-1$
                                                                + commitToPick.getShortMessage());
                                                newHead = commit.call();
-                                       } else
-                                               newHead = cherryPickResult.getNewHead();
-                                       break;
-                               }
-                       } else {
-                               // Use the merge strategy to redo merges, which had some of
-                               // their non-first parents rewritten
-                               MergeCommand merge = new Git(repo).merge()
-                                               .setFastForward(MergeCommand.FastForwardMode.NO_FF)
-                                               .setCommit(false);
-                               for (int i = 1; i < commitToPick.getParentCount(); i++)
-                                       merge.include(newParents.get(i));
-                               MergeResult mergeResult = merge.call();
-                               if (mergeResult.getMergeStatus().isSuccessful()) {
-                                       CommitCommand commit = new Git(repo).commit();
-                                       commit.setAuthor(commitToPick.getAuthorIdent());
-                                       commit.setMessage(commitToPick.getFullMessage());
-                                       commit.setReflogComment(REFLOG_PREFIX + " " //$NON-NLS-1$
-                                                       + commitToPick.getShortMessage());
-                                       newHead = commit.call();
-                               } else {
-                                       if (operation == Operation.BEGIN
-                                                       && mergeResult.getMergeStatus() == MergeResult.MergeStatus.FAILED)
-                                               return abort(RebaseResult.failed(mergeResult
-                                                               .getFailingPaths()));
-                                       return stop(commitToPick, Status.STOPPED);
+                                       } else {
+                                               if (operation == Operation.BEGIN && mergeResult
+                                                               .getMergeStatus() == MergeResult.MergeStatus.FAILED)
+                                                       return abort(RebaseResult
+                                                                       .failed(mergeResult.getFailingPaths()));
+                                               return stop(commitToPick, Status.STOPPED);
+                                       }
                                }
                        }
                }
@@ -758,24 +766,25 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                String commitMessage = rebaseState
                                .readFile(MESSAGE_SQUASH);
 
-               if (nextStep == null
-                               || ((nextStep.getAction() != Action.FIXUP) && (nextStep
-                                               .getAction() != Action.SQUASH))) {
-                       // this is the last step in this sequence
-                       if (sequenceContainsSquash) {
-                               commitMessage = interactiveHandler
-                                               .modifyCommitMessage(commitMessage);
-                       }
-                       retNewHead = new Git(repo).commit()
-                                       .setMessage(stripCommentLines(commitMessage))
-                                       .setAmend(true).setNoVerify(true).call();
-                       rebaseState.getFile(MESSAGE_SQUASH).delete();
-                       rebaseState.getFile(MESSAGE_FIXUP).delete();
+               try (Git git = new Git(repo)) {
+                       if (nextStep == null || ((nextStep.getAction() != Action.FIXUP)
+                                       && (nextStep.getAction() != Action.SQUASH))) {
+                               // this is the last step in this sequence
+                               if (sequenceContainsSquash) {
+                                       commitMessage = interactiveHandler
+                                                       .modifyCommitMessage(commitMessage);
+                               }
+                               retNewHead = git.commit()
+                                               .setMessage(stripCommentLines(commitMessage))
+                                               .setAmend(true).setNoVerify(true).call();
+                               rebaseState.getFile(MESSAGE_SQUASH).delete();
+                               rebaseState.getFile(MESSAGE_FIXUP).delete();
 
-               } else {
-                       // Next step is either Squash or Fixup
-                       retNewHead = new Git(repo).commit().setMessage(commitMessage)
-                                       .setAmend(true).setNoVerify(true).call();
+                       } else {
+                               // Next step is either Squash or Fixup
+                               retNewHead = git.commit().setMessage(commitMessage)
+                                               .setAmend(true).setNoVerify(true).call();
+                       }
                }
                return retNewHead;
        }
@@ -917,10 +926,10 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                } finally {
                        dc.unlock();
                }
-               RevWalk rw = new RevWalk(repo);
-               RevCommit commit = rw.parseCommit(repo.resolve(Constants.HEAD));
-               rw.release();
-               return commit;
+               try (RevWalk rw = new RevWalk(repo)) {
+                       RevCommit commit = rw.parseCommit(repo.resolve(Constants.HEAD));
+                       return commit;
+               }
        }
 
        /**
@@ -936,27 +945,29 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                        throw new UnmergedPathsException();
 
                // determine whether we need to commit
-               TreeWalk treeWalk = new TreeWalk(repo);
-               treeWalk.reset();
-               treeWalk.setRecursive(true);
-               treeWalk.addTree(new DirCacheIterator(dc));
-               ObjectId id = repo.resolve(Constants.HEAD + "^{tree}"); //$NON-NLS-1$
-               if (id == null)
-                       throw new NoHeadException(
-                                       JGitText.get().cannotRebaseWithoutCurrentHead);
+               boolean needsCommit;
+               try (TreeWalk treeWalk = new TreeWalk(repo)) {
+                       treeWalk.reset();
+                       treeWalk.setRecursive(true);
+                       treeWalk.addTree(new DirCacheIterator(dc));
+                       ObjectId id = repo.resolve(Constants.HEAD + "^{tree}"); //$NON-NLS-1$
+                       if (id == null)
+                               throw new NoHeadException(
+                                               JGitText.get().cannotRebaseWithoutCurrentHead);
 
-               treeWalk.addTree(id);
+                       treeWalk.addTree(id);
 
-               treeWalk.setFilter(TreeFilter.ANY_DIFF);
-
-               boolean needsCommit = treeWalk.next();
-               treeWalk.release();
+                       treeWalk.setFilter(TreeFilter.ANY_DIFF);
 
+                       needsCommit = treeWalk.next();
+               }
                if (needsCommit) {
-                       CommitCommand commit = new Git(repo).commit();
-                       commit.setMessage(rebaseState.readFile(MESSAGE));
-                       commit.setAuthor(parseAuthor());
-                       return commit.call();
+                       try (Git git = new Git(repo)) {
+                               CommitCommand commit = git.commit();
+                               commit.setMessage(rebaseState.readFile(MESSAGE));
+                               commit.setAuthor(parseAuthor());
+                               return commit.call();
+                       }
                }
                return null;
        }
@@ -979,9 +990,10 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                rebaseState.createFile(AUTHOR_SCRIPT, authorScript);
                rebaseState.createFile(MESSAGE, commitToPick.getFullMessage());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
-               DiffFormatter df = new DiffFormatter(bos);
-               df.setRepository(repo);
-               df.format(commitToPick.getParent(0), commitToPick);
+               try (DiffFormatter df = new DiffFormatter(bos)) {
+                       df.setRepository(repo);
+                       df.format(commitToPick.getParent(0), commitToPick);
+               }
                rebaseState.createFile(PATCH, new String(bos.toByteArray(),
                                Constants.CHARACTER_ENCODING));
                rebaseState.createFile(STOPPED_SHA,
@@ -1124,9 +1136,11 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
 
        private List<RevCommit> calculatePickList(RevCommit headCommit)
                        throws GitAPIException, NoHeadException, IOException {
-               LogCommand cmd = new Git(repo).log().addRange(upstreamCommit,
-                               headCommit);
-               Iterable<RevCommit> commitsToUse = cmd.call();
+               Iterable<RevCommit> commitsToUse;
+               try (Git git = new Git(repo)) {
+                       LogCommand cmd = git.log().addRange(upstreamCommit, headCommit);
+                       commitsToUse = cmd.call();
+               }
                List<RevCommit> cherryPickList = new ArrayList<RevCommit>();
                for (RevCommit commit : commitsToUse) {
                        if (preserveMerges || commit.getParentCount() == 1)
@@ -1312,7 +1326,7 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                        }
                        dco.setFailOnConflict(false);
                        dco.checkout();
-                       walk.release();
+                       walk.close();
                } finally {
                        monitor.endTask();
                }
@@ -1387,7 +1401,7 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                                throw new IOException("Could not rewind to upstream commit");
                        }
                } finally {
-                       walk.release();
+                       walk.close();
                        monitor.endTask();
                }
                return true;
index d1e27709311ea5dc2a07a06ac3efa9e9de3b8b15..af09f65f170b1efd08239c3ea15888dba5f69c52 100644 (file)
@@ -81,11 +81,10 @@ public class RemoveNoteCommand extends GitCommand<Note> {
 
        public Note call() throws GitAPIException {
                checkCallable();
-               RevWalk walk = new RevWalk(repo);
-               ObjectInserter inserter = repo.newObjectInserter();
-               NoteMap map = NoteMap.newEmptyMap();
-               RevCommit notesCommit = null;
-               try {
+               try (RevWalk walk = new RevWalk(repo);
+                               ObjectInserter inserter = repo.newObjectInserter()) {
+                       NoteMap map = NoteMap.newEmptyMap();
+                       RevCommit notesCommit = null;
                        Ref ref = repo.getRef(notesRef);
                        // if we have a notes ref, use it
                        if (ref != null) {
@@ -98,9 +97,6 @@ public class RemoveNoteCommand extends GitCommand<Note> {
                        return map.getNote(id);
                } catch (IOException e) {
                        throw new JGitInternalException(e.getMessage(), e);
-               } finally {
-                       inserter.release();
-                       walk.release();
                }
        }
 
index ac67037b8afa7ed113ff26c1d51c5ab37dcf4e80..6183da033bc5f41be1795234c4406a92a80f732d 100644 (file)
@@ -234,17 +234,12 @@ public class ResetCommand extends GitCommand<Ref> {
        }
 
        private RevCommit parseCommit(final ObjectId commitId) {
-               RevCommit commit;
-               RevWalk rw = new RevWalk(repo);
-               try {
-                       commit = rw.parseCommit(commitId);
+               try (RevWalk rw = new RevWalk(repo)) {
+                       return rw.parseCommit(commitId);
                } catch (IOException e) {
                        throw new JGitInternalException(MessageFormat.format(
                                        JGitText.get().cannotReadCommit, commitId.toString()), e);
-               } finally {
-                       rw.release();
                }
-               return commit;
        }
 
        private ObjectId resolveRefToCommitId() {
@@ -305,11 +300,10 @@ public class ResetCommand extends GitCommand<Ref> {
 
        private void resetIndexForPaths(ObjectId commitTree) {
                DirCache dc = null;
-               try {
+               try (final TreeWalk tw = new TreeWalk(repo)) {
                        dc = repo.lockDirCache();
                        DirCacheBuilder builder = dc.builder();
 
-                       final TreeWalk tw = new TreeWalk(repo);
                        tw.addTree(new DirCacheBuildIterator(builder));
                        if (commitTree != null)
                                tw.addTree(commitTree);
@@ -342,11 +336,9 @@ public class ResetCommand extends GitCommand<Ref> {
 
        private void resetIndex(ObjectId commitTree) throws IOException {
                DirCache dc = repo.lockDirCache();
-               TreeWalk walk = null;
-               try {
+               try (TreeWalk walk = new TreeWalk(repo)) {
                        DirCacheBuilder builder = dc.builder();
 
-                       walk = new TreeWalk(repo);
                        if (commitTree != null)
                                walk.addTree(commitTree);
                        else
@@ -380,8 +372,6 @@ public class ResetCommand extends GitCommand<Ref> {
                        builder.commit();
                } finally {
                        dc.unlock();
-                       if (walk != null)
-                               walk.release();
                }
        }
 
index 470d823acac8001693073c27fcd737d47f9b5fa3..8015773624184f79d4abc5b4c2696d5b40504ba3 100644 (file)
@@ -126,8 +126,7 @@ public class RevertCommand extends GitCommand<RevCommit> {
                RevCommit newHead = null;
                checkCallable();
 
-               RevWalk revWalk = new RevWalk(repo);
-               try {
+               try (RevWalk revWalk = new RevWalk(repo)) {
 
                        // get the head commit
                        Ref headRef = repo.getRef(Constants.HEAD);
@@ -182,9 +181,11 @@ public class RevertCommand extends GitCommand<RevCommit> {
                                                        merger.getResultTreeId());
                                        dco.setFailOnConflict(true);
                                        dco.checkout();
-                                       newHead = new Git(getRepository()).commit()
-                                                       .setMessage(newMessage)
-                                                       .setReflogComment("revert: " + shortMessage).call(); //$NON-NLS-1$
+                                       try (Git git = new Git(getRepository())) {
+                                               newHead = git.commit().setMessage(newMessage)
+                                                               .setReflogComment("revert: " + shortMessage) //$NON-NLS-1$
+                                                               .call();
+                                       }
                                        revertedRefs.add(src);
                                        headCommit = newHead;
                                } else {
@@ -220,8 +221,6 @@ public class RevertCommand extends GitCommand<RevCommit> {
                                        MessageFormat.format(
                                                                        JGitText.get().exceptionCaughtDuringExecutionOfRevertCommand,
                                                        e), e);
-               } finally {
-                       revWalk.release();
                }
                return newHead;
        }
index c70b4aeaf25c56d987dc4b6d3387891cf117cc69..fd2cbe01293054befa92971f9121faaff1118002 100644 (file)
@@ -144,10 +144,9 @@ public class RmCommand extends GitCommand<DirCache> {
                checkCallable();
                DirCache dc = null;
 
-               try {
+               try (final TreeWalk tw = new TreeWalk(repo)) {
                        dc = repo.lockDirCache();
                        DirCacheBuilder builder = dc.builder();
-                       final TreeWalk tw = new TreeWalk(repo);
                        tw.reset(); // drop the first empty tree, which we do not need here
                        tw.setRecursive(true);
                        tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));
index 7d411c3ecf06987f075bfd71072aef620f929e8e..82db88190f158f60ed1c4fd4035d0519c98d904d 100644 (file)
@@ -76,10 +76,9 @@ public class ShowNoteCommand extends GitCommand<Note> {
 
        public Note call() throws GitAPIException {
                checkCallable();
-               RevWalk walk = new RevWalk(repo);
                NoteMap map = NoteMap.newEmptyMap();
                RevCommit notesCommit = null;
-               try {
+               try (RevWalk walk = new RevWalk(repo)) {
                        Ref ref = repo.getRef(notesRef);
                        // if we have a notes ref, use it
                        if (ref != null) {
@@ -89,8 +88,6 @@ public class ShowNoteCommand extends GitCommand<Note> {
                        return map.getNote(id);
                } catch (IOException e) {
                        throw new JGitInternalException(e.getMessage(), e);
-               } finally {
-                       walk.release();
                }
        }
 
index 356723db4be0c4e74e49d5f504cd63407b9073f9..16bdc7b4fd2d4f6d58b471cddeca149aaec34c20 100644 (file)
@@ -166,9 +166,8 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
                                        JGitText.get().stashApplyOnUnsafeRepository,
                                        repo.getRepositoryState()));
 
-               ObjectReader reader = repo.newObjectReader();
-               try {
-                       RevWalk revWalk = new RevWalk(reader);
+               try (ObjectReader reader = repo.newObjectReader();
+                               RevWalk revWalk = new RevWalk(reader)) {
 
                        ObjectId headCommit = repo.resolve(Constants.HEAD);
                        if (headCommit == null)
@@ -250,8 +249,6 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
                        throw e;
                } catch (IOException e) {
                        throw new JGitInternalException(JGitText.get().stashApplyFailed, e);
-               } finally {
-                       reader.release();
                }
        }
 
@@ -286,11 +283,9 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
 
        private void resetIndex(RevTree tree) throws IOException {
                DirCache dc = repo.lockDirCache();
-               TreeWalk walk = null;
-               try {
+               try (TreeWalk walk = new TreeWalk(repo)) {
                        DirCacheBuilder builder = dc.builder();
 
-                       walk = new TreeWalk(repo);
                        walk.addTree(tree);
                        walk.addTree(new DirCacheIterator(dc));
                        walk.setRecursive(true);
@@ -321,15 +316,13 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
                        builder.commit();
                } finally {
                        dc.unlock();
-                       if (walk != null)
-                               walk.release();
                }
        }
 
        private void resetUntracked(RevTree tree) throws CheckoutConflictException,
                        IOException {
-               TreeWalk walk = new TreeWalk(repo); // maybe NameConflictTreeWalk;
-               try {
+               // TODO maybe NameConflictTreeWalk ?
+               try (TreeWalk walk = new TreeWalk(repo)) {
                        walk.addTree(tree);
                        walk.addTree(new FileTreeIterator(repo));
                        walk.setRecursive(true);
@@ -359,8 +352,6 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
 
                                checkoutPath(entry, reader);
                        }
-               } finally {
-                       walk.release();
                }
        }
 
index f4d443d7254238c5074597a48360cc59cdc850a8..5167221743ca137f9fe0db016001000b4bc85cb6 100644 (file)
@@ -187,8 +187,9 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
 
        private RevCommit parseCommit(final ObjectReader reader,
                        final ObjectId headId) throws IOException {
-               final RevWalk walk = new RevWalk(reader);
-               return walk.parseCommit(headId);
+               try (final RevWalk walk = new RevWalk(reader)) {
+                       return walk.parseCommit(headId);
+               }
        }
 
        private CommitBuilder createBuilder() {
@@ -239,14 +240,13 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
                checkCallable();
 
                Ref head = getHead();
-               ObjectReader reader = repo.newObjectReader();
-               try {
+               try (ObjectReader reader = repo.newObjectReader()) {
                        RevCommit headCommit = parseCommit(reader, head.getObjectId());
                        DirCache cache = repo.lockDirCache();
-                       ObjectInserter inserter = repo.newObjectInserter();
                        ObjectId commitId;
-                       try {
-                               TreeWalk treeWalk = new TreeWalk(reader);
+                       try (ObjectInserter inserter = repo.newObjectInserter();
+                                       TreeWalk treeWalk = new TreeWalk(reader)) {
+
                                treeWalk.setRecursive(true);
                                treeWalk.addTree(headCommit.getTree());
                                treeWalk.addTree(new DirCacheIterator(cache));
@@ -380,7 +380,6 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
                                }
 
                        } finally {
-                               inserter.release();
                                cache.unlock();
                        }
 
@@ -391,8 +390,6 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
                        return parseCommit(reader, commitId);
                } catch (IOException e) {
                        throw new JGitInternalException(JGitText.get().stashFailed, e);
-               } finally {
-                       reader.release();
                }
        }
 }
index 81a30156a64361751db741e27e3779f47994288f..e288d7755b5dbcbb642679a47acf76390a3b18d9 100644 (file)
@@ -143,8 +143,7 @@ public class SubmoduleUpdateCommand extends
                        RefNotFoundException, GitAPIException {
                checkCallable();
 
-               try {
-                       SubmoduleWalk generator = SubmoduleWalk.forIndex(repo);
+               try (SubmoduleWalk generator = SubmoduleWalk.forIndex(repo)) {
                        if (!paths.isEmpty())
                                generator.setFilter(PathFilterGroup.createFromStrings(paths));
                        List<String> updated = new ArrayList<String>();
@@ -171,8 +170,7 @@ public class SubmoduleUpdateCommand extends
                                        submoduleRepo = clone.call().getRepository();
                                }
 
-                               try {
-                                       RevWalk walk = new RevWalk(submoduleRepo);
+                               try (RevWalk walk = new RevWalk(submoduleRepo)) {
                                        RevCommit commit = walk
                                                        .parseCommit(generator.getObjectId());
 
index 8570baa74188f381ebfef712dc0bf2398b9a6e5b..07dce06d6e095ae53829f0c6653117c5682609a3 100644 (file)
@@ -128,8 +128,7 @@ public class TagCommand extends GitCommand<Ref> {
                RepositoryState state = repo.getRepositoryState();
                processOptions(state);
 
-               RevWalk revWalk = new RevWalk(repo);
-               try {
+               try (RevWalk revWalk = new RevWalk(repo)) {
                        // if no id is set, we should attempt to use HEAD
                        if (id == null) {
                                ObjectId objectId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$
@@ -157,24 +156,19 @@ public class TagCommand extends GitCommand<Ref> {
                        newTag.setObjectId(id);
 
                        // write the tag object
-                       ObjectInserter inserter = repo.newObjectInserter();
-                       try {
+                       try (ObjectInserter inserter = repo.newObjectInserter()) {
                                ObjectId tagId = inserter.insert(newTag);
                                inserter.flush();
 
                                String tag = newTag.getTag();
                                return updateTagRef(tagId, revWalk, tag, newTag.toString());
 
-                       } finally {
-                               inserter.release();
                        }
 
                } catch (IOException e) {
                        throw new JGitInternalException(
                                        JGitText.get().exceptionCaughtDuringExecutionOfTagCommand,
                                        e);
-               } finally {
-                       revWalk.release();
                }
        }
 
index ae713e2e96ed9400e43f2d544f7adc475bdd1448..a6d672e7f3738579999419a992d59999f43c7993 100644 (file)
@@ -172,7 +172,7 @@ public class BlameGenerator implements AutoCloseable {
                        throw new IllegalStateException();
 
                if (revPool != null)
-                       revPool.release();
+                       revPool.close();
 
                if (reverse)
                        revPool = new ReverseWalk(getRepository());
@@ -450,7 +450,7 @@ public class BlameGenerator implements AutoCloseable {
                                r.computeAll();
                        return r;
                } finally {
-                       release();
+                       close();
                }
        }
 
@@ -513,7 +513,7 @@ public class BlameGenerator implements AutoCloseable {
        }
 
        private boolean done() {
-               release();
+               close();
                return false;
        }
 
index b819ad07337703229285df0687b2d2038873fdd4..8865b62c24a80f4940b45356e421b5cc0887b76d 100644 (file)
@@ -323,7 +323,7 @@ public class RenameDetector {
                        try {
                                return compute(objectReader, pm);
                        } finally {
-                               objectReader.release();
+                               objectReader.close();
                        }
                }
                return Collections.unmodifiableList(entries);
index f1241652b819f44d5e8b10d2bcd809f8c4e3ce58..f316ea99a1c230444cf71d8e394f75ebcfd64eb2 100644 (file)
@@ -403,8 +403,7 @@ public class DirCacheCheckout {
                        MissingObjectException, IncorrectObjectTypeException,
                        CheckoutConflictException, IndexWriteException {
                toBeDeleted.clear();
-               ObjectReader objectReader = repo.getObjectDatabase().newReader();
-               try {
+               try (ObjectReader objectReader = repo.getObjectDatabase().newReader()) {
                        if (headCommitTree != null)
                                preScanTwoTrees();
                        else
@@ -454,8 +453,6 @@ public class DirCacheCheckout {
                        // commit the index builder - a new index is persisted
                        if (!builder.commit())
                                throw new IndexWriteException();
-               } finally {
-                       objectReader.release();
                }
                return toBeDeleted.size() == 0;
        }
@@ -1056,8 +1053,7 @@ public class DirCacheCheckout {
         */
        private boolean isModifiedSubtree_IndexWorkingtree(String path)
                        throws CorruptObjectException, IOException {
-               NameConflictTreeWalk tw = new NameConflictTreeWalk(repo);
-               try {
+               try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
                        tw.addTree(new DirCacheIterator(dc));
                        tw.addTree(new FileTreeIterator(repo));
                        tw.setRecursive(true);
@@ -1075,8 +1071,6 @@ public class DirCacheCheckout {
                                }
                        }
                        return false;
-               } finally {
-                       tw.release();
                }
        }
 
@@ -1105,8 +1099,7 @@ public class DirCacheCheckout {
         */
        private boolean isModifiedSubtree_IndexTree(String path, ObjectId tree)
                        throws CorruptObjectException, IOException {
-               NameConflictTreeWalk tw = new NameConflictTreeWalk(repo);
-               try {
+               try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
                        tw.addTree(new DirCacheIterator(dc));
                        tw.addTree(tree);
                        tw.setRecursive(true);
@@ -1124,8 +1117,6 @@ public class DirCacheCheckout {
                                        return true;
                        }
                        return false;
-               } finally {
-                       tw.release();
                }
        }
 
index a4b444ed2c571b280ec77daf740055a5749e6cdd..0cf5c7e48f735f6c8cb1e9300b054bca80f2ddf4 100644 (file)
@@ -214,15 +214,10 @@ public class RepoCommand extends GitCommand<RevCommit> {
                 */
                protected byte[] readFileFromRepo(Repository repo,
                                String ref, String path) throws GitAPIException, IOException {
-                       ObjectReader reader = repo.newObjectReader();
-                       byte[] result;
-                       try {
+                       try (ObjectReader reader = repo.newObjectReader()) {
                                ObjectId oid = repo.resolve(ref + ":" + path); //$NON-NLS-1$
-                               result = reader.open(oid).getBytes(Integer.MAX_VALUE);
-                       } finally {
-                               reader.release();
+                               return reader.open(oid).getBytes(Integer.MAX_VALUE);
                        }
-                       return result;
                }
        }
 
@@ -748,8 +743,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
                        DirCache index = DirCache.newInCore();
                        DirCacheBuilder builder = index.builder();
                        ObjectInserter inserter = repo.newObjectInserter();
-                       RevWalk rw = new RevWalk(repo);
-                       try {
+                       try (RevWalk rw = new RevWalk(repo)) {
                                Config cfg = new Config();
                                for (Project proj : bareProjects) {
                                        String name = proj.path;
@@ -831,8 +825,6 @@ public class RepoCommand extends GitCommand<RevCommit> {
                                return rw.parseCommit(commitId);
                        } catch (IOException e) {
                                throw new ManifestErrorException(e);
-                       } finally {
-                               rw.release();
                        }
                } else {
                        return git
@@ -859,8 +851,10 @@ public class RepoCommand extends GitCommand<RevCommit> {
                        try {
                                Repository subRepo = add.call();
                                if (revision != null) {
-                                       Git sub = new Git(subRepo);
-                                       sub.checkout().setName(findRef(revision, subRepo)).call();
+                                       try (Git sub = new Git(subRepo)) {
+                                               sub.checkout().setName(findRef(revision, subRepo))
+                                                               .call();
+                                       }
                                        subRepo.close();
                                        git.add().addFilepattern(name).call();
                                }
index fed533880595417fb6628a1e9819c7fa2129a47c..de662922089d6282463b2b5d323df95dd20e5987 100644 (file)
@@ -230,7 +230,7 @@ public class DfsGarbageCollector {
                                        objdb.rollbackPack(newPackDesc);
                        }
                } finally {
-                       ctx.release();
+                       ctx.close();
                }
        }
 
index 5cab473c33036ce8c70012032229d38cfc753f64..578a3d6cd1eb80edb1b13e82fdd982f20a521e55 100644 (file)
@@ -601,7 +601,7 @@ public class DfsInserter extends ObjectInserter {
 
                @Override
                public void release() {
-                       ctx.release();
+                       ctx.close();
                }
        }
 
@@ -631,7 +631,7 @@ public class DfsInserter extends ObjectInserter {
                                        // The newly created pack is registered in the cache.
                                        return ctx.open(id, type).openStream();
                                } finally {
-                                       ctx.release();
+                                       ctx.close();
                                }
                        }
 
@@ -642,7 +642,7 @@ public class DfsInserter extends ObjectInserter {
                                                        new ReadBackStream(pos), inf, bufsz), bufsz)) {
                                @Override
                                public void close() throws IOException {
-                                       ctx.release();
+                                       ctx.close();
                                        super.close();
                                }
                        };
index 83728842b767af0c8875eb6cd73ab49151260ea5..dbe72b2d433a628a60ece92355e6e510dbea8541 100644 (file)
@@ -179,11 +179,8 @@ public class DfsPackCompactor {
         */
        public DfsPackCompactor exclude(DfsPackFile pack) throws IOException {
                final PackIndex idx;
-               DfsReader ctx = (DfsReader) repo.newObjectReader();
-               try {
+               try (DfsReader ctx = (DfsReader) repo.newObjectReader()) {
                        idx = pack.getPackIndex(ctx);
-               } finally {
-                       ctx.release();
                }
                return exclude(new PackWriter.ObjectIdSet() {
                        public boolean contains(AnyObjectId id) {
@@ -206,8 +203,7 @@ public class DfsPackCompactor {
                        pm = NullProgressMonitor.INSTANCE;
 
                DfsObjDatabase objdb = repo.getObjectDatabase();
-               DfsReader ctx = (DfsReader) objdb.newReader();
-               try {
+               try (DfsReader ctx = (DfsReader) objdb.newReader()) {
                        PackConfig pc = new PackConfig(repo);
                        pc.setIndexVersion(2);
                        pc.setDeltaCompress(false);
@@ -236,7 +232,7 @@ public class DfsPackCompactor {
                                        writeIndex(objdb, pack, pw);
 
                                        PackWriter.Statistics stats = pw.getStatistics();
-                                       pw.release();
+                                       pw.close();
                                        pw = null;
 
                                        pack.setPackStats(stats);
@@ -250,11 +246,10 @@ public class DfsPackCompactor {
                                }
                        } finally {
                                if (pw != null)
-                                       pw.release();
+                                       pw.close();
                        }
                } finally {
                        rw = null;
-                       ctx.release();
                }
        }
 
index 593aaac15bff0d5275a004f59b7edbca9899b2fd..ed8acd578fb8480bf48c30249081758b5586b69b 100644 (file)
@@ -183,8 +183,7 @@ public abstract class DfsRefDatabase extends RefDatabase {
 
        private Ref doPeel(final Ref leaf) throws MissingObjectException,
                        IOException {
-               RevWalk rw = new RevWalk(repository);
-               try {
+               try (RevWalk rw = new RevWalk(repository)) {
                        RevObject obj = rw.parseAny(leaf.getObjectId());
                        if (obj instanceof RevTag) {
                                return new ObjectIdRef.PeeledTag(
@@ -198,8 +197,6 @@ public abstract class DfsRefDatabase extends RefDatabase {
                                                leaf.getName(),
                                                leaf.getObjectId());
                        }
-               } finally {
-                       rw.release();
                }
        }
 
index 4b050c5669901048f91e39da64e5f407b554fc67..6d40a7505e1bccfed61c9a27d4d47f7b696831b4 100644 (file)
@@ -112,8 +112,10 @@ final class LargePackedWholeObject extends ObjectLoader {
                                ObjectId obj = pack.getReverseIdx(ctx).findObject(objectOffset);
                                return ctx.open(obj, type).openStream();
                        } finally {
-                               ctx.release();
+                               ctx.close();
                        }
+               } finally {
+                       ctx.close();
                }
 
                // Align buffer to inflater size, at a larger than default block.
index 805d243846b7b116e2279dbc12b3a6f685435b58..bb8445bcd2a5a712288da55a76be63f3ca2c7ec9 100644 (file)
@@ -80,6 +80,6 @@ final class PackInputStream extends InputStream {
 
        @Override
        public void close() {
-               ctx.release();
+               ctx.close();
        }
 }
index 0abf0c89913b4db4fa618783ed10cd3ed0894050..6d6d0cabd01eb13853b8b0b2a1f74c746485346a 100644 (file)
@@ -477,8 +477,7 @@ public class RefDirectory extends RefDatabase {
 
        private ObjectIdRef doPeel(final Ref leaf) throws MissingObjectException,
                        IOException {
-               RevWalk rw = new RevWalk(getRepository());
-               try {
+               try (RevWalk rw = new RevWalk(getRepository())) {
                        RevObject obj = rw.parseAny(leaf.getObjectId());
                        if (obj instanceof RevTag) {
                                return new ObjectIdRef.PeeledTag(leaf.getStorage(), leaf
@@ -487,8 +486,6 @@ public class RefDirectory extends RefDatabase {
                                return new ObjectIdRef.PeeledNonTag(leaf.getStorage(), leaf
                                                .getName(), leaf.getObjectId());
                        }
-               } finally {
-                       rw.release();
                }
        }
 
index 878fc1951be37734cc57ba5150e9b385f3828e24..ba4a63d7fe0e82a23e593fa6921cef4236b00f9e 100644 (file)
@@ -96,8 +96,7 @@ class RefDirectoryRename extends RefRename {
                objId = source.getOldObjectId();
                updateHEAD = needToUpdateHEAD();
                tmp = refdb.newTemporaryUpdate();
-               final RevWalk rw = new RevWalk(refdb.getRepository());
-               try {
+               try (final RevWalk rw = new RevWalk(refdb.getRepository())) {
                        // First backup the source so its never unreachable.
                        tmp.setNewObjectId(objId);
                        tmp.setForceUpdate(true);
@@ -178,7 +177,6 @@ class RefDirectoryRename extends RefRename {
                        } catch (IOException err) {
                                FileUtils.delete(refdb.fileFor(tmp.getName()));
                        }
-                       rw.release();
                }
        }
 
index 9534053bfe16c56747fb48b55731f1a5d2ff2631..8ea0c23eaf092246b245cfaf7c4077162c8b59a1 100644 (file)
@@ -288,7 +288,7 @@ final class DeltaTask implements Callable<Object> {
                                runWindow(w);
                } finally {
                        block.pm.endWorker();
-                       or.release();
+                       or.close();
                        or = null;
                }
                return null;
index b30315af7152f5d709e7fefa0e9517a98a89f9f2..baaed846c14463810df412c367f7e392408cd491 100644 (file)
@@ -987,7 +987,7 @@ public class PackWriter implements AutoCloseable {
                }
 
                stats.totalBytes = out.length();
-               reader.release();
+               reader.close();
                endPhase(writeMonitor);
        }
 
index 1b049f61559303ac414182977623ec85b8467751..a3961118336a4ff13602b04ff6082c788cea7e09 100644 (file)
@@ -400,116 +400,120 @@ public class IndexDiff {
                        throws IOException {
                dirCache = repository.readDirCache();
 
-               TreeWalk treeWalk = new TreeWalk(repository);
-               treeWalk.setRecursive(true);
-               // add the trees (tree, dirchache, workdir)
-               if (tree != null)
-                       treeWalk.addTree(tree);
-               else
-                       treeWalk.addTree(new EmptyTreeIterator());
-               treeWalk.addTree(new DirCacheIterator(dirCache));
-               treeWalk.addTree(initialWorkingTreeIterator);
-               Collection<TreeFilter> filters = new ArrayList<TreeFilter>(4);
-
-               if (monitor != null) {
-                       // Get the maximum size of the work tree and index
-                       // and add some (quite arbitrary)
-                       if (estIndexSize == 0)
-                               estIndexSize = dirCache.getEntryCount();
-                       int total = Math.max(estIndexSize * 10 / 9,
-                                       estWorkTreeSize * 10 / 9);
-                       monitor.beginTask(title, total);
-                       filters.add(new ProgressReportingFilter(monitor, total));
-               }
+               try (TreeWalk treeWalk = new TreeWalk(repository)) {
+                       treeWalk.setRecursive(true);
+                       // add the trees (tree, dirchache, workdir)
+                       if (tree != null)
+                               treeWalk.addTree(tree);
+                       else
+                               treeWalk.addTree(new EmptyTreeIterator());
+                       treeWalk.addTree(new DirCacheIterator(dirCache));
+                       treeWalk.addTree(initialWorkingTreeIterator);
+                       Collection<TreeFilter> filters = new ArrayList<TreeFilter>(4);
+
+                       if (monitor != null) {
+                               // Get the maximum size of the work tree and index
+                               // and add some (quite arbitrary)
+                               if (estIndexSize == 0)
+                                       estIndexSize = dirCache.getEntryCount();
+                               int total = Math.max(estIndexSize * 10 / 9,
+                                               estWorkTreeSize * 10 / 9);
+                               monitor.beginTask(title, total);
+                               filters.add(new ProgressReportingFilter(monitor, total));
+                       }
 
-               if (filter != null)
-                       filters.add(filter);
-               filters.add(new SkipWorkTreeFilter(INDEX));
-               indexDiffFilter = new IndexDiffFilter(INDEX, WORKDIR);
-               filters.add(indexDiffFilter);
-               treeWalk.setFilter(AndTreeFilter.create(filters));
-               fileModes.clear();
-               while (treeWalk.next()) {
-                       AbstractTreeIterator treeIterator = treeWalk.getTree(TREE,
-                                       AbstractTreeIterator.class);
-                       DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX,
-                                       DirCacheIterator.class);
-                       WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR,
-                                       WorkingTreeIterator.class);
-
-                       if (dirCacheIterator != null) {
-                               final DirCacheEntry dirCacheEntry = dirCacheIterator
-                                               .getDirCacheEntry();
-                               if (dirCacheEntry != null) {
-                                       int stage = dirCacheEntry.getStage();
-                                       if (stage > 0) {
-                                               String path = treeWalk.getPathString();
-                                               addConflict(path, stage);
-                                               continue;
+                       if (filter != null)
+                               filters.add(filter);
+                       filters.add(new SkipWorkTreeFilter(INDEX));
+                       indexDiffFilter = new IndexDiffFilter(INDEX, WORKDIR);
+                       filters.add(indexDiffFilter);
+                       treeWalk.setFilter(AndTreeFilter.create(filters));
+                       fileModes.clear();
+                       while (treeWalk.next()) {
+                               AbstractTreeIterator treeIterator = treeWalk.getTree(TREE,
+                                               AbstractTreeIterator.class);
+                               DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX,
+                                               DirCacheIterator.class);
+                               WorkingTreeIterator workingTreeIterator = treeWalk
+                                               .getTree(WORKDIR, WorkingTreeIterator.class);
+
+                               if (dirCacheIterator != null) {
+                                       final DirCacheEntry dirCacheEntry = dirCacheIterator
+                                                       .getDirCacheEntry();
+                                       if (dirCacheEntry != null) {
+                                               int stage = dirCacheEntry.getStage();
+                                               if (stage > 0) {
+                                                       String path = treeWalk.getPathString();
+                                                       addConflict(path, stage);
+                                                       continue;
+                                               }
                                        }
                                }
-                       }
 
-                       if (treeIterator != null) {
-                               if (dirCacheIterator != null) {
-                                       if (!treeIterator.idEqual(dirCacheIterator)
-                                                       || treeIterator.getEntryRawMode()
-                                                       != dirCacheIterator.getEntryRawMode()) {
-                                               // in repo, in index, content diff => changed
+                               if (treeIterator != null) {
+                                       if (dirCacheIterator != null) {
+                                               if (!treeIterator.idEqual(dirCacheIterator)
+                                                               || treeIterator
+                                                                               .getEntryRawMode() != dirCacheIterator
+                                                                                               .getEntryRawMode()) {
+                                                       // in repo, in index, content diff => changed
+                                                       if (!isEntryGitLink(treeIterator)
+                                                                       || !isEntryGitLink(dirCacheIterator)
+                                                                       || ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL)
+                                                               changed.add(treeWalk.getPathString());
+                                               }
+                                       } else {
+                                               // in repo, not in index => removed
                                                if (!isEntryGitLink(treeIterator)
-                                                               || !isEntryGitLink(dirCacheIterator)
                                                                || ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL)
-                                                       changed.add(treeWalk.getPathString());
+                                                       removed.add(treeWalk.getPathString());
+                                               if (workingTreeIterator != null)
+                                                       untracked.add(treeWalk.getPathString());
                                        }
                                } else {
-                                       // in repo, not in index => removed
-                                       if (!isEntryGitLink(treeIterator)
-                                                       || ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL)
-                                               removed.add(treeWalk.getPathString());
-                                       if (workingTreeIterator != null)
-                                               untracked.add(treeWalk.getPathString());
-                               }
-                       } else {
-                               if (dirCacheIterator != null) {
-                                       // not in repo, in index => added
-                                       if (!isEntryGitLink(dirCacheIterator)
-                                                       || ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL)
-                                               added.add(treeWalk.getPathString());
-                               } else {
-                                       // not in repo, not in index => untracked
-                                       if (workingTreeIterator != null
-                                                       && !workingTreeIterator.isEntryIgnored()) {
-                                               untracked.add(treeWalk.getPathString());
+                                       if (dirCacheIterator != null) {
+                                               // not in repo, in index => added
+                                               if (!isEntryGitLink(dirCacheIterator)
+                                                               || ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL)
+                                                       added.add(treeWalk.getPathString());
+                                       } else {
+                                               // not in repo, not in index => untracked
+                                               if (workingTreeIterator != null
+                                                               && !workingTreeIterator.isEntryIgnored()) {
+                                                       untracked.add(treeWalk.getPathString());
+                                               }
                                        }
                                }
-                       }
 
-                       if (dirCacheIterator != null) {
-                               if (workingTreeIterator == null) {
-                                       // in index, not in workdir => missing
-                                       if (!isEntryGitLink(dirCacheIterator)
-                                                       || ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL)
-                                               missing.add(treeWalk.getPathString());
-                               } else {
-                                       if (workingTreeIterator.isModified(
-                                                       dirCacheIterator.getDirCacheEntry(), true,
-                                                       treeWalk.getObjectReader())) {
-                                               // in index, in workdir, content differs => modified
-                                               if (!isEntryGitLink(dirCacheIterator) || !isEntryGitLink(workingTreeIterator)
-                                                               || (ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL && ignoreSubmoduleMode != IgnoreSubmoduleMode.DIRTY))
-                                                       modified.add(treeWalk.getPathString());
+                               if (dirCacheIterator != null) {
+                                       if (workingTreeIterator == null) {
+                                               // in index, not in workdir => missing
+                                               if (!isEntryGitLink(dirCacheIterator)
+                                                               || ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL)
+                                                       missing.add(treeWalk.getPathString());
+                                       } else {
+                                               if (workingTreeIterator.isModified(
+                                                               dirCacheIterator.getDirCacheEntry(), true,
+                                                               treeWalk.getObjectReader())) {
+                                                       // in index, in workdir, content differs => modified
+                                                       if (!isEntryGitLink(dirCacheIterator)
+                                                                       || !isEntryGitLink(workingTreeIterator)
+                                                                       || (ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL
+                                                                                       && ignoreSubmoduleMode != IgnoreSubmoduleMode.DIRTY))
+                                                               modified.add(treeWalk.getPathString());
+                                               }
                                        }
                                }
-                       }
 
-                       for (int i = 0; i < treeWalk.getTreeCount(); i++) {
-                               Set<String> values = fileModes.get(treeWalk.getFileMode(i));
-                               String path = treeWalk.getPathString();
-                               if (path != null) {
-                                       if (values == null)
-                                               values = new HashSet<String>();
-                                       values.add(path);
-                                       fileModes.put(treeWalk.getFileMode(i), values);
+                               for (int i = 0; i < treeWalk.getTreeCount(); i++) {
+                                       Set<String> values = fileModes.get(treeWalk.getFileMode(i));
+                                       String path = treeWalk.getPathString();
+                                       if (path != null) {
+                                               if (values == null)
+                                                       values = new HashSet<String>();
+                                               values.add(path);
+                                               fileModes.put(treeWalk.getFileMode(i), values);
+                                       }
                                }
                        }
                }
index f47dff75a84850e8faf40d517f15a0d550d6d6be..aeef9f07449c80bda49051f7a19ba698d42b04ba 100644 (file)
@@ -460,11 +460,8 @@ public abstract class RefUpdate {
         *             an unexpected IO error occurred while writing changes.
         */
        public Result update() throws IOException {
-               RevWalk rw = new RevWalk(getRepository());
-               try {
+               try (RevWalk rw = new RevWalk(getRepository())) {
                        return update(rw);
-               } finally {
-                       rw.release();
                }
        }
 
@@ -510,11 +507,8 @@ public abstract class RefUpdate {
         * @throws IOException
         */
        public Result delete() throws IOException {
-               RevWalk rw = new RevWalk(getRepository());
-               try {
+               try (RevWalk rw = new RevWalk(getRepository())) {
                        return delete(rw);
-               } finally {
-                       rw.release();
                }
        }
 
index b0fe331676a5f46bb998ccf74fde2d3e8f31d12d..fa28763f0dffaeb8d00ce8bf71325355762f524a 100644 (file)
@@ -379,8 +379,7 @@ public abstract class Repository implements AutoCloseable {
        public ObjectId resolve(final String revstr)
                        throws AmbiguousObjectException, IncorrectObjectTypeException,
                        RevisionSyntaxException, IOException {
-               RevWalk rw = new RevWalk(this);
-               try {
+               try (RevWalk rw = new RevWalk(this)) {
                        Object resolved = resolve(rw, revstr);
                        if (resolved instanceof String) {
                                final Ref ref = getRef((String)resolved);
@@ -388,8 +387,6 @@ public abstract class Repository implements AutoCloseable {
                        } else {
                                return (ObjectId) resolved;
                        }
-               } finally {
-                       rw.release();
                }
        }
 
@@ -406,8 +403,7 @@ public abstract class Repository implements AutoCloseable {
         */
        public String simplify(final String revstr)
                        throws AmbiguousObjectException, IOException {
-               RevWalk rw = new RevWalk(this);
-               try {
+               try (RevWalk rw = new RevWalk(this)) {
                        Object resolved = resolve(rw, revstr);
                        if (resolved != null)
                                if (resolved instanceof String)
@@ -415,8 +411,6 @@ public abstract class Repository implements AutoCloseable {
                                else
                                        return ((AnyObjectId) resolved).getName();
                        return null;
-               } finally {
-                       rw.release();
                }
        }
 
@@ -791,8 +785,7 @@ public abstract class Repository implements AutoCloseable {
        private ObjectId resolveAbbreviation(final String revstr) throws IOException,
                        AmbiguousObjectException {
                AbbreviatedObjectId id = AbbreviatedObjectId.fromString(revstr);
-               ObjectReader reader = newObjectReader();
-               try {
+               try (ObjectReader reader = newObjectReader()) {
                        Collection<ObjectId> matches = reader.resolve(id);
                        if (matches.size() == 0)
                                return null;
@@ -800,8 +793,6 @@ public abstract class Repository implements AutoCloseable {
                                return matches.iterator().next();
                        else
                                throw new AmbiguousObjectException(id, matches);
-               } finally {
-                       reader.release();
                }
        }
 
index 36ffe7a632c8a613ad0bd57cc13ea74348e2dcd3..aef47c58cff06ad605fbb9fc05c63724368f0e63 100644 (file)
@@ -269,14 +269,15 @@ public class RecursiveMerger extends ResolveMerger {
        private DirCache dircacheFromTree(ObjectId treeId) throws IOException {
                DirCache ret = DirCache.newInCore();
                DirCacheBuilder aBuilder = ret.builder();
-               TreeWalk atw = new TreeWalk(reader);
-               atw.addTree(treeId);
-               atw.setRecursive(true);
-               while (atw.next()) {
-                       DirCacheEntry e = new DirCacheEntry(atw.getRawPath());
-                       e.setFileMode(atw.getFileMode(0));
-                       e.setObjectId(atw.getObjectId(0));
-                       aBuilder.add(e);
+               try (TreeWalk atw = new TreeWalk(reader)) {
+                       atw.addTree(treeId);
+                       atw.setRecursive(true);
+                       while (atw.next()) {
+                               DirCacheEntry e = new DirCacheEntry(atw.getRawPath());
+                               e.setFileMode(atw.getFileMode(0));
+                               e.setObjectId(atw.getObjectId(0));
+                               aBuilder.add(e);
+                       }
                }
                aBuilder.finish();
                return ret;
index ea4d7bc866f4af645f8c3d2e65520a831dbdb9cc..41f7501e5127a2371c2666f9eb6ee8932b7de105 100644 (file)
@@ -53,6 +53,7 @@ import java.util.NoSuchElementException;
 import org.eclipse.jgit.lib.AnyObjectId;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.ObjectInserter;
+import org.eclipse.jgit.lib.ObjectInserter.Formatter;
 import org.eclipse.jgit.lib.ObjectReader;
 import org.eclipse.jgit.lib.TreeFormatter;
 
@@ -183,7 +184,9 @@ class LeafBucket extends InMemoryNoteBucket {
 
        @Override
        ObjectId getTreeId() {
-               return new ObjectInserter.Formatter().idFor(build());
+               try (Formatter f = new ObjectInserter.Formatter()) {
+                       return f.idFor(build());
+               }
        }
 
        private TreeFormatter build() {
index a01f006c4c93dc0ed7082223c7d848a1a753223d..40f1d22ed220cf08287161e2348b196cce72cfd6 100644 (file)
@@ -122,7 +122,7 @@ public class SubmoduleWalk implements AutoCloseable {
                        DirCache index = repository.readDirCache();
                        generator.setTree(new DirCacheIterator(index));
                } catch (IOException e) {
-                       generator.release();
+                       generator.close();
                        throw e;
                }
                return generator;
@@ -152,10 +152,10 @@ public class SubmoduleWalk implements AutoCloseable {
                                if (filter.isDone(generator.walk))
                                        return generator;
                } catch (IOException e) {
-                       generator.release();
+                       generator.close();
                        throw e;
                }
-               generator.release();
+               generator.close();
                return null;
        }
 
@@ -183,10 +183,10 @@ public class SubmoduleWalk implements AutoCloseable {
                                if (filter.isDone(generator.walk))
                                        return generator;
                } catch (IOException e) {
-                       generator.release();
+                       generator.close();
                        throw e;
                }
-               generator.release();
+               generator.close();
                return null;
        }
 
@@ -419,8 +419,7 @@ public class SubmoduleWalk implements AutoCloseable {
                        config.load();
                        modulesConfig = config;
                } else {
-                       TreeWalk configWalk = new TreeWalk(repository);
-                       try {
+                       try (TreeWalk configWalk = new TreeWalk(repository)) {
                                configWalk.addTree(rootTree);
 
                                // The root tree may be part of the submodule walk, so we need to revert
@@ -446,8 +445,6 @@ public class SubmoduleWalk implements AutoCloseable {
                                        if (idx > 0)
                                                rootTree.next(idx);
                                }
-                       } finally {
-                               configWalk.release();
                        }
                }
                return this;
index 00f84f70eae6c91e6d097a0b061a4abc1270660a..9721ee9eb0a6f3e09c5627dc01c618d64ecce9ea 100644 (file)
@@ -104,7 +104,7 @@ class PushProcess {
 
        /**
         * Create process for specified transport and refs updates specification.
-        * 
+        *
         * @param transport
         *            transport between remote and local repository, used to create
         *            connection.
@@ -177,7 +177,7 @@ class PushProcess {
                        }
                        return res;
                } finally {
-                       walker.release();
+                       walker.close();
                }
        }
 
index 3afdb6114da0df5c4f15577f28fc3981fa6df346..c60590dda40f75a587457ee8fc156351d33c5f45 100644 (file)
@@ -630,7 +630,7 @@ public class UploadPack {
                        service();
                } finally {
                        msgOut = NullOutputStream.INSTANCE;
-                       walk.release();
+                       walk.close();
                        if (timer != null) {
                                try {
                                        timer.terminate();
@@ -737,35 +737,35 @@ public class UploadPack {
        }
 
        private void processShallow() throws IOException {
-               DepthWalk.RevWalk depthWalk =
-                       new DepthWalk.RevWalk(walk.getObjectReader(), depth);
+               try (DepthWalk.RevWalk depthWalk = new DepthWalk.RevWalk(
+                               walk.getObjectReader(), depth)) {
 
-               // Find all the commits which will be shallow
-               for (ObjectId o : wantIds) {
-                       try {
-                               depthWalk.markRoot(depthWalk.parseCommit(o));
-                       } catch (IncorrectObjectTypeException notCommit) {
-                               // Ignore non-commits in this loop.
+                       // Find all the commits which will be shallow
+                       for (ObjectId o : wantIds) {
+                               try {
+                                       depthWalk.markRoot(depthWalk.parseCommit(o));
+                               } catch (IncorrectObjectTypeException notCommit) {
+                                       // Ignore non-commits in this loop.
+                               }
                        }
-               }
 
-               RevCommit o;
-               while ((o = depthWalk.next()) != null) {
-                       DepthWalk.Commit c = (DepthWalk.Commit) o;
+                       RevCommit o;
+                       while ((o = depthWalk.next()) != null) {
+                               DepthWalk.Commit c = (DepthWalk.Commit) o;
 
-                       // Commits at the boundary which aren't already shallow in
-                       // the client need to be marked as such
-                       if (c.getDepth() == depth && !clientShallowCommits.contains(c))
-                               pckOut.writeString("shallow " + o.name()); //$NON-NLS-1$
+                               // Commits at the boundary which aren't already shallow in
+                               // the client need to be marked as such
+                               if (c.getDepth() == depth && !clientShallowCommits.contains(c))
+                                       pckOut.writeString("shallow " + o.name()); //$NON-NLS-1$
 
-                       // Commits not on the boundary which are shallow in the client
-                       // need to become unshallowed
-                       if (c.getDepth() < depth && clientShallowCommits.remove(c)) {
-                               unshallowCommits.add(c.copy());
-                               pckOut.writeString("unshallow " + c.name()); //$NON-NLS-1$
+                               // Commits not on the boundary which are shallow in the client
+                               // need to become unshallowed
+                               if (c.getDepth() < depth && clientShallowCommits.remove(c)) {
+                                       unshallowCommits.add(c.copy());
+                                       pckOut.writeString("unshallow " + c.name()); //$NON-NLS-1$
+                               }
                        }
                }
-
                pckOut.end();
        }
 
@@ -1460,7 +1460,7 @@ public class UploadPack {
                        statistics = pw.getStatistics();
                        if (statistics != null)
                                logger.onPackStatistics(statistics);
-                       pw.release();
+                       pw.close();
                }
 
                if (sideband)
index 6b7183b7592b093d928e4d69396f39ff2b01638e..a71aac59c0ba8b93acbaeab8058f7c0d1a9f7dc3 100644 (file)
@@ -252,8 +252,8 @@ class WalkFetchConnection extends BaseFetchConnection {
 
        @Override
        public void close() {
-               inserter.release();
-               reader.release();
+               inserter.close();
+               reader.close();
                for (final RemotePack p : unfetchedPacks) {
                        if (p.tmpIdx != null)
                                p.tmpIdx.delete();
index 02960bf293b8ad785d136a4ba0ec540d77d84e27..deecb8e15ff5eb530821375100c784bd7079641a 100644 (file)
@@ -220,9 +220,9 @@ class WalkPushConnection extends BaseConnection implements PushConnection {
                String pathPack = null;
                String pathIdx = null;
 
-               final PackWriter writer = new PackWriter(transport.getPackConfig(),
-                               local.newObjectReader());
-               try {
+               try (final PackWriter writer = new PackWriter(transport.getPackConfig(),
+                               local.newObjectReader())) {
+
                        final Set<ObjectId> need = new HashSet<ObjectId>();
                        final Set<ObjectId> have = new HashSet<ObjectId>();
                        for (final RemoteRefUpdate r : updates)
@@ -293,8 +293,6 @@ class WalkPushConnection extends BaseConnection implements PushConnection {
                        safeDelete(pathPack);
 
                        throw new TransportException(uri, JGitText.get().cannotStoreObjects, err);
-               } finally {
-                       writer.release();
                }
        }