Browse Source

Use AutoClosable to close resources in bundle org.eclipse.jgit

- 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>
stable-4.1
Matthias Sohn 10 years ago
parent
commit
0e73d39506
  1. 5
      org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java
  2. 9
      org.eclipse.jgit/src/org/eclipse/jgit/api/DeleteBranchCommand.java
  3. 5
      org.eclipse.jgit/src/org/eclipse/jgit/api/ListBranchCommand.java
  4. 5
      org.eclipse.jgit/src/org/eclipse/jgit/api/ListNotesCommand.java
  5. 5
      org.eclipse.jgit/src/org/eclipse/jgit/api/ListTagCommand.java
  6. 6
      org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java
  7. 84
      org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java
  8. 8
      org.eclipse.jgit/src/org/eclipse/jgit/api/RemoveNoteCommand.java
  9. 18
      org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java
  10. 13
      org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java
  11. 3
      org.eclipse.jgit/src/org/eclipse/jgit/api/RmCommand.java
  12. 5
      org.eclipse.jgit/src/org/eclipse/jgit/api/ShowNoteCommand.java
  13. 19
      org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java
  14. 15
      org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java
  15. 6
      org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java
  16. 10
      org.eclipse.jgit/src/org/eclipse/jgit/api/TagCommand.java
  17. 6
      org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java
  18. 2
      org.eclipse.jgit/src/org/eclipse/jgit/diff/RenameDetector.java
  19. 15
      org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java
  20. 20
      org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java
  21. 2
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java
  22. 6
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java
  23. 13
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackCompactor.java
  24. 5
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsRefDatabase.java
  25. 4
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/LargePackedWholeObject.java
  26. 2
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/PackInputStream.java
  27. 5
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java
  28. 4
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectoryRename.java
  29. 2
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/DeltaTask.java
  30. 2
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackWriter.java
  31. 18
      org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexDiff.java
  32. 10
      org.eclipse.jgit/src/org/eclipse/jgit/lib/RefUpdate.java
  33. 15
      org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
  34. 3
      org.eclipse.jgit/src/org/eclipse/jgit/merge/RecursiveMerger.java
  35. 5
      org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java
  36. 15
      org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java
  37. 2
      org.eclipse.jgit/src/org/eclipse/jgit/transport/PushProcess.java
  38. 10
      org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java
  39. 4
      org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java
  40. 8
      org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkPushConnection.java

5
org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java

@ -124,8 +124,7 @@ public class CreateBranchCommand extends GitCommand<Ref> {
RefNotFoundException, InvalidRefNameException { RefNotFoundException, InvalidRefNameException {
checkCallable(); checkCallable();
processOptions(); processOptions();
RevWalk revWalk = new RevWalk(repo); try (RevWalk revWalk = new RevWalk(repo)) {
try {
Ref refToCheck = repo.getRef(name); Ref refToCheck = repo.getRef(name);
boolean exists = refToCheck != null boolean exists = refToCheck != null
&& refToCheck.getName().startsWith(Constants.R_HEADS); && refToCheck.getName().startsWith(Constants.R_HEADS);
@ -270,8 +269,6 @@ public class CreateBranchCommand extends GitCommand<Ref> {
return result; return result;
} catch (IOException ioe) { } catch (IOException ioe) {
throw new JGitInternalException(ioe.getMessage(), ioe); throw new JGitInternalException(ioe.getMessage(), ioe);
} finally {
revWalk.release();
} }
} }

9
org.eclipse.jgit/src/org/eclipse/jgit/api/DeleteBranchCommand.java

@ -108,8 +108,9 @@ public class DeleteBranchCommand extends GitCommand<List<String>> {
if (!force) { if (!force) {
// check if the branches to be deleted // check if the branches to be deleted
// are all merged into the current branch // are all merged into the current branch
RevWalk walk = new RevWalk(repo); try (RevWalk walk = new RevWalk(repo)) {
RevCommit tip = walk.parseCommit(repo.resolve(Constants.HEAD)); RevCommit tip = walk
.parseCommit(repo.resolve(Constants.HEAD));
for (String branchName : branchNames) { for (String branchName : branchNames) {
if (branchName == null) if (branchName == null)
continue; continue;
@ -117,12 +118,14 @@ public class DeleteBranchCommand extends GitCommand<List<String>> {
if (currentRef == null) if (currentRef == null)
continue; continue;
RevCommit base = walk.parseCommit(repo.resolve(branchName)); RevCommit base = walk
.parseCommit(repo.resolve(branchName));
if (!walk.isMergedInto(base, tip)) { if (!walk.isMergedInto(base, tip)) {
throw new NotMergedException(); throw new NotMergedException();
} }
} }
} }
}
setCallable(false); setCallable(false);
for (String branchName : branchNames) { for (String branchName : branchNames) {
if (branchName == null) if (branchName == null)

5
org.eclipse.jgit/src/org/eclipse/jgit/api/ListBranchCommand.java

@ -139,8 +139,7 @@ public class ListBranchCommand extends GitCommand<List<Ref>> {
if (containsCommitish == null) if (containsCommitish == null)
return refs; return refs;
RevWalk walk = new RevWalk(repo); try (RevWalk walk = new RevWalk(repo)) {
try {
ObjectId resolved = repo.resolve(containsCommitish); ObjectId resolved = repo.resolve(containsCommitish);
if (resolved == null) if (resolved == null)
throw new RefNotFoundException(MessageFormat.format( throw new RefNotFoundException(MessageFormat.format(
@ -149,8 +148,6 @@ public class ListBranchCommand extends GitCommand<List<Ref>> {
RevCommit containsCommit = walk.parseCommit(resolved); RevCommit containsCommit = walk.parseCommit(resolved);
return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk, return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk,
refs); refs);
} finally {
walk.release();
} }
} }

5
org.eclipse.jgit/src/org/eclipse/jgit/api/ListNotesCommand.java

@ -80,9 +80,8 @@ public class ListNotesCommand extends GitCommand<List<Note>> {
public List<Note> call() throws GitAPIException { public List<Note> call() throws GitAPIException {
checkCallable(); checkCallable();
List<Note> notes = new ArrayList<Note>(); List<Note> notes = new ArrayList<Note>();
RevWalk walk = new RevWalk(repo);
NoteMap map = NoteMap.newEmptyMap(); NoteMap map = NoteMap.newEmptyMap();
try { try (RevWalk walk = new RevWalk(repo)) {
Ref ref = repo.getRef(notesRef); Ref ref = repo.getRef(notesRef);
// if we have a notes ref, use it // if we have a notes ref, use it
if (ref != null) { if (ref != null) {
@ -95,8 +94,6 @@ public class ListNotesCommand extends GitCommand<List<Note>> {
notes.add(i.next()); notes.add(i.next());
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e); throw new JGitInternalException(e.getMessage(), e);
} finally {
walk.release();
} }
return notes; return notes;

5
org.eclipse.jgit/src/org/eclipse/jgit/api/ListTagCommand.java

@ -78,16 +78,13 @@ public class ListTagCommand extends GitCommand<List<Ref>> {
checkCallable(); checkCallable();
Map<String, Ref> refList; Map<String, Ref> refList;
List<Ref> tags = new ArrayList<Ref>(); List<Ref> tags = new ArrayList<Ref>();
RevWalk revWalk = new RevWalk(repo); try (RevWalk revWalk = new RevWalk(repo)) {
try {
refList = repo.getRefDatabase().getRefs(Constants.R_TAGS); refList = repo.getRefDatabase().getRefs(Constants.R_TAGS);
for (Ref ref : refList.values()) { for (Ref ref : refList.values()) {
tags.add(ref); tags.add(ref);
} }
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e); throw new JGitInternalException(e.getMessage(), e);
} finally {
revWalk.release();
} }
Collections.sort(tags, new Comparator<Ref>() { Collections.sort(tags, new Comparator<Ref>() {
public int compare(Ref o1, Ref o2) { public int compare(Ref o1, Ref o2) {

6
org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java

@ -369,9 +369,11 @@ public class MergeCommand extends GitCommand<MergeResult> {
mergeStatus = MergeStatus.MERGED_NOT_COMMITTED; mergeStatus = MergeStatus.MERGED_NOT_COMMITTED;
} }
if (commit && !squash) { if (commit && !squash) {
newHeadId = new Git(getRepository()).commit() try (Git git = new Git(getRepository())) {
newHeadId = git.commit()
.setReflogComment(refLogMessage.toString()) .setReflogComment(refLogMessage.toString())
.call().getId(); .call().getId();
}
mergeStatus = MergeStatus.MERGED; mergeStatus = MergeStatus.MERGED;
} }
if (commit && squash) { if (commit && squash) {
@ -416,7 +418,7 @@ public class MergeCommand extends GitCommand<MergeResult> {
e), e); e), e);
} finally { } finally {
if (revWalk != null) if (revWalk != null)
revWalk.release(); revWalk.close();
} }
} }

84
org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java

@ -405,13 +405,14 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
.call(); .call();
} catch (StashApplyFailureException e) { } catch (StashApplyFailureException e) {
conflicts = true; conflicts = true;
RevWalk rw = new RevWalk(repo); try (RevWalk rw = new RevWalk(repo)) {
ObjectId stashId = repo.resolve(stash); ObjectId stashId = repo.resolve(stash);
RevCommit commit = rw.parseCommit(stashId); RevCommit commit = rw.parseCommit(stashId);
updateStashRef(commit, commit.getAuthorIdent(), updateStashRef(commit, commit.getAuthorIdent(),
commit.getShortMessage()); commit.getShortMessage());
} }
} }
}
return conflicts; return conflicts;
} }
@ -518,15 +519,16 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
// here we should skip this step in order to avoid // here we should skip this step in order to avoid
// confusing pseudo-changed // confusing pseudo-changed
String ourCommitName = getOurCommitName(); String ourCommitName = getOurCommitName();
CherryPickResult cherryPickResult = new Git(repo).cherryPick() try (Git git = new Git(repo)) {
CherryPickResult cherryPickResult = git.cherryPick()
.include(commitToPick).setOurCommitName(ourCommitName) .include(commitToPick).setOurCommitName(ourCommitName)
.setReflogPrefix(REFLOG_PREFIX).setStrategy(strategy) .setReflogPrefix(REFLOG_PREFIX).setStrategy(strategy)
.call(); .call();
switch (cherryPickResult.getStatus()) { switch (cherryPickResult.getStatus()) {
case FAILED: case FAILED:
if (operation == Operation.BEGIN) if (operation == Operation.BEGIN)
return abort(RebaseResult.failed(cherryPickResult return abort(RebaseResult
.getFailingPaths())); .failed(cherryPickResult.getFailingPaths()));
else else
return stop(commitToPick, Status.STOPPED); return stop(commitToPick, Status.STOPPED);
case CONFLICTING: case CONFLICTING:
@ -535,6 +537,7 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
newHead = cherryPickResult.getNewHead(); newHead = cherryPickResult.getNewHead();
} }
} }
}
return null; return null;
} }
@ -563,12 +566,15 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
// Use the cherry-pick strategy if all non-first parents did not // Use the cherry-pick strategy if all non-first parents did not
// change. This is different from C Git, which always uses the merge // change. This is different from C Git, which always uses the merge
// strategy (see below). // strategy (see below).
try (Git git = new Git(repo)) {
if (otherParentsUnchanged) { if (otherParentsUnchanged) {
boolean isMerge = commitToPick.getParentCount() > 1; boolean isMerge = commitToPick.getParentCount() > 1;
String ourCommitName = getOurCommitName(); String ourCommitName = getOurCommitName();
CherryPickCommand pickCommand = new Git(repo).cherryPick() CherryPickCommand pickCommand = git.cherryPick()
.include(commitToPick).setOurCommitName(ourCommitName) .include(commitToPick)
.setReflogPrefix(REFLOG_PREFIX).setStrategy(strategy); .setOurCommitName(ourCommitName)
.setReflogPrefix(REFLOG_PREFIX)
.setStrategy(strategy);
if (isMerge) { if (isMerge) {
pickCommand.setMainlineParentNumber(1); pickCommand.setMainlineParentNumber(1);
// We write a MERGE_HEAD and later commit explicitly // We write a MERGE_HEAD and later commit explicitly
@ -579,16 +585,17 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
switch (cherryPickResult.getStatus()) { switch (cherryPickResult.getStatus()) {
case FAILED: case FAILED:
if (operation == Operation.BEGIN) if (operation == Operation.BEGIN)
return abort(RebaseResult.failed(cherryPickResult return abort(RebaseResult.failed(
.getFailingPaths())); cherryPickResult.getFailingPaths()));
else else
return stop(commitToPick, Status.STOPPED); return stop(commitToPick, Status.STOPPED);
case CONFLICTING: case CONFLICTING:
return stop(commitToPick, Status.STOPPED); return stop(commitToPick, Status.STOPPED);
case OK: case OK:
if (isMerge) { if (isMerge) {
// Commit the merge (setup above using writeMergeInfo()) // Commit the merge (setup above using
CommitCommand commit = new Git(repo).commit(); // writeMergeInfo())
CommitCommand commit = git.commit();
commit.setAuthor(commitToPick.getAuthorIdent()); commit.setAuthor(commitToPick.getAuthorIdent());
commit.setReflogComment(REFLOG_PREFIX + " " //$NON-NLS-1$ commit.setReflogComment(REFLOG_PREFIX + " " //$NON-NLS-1$
+ commitToPick.getShortMessage()); + commitToPick.getShortMessage());
@ -600,28 +607,29 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
} else { } else {
// Use the merge strategy to redo merges, which had some of // Use the merge strategy to redo merges, which had some of
// their non-first parents rewritten // their non-first parents rewritten
MergeCommand merge = new Git(repo).merge() MergeCommand merge = git.merge()
.setFastForward(MergeCommand.FastForwardMode.NO_FF) .setFastForward(MergeCommand.FastForwardMode.NO_FF)
.setCommit(false); .setCommit(false);
for (int i = 1; i < commitToPick.getParentCount(); i++) for (int i = 1; i < commitToPick.getParentCount(); i++)
merge.include(newParents.get(i)); merge.include(newParents.get(i));
MergeResult mergeResult = merge.call(); MergeResult mergeResult = merge.call();
if (mergeResult.getMergeStatus().isSuccessful()) { if (mergeResult.getMergeStatus().isSuccessful()) {
CommitCommand commit = new Git(repo).commit(); CommitCommand commit = git.commit();
commit.setAuthor(commitToPick.getAuthorIdent()); commit.setAuthor(commitToPick.getAuthorIdent());
commit.setMessage(commitToPick.getFullMessage()); commit.setMessage(commitToPick.getFullMessage());
commit.setReflogComment(REFLOG_PREFIX + " " //$NON-NLS-1$ commit.setReflogComment(REFLOG_PREFIX + " " //$NON-NLS-1$
+ commitToPick.getShortMessage()); + commitToPick.getShortMessage());
newHead = commit.call(); newHead = commit.call();
} else { } else {
if (operation == Operation.BEGIN if (operation == Operation.BEGIN && mergeResult
&& mergeResult.getMergeStatus() == MergeResult.MergeStatus.FAILED) .getMergeStatus() == MergeResult.MergeStatus.FAILED)
return abort(RebaseResult.failed(mergeResult return abort(RebaseResult
.getFailingPaths())); .failed(mergeResult.getFailingPaths()));
return stop(commitToPick, Status.STOPPED); return stop(commitToPick, Status.STOPPED);
} }
} }
} }
}
return null; return null;
} }
@ -758,15 +766,15 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
String commitMessage = rebaseState String commitMessage = rebaseState
.readFile(MESSAGE_SQUASH); .readFile(MESSAGE_SQUASH);
if (nextStep == null try (Git git = new Git(repo)) {
|| ((nextStep.getAction() != Action.FIXUP) && (nextStep if (nextStep == null || ((nextStep.getAction() != Action.FIXUP)
.getAction() != Action.SQUASH))) { && (nextStep.getAction() != Action.SQUASH))) {
// this is the last step in this sequence // this is the last step in this sequence
if (sequenceContainsSquash) { if (sequenceContainsSquash) {
commitMessage = interactiveHandler commitMessage = interactiveHandler
.modifyCommitMessage(commitMessage); .modifyCommitMessage(commitMessage);
} }
retNewHead = new Git(repo).commit() retNewHead = git.commit()
.setMessage(stripCommentLines(commitMessage)) .setMessage(stripCommentLines(commitMessage))
.setAmend(true).setNoVerify(true).call(); .setAmend(true).setNoVerify(true).call();
rebaseState.getFile(MESSAGE_SQUASH).delete(); rebaseState.getFile(MESSAGE_SQUASH).delete();
@ -774,9 +782,10 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
} else { } else {
// Next step is either Squash or Fixup // Next step is either Squash or Fixup
retNewHead = new Git(repo).commit().setMessage(commitMessage) retNewHead = git.commit().setMessage(commitMessage)
.setAmend(true).setNoVerify(true).call(); .setAmend(true).setNoVerify(true).call();
} }
}
return retNewHead; return retNewHead;
} }
@ -917,11 +926,11 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
} finally { } finally {
dc.unlock(); dc.unlock();
} }
RevWalk rw = new RevWalk(repo); try (RevWalk rw = new RevWalk(repo)) {
RevCommit commit = rw.parseCommit(repo.resolve(Constants.HEAD)); RevCommit commit = rw.parseCommit(repo.resolve(Constants.HEAD));
rw.release();
return commit; return commit;
} }
}
/** /**
* @return the commit if we had to do a commit, otherwise null * @return the commit if we had to do a commit, otherwise null
@ -936,7 +945,8 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
throw new UnmergedPathsException(); throw new UnmergedPathsException();
// determine whether we need to commit // determine whether we need to commit
TreeWalk treeWalk = new TreeWalk(repo); boolean needsCommit;
try (TreeWalk treeWalk = new TreeWalk(repo)) {
treeWalk.reset(); treeWalk.reset();
treeWalk.setRecursive(true); treeWalk.setRecursive(true);
treeWalk.addTree(new DirCacheIterator(dc)); treeWalk.addTree(new DirCacheIterator(dc));
@ -949,15 +959,16 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
treeWalk.setFilter(TreeFilter.ANY_DIFF); treeWalk.setFilter(TreeFilter.ANY_DIFF);
boolean needsCommit = treeWalk.next(); needsCommit = treeWalk.next();
treeWalk.release(); }
if (needsCommit) { if (needsCommit) {
CommitCommand commit = new Git(repo).commit(); try (Git git = new Git(repo)) {
CommitCommand commit = git.commit();
commit.setMessage(rebaseState.readFile(MESSAGE)); commit.setMessage(rebaseState.readFile(MESSAGE));
commit.setAuthor(parseAuthor()); commit.setAuthor(parseAuthor());
return commit.call(); return commit.call();
} }
}
return null; return null;
} }
@ -979,9 +990,10 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
rebaseState.createFile(AUTHOR_SCRIPT, authorScript); rebaseState.createFile(AUTHOR_SCRIPT, authorScript);
rebaseState.createFile(MESSAGE, commitToPick.getFullMessage()); rebaseState.createFile(MESSAGE, commitToPick.getFullMessage());
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
DiffFormatter df = new DiffFormatter(bos); try (DiffFormatter df = new DiffFormatter(bos)) {
df.setRepository(repo); df.setRepository(repo);
df.format(commitToPick.getParent(0), commitToPick); df.format(commitToPick.getParent(0), commitToPick);
}
rebaseState.createFile(PATCH, new String(bos.toByteArray(), rebaseState.createFile(PATCH, new String(bos.toByteArray(),
Constants.CHARACTER_ENCODING)); Constants.CHARACTER_ENCODING));
rebaseState.createFile(STOPPED_SHA, rebaseState.createFile(STOPPED_SHA,
@ -1124,9 +1136,11 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
private List<RevCommit> calculatePickList(RevCommit headCommit) private List<RevCommit> calculatePickList(RevCommit headCommit)
throws GitAPIException, NoHeadException, IOException { throws GitAPIException, NoHeadException, IOException {
LogCommand cmd = new Git(repo).log().addRange(upstreamCommit, Iterable<RevCommit> commitsToUse;
headCommit); try (Git git = new Git(repo)) {
Iterable<RevCommit> commitsToUse = cmd.call(); LogCommand cmd = git.log().addRange(upstreamCommit, headCommit);
commitsToUse = cmd.call();
}
List<RevCommit> cherryPickList = new ArrayList<RevCommit>(); List<RevCommit> cherryPickList = new ArrayList<RevCommit>();
for (RevCommit commit : commitsToUse) { for (RevCommit commit : commitsToUse) {
if (preserveMerges || commit.getParentCount() == 1) if (preserveMerges || commit.getParentCount() == 1)
@ -1312,7 +1326,7 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
} }
dco.setFailOnConflict(false); dco.setFailOnConflict(false);
dco.checkout(); dco.checkout();
walk.release(); walk.close();
} finally { } finally {
monitor.endTask(); monitor.endTask();
} }
@ -1387,7 +1401,7 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
throw new IOException("Could not rewind to upstream commit"); throw new IOException("Could not rewind to upstream commit");
} }
} finally { } finally {
walk.release(); walk.close();
monitor.endTask(); monitor.endTask();
} }
return true; return true;

8
org.eclipse.jgit/src/org/eclipse/jgit/api/RemoveNoteCommand.java

@ -81,11 +81,10 @@ public class RemoveNoteCommand extends GitCommand<Note> {
public Note call() throws GitAPIException { public Note call() throws GitAPIException {
checkCallable(); checkCallable();
RevWalk walk = new RevWalk(repo); try (RevWalk walk = new RevWalk(repo);
ObjectInserter inserter = repo.newObjectInserter(); ObjectInserter inserter = repo.newObjectInserter()) {
NoteMap map = NoteMap.newEmptyMap(); NoteMap map = NoteMap.newEmptyMap();
RevCommit notesCommit = null; RevCommit notesCommit = null;
try {
Ref ref = repo.getRef(notesRef); Ref ref = repo.getRef(notesRef);
// if we have a notes ref, use it // if we have a notes ref, use it
if (ref != null) { if (ref != null) {
@ -98,9 +97,6 @@ public class RemoveNoteCommand extends GitCommand<Note> {
return map.getNote(id); return map.getNote(id);
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e); throw new JGitInternalException(e.getMessage(), e);
} finally {
inserter.release();
walk.release();
} }
} }

18
org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java

@ -234,17 +234,12 @@ public class ResetCommand extends GitCommand<Ref> {
} }
private RevCommit parseCommit(final ObjectId commitId) { private RevCommit parseCommit(final ObjectId commitId) {
RevCommit commit; try (RevWalk rw = new RevWalk(repo)) {
RevWalk rw = new RevWalk(repo); return rw.parseCommit(commitId);
try {
commit = rw.parseCommit(commitId);
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(MessageFormat.format( throw new JGitInternalException(MessageFormat.format(
JGitText.get().cannotReadCommit, commitId.toString()), e); JGitText.get().cannotReadCommit, commitId.toString()), e);
} finally {
rw.release();
} }
return commit;
} }
private ObjectId resolveRefToCommitId() { private ObjectId resolveRefToCommitId() {
@ -305,11 +300,10 @@ public class ResetCommand extends GitCommand<Ref> {
private void resetIndexForPaths(ObjectId commitTree) { private void resetIndexForPaths(ObjectId commitTree) {
DirCache dc = null; DirCache dc = null;
try { try (final TreeWalk tw = new TreeWalk(repo)) {
dc = repo.lockDirCache(); dc = repo.lockDirCache();
DirCacheBuilder builder = dc.builder(); DirCacheBuilder builder = dc.builder();
final TreeWalk tw = new TreeWalk(repo);
tw.addTree(new DirCacheBuildIterator(builder)); tw.addTree(new DirCacheBuildIterator(builder));
if (commitTree != null) if (commitTree != null)
tw.addTree(commitTree); tw.addTree(commitTree);
@ -342,11 +336,9 @@ public class ResetCommand extends GitCommand<Ref> {
private void resetIndex(ObjectId commitTree) throws IOException { private void resetIndex(ObjectId commitTree) throws IOException {
DirCache dc = repo.lockDirCache(); DirCache dc = repo.lockDirCache();
TreeWalk walk = null; try (TreeWalk walk = new TreeWalk(repo)) {
try {
DirCacheBuilder builder = dc.builder(); DirCacheBuilder builder = dc.builder();
walk = new TreeWalk(repo);
if (commitTree != null) if (commitTree != null)
walk.addTree(commitTree); walk.addTree(commitTree);
else else
@ -380,8 +372,6 @@ public class ResetCommand extends GitCommand<Ref> {
builder.commit(); builder.commit();
} finally { } finally {
dc.unlock(); dc.unlock();
if (walk != null)
walk.release();
} }
} }

13
org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java

@ -126,8 +126,7 @@ public class RevertCommand extends GitCommand<RevCommit> {
RevCommit newHead = null; RevCommit newHead = null;
checkCallable(); checkCallable();
RevWalk revWalk = new RevWalk(repo); try (RevWalk revWalk = new RevWalk(repo)) {
try {
// get the head commit // get the head commit
Ref headRef = repo.getRef(Constants.HEAD); Ref headRef = repo.getRef(Constants.HEAD);
@ -182,9 +181,11 @@ public class RevertCommand extends GitCommand<RevCommit> {
merger.getResultTreeId()); merger.getResultTreeId());
dco.setFailOnConflict(true); dco.setFailOnConflict(true);
dco.checkout(); dco.checkout();
newHead = new Git(getRepository()).commit() try (Git git = new Git(getRepository())) {
.setMessage(newMessage) newHead = git.commit().setMessage(newMessage)
.setReflogComment("revert: " + shortMessage).call(); //$NON-NLS-1$ .setReflogComment("revert: " + shortMessage) //$NON-NLS-1$
.call();
}
revertedRefs.add(src); revertedRefs.add(src);
headCommit = newHead; headCommit = newHead;
} else { } else {
@ -220,8 +221,6 @@ public class RevertCommand extends GitCommand<RevCommit> {
MessageFormat.format( MessageFormat.format(
JGitText.get().exceptionCaughtDuringExecutionOfRevertCommand, JGitText.get().exceptionCaughtDuringExecutionOfRevertCommand,
e), e); e), e);
} finally {
revWalk.release();
} }
return newHead; return newHead;
} }

3
org.eclipse.jgit/src/org/eclipse/jgit/api/RmCommand.java

@ -144,10 +144,9 @@ public class RmCommand extends GitCommand<DirCache> {
checkCallable(); checkCallable();
DirCache dc = null; DirCache dc = null;
try { try (final TreeWalk tw = new TreeWalk(repo)) {
dc = repo.lockDirCache(); dc = repo.lockDirCache();
DirCacheBuilder builder = dc.builder(); DirCacheBuilder builder = dc.builder();
final TreeWalk tw = new TreeWalk(repo);
tw.reset(); // drop the first empty tree, which we do not need here tw.reset(); // drop the first empty tree, which we do not need here
tw.setRecursive(true); tw.setRecursive(true);
tw.setFilter(PathFilterGroup.createFromStrings(filepatterns)); tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));

5
org.eclipse.jgit/src/org/eclipse/jgit/api/ShowNoteCommand.java

@ -76,10 +76,9 @@ public class ShowNoteCommand extends GitCommand<Note> {
public Note call() throws GitAPIException { public Note call() throws GitAPIException {
checkCallable(); checkCallable();
RevWalk walk = new RevWalk(repo);
NoteMap map = NoteMap.newEmptyMap(); NoteMap map = NoteMap.newEmptyMap();
RevCommit notesCommit = null; RevCommit notesCommit = null;
try { try (RevWalk walk = new RevWalk(repo)) {
Ref ref = repo.getRef(notesRef); Ref ref = repo.getRef(notesRef);
// if we have a notes ref, use it // if we have a notes ref, use it
if (ref != null) { if (ref != null) {
@ -89,8 +88,6 @@ public class ShowNoteCommand extends GitCommand<Note> {
return map.getNote(id); return map.getNote(id);
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e); throw new JGitInternalException(e.getMessage(), e);
} finally {
walk.release();
} }
} }

19
org.eclipse.jgit/src/org/eclipse/jgit/api/StashApplyCommand.java

@ -166,9 +166,8 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
JGitText.get().stashApplyOnUnsafeRepository, JGitText.get().stashApplyOnUnsafeRepository,
repo.getRepositoryState())); repo.getRepositoryState()));
ObjectReader reader = repo.newObjectReader(); try (ObjectReader reader = repo.newObjectReader();
try { RevWalk revWalk = new RevWalk(reader)) {
RevWalk revWalk = new RevWalk(reader);
ObjectId headCommit = repo.resolve(Constants.HEAD); ObjectId headCommit = repo.resolve(Constants.HEAD);
if (headCommit == null) if (headCommit == null)
@ -250,8 +249,6 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
throw e; throw e;
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(JGitText.get().stashApplyFailed, 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 { private void resetIndex(RevTree tree) throws IOException {
DirCache dc = repo.lockDirCache(); DirCache dc = repo.lockDirCache();
TreeWalk walk = null; try (TreeWalk walk = new TreeWalk(repo)) {
try {
DirCacheBuilder builder = dc.builder(); DirCacheBuilder builder = dc.builder();
walk = new TreeWalk(repo);
walk.addTree(tree); walk.addTree(tree);
walk.addTree(new DirCacheIterator(dc)); walk.addTree(new DirCacheIterator(dc));
walk.setRecursive(true); walk.setRecursive(true);
@ -321,15 +316,13 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
builder.commit(); builder.commit();
} finally { } finally {
dc.unlock(); dc.unlock();
if (walk != null)
walk.release();
} }
} }
private void resetUntracked(RevTree tree) throws CheckoutConflictException, private void resetUntracked(RevTree tree) throws CheckoutConflictException,
IOException { IOException {
TreeWalk walk = new TreeWalk(repo); // maybe NameConflictTreeWalk; // TODO maybe NameConflictTreeWalk ?
try { try (TreeWalk walk = new TreeWalk(repo)) {
walk.addTree(tree); walk.addTree(tree);
walk.addTree(new FileTreeIterator(repo)); walk.addTree(new FileTreeIterator(repo));
walk.setRecursive(true); walk.setRecursive(true);
@ -359,8 +352,6 @@ public class StashApplyCommand extends GitCommand<ObjectId> {
checkoutPath(entry, reader); checkoutPath(entry, reader);
} }
} finally {
walk.release();
} }
} }

15
org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java

@ -187,9 +187,10 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
private RevCommit parseCommit(final ObjectReader reader, private RevCommit parseCommit(final ObjectReader reader,
final ObjectId headId) throws IOException { final ObjectId headId) throws IOException {
final RevWalk walk = new RevWalk(reader); try (final RevWalk walk = new RevWalk(reader)) {
return walk.parseCommit(headId); return walk.parseCommit(headId);
} }
}
private CommitBuilder createBuilder() { private CommitBuilder createBuilder() {
CommitBuilder builder = new CommitBuilder(); CommitBuilder builder = new CommitBuilder();
@ -239,14 +240,13 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
checkCallable(); checkCallable();
Ref head = getHead(); Ref head = getHead();
ObjectReader reader = repo.newObjectReader(); try (ObjectReader reader = repo.newObjectReader()) {
try {
RevCommit headCommit = parseCommit(reader, head.getObjectId()); RevCommit headCommit = parseCommit(reader, head.getObjectId());
DirCache cache = repo.lockDirCache(); DirCache cache = repo.lockDirCache();
ObjectInserter inserter = repo.newObjectInserter();
ObjectId commitId; ObjectId commitId;
try { try (ObjectInserter inserter = repo.newObjectInserter();
TreeWalk treeWalk = new TreeWalk(reader); TreeWalk treeWalk = new TreeWalk(reader)) {
treeWalk.setRecursive(true); treeWalk.setRecursive(true);
treeWalk.addTree(headCommit.getTree()); treeWalk.addTree(headCommit.getTree());
treeWalk.addTree(new DirCacheIterator(cache)); treeWalk.addTree(new DirCacheIterator(cache));
@ -380,7 +380,6 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
} }
} finally { } finally {
inserter.release();
cache.unlock(); cache.unlock();
} }
@ -391,8 +390,6 @@ public class StashCreateCommand extends GitCommand<RevCommit> {
return parseCommit(reader, commitId); return parseCommit(reader, commitId);
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException(JGitText.get().stashFailed, e); throw new JGitInternalException(JGitText.get().stashFailed, e);
} finally {
reader.release();
} }
} }
} }

6
org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleUpdateCommand.java

@ -143,8 +143,7 @@ public class SubmoduleUpdateCommand extends
RefNotFoundException, GitAPIException { RefNotFoundException, GitAPIException {
checkCallable(); checkCallable();
try { try (SubmoduleWalk generator = SubmoduleWalk.forIndex(repo)) {
SubmoduleWalk generator = SubmoduleWalk.forIndex(repo);
if (!paths.isEmpty()) if (!paths.isEmpty())
generator.setFilter(PathFilterGroup.createFromStrings(paths)); generator.setFilter(PathFilterGroup.createFromStrings(paths));
List<String> updated = new ArrayList<String>(); List<String> updated = new ArrayList<String>();
@ -171,8 +170,7 @@ public class SubmoduleUpdateCommand extends
submoduleRepo = clone.call().getRepository(); submoduleRepo = clone.call().getRepository();
} }
try { try (RevWalk walk = new RevWalk(submoduleRepo)) {
RevWalk walk = new RevWalk(submoduleRepo);
RevCommit commit = walk RevCommit commit = walk
.parseCommit(generator.getObjectId()); .parseCommit(generator.getObjectId());

10
org.eclipse.jgit/src/org/eclipse/jgit/api/TagCommand.java

@ -128,8 +128,7 @@ public class TagCommand extends GitCommand<Ref> {
RepositoryState state = repo.getRepositoryState(); RepositoryState state = repo.getRepositoryState();
processOptions(state); processOptions(state);
RevWalk revWalk = new RevWalk(repo); try (RevWalk revWalk = new RevWalk(repo)) {
try {
// if no id is set, we should attempt to use HEAD // if no id is set, we should attempt to use HEAD
if (id == null) { if (id == null) {
ObjectId objectId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$ ObjectId objectId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$
@ -157,24 +156,19 @@ public class TagCommand extends GitCommand<Ref> {
newTag.setObjectId(id); newTag.setObjectId(id);
// write the tag object // write the tag object
ObjectInserter inserter = repo.newObjectInserter(); try (ObjectInserter inserter = repo.newObjectInserter()) {
try {
ObjectId tagId = inserter.insert(newTag); ObjectId tagId = inserter.insert(newTag);
inserter.flush(); inserter.flush();
String tag = newTag.getTag(); String tag = newTag.getTag();
return updateTagRef(tagId, revWalk, tag, newTag.toString()); return updateTagRef(tagId, revWalk, tag, newTag.toString());
} finally {
inserter.release();
} }
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException( throw new JGitInternalException(
JGitText.get().exceptionCaughtDuringExecutionOfTagCommand, JGitText.get().exceptionCaughtDuringExecutionOfTagCommand,
e); e);
} finally {
revWalk.release();
} }
} }

6
org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java

@ -172,7 +172,7 @@ public class BlameGenerator implements AutoCloseable {
throw new IllegalStateException(); throw new IllegalStateException();
if (revPool != null) if (revPool != null)
revPool.release(); revPool.close();
if (reverse) if (reverse)
revPool = new ReverseWalk(getRepository()); revPool = new ReverseWalk(getRepository());
@ -450,7 +450,7 @@ public class BlameGenerator implements AutoCloseable {
r.computeAll(); r.computeAll();
return r; return r;
} finally { } finally {
release(); close();
} }
} }
@ -513,7 +513,7 @@ public class BlameGenerator implements AutoCloseable {
} }
private boolean done() { private boolean done() {
release(); close();
return false; return false;
} }

2
org.eclipse.jgit/src/org/eclipse/jgit/diff/RenameDetector.java

@ -323,7 +323,7 @@ public class RenameDetector {
try { try {
return compute(objectReader, pm); return compute(objectReader, pm);
} finally { } finally {
objectReader.release(); objectReader.close();
} }
} }
return Collections.unmodifiableList(entries); return Collections.unmodifiableList(entries);

15
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java vendored

@ -403,8 +403,7 @@ public class DirCacheCheckout {
MissingObjectException, IncorrectObjectTypeException, MissingObjectException, IncorrectObjectTypeException,
CheckoutConflictException, IndexWriteException { CheckoutConflictException, IndexWriteException {
toBeDeleted.clear(); toBeDeleted.clear();
ObjectReader objectReader = repo.getObjectDatabase().newReader(); try (ObjectReader objectReader = repo.getObjectDatabase().newReader()) {
try {
if (headCommitTree != null) if (headCommitTree != null)
preScanTwoTrees(); preScanTwoTrees();
else else
@ -454,8 +453,6 @@ public class DirCacheCheckout {
// commit the index builder - a new index is persisted // commit the index builder - a new index is persisted
if (!builder.commit()) if (!builder.commit())
throw new IndexWriteException(); throw new IndexWriteException();
} finally {
objectReader.release();
} }
return toBeDeleted.size() == 0; return toBeDeleted.size() == 0;
} }
@ -1056,8 +1053,7 @@ public class DirCacheCheckout {
*/ */
private boolean isModifiedSubtree_IndexWorkingtree(String path) private boolean isModifiedSubtree_IndexWorkingtree(String path)
throws CorruptObjectException, IOException { throws CorruptObjectException, IOException {
NameConflictTreeWalk tw = new NameConflictTreeWalk(repo); try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
try {
tw.addTree(new DirCacheIterator(dc)); tw.addTree(new DirCacheIterator(dc));
tw.addTree(new FileTreeIterator(repo)); tw.addTree(new FileTreeIterator(repo));
tw.setRecursive(true); tw.setRecursive(true);
@ -1075,8 +1071,6 @@ public class DirCacheCheckout {
} }
} }
return false; return false;
} finally {
tw.release();
} }
} }
@ -1105,8 +1099,7 @@ public class DirCacheCheckout {
*/ */
private boolean isModifiedSubtree_IndexTree(String path, ObjectId tree) private boolean isModifiedSubtree_IndexTree(String path, ObjectId tree)
throws CorruptObjectException, IOException { throws CorruptObjectException, IOException {
NameConflictTreeWalk tw = new NameConflictTreeWalk(repo); try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
try {
tw.addTree(new DirCacheIterator(dc)); tw.addTree(new DirCacheIterator(dc));
tw.addTree(tree); tw.addTree(tree);
tw.setRecursive(true); tw.setRecursive(true);
@ -1124,8 +1117,6 @@ public class DirCacheCheckout {
return true; return true;
} }
return false; return false;
} finally {
tw.release();
} }
} }

20
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java

@ -214,15 +214,10 @@ public class RepoCommand extends GitCommand<RevCommit> {
*/ */
protected byte[] readFileFromRepo(Repository repo, protected byte[] readFileFromRepo(Repository repo,
String ref, String path) throws GitAPIException, IOException { String ref, String path) throws GitAPIException, IOException {
ObjectReader reader = repo.newObjectReader(); try (ObjectReader reader = repo.newObjectReader()) {
byte[] result;
try {
ObjectId oid = repo.resolve(ref + ":" + path); //$NON-NLS-1$ ObjectId oid = repo.resolve(ref + ":" + path); //$NON-NLS-1$
result = reader.open(oid).getBytes(Integer.MAX_VALUE); return reader.open(oid).getBytes(Integer.MAX_VALUE);
} finally {
reader.release();
} }
return result;
} }
} }
@ -748,8 +743,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
DirCache index = DirCache.newInCore(); DirCache index = DirCache.newInCore();
DirCacheBuilder builder = index.builder(); DirCacheBuilder builder = index.builder();
ObjectInserter inserter = repo.newObjectInserter(); ObjectInserter inserter = repo.newObjectInserter();
RevWalk rw = new RevWalk(repo); try (RevWalk rw = new RevWalk(repo)) {
try {
Config cfg = new Config(); Config cfg = new Config();
for (Project proj : bareProjects) { for (Project proj : bareProjects) {
String name = proj.path; String name = proj.path;
@ -831,8 +825,6 @@ public class RepoCommand extends GitCommand<RevCommit> {
return rw.parseCommit(commitId); return rw.parseCommit(commitId);
} catch (IOException e) { } catch (IOException e) {
throw new ManifestErrorException(e); throw new ManifestErrorException(e);
} finally {
rw.release();
} }
} else { } else {
return git return git
@ -859,8 +851,10 @@ public class RepoCommand extends GitCommand<RevCommit> {
try { try {
Repository subRepo = add.call(); Repository subRepo = add.call();
if (revision != null) { if (revision != null) {
Git sub = new Git(subRepo); try (Git sub = new Git(subRepo)) {
sub.checkout().setName(findRef(revision, subRepo)).call(); sub.checkout().setName(findRef(revision, subRepo))
.call();
}
subRepo.close(); subRepo.close();
git.add().addFilepattern(name).call(); git.add().addFilepattern(name).call();
} }

2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java

@ -230,7 +230,7 @@ public class DfsGarbageCollector {
objdb.rollbackPack(newPackDesc); objdb.rollbackPack(newPackDesc);
} }
} finally { } finally {
ctx.release(); ctx.close();
} }
} }

6
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java

@ -601,7 +601,7 @@ public class DfsInserter extends ObjectInserter {
@Override @Override
public void release() { 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. // The newly created pack is registered in the cache.
return ctx.open(id, type).openStream(); return ctx.open(id, type).openStream();
} finally { } finally {
ctx.release(); ctx.close();
} }
} }
@ -642,7 +642,7 @@ public class DfsInserter extends ObjectInserter {
new ReadBackStream(pos), inf, bufsz), bufsz)) { new ReadBackStream(pos), inf, bufsz), bufsz)) {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
ctx.release(); ctx.close();
super.close(); super.close();
} }
}; };

13
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackCompactor.java

@ -179,11 +179,8 @@ public class DfsPackCompactor {
*/ */
public DfsPackCompactor exclude(DfsPackFile pack) throws IOException { public DfsPackCompactor exclude(DfsPackFile pack) throws IOException {
final PackIndex idx; final PackIndex idx;
DfsReader ctx = (DfsReader) repo.newObjectReader(); try (DfsReader ctx = (DfsReader) repo.newObjectReader()) {
try {
idx = pack.getPackIndex(ctx); idx = pack.getPackIndex(ctx);
} finally {
ctx.release();
} }
return exclude(new PackWriter.ObjectIdSet() { return exclude(new PackWriter.ObjectIdSet() {
public boolean contains(AnyObjectId id) { public boolean contains(AnyObjectId id) {
@ -206,8 +203,7 @@ public class DfsPackCompactor {
pm = NullProgressMonitor.INSTANCE; pm = NullProgressMonitor.INSTANCE;
DfsObjDatabase objdb = repo.getObjectDatabase(); DfsObjDatabase objdb = repo.getObjectDatabase();
DfsReader ctx = (DfsReader) objdb.newReader(); try (DfsReader ctx = (DfsReader) objdb.newReader()) {
try {
PackConfig pc = new PackConfig(repo); PackConfig pc = new PackConfig(repo);
pc.setIndexVersion(2); pc.setIndexVersion(2);
pc.setDeltaCompress(false); pc.setDeltaCompress(false);
@ -236,7 +232,7 @@ public class DfsPackCompactor {
writeIndex(objdb, pack, pw); writeIndex(objdb, pack, pw);
PackWriter.Statistics stats = pw.getStatistics(); PackWriter.Statistics stats = pw.getStatistics();
pw.release(); pw.close();
pw = null; pw = null;
pack.setPackStats(stats); pack.setPackStats(stats);
@ -250,11 +246,10 @@ public class DfsPackCompactor {
} }
} finally { } finally {
if (pw != null) if (pw != null)
pw.release(); pw.close();
} }
} finally { } finally {
rw = null; rw = null;
ctx.release();
} }
} }

5
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsRefDatabase.java

@ -183,8 +183,7 @@ public abstract class DfsRefDatabase extends RefDatabase {
private Ref doPeel(final Ref leaf) throws MissingObjectException, private Ref doPeel(final Ref leaf) throws MissingObjectException,
IOException { IOException {
RevWalk rw = new RevWalk(repository); try (RevWalk rw = new RevWalk(repository)) {
try {
RevObject obj = rw.parseAny(leaf.getObjectId()); RevObject obj = rw.parseAny(leaf.getObjectId());
if (obj instanceof RevTag) { if (obj instanceof RevTag) {
return new ObjectIdRef.PeeledTag( return new ObjectIdRef.PeeledTag(
@ -198,8 +197,6 @@ public abstract class DfsRefDatabase extends RefDatabase {
leaf.getName(), leaf.getName(),
leaf.getObjectId()); leaf.getObjectId());
} }
} finally {
rw.release();
} }
} }

4
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/LargePackedWholeObject.java

@ -112,8 +112,10 @@ final class LargePackedWholeObject extends ObjectLoader {
ObjectId obj = pack.getReverseIdx(ctx).findObject(objectOffset); ObjectId obj = pack.getReverseIdx(ctx).findObject(objectOffset);
return ctx.open(obj, type).openStream(); return ctx.open(obj, type).openStream();
} finally { } finally {
ctx.release(); ctx.close();
} }
} finally {
ctx.close();
} }
// Align buffer to inflater size, at a larger than default block. // Align buffer to inflater size, at a larger than default block.

2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/PackInputStream.java

@ -80,6 +80,6 @@ final class PackInputStream extends InputStream {
@Override @Override
public void close() { public void close() {
ctx.release(); ctx.close();
} }
} }

5
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java

@ -477,8 +477,7 @@ public class RefDirectory extends RefDatabase {
private ObjectIdRef doPeel(final Ref leaf) throws MissingObjectException, private ObjectIdRef doPeel(final Ref leaf) throws MissingObjectException,
IOException { IOException {
RevWalk rw = new RevWalk(getRepository()); try (RevWalk rw = new RevWalk(getRepository())) {
try {
RevObject obj = rw.parseAny(leaf.getObjectId()); RevObject obj = rw.parseAny(leaf.getObjectId());
if (obj instanceof RevTag) { if (obj instanceof RevTag) {
return new ObjectIdRef.PeeledTag(leaf.getStorage(), leaf return new ObjectIdRef.PeeledTag(leaf.getStorage(), leaf
@ -487,8 +486,6 @@ public class RefDirectory extends RefDatabase {
return new ObjectIdRef.PeeledNonTag(leaf.getStorage(), leaf return new ObjectIdRef.PeeledNonTag(leaf.getStorage(), leaf
.getName(), leaf.getObjectId()); .getName(), leaf.getObjectId());
} }
} finally {
rw.release();
} }
} }

4
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectoryRename.java

@ -96,8 +96,7 @@ class RefDirectoryRename extends RefRename {
objId = source.getOldObjectId(); objId = source.getOldObjectId();
updateHEAD = needToUpdateHEAD(); updateHEAD = needToUpdateHEAD();
tmp = refdb.newTemporaryUpdate(); tmp = refdb.newTemporaryUpdate();
final RevWalk rw = new RevWalk(refdb.getRepository()); try (final RevWalk rw = new RevWalk(refdb.getRepository())) {
try {
// First backup the source so its never unreachable. // First backup the source so its never unreachable.
tmp.setNewObjectId(objId); tmp.setNewObjectId(objId);
tmp.setForceUpdate(true); tmp.setForceUpdate(true);
@ -178,7 +177,6 @@ class RefDirectoryRename extends RefRename {
} catch (IOException err) { } catch (IOException err) {
FileUtils.delete(refdb.fileFor(tmp.getName())); FileUtils.delete(refdb.fileFor(tmp.getName()));
} }
rw.release();
} }
} }

2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/DeltaTask.java

@ -288,7 +288,7 @@ final class DeltaTask implements Callable<Object> {
runWindow(w); runWindow(w);
} finally { } finally {
block.pm.endWorker(); block.pm.endWorker();
or.release(); or.close();
or = null; or = null;
} }
return null; return null;

2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackWriter.java

@ -987,7 +987,7 @@ public class PackWriter implements AutoCloseable {
} }
stats.totalBytes = out.length(); stats.totalBytes = out.length();
reader.release(); reader.close();
endPhase(writeMonitor); endPhase(writeMonitor);
} }

18
org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexDiff.java

@ -400,7 +400,7 @@ public class IndexDiff {
throws IOException { throws IOException {
dirCache = repository.readDirCache(); dirCache = repository.readDirCache();
TreeWalk treeWalk = new TreeWalk(repository); try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.setRecursive(true); treeWalk.setRecursive(true);
// add the trees (tree, dirchache, workdir) // add the trees (tree, dirchache, workdir)
if (tree != null) if (tree != null)
@ -434,8 +434,8 @@ public class IndexDiff {
AbstractTreeIterator.class); AbstractTreeIterator.class);
DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX, DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX,
DirCacheIterator.class); DirCacheIterator.class);
WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR, WorkingTreeIterator workingTreeIterator = treeWalk
WorkingTreeIterator.class); .getTree(WORKDIR, WorkingTreeIterator.class);
if (dirCacheIterator != null) { if (dirCacheIterator != null) {
final DirCacheEntry dirCacheEntry = dirCacheIterator final DirCacheEntry dirCacheEntry = dirCacheIterator
@ -453,8 +453,9 @@ public class IndexDiff {
if (treeIterator != null) { if (treeIterator != null) {
if (dirCacheIterator != null) { if (dirCacheIterator != null) {
if (!treeIterator.idEqual(dirCacheIterator) if (!treeIterator.idEqual(dirCacheIterator)
|| treeIterator.getEntryRawMode() || treeIterator
!= dirCacheIterator.getEntryRawMode()) { .getEntryRawMode() != dirCacheIterator
.getEntryRawMode()) {
// in repo, in index, content diff => changed // in repo, in index, content diff => changed
if (!isEntryGitLink(treeIterator) if (!isEntryGitLink(treeIterator)
|| !isEntryGitLink(dirCacheIterator) || !isEntryGitLink(dirCacheIterator)
@ -495,8 +496,10 @@ public class IndexDiff {
dirCacheIterator.getDirCacheEntry(), true, dirCacheIterator.getDirCacheEntry(), true,
treeWalk.getObjectReader())) { treeWalk.getObjectReader())) {
// in index, in workdir, content differs => modified // in index, in workdir, content differs => modified
if (!isEntryGitLink(dirCacheIterator) || !isEntryGitLink(workingTreeIterator) if (!isEntryGitLink(dirCacheIterator)
|| (ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL && ignoreSubmoduleMode != IgnoreSubmoduleMode.DIRTY)) || !isEntryGitLink(workingTreeIterator)
|| (ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL
&& ignoreSubmoduleMode != IgnoreSubmoduleMode.DIRTY))
modified.add(treeWalk.getPathString()); modified.add(treeWalk.getPathString());
} }
} }
@ -513,6 +516,7 @@ public class IndexDiff {
} }
} }
} }
}
if (ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL) { if (ignoreSubmoduleMode != IgnoreSubmoduleMode.ALL) {
IgnoreSubmoduleMode localIgnoreSubmoduleMode = ignoreSubmoduleMode; IgnoreSubmoduleMode localIgnoreSubmoduleMode = ignoreSubmoduleMode;

10
org.eclipse.jgit/src/org/eclipse/jgit/lib/RefUpdate.java

@ -460,11 +460,8 @@ public abstract class RefUpdate {
* an unexpected IO error occurred while writing changes. * an unexpected IO error occurred while writing changes.
*/ */
public Result update() throws IOException { public Result update() throws IOException {
RevWalk rw = new RevWalk(getRepository()); try (RevWalk rw = new RevWalk(getRepository())) {
try {
return update(rw); return update(rw);
} finally {
rw.release();
} }
} }
@ -510,11 +507,8 @@ public abstract class RefUpdate {
* @throws IOException * @throws IOException
*/ */
public Result delete() throws IOException { public Result delete() throws IOException {
RevWalk rw = new RevWalk(getRepository()); try (RevWalk rw = new RevWalk(getRepository())) {
try {
return delete(rw); return delete(rw);
} finally {
rw.release();
} }
} }

15
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java

@ -379,8 +379,7 @@ public abstract class Repository implements AutoCloseable {
public ObjectId resolve(final String revstr) public ObjectId resolve(final String revstr)
throws AmbiguousObjectException, IncorrectObjectTypeException, throws AmbiguousObjectException, IncorrectObjectTypeException,
RevisionSyntaxException, IOException { RevisionSyntaxException, IOException {
RevWalk rw = new RevWalk(this); try (RevWalk rw = new RevWalk(this)) {
try {
Object resolved = resolve(rw, revstr); Object resolved = resolve(rw, revstr);
if (resolved instanceof String) { if (resolved instanceof String) {
final Ref ref = getRef((String)resolved); final Ref ref = getRef((String)resolved);
@ -388,8 +387,6 @@ public abstract class Repository implements AutoCloseable {
} else { } else {
return (ObjectId) resolved; return (ObjectId) resolved;
} }
} finally {
rw.release();
} }
} }
@ -406,8 +403,7 @@ public abstract class Repository implements AutoCloseable {
*/ */
public String simplify(final String revstr) public String simplify(final String revstr)
throws AmbiguousObjectException, IOException { throws AmbiguousObjectException, IOException {
RevWalk rw = new RevWalk(this); try (RevWalk rw = new RevWalk(this)) {
try {
Object resolved = resolve(rw, revstr); Object resolved = resolve(rw, revstr);
if (resolved != null) if (resolved != null)
if (resolved instanceof String) if (resolved instanceof String)
@ -415,8 +411,6 @@ public abstract class Repository implements AutoCloseable {
else else
return ((AnyObjectId) resolved).getName(); return ((AnyObjectId) resolved).getName();
return null; return null;
} finally {
rw.release();
} }
} }
@ -791,8 +785,7 @@ public abstract class Repository implements AutoCloseable {
private ObjectId resolveAbbreviation(final String revstr) throws IOException, private ObjectId resolveAbbreviation(final String revstr) throws IOException,
AmbiguousObjectException { AmbiguousObjectException {
AbbreviatedObjectId id = AbbreviatedObjectId.fromString(revstr); AbbreviatedObjectId id = AbbreviatedObjectId.fromString(revstr);
ObjectReader reader = newObjectReader(); try (ObjectReader reader = newObjectReader()) {
try {
Collection<ObjectId> matches = reader.resolve(id); Collection<ObjectId> matches = reader.resolve(id);
if (matches.size() == 0) if (matches.size() == 0)
return null; return null;
@ -800,8 +793,6 @@ public abstract class Repository implements AutoCloseable {
return matches.iterator().next(); return matches.iterator().next();
else else
throw new AmbiguousObjectException(id, matches); throw new AmbiguousObjectException(id, matches);
} finally {
reader.release();
} }
} }

3
org.eclipse.jgit/src/org/eclipse/jgit/merge/RecursiveMerger.java

@ -269,7 +269,7 @@ public class RecursiveMerger extends ResolveMerger {
private DirCache dircacheFromTree(ObjectId treeId) throws IOException { private DirCache dircacheFromTree(ObjectId treeId) throws IOException {
DirCache ret = DirCache.newInCore(); DirCache ret = DirCache.newInCore();
DirCacheBuilder aBuilder = ret.builder(); DirCacheBuilder aBuilder = ret.builder();
TreeWalk atw = new TreeWalk(reader); try (TreeWalk atw = new TreeWalk(reader)) {
atw.addTree(treeId); atw.addTree(treeId);
atw.setRecursive(true); atw.setRecursive(true);
while (atw.next()) { while (atw.next()) {
@ -278,6 +278,7 @@ public class RecursiveMerger extends ResolveMerger {
e.setObjectId(atw.getObjectId(0)); e.setObjectId(atw.getObjectId(0));
aBuilder.add(e); aBuilder.add(e);
} }
}
aBuilder.finish(); aBuilder.finish();
return ret; return ret;
} }

5
org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java

@ -53,6 +53,7 @@ import java.util.NoSuchElementException;
import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.ObjectInserter.Formatter;
import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.TreeFormatter; import org.eclipse.jgit.lib.TreeFormatter;
@ -183,7 +184,9 @@ class LeafBucket extends InMemoryNoteBucket {
@Override @Override
ObjectId getTreeId() { ObjectId getTreeId() {
return new ObjectInserter.Formatter().idFor(build()); try (Formatter f = new ObjectInserter.Formatter()) {
return f.idFor(build());
}
} }
private TreeFormatter build() { private TreeFormatter build() {

15
org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java

@ -122,7 +122,7 @@ public class SubmoduleWalk implements AutoCloseable {
DirCache index = repository.readDirCache(); DirCache index = repository.readDirCache();
generator.setTree(new DirCacheIterator(index)); generator.setTree(new DirCacheIterator(index));
} catch (IOException e) { } catch (IOException e) {
generator.release(); generator.close();
throw e; throw e;
} }
return generator; return generator;
@ -152,10 +152,10 @@ public class SubmoduleWalk implements AutoCloseable {
if (filter.isDone(generator.walk)) if (filter.isDone(generator.walk))
return generator; return generator;
} catch (IOException e) { } catch (IOException e) {
generator.release(); generator.close();
throw e; throw e;
} }
generator.release(); generator.close();
return null; return null;
} }
@ -183,10 +183,10 @@ public class SubmoduleWalk implements AutoCloseable {
if (filter.isDone(generator.walk)) if (filter.isDone(generator.walk))
return generator; return generator;
} catch (IOException e) { } catch (IOException e) {
generator.release(); generator.close();
throw e; throw e;
} }
generator.release(); generator.close();
return null; return null;
} }
@ -419,8 +419,7 @@ public class SubmoduleWalk implements AutoCloseable {
config.load(); config.load();
modulesConfig = config; modulesConfig = config;
} else { } else {
TreeWalk configWalk = new TreeWalk(repository); try (TreeWalk configWalk = new TreeWalk(repository)) {
try {
configWalk.addTree(rootTree); configWalk.addTree(rootTree);
// The root tree may be part of the submodule walk, so we need to revert // 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) if (idx > 0)
rootTree.next(idx); rootTree.next(idx);
} }
} finally {
configWalk.release();
} }
} }
return this; return this;

2
org.eclipse.jgit/src/org/eclipse/jgit/transport/PushProcess.java

@ -177,7 +177,7 @@ class PushProcess {
} }
return res; return res;
} finally { } finally {
walker.release(); walker.close();
} }
} }

10
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

@ -630,7 +630,7 @@ public class UploadPack {
service(); service();
} finally { } finally {
msgOut = NullOutputStream.INSTANCE; msgOut = NullOutputStream.INSTANCE;
walk.release(); walk.close();
if (timer != null) { if (timer != null) {
try { try {
timer.terminate(); timer.terminate();
@ -737,8 +737,8 @@ public class UploadPack {
} }
private void processShallow() throws IOException { private void processShallow() throws IOException {
DepthWalk.RevWalk depthWalk = try (DepthWalk.RevWalk depthWalk = new DepthWalk.RevWalk(
new DepthWalk.RevWalk(walk.getObjectReader(), depth); walk.getObjectReader(), depth)) {
// Find all the commits which will be shallow // Find all the commits which will be shallow
for (ObjectId o : wantIds) { for (ObjectId o : wantIds) {
@ -765,7 +765,7 @@ public class UploadPack {
pckOut.writeString("unshallow " + c.name()); //$NON-NLS-1$ pckOut.writeString("unshallow " + c.name()); //$NON-NLS-1$
} }
} }
}
pckOut.end(); pckOut.end();
} }
@ -1460,7 +1460,7 @@ public class UploadPack {
statistics = pw.getStatistics(); statistics = pw.getStatistics();
if (statistics != null) if (statistics != null)
logger.onPackStatistics(statistics); logger.onPackStatistics(statistics);
pw.release(); pw.close();
} }
if (sideband) if (sideband)

4
org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java

@ -252,8 +252,8 @@ class WalkFetchConnection extends BaseFetchConnection {
@Override @Override
public void close() { public void close() {
inserter.release(); inserter.close();
reader.release(); reader.close();
for (final RemotePack p : unfetchedPacks) { for (final RemotePack p : unfetchedPacks) {
if (p.tmpIdx != null) if (p.tmpIdx != null)
p.tmpIdx.delete(); p.tmpIdx.delete();

8
org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkPushConnection.java

@ -220,9 +220,9 @@ class WalkPushConnection extends BaseConnection implements PushConnection {
String pathPack = null; String pathPack = null;
String pathIdx = null; String pathIdx = null;
final PackWriter writer = new PackWriter(transport.getPackConfig(), try (final PackWriter writer = new PackWriter(transport.getPackConfig(),
local.newObjectReader()); local.newObjectReader())) {
try {
final Set<ObjectId> need = new HashSet<ObjectId>(); final Set<ObjectId> need = new HashSet<ObjectId>();
final Set<ObjectId> have = new HashSet<ObjectId>(); final Set<ObjectId> have = new HashSet<ObjectId>();
for (final RemoteRefUpdate r : updates) for (final RemoteRefUpdate r : updates)
@ -293,8 +293,6 @@ class WalkPushConnection extends BaseConnection implements PushConnection {
safeDelete(pathPack); safeDelete(pathPack);
throw new TransportException(uri, JGitText.get().cannotStoreObjects, err); throw new TransportException(uri, JGitText.get().cannotStoreObjects, err);
} finally {
writer.release();
} }
} }

Loading…
Cancel
Save