From ae24740051491a4aa5d02ef4f94ca8d3399d90f2 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Thu, 21 May 2015 01:45:20 +0200 Subject: [PATCH] Use try-with-resource to avoid leaks with RevWalk and TreeWalk Change-Id: I1ea48039ea38766bb07f079e5ef4569de034d577 Signed-off-by: Matthias Sohn --- .../core/internal/merge/ResourceVariantTest.java | 121 +++++----- .../GitResourceVariantComparatorTest.java | 31 +-- .../core/synchronize/ThreeWayDiffEntryTest.java | 261 +++++++++++---------- .../core/test/AdaptableFileTreeIteratorTest.java | 39 +-- .../ContainerTreeIteratorResourceFilterTest.java | 24 +- .../org/eclipse/egit/core/test/TestRepository.java | 10 +- .../src/org/eclipse/egit/core/test/TestUtils.java | 52 ++-- .../egit/core/test/op/CommitOperationTest.java | 64 ++--- .../egit/core/test/op/RebaseOperationTest.java | 16 +- .../core/test/op/StashCreateOperationTest.java | 26 +- .../egit/core/test/op/TagOperationTest.java | 4 +- .../src/org/eclipse/egit/core/CommitUtil.java | 45 ++-- .../src/org/eclipse/egit/core/RepositoryUtil.java | 15 +- .../src/org/eclipse/egit/core/RevUtils.java | 4 +- .../egit/core/internal/CompareCoreUtils.java | 45 ++-- .../core/synchronize/GitCommitsModelCache.java | 104 ++++---- .../mylyn/ui/commit/TaskReferenceFactory.java | 8 +- .../src/org/eclipse/egit/ui/test/TestUtil.java | 59 +++-- .../test/team/actions/FetchAndMergeActionTest.java | 7 +- .../egit/ui/test/team/actions/PushActionTest.java | 70 +++--- .../internal/actions/RepositoryActionHandler.java | 8 +- .../ui/internal/actions/SynchronizeWithMenu.java | 1 + .../egit/ui/internal/commit/CommitEditorPage.java | 3 +- .../egit/ui/internal/commit/CommitHelper.java | 8 +- .../ui/internal/commit/CommitSelectionDialog.java | 13 +- .../ui/internal/components/RefContentProposal.java | 17 +- .../dialogs/ResetTargetSelectionDialog.java | 34 +-- .../ui/internal/fetch/FetchGerritChangePage.java | 6 +- .../ui/internal/history/CommitInfoBuilder.java | 86 +++---- .../egit/ui/internal/history/GitHistoryPage.java | 9 +- .../command/AbstractHistoryCommandHandler.java | 15 +- .../command/CompareWithWorkingTreeHandler.java | 4 +- .../history/command/ShowVersionsHandler.java | 4 +- .../ui/internal/merge/GitCompareEditorInput.java | 120 +++++----- .../egit/ui/internal/merge/MergeResultDialog.java | 29 +-- .../egit/ui/internal/push/PushBranchPage.java | 3 +- .../egit/ui/internal/push/RefUpdateElement.java | 3 +- .../ui/internal/repository/CreateBranchWizard.java | 8 +- .../tree/command/CreateBranchCommand.java | 4 +- .../repository/tree/command/RemoveCommand.java | 31 +-- .../egit/ui/internal/search/CommitSearchQuery.java | 5 +- .../ui/internal/sharing/ExistingOrNewPage.java | 20 +- .../internal/synchronize/model/GitModelBlob.java | 7 +- 43 files changed, 744 insertions(+), 699 deletions(-) diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/merge/ResourceVariantTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/merge/ResourceVariantTest.java index ede07fa3d..767fcd7d0 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/merge/ResourceVariantTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/merge/ResourceVariantTest.java @@ -160,26 +160,29 @@ public class ResourceVariantTest extends VariantsTestCase { setupUnconflictingBranches(); ObjectId baseId = repo.resolve(BRANCH); - RevWalk walk = new RevWalk(repo); - TreeWalk tw = new TreeWalk(repo); - tw.addTree(walk.parseTree(baseId)); - - while (tw.next()) { - AbstractGitResourceVariant variant = TreeParserResourceVariant - .create(repo, tw.getTree(0, CanonicalTreeParser.class)); - - assertEquals(tw.getObjectId(0).getName(), - variant.getContentIdentifier()); - assertEquals(tw.getObjectId(0), variant.getObjectId()); - assertEquals(tw.getRawMode(0), variant.getRawMode()); - if (iFile1.getName().equals(variant.getName())) { - assertContentEquals(variant, BRANCH_CHANGE + INITIAL_CONTENT_1); - } else if (!tw.isSubtree()) { - assertContentEquals(variant, BRANCH_CHANGE + INITIAL_CONTENT_2); - } + try (RevWalk walk = new RevWalk(repo); + TreeWalk tw = new TreeWalk(repo)) { + tw.addTree(walk.parseTree(baseId)); + + while (tw.next()) { + AbstractGitResourceVariant variant = TreeParserResourceVariant + .create(repo, tw.getTree(0, CanonicalTreeParser.class)); + + assertEquals(tw.getObjectId(0).getName(), + variant.getContentIdentifier()); + assertEquals(tw.getObjectId(0), variant.getObjectId()); + assertEquals(tw.getRawMode(0), variant.getRawMode()); + if (iFile1.getName().equals(variant.getName())) { + assertContentEquals(variant, + BRANCH_CHANGE + INITIAL_CONTENT_1); + } else if (!tw.isSubtree()) { + assertContentEquals(variant, + BRANCH_CHANGE + INITIAL_CONTENT_2); + } - if (tw.isSubtree()) { - tw.enterSubtree(); + if (tw.isSubtree()) { + tw.enterSubtree(); + } } } } @@ -192,26 +195,29 @@ public class ResourceVariantTest extends VariantsTestCase { setupUnconflictingBranches(); ObjectId baseId = repo.resolve(MASTER); - RevWalk walk = new RevWalk(repo); - TreeWalk tw = new TreeWalk(repo); - tw.addTree(walk.parseTree(baseId)); - - while (tw.next()) { - AbstractGitResourceVariant variant = TreeParserResourceVariant - .create(repo, tw.getTree(0, CanonicalTreeParser.class)); - - assertEquals(tw.getObjectId(0).getName(), - variant.getContentIdentifier()); - assertEquals(tw.getObjectId(0), variant.getObjectId()); - assertEquals(tw.getRawMode(0), variant.getRawMode()); - if (iFile1.getName().equals(variant.getName())) { - assertContentEquals(variant, INITIAL_CONTENT_1 + MASTER_CHANGE); - } else if (!tw.isSubtree()) { - assertContentEquals(variant, INITIAL_CONTENT_2 + MASTER_CHANGE); - } + try (RevWalk walk = new RevWalk(repo); + TreeWalk tw = new TreeWalk(repo)) { + tw.addTree(walk.parseTree(baseId)); + + while (tw.next()) { + AbstractGitResourceVariant variant = TreeParserResourceVariant + .create(repo, tw.getTree(0, CanonicalTreeParser.class)); + + assertEquals(tw.getObjectId(0).getName(), + variant.getContentIdentifier()); + assertEquals(tw.getObjectId(0), variant.getObjectId()); + assertEquals(tw.getRawMode(0), variant.getRawMode()); + if (iFile1.getName().equals(variant.getName())) { + assertContentEquals(variant, + INITIAL_CONTENT_1 + MASTER_CHANGE); + } else if (!tw.isSubtree()) { + assertContentEquals(variant, + INITIAL_CONTENT_2 + MASTER_CHANGE); + } - if (tw.isSubtree()) { - tw.enterSubtree(); + if (tw.isSubtree()) { + tw.enterSubtree(); + } } } } @@ -224,26 +230,27 @@ public class ResourceVariantTest extends VariantsTestCase { setupUnconflictingBranches(); ObjectId baseId = repo.resolve(BASE_BRANCH); - RevWalk walk = new RevWalk(repo); - TreeWalk tw = new TreeWalk(repo); - tw.addTree(walk.parseTree(baseId)); - - while (tw.next()) { - AbstractGitResourceVariant variant = TreeParserResourceVariant - .create(repo, tw.getTree(0, CanonicalTreeParser.class)); - - assertEquals(tw.getObjectId(0).getName(), - variant.getContentIdentifier()); - assertEquals(tw.getObjectId(0), variant.getObjectId()); - assertEquals(tw.getRawMode(0), variant.getRawMode()); - if (iFile1.getName().equals(variant.getName())) { - assertContentEquals(variant, INITIAL_CONTENT_1); - } else if (!tw.isSubtree()) { - fail("file2 shouldn't exist in our base."); - } + try (RevWalk walk = new RevWalk(repo); + TreeWalk tw = new TreeWalk(repo)) { + tw.addTree(walk.parseTree(baseId)); + + while (tw.next()) { + AbstractGitResourceVariant variant = TreeParserResourceVariant + .create(repo, tw.getTree(0, CanonicalTreeParser.class)); + + assertEquals(tw.getObjectId(0).getName(), + variant.getContentIdentifier()); + assertEquals(tw.getObjectId(0), variant.getObjectId()); + assertEquals(tw.getRawMode(0), variant.getRawMode()); + if (iFile1.getName().equals(variant.getName())) { + assertContentEquals(variant, INITIAL_CONTENT_1); + } else if (!tw.isSubtree()) { + fail("file2 shouldn't exist in our base."); + } - if (tw.isSubtree()) { - tw.enterSubtree(); + if (tw.isSubtree()) { + tw.enterSubtree(); + } } } } diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java index 86f431ce8..cd6307efa 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java @@ -586,21 +586,22 @@ public class GitResourceVariantComparatorTest extends GitTestCase { testRepo.addToIndex(testRepo.getIFile(iProject, file2)); RevCommit commit = testRepo.commit("initial commit"); - TreeWalk tw = new TreeWalk(repo); - int nth = tw.addTree(commit.getTree()); - - tw.next(); - tw.enterSubtree(); // enter project node - tw.next(); - GitRemoteFolder base = new GitRemoteFolder(repo, null, commit, - tw.getObjectId(nth), tw.getNameString()); - - tw.next(); - GitRemoteFolder remote = new GitRemoteFolder(repo, null, commit, - tw.getObjectId(nth), tw.getNameString()); - - // then - assertFalse(grvc.compare(base, remote)); + try (TreeWalk tw = new TreeWalk(repo)) { + int nth = tw.addTree(commit.getTree()); + + tw.next(); + tw.enterSubtree(); // enter project node + tw.next(); + GitRemoteFolder base = new GitRemoteFolder(repo, null, commit, + tw.getObjectId(nth), tw.getNameString()); + + tw.next(); + GitRemoteFolder remote = new GitRemoteFolder(repo, null, commit, + tw.getObjectId(nth), tw.getNameString()); + + // then + assertFalse(grvc.compare(base, remote)); + } } /** diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/ThreeWayDiffEntryTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/ThreeWayDiffEntryTest.java index 402b1197e..f1e6a28bd 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/ThreeWayDiffEntryTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/ThreeWayDiffEntryTest.java @@ -48,20 +48,21 @@ public class ThreeWayDiffEntryTest extends LocalDiskRepositoryTestCase { RevCommit c = git.commit().setMessage("initial commit").call(); // when - TreeWalk walk = new TreeWalk(db); - walk.addTree(c.getTree()); - walk.addTree(new EmptyTreeIterator()); - walk.addTree(new EmptyTreeIterator()); - List result = ThreeWayDiffEntry.scan(walk); - - // then - assertThat(result, notNullValue()); - assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); - - ThreeWayDiffEntry entry = result.get(0); - assertThat(entry.getDirection(), is(Direction.OUTGOING)); - assertThat(entry.getChangeType(), is(ChangeType.ADD)); - assertThat(entry.getPath(), is("a.txt")); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(c.getTree()); + walk.addTree(new EmptyTreeIterator()); + walk.addTree(new EmptyTreeIterator()); + List result = ThreeWayDiffEntry.scan(walk); + + // then + assertThat(result, notNullValue()); + assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); + + ThreeWayDiffEntry entry = result.get(0); + assertThat(entry.getDirection(), is(Direction.OUTGOING)); + assertThat(entry.getChangeType(), is(ChangeType.ADD)); + assertThat(entry.getPath(), is("a.txt")); + } } @Test @@ -73,20 +74,21 @@ public class ThreeWayDiffEntryTest extends LocalDiskRepositoryTestCase { RevCommit c = git.commit().setMessage("initial commit").call(); // when - TreeWalk walk = new TreeWalk(db); - walk.addTree(new EmptyTreeIterator()); - walk.addTree(new EmptyTreeIterator()); - walk.addTree(c.getTree()); - List result = ThreeWayDiffEntry.scan(walk); - - // then - assertThat(result, notNullValue()); - assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); - - ThreeWayDiffEntry entry = result.get(0); - assertThat(entry.getDirection(), is(Direction.INCOMING)); - assertThat(entry.getChangeType(), is(ChangeType.ADD)); - assertThat(entry.getPath(), is("a.txt")); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(new EmptyTreeIterator()); + walk.addTree(new EmptyTreeIterator()); + walk.addTree(c.getTree()); + List result = ThreeWayDiffEntry.scan(walk); + + // then + assertThat(result, notNullValue()); + assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); + + ThreeWayDiffEntry entry = result.get(0); + assertThat(entry.getDirection(), is(Direction.INCOMING)); + assertThat(entry.getChangeType(), is(ChangeType.ADD)); + assertThat(entry.getPath(), is("a.txt")); + } } @Test @@ -98,20 +100,21 @@ public class ThreeWayDiffEntryTest extends LocalDiskRepositoryTestCase { RevCommit c = git.commit().setMessage("initial commit").call(); // when - TreeWalk walk = new TreeWalk(db); - walk.addTree(new EmptyTreeIterator()); - walk.addTree(c.getTree()); - walk.addTree(c.getTree()); - List result = ThreeWayDiffEntry.scan(walk); - - // then - assertThat(result, notNullValue()); - assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); - - ThreeWayDiffEntry entry = result.get(0); - assertThat(entry.getDirection(), is(Direction.OUTGOING)); - assertThat(entry.getChangeType(), is(ChangeType.DELETE)); - assertThat(entry.getPath(), is("a.txt")); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(new EmptyTreeIterator()); + walk.addTree(c.getTree()); + walk.addTree(c.getTree()); + List result = ThreeWayDiffEntry.scan(walk); + + // then + assertThat(result, notNullValue()); + assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); + + ThreeWayDiffEntry entry = result.get(0); + assertThat(entry.getDirection(), is(Direction.OUTGOING)); + assertThat(entry.getChangeType(), is(ChangeType.DELETE)); + assertThat(entry.getPath(), is("a.txt")); + } } @Test @@ -123,20 +126,21 @@ public class ThreeWayDiffEntryTest extends LocalDiskRepositoryTestCase { RevCommit c = git.commit().setMessage("initial commit").call(); // when - TreeWalk walk = new TreeWalk(db); - walk.addTree(c.getTree()); - walk.addTree(c.getTree()); - walk.addTree(new EmptyTreeIterator()); - List result = ThreeWayDiffEntry.scan(walk); - - // then - assertThat(result, notNullValue()); - assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); - - ThreeWayDiffEntry entry = result.get(0); - assertThat(entry.getDirection(), is(Direction.INCOMING)); - assertThat(entry.getChangeType(), is(ChangeType.DELETE)); - assertThat(entry.getPath(), is("a.txt")); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(c.getTree()); + walk.addTree(c.getTree()); + walk.addTree(new EmptyTreeIterator()); + List result = ThreeWayDiffEntry.scan(walk); + + // then + assertThat(result, notNullValue()); + assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); + + ThreeWayDiffEntry entry = result.get(0); + assertThat(entry.getDirection(), is(Direction.INCOMING)); + assertThat(entry.getChangeType(), is(ChangeType.DELETE)); + assertThat(entry.getPath(), is("a.txt")); + } } @Test @@ -148,21 +152,21 @@ public class ThreeWayDiffEntryTest extends LocalDiskRepositoryTestCase { RevCommit c = git.commit().setMessage("initial commit").call(); // when - TreeWalk walk = new TreeWalk(db); - walk.addTree(new EmptyTreeIterator()); - walk.addTree(c.getTree()); - walk.addTree(new EmptyTreeIterator()); - List result = ThreeWayDiffEntry.scan(walk); - - // then - assertThat(result, notNullValue()); - assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); - - ThreeWayDiffEntry entry = result.get(0); - assertThat(entry.getDirection(), is(Direction.CONFLICTING)); - assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); - // assertThat(entry.getNewPath(), is("a.txt")); - // assertThat(entry.getOldPath(), is(DEV_NULL)); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(new EmptyTreeIterator()); + walk.addTree(c.getTree()); + walk.addTree(new EmptyTreeIterator()); + List result = ThreeWayDiffEntry.scan(walk); + // then + assertThat(result, notNullValue()); + assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); + + ThreeWayDiffEntry entry = result.get(0); + assertThat(entry.getDirection(), is(Direction.CONFLICTING)); + assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); + // assertThat(entry.getNewPath(), is("a.txt")); + // assertThat(entry.getOldPath(), is(DEV_NULL)); + } } @Test @@ -174,20 +178,20 @@ public class ThreeWayDiffEntryTest extends LocalDiskRepositoryTestCase { RevCommit c = git.commit().setMessage("initial commit").call(); // when - TreeWalk walk = new TreeWalk(db); - walk.addTree(c.getTree()); - walk.addTree(new EmptyTreeIterator()); - walk.addTree(c.getTree()); - List result = ThreeWayDiffEntry.scan(walk); - - // then - assertThat(result, notNullValue()); - assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); - - ThreeWayDiffEntry entry = result.get(0); - assertThat(entry.getDirection(), is(Direction.CONFLICTING)); - assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); - assertThat(entry.getPath(), is("a.txt")); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(c.getTree()); + walk.addTree(new EmptyTreeIterator()); + walk.addTree(c.getTree()); + List result = ThreeWayDiffEntry.scan(walk); + // then + assertThat(result, notNullValue()); + assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); + + ThreeWayDiffEntry entry = result.get(0); + assertThat(entry.getDirection(), is(Direction.CONFLICTING)); + assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); + assertThat(entry.getPath(), is("a.txt")); + } } @Test @@ -202,20 +206,21 @@ public class ThreeWayDiffEntryTest extends LocalDiskRepositoryTestCase { .call(); // when - TreeWalk walk = new TreeWalk(db); - walk.addTree(c.getTree()); - walk.addTree(c.getTree()); - walk.addTree(c1.getTree()); - List result = ThreeWayDiffEntry.scan(walk); - - // then - assertThat(result, notNullValue()); - assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); - - ThreeWayDiffEntry entry = result.get(0); - assertThat(entry.getDirection(), is(Direction.INCOMING)); - assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); - assertThat(entry.getPath(), is("a.txt")); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(c.getTree()); + walk.addTree(c.getTree()); + walk.addTree(c1.getTree()); + List result = ThreeWayDiffEntry.scan(walk); + + // then + assertThat(result, notNullValue()); + assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); + + ThreeWayDiffEntry entry = result.get(0); + assertThat(entry.getDirection(), is(Direction.INCOMING)); + assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); + assertThat(entry.getPath(), is("a.txt")); + } } @Test @@ -230,20 +235,21 @@ public class ThreeWayDiffEntryTest extends LocalDiskRepositoryTestCase { .call(); // when - TreeWalk walk = new TreeWalk(db); - walk.addTree(c1.getTree()); - walk.addTree(c.getTree()); - walk.addTree(c.getTree()); - List result = ThreeWayDiffEntry.scan(walk); - - // then - assertThat(result, notNullValue()); - assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); - - ThreeWayDiffEntry entry = result.get(0); - assertThat(entry.getDirection(), is(Direction.OUTGOING)); - assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); - assertThat(entry.getPath(), is("a.txt")); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(c1.getTree()); + walk.addTree(c.getTree()); + walk.addTree(c.getTree()); + List result = ThreeWayDiffEntry.scan(walk); + + // then + assertThat(result, notNullValue()); + assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); + + ThreeWayDiffEntry entry = result.get(0); + assertThat(entry.getDirection(), is(Direction.OUTGOING)); + assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); + assertThat(entry.getPath(), is("a.txt")); + } } @Test @@ -258,20 +264,21 @@ public class ThreeWayDiffEntryTest extends LocalDiskRepositoryTestCase { .call(); // when - TreeWalk walk = new TreeWalk(db); - walk.addTree(c1.getTree()); - walk.addTree(c.getTree()); - walk.addTree(c1.getTree()); - List result = ThreeWayDiffEntry.scan(walk); - - // then - assertThat(result, notNullValue()); - assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); - - ThreeWayDiffEntry entry = result.get(0); - assertThat(entry.getDirection(), is(Direction.CONFLICTING)); - assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); - assertThat(entry.getPath(), is("a.txt")); + try (TreeWalk walk = new TreeWalk(db)) { + walk.addTree(c1.getTree()); + walk.addTree(c.getTree()); + walk.addTree(c1.getTree()); + List result = ThreeWayDiffEntry.scan(walk); + + // then + assertThat(result, notNullValue()); + assertThat(Integer.valueOf(result.size()), is(Integer.valueOf(1))); + + ThreeWayDiffEntry entry = result.get(0); + assertThat(entry.getDirection(), is(Direction.CONFLICTING)); + assertThat(entry.getChangeType(), is(ChangeType.MODIFY)); + assertThat(entry.getPath(), is("a.txt")); + } } // copied from org.eclipse.jgit.lib.RepositoryTestCase diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/AdaptableFileTreeIteratorTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/AdaptableFileTreeIteratorTest.java index 76c306736..c0e35fade 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/AdaptableFileTreeIteratorTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/AdaptableFileTreeIteratorTest.java @@ -62,23 +62,26 @@ public class AdaptableFileTreeIteratorTest extends GitTestCase { final IWorkspaceRoot root = project.getProject().getWorkspace() .getRoot(); - final TreeWalk treeWalk = new TreeWalk(repository); - treeWalk.addTree(new AdaptableFileTreeIterator(repository, root)); - treeWalk.setRecursive(true); - - final IFile eclipseFile = project.getProject().getFile(file.getName()); - final RepositoryMapping mapping = RepositoryMapping - .getMapping(eclipseFile); - final Set repositoryPaths = Collections.singleton(mapping - .getRepoRelativePath(eclipseFile)); - - assertEquals(1, repositoryPaths.size()); - treeWalk.setFilter(PathFilterGroup.createFromStrings(repositoryPaths)); - - assertTrue(treeWalk.next()); - - final WorkingTreeIterator iterator = treeWalk.getTree(0, - WorkingTreeIterator.class); - assertTrue(iterator instanceof ContainerTreeIterator); + try (final TreeWalk treeWalk = new TreeWalk(repository)) { + treeWalk.addTree(new AdaptableFileTreeIterator(repository, root)); + treeWalk.setRecursive(true); + + final IFile eclipseFile = project.getProject() + .getFile(file.getName()); + final RepositoryMapping mapping = RepositoryMapping + .getMapping(eclipseFile); + final Set repositoryPaths = Collections + .singleton(mapping.getRepoRelativePath(eclipseFile)); + + assertEquals(1, repositoryPaths.size()); + treeWalk.setFilter( + PathFilterGroup.createFromStrings(repositoryPaths)); + + assertTrue(treeWalk.next()); + + final WorkingTreeIterator iterator = treeWalk.getTree(0, + WorkingTreeIterator.class); + assertTrue(iterator instanceof ContainerTreeIterator); + } } } diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/ContainerTreeIteratorResourceFilterTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/ContainerTreeIteratorResourceFilterTest.java index 5ac5baf92..522b14e24 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/ContainerTreeIteratorResourceFilterTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/ContainerTreeIteratorResourceFilterTest.java @@ -174,17 +174,21 @@ public class ContainerTreeIteratorResourceFilterTest extends GitTestCase { } private List walkTree() throws IOException { - TreeWalk treeWalk = new TreeWalk(repository); - ContainerTreeIterator tree = new ContainerTreeIterator(repository, project.getProject()); - int treeIndex = treeWalk.addTree(tree); - treeWalk.setRecursive(true); - List entries = new ArrayList(); - while (treeWalk.next()) { - AbstractTreeIterator it = treeWalk.getTree(treeIndex, AbstractTreeIterator.class); - Entry entry = new Entry(treeWalk.getPathString(), it.getClass()); - entries.add(entry); + try (TreeWalk treeWalk = new TreeWalk(repository)) { + ContainerTreeIterator tree = new ContainerTreeIterator(repository, + project.getProject()); + int treeIndex = treeWalk.addTree(tree); + treeWalk.setRecursive(true); + List entries = new ArrayList(); + while (treeWalk.next()) { + AbstractTreeIterator it = treeWalk.getTree(treeIndex, + AbstractTreeIterator.class); + Entry entry = new Entry(treeWalk.getPathString(), + it.getClass()); + entries.add(entry); + } + return entries; } - return entries; } private static Entry containerTreeEntry(String path) { diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java index 7be77c529..2d5e08532 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java @@ -446,10 +446,14 @@ public class TestRepository { return true; Ref ref = repository.getRef(Constants.HEAD); - RevCommit c = new RevWalk(repository).parseCommit(ref.getObjectId()); - TreeWalk tw = TreeWalk.forPath(repository, getRepoRelativePath(absolutePath), c.getTree()); + try (RevWalk rw = new RevWalk(repository)) { + RevCommit c = rw.parseCommit(ref.getObjectId()); - return tw == null || dc.getObjectId().equals(tw.getObjectId(0)); + try (TreeWalk tw = TreeWalk.forPath(repository, + getRepoRelativePath(absolutePath), c.getTree())) { + return tw == null || dc.getObjectId().equals(tw.getObjectId(0)); + } + } } public long lastModifiedInIndex(String path) throws IOException { diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java index 22272de74..20654bb47 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java @@ -238,14 +238,16 @@ public class TestUtils { Set expectedfiles = new HashSet(); for (String path : paths) expectedfiles.add(path); - TreeWalk treeWalk = new TreeWalk(repository); - treeWalk.addTree(repository.resolve("HEAD^{tree}")); - treeWalk.setRecursive(true); - while (treeWalk.next()) { - String path = treeWalk.getPathString(); - if (!expectedfiles.contains(path)) - fail("Repository contains unexpected expected file " + path); - expectedfiles.remove(path); + try (TreeWalk treeWalk = new TreeWalk(repository)) { + treeWalk.addTree(repository.resolve("HEAD^{tree}")); + treeWalk.setRecursive(true); + while (treeWalk.next()) { + String path = treeWalk.getPathString(); + if (!expectedfiles.contains(path)) + fail("Repository contains unexpected expected file " + + path); + expectedfiles.remove(path); + } } if (expectedfiles.size() > 0) { StringBuilder message = new StringBuilder( @@ -273,23 +275,25 @@ public class TestUtils { public void assertRepositoryContainsFilesWithContent(Repository repository, String... args) throws Exception { HashMap expectedfiles = mkmap(args); - TreeWalk treeWalk = new TreeWalk(repository); - treeWalk.addTree(repository.resolve("HEAD^{tree}")); - treeWalk.setRecursive(true); - while (treeWalk.next()) { - String path = treeWalk.getPathString(); - assertTrue(expectedfiles.containsKey(path)); - ObjectId objectId = treeWalk.getObjectId(0); - byte[] expectedContent = expectedfiles.get(path).getBytes("UTF-8"); - byte[] repoContent = treeWalk.getObjectReader().open(objectId) - .getBytes(); - if (!Arrays.equals(repoContent, expectedContent)) { - fail("File " + path + " has repository content " - + new String(repoContent, "UTF-8") - + " instead of expected content " - + new String(expectedContent, "UTF-8")); + try (TreeWalk treeWalk = new TreeWalk(repository)) { + treeWalk.addTree(repository.resolve("HEAD^{tree}")); + treeWalk.setRecursive(true); + while (treeWalk.next()) { + String path = treeWalk.getPathString(); + assertTrue(expectedfiles.containsKey(path)); + ObjectId objectId = treeWalk.getObjectId(0); + byte[] expectedContent = expectedfiles.get(path) + .getBytes("UTF-8"); + byte[] repoContent = treeWalk.getObjectReader().open(objectId) + .getBytes(); + if (!Arrays.equals(repoContent, expectedContent)) { + fail("File " + path + " has repository content " + + new String(repoContent, "UTF-8") + + " instead of expected content " + + new String(expectedContent, "UTF-8")); + } + expectedfiles.remove(path); } - expectedfiles.remove(path); } if (expectedfiles.size() > 0) { StringBuilder message = new StringBuilder( diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/CommitOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/CommitOperationTest.java index fae6e5860..3b3f3b3c1 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/CommitOperationTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/CommitOperationTest.java @@ -101,14 +101,15 @@ public class CommitOperationTest extends GitTestCase { assertEquals("No changes", e.getCause().getMessage()); } - TreeWalk treeWalk = new TreeWalk(repository); - treeWalk.addTree(repository.resolve("HEAD^{tree}")); - assertTrue(treeWalk.next()); - assertEquals("foo", treeWalk.getPathString()); - treeWalk.enterSubtree(); - assertTrue(treeWalk.next()); - assertEquals("foo/a.txt", treeWalk.getPathString()); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = new TreeWalk(repository)) { + treeWalk.addTree(repository.resolve("HEAD^{tree}")); + assertTrue(treeWalk.next()); + assertEquals("foo", treeWalk.getPathString()); + treeWalk.enterSubtree(); + assertTrue(treeWalk.next()); + assertEquals("foo/a.txt", treeWalk.getPathString()); + assertFalse(treeWalk.next()); + } } @Test @@ -173,20 +174,20 @@ public class CommitOperationTest extends GitTestCase { Git git = new Git(repository); Iterator commits = git.log().call().iterator(); RevCommit secondCommit = commits.next(); - TreeWalk treeWalk = new TreeWalk(repository); - treeWalk.addTree(secondCommit.getTree().getId()); - treeWalk.setRecursive(true); - treeWalk.setPostOrderTraversal(true); - assertTrue(treeWalk.next()); - assertEquals("sub1/a.txt", treeWalk.getPathString()); - assertTrue(treeWalk.next()); - assertEquals("sub1", treeWalk.getPathString()); - assertTrue(treeWalk.next()); - assertEquals("sub2/b.txt", treeWalk.getPathString()); - assertTrue(treeWalk.next()); - assertEquals("sub2", treeWalk.getPathString()); - assertFalse(treeWalk.next()); - + try (TreeWalk treeWalk = new TreeWalk(repository)) { + treeWalk.addTree(secondCommit.getTree().getId()); + treeWalk.setRecursive(true); + treeWalk.setPostOrderTraversal(true); + assertTrue(treeWalk.next()); + assertEquals("sub1/a.txt", treeWalk.getPathString()); + assertTrue(treeWalk.next()); + assertEquals("sub1", treeWalk.getPathString()); + assertTrue(treeWalk.next()); + assertEquals("sub2/b.txt", treeWalk.getPathString()); + assertTrue(treeWalk.next()); + assertEquals("sub2", treeWalk.getPathString()); + assertFalse(treeWalk.next()); + } project.getProject().getFolder("sub2").delete(IResource.FORCE, null); IFile[] filesToCommit = { project.getProject().getFile("sub2/b.txt") }; ArrayList notIndexed = new ArrayList(); @@ -199,15 +200,16 @@ public class CommitOperationTest extends GitTestCase { git = new Git(repository); commits = git.log().call().iterator(); secondCommit = commits.next(); - treeWalk = new TreeWalk(repository); - treeWalk.addTree(secondCommit.getTree().getId()); - treeWalk.setRecursive(true); - treeWalk.setPostOrderTraversal(true); - assertTrue(treeWalk.next()); - assertEquals("sub1/a.txt", treeWalk.getPathString()); - assertTrue(treeWalk.next()); - assertEquals("sub1", treeWalk.getPathString()); - assertFalse(treeWalk.next()); + try (TreeWalk treeWalk = new TreeWalk(repository)) { + treeWalk.addTree(secondCommit.getTree().getId()); + treeWalk.setRecursive(true); + treeWalk.setPostOrderTraversal(true); + assertTrue(treeWalk.next()); + assertEquals("sub1/a.txt", treeWalk.getPathString()); + assertTrue(treeWalk.next()); + assertEquals("sub1", treeWalk.getPathString()); + assertFalse(treeWalk.next()); + } } @Test diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/RebaseOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/RebaseOperationTest.java index 86fbb9c08..24f56497a 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/RebaseOperationTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/RebaseOperationTest.java @@ -89,10 +89,11 @@ public class RebaseOperationTest extends GitTestCase { RebaseResult res = op.getResult(); assertEquals(RebaseResult.Status.UP_TO_DATE, res.getStatus()); - RevCommit newTopic = new RevWalk(repository).parseCommit(repository - .resolve(TOPIC)); - assertEquals(topicCommit, newTopic); - assertEquals(first, newTopic.getParent(0)); + try (RevWalk rw = new RevWalk(repository)) { + RevCommit newTopic = rw.parseCommit(repository.resolve(TOPIC)); + assertEquals(topicCommit, newTopic); + assertEquals(first, newTopic.getParent(0)); + } } @Test @@ -133,9 +134,10 @@ public class RebaseOperationTest extends GitTestCase { RebaseResult res = op.getResult(); assertEquals(RebaseResult.Status.OK, res.getStatus()); - RevCommit newTopic = new RevWalk(repository).parseCommit(repository - .resolve(TOPIC)); - assertEquals(second, newTopic.getParent(0)); + try (RevWalk rw = new RevWalk(repository)) { + RevCommit newTopic = rw.parseCommit(repository.resolve(TOPIC)); + assertEquals(second, newTopic.getParent(0)); + } } @Test diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/StashCreateOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/StashCreateOperationTest.java index 8de5d3ff0..026119079 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/StashCreateOperationTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/StashCreateOperationTest.java @@ -60,9 +60,11 @@ public class StashCreateOperationTest extends GitTestCase { StashCreateOperation stashCreateOperation = new StashCreateOperation(repository); stashCreateOperation.execute(null); - RevWalk revWalk = new RevWalk(repository); - RevCommit commit = revWalk.parseCommit(repository.resolve("stash@{0}")); - assertTrue(commit.getFullMessage().length() > 0); + try (RevWalk revWalk = new RevWalk(repository)) { + RevCommit commit = revWalk + .parseCommit(repository.resolve("stash@{0}")); + assertTrue(commit.getFullMessage().length() > 0); + } } @Test @@ -74,9 +76,11 @@ public class StashCreateOperationTest extends GitTestCase { StashCreateOperation stashCreateOperation = new StashCreateOperation(repository, message); stashCreateOperation.execute(null); - RevWalk revWalk = new RevWalk(repository); - RevCommit commit = revWalk.parseCommit(repository.resolve("stash@{0}")); - assertEquals(message, commit.getFullMessage()); + try (RevWalk revWalk = new RevWalk(repository)) { + RevCommit commit = revWalk + .parseCommit(repository.resolve("stash@{0}")); + assertEquals(message, commit.getFullMessage()); + } } @Test @@ -88,10 +92,12 @@ public class StashCreateOperationTest extends GitTestCase { repository, message, true); stashCreateOperation.execute(null); - RevWalk revWalk = new RevWalk(repository); - RevCommit commit = revWalk.parseCommit(repository.resolve("stash@{0}")); - // untracked commit is the third parent - assertEquals(commit.getParentCount(), 3); + try (RevWalk revWalk = new RevWalk(repository)) { + RevCommit commit = revWalk + .parseCommit(repository.resolve("stash@{0}")); + // untracked commit is the third parent + assertEquals(commit.getParentCount(), 3); + } } } diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/TagOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/TagOperationTest.java index 6f515d3da..befd6edfc 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/TagOperationTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/TagOperationTest.java @@ -94,8 +94,7 @@ public class TagOperationTest extends DualRepositoryTestCase { // expected } Ref tagRef = repository1.getRepository().getTags().get("TheNewTag"); - RevWalk walk = new RevWalk(repository1.getRepository()); - try { + try (RevWalk walk = new RevWalk(repository1.getRepository())) { RevTag tag = walk.parseTag(repository1.getRepository().resolve( tagRef.getName())); @@ -107,7 +106,6 @@ public class TagOperationTest extends DualRepositoryTestCase { tagRef.getName())); assertTrue("Messages be same", tag.getFullMessage().equals(newTag.getMessage())); - } finally { walk.dispose(); } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/CommitUtil.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/CommitUtil.java index 38f5248fa..a1baf4671 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/CommitUtil.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/CommitUtil.java @@ -102,33 +102,34 @@ public class CommitUtil { public static boolean areCommitsInCurrentBranch( Collection commits, Repository repository) throws IOException { - RevWalk walk = new RevWalk(repository); - ObjectId headCommitId = repository.resolve(Constants.HEAD); - RevCommit headCommit = walk.parseCommit(headCommitId); + try (RevWalk walk = new RevWalk(repository)) { + ObjectId headCommitId = repository.resolve(Constants.HEAD); + RevCommit headCommit = walk.parseCommit(headCommitId); - for (final RevCommit commit : commits) { - walk.reset(); - walk.markStart(headCommit); + for (final RevCommit commit : commits) { + walk.reset(); + walk.markStart(headCommit); - RevFilter revFilter = new RevFilter() { - @Override - public boolean include(RevWalk walker, RevCommit cmit) - throws StopWalkException, MissingObjectException, - IncorrectObjectTypeException, IOException { + RevFilter revFilter = new RevFilter() { + @Override + public boolean include(RevWalk walker, RevCommit cmit) + throws StopWalkException, MissingObjectException, + IncorrectObjectTypeException, IOException { - return cmit.equals(commit); - } + return cmit.equals(commit); + } - @Override - public RevFilter clone() { - return null; - } - }; - walk.setRevFilter(revFilter); + @Override + public RevFilter clone() { + return null; + } + }; + walk.setRevFilter(revFilter); - if (walk.next() == null) - return false; + if (walk.next() == null) + return false; + } + return true; } - return true; } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java index 53c612c45..257c7c715 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java @@ -161,8 +161,7 @@ public class RepositoryUtil { } Map tagMap = new HashMap(); - try { - RevWalk rw = new RevWalk(repository); + try (RevWalk rw = new RevWalk(repository)) { Map tags = repository.getRefDatabase().getRefs( Constants.R_TAGS); for (Ref tagRef : tags.values()) { @@ -451,20 +450,15 @@ public class RepositoryUtil { * @since 2.2 */ public RevCommit parseHeadCommit(Repository repository) { - RevWalk walk = null; - try { + try (RevWalk walk = new RevWalk(repository)) { Ref head = repository.getRef(Constants.HEAD); if (head == null || head.getObjectId() == null) return null; - walk = new RevWalk(repository); RevCommit commit = walk.parseCommit(head.getObjectId()); return commit; } catch (IOException e) { return null; - } finally { - if (walk != null) - walk.close(); } } @@ -488,8 +482,7 @@ public class RepositoryUtil { if (treeIterator == null) return true; String repoRelativePath = mapping.getRepoRelativePath(path); - TreeWalk walk = new TreeWalk(repository); - try { + try (TreeWalk walk = new TreeWalk(repository)) { walk.addTree(treeIterator); walk.setFilter(PathFilter.create(repoRelativePath)); while (walk.next()) { @@ -501,8 +494,6 @@ public class RepositoryUtil { .equals(FileMode.TREE)) walk.enterSubtree(); } - } finally { - walk.close(); } return false; } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/RevUtils.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/RevUtils.java index 698252d4d..c5e35a8f0 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/RevUtils.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/RevUtils.java @@ -138,8 +138,7 @@ public class RevUtils { final int skew = 24 * 60 * 60; // one day clock skew - RevWalk walk = new RevWalk(repo); - try { + try (RevWalk walk = new RevWalk(repo)) { RevCommit commit = walk.parseCommit(commitId); for (Ref ref : refs) { RevCommit refCommit = walk.parseCommit(ref.getObjectId()); @@ -154,7 +153,6 @@ public class RevUtils { if (contained) return true; } - } finally { walk.dispose(); } return false; diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/CompareCoreUtils.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/CompareCoreUtils.java index 936f60ae7..c3f2b6230 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/CompareCoreUtils.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/CompareCoreUtils.java @@ -101,32 +101,33 @@ public class CompareCoreUtils { public static DiffEntry getChangeDiffEntry(Repository repository, String newPath, RevCommit newCommit, RevCommit oldCommit, ObjectReader objectReader) throws IOException { - TreeWalk walk = new TreeWalk(objectReader); - walk.setRecursive(true); - walk.addTree(oldCommit.getTree()); - walk.addTree(newCommit.getTree()); + try (TreeWalk walk = new TreeWalk(objectReader)) { + walk.setRecursive(true); + walk.addTree(oldCommit.getTree()); + walk.addTree(newCommit.getTree()); - List entries = DiffEntry.scan(walk); + List entries = DiffEntry.scan(walk); - for (DiffEntry diff : entries) { - if (diff.getChangeType() == ChangeType.MODIFY - && newPath.equals(diff.getNewPath())) - return diff; - } + for (DiffEntry diff : entries) { + if (diff.getChangeType() == ChangeType.MODIFY + && newPath.equals(diff.getNewPath())) + return diff; + } - if (entries.size() < 2) - return null; + if (entries.size() < 2) + return null; - RenameDetector detector = new RenameDetector(repository); - detector.addAll(entries); - List renames = detector.compute(walk.getObjectReader(), - NullProgressMonitor.INSTANCE); - for (DiffEntry diff : renames) { - if (diff.getChangeType() == ChangeType.RENAME - && newPath.equals(diff.getNewPath())) - return diff; - } + RenameDetector detector = new RenameDetector(repository); + detector.addAll(entries); + List renames = detector.compute(walk.getObjectReader(), + NullProgressMonitor.INSTANCE); + for (DiffEntry diff : renames) { + if (diff.getChangeType() == ChangeType.RENAME + && newPath.equals(diff.getNewPath())) + return diff; + } - return null; + return null; + } } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitCommitsModelCache.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitCommitsModelCache.java index 47f9f62f7..5b672ed99 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitCommitsModelCache.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitCommitsModelCache.java @@ -331,59 +331,59 @@ public class GitCommitsModelCache { if (dstId.equals(srcId)) return new ArrayList(0); - final RevWalk rw = new RevWalk(repo); - - final RevFlag localFlag = rw.newFlag("local"); //$NON-NLS-1$ - final RevFlag remoteFlag = rw.newFlag("remote"); //$NON-NLS-1$ - final RevFlagSet allFlags = new RevFlagSet(); - allFlags.add(localFlag); - allFlags.add(remoteFlag); - rw.carry(allFlags); - - RevCommit srcCommit = rw.parseCommit(srcId); - srcCommit.add(localFlag); - rw.markStart(srcCommit); - srcCommit = null; // free not needed resources - - RevCommit dstCommit = rw.parseCommit(dstId); - dstCommit.add(remoteFlag); - rw.markStart(dstCommit); - dstCommit = null; // free not needed resources - - if (pathFilter != null) - rw.setTreeFilter(pathFilter); - - List result = new ArrayList(); - for (RevCommit revCommit : rw) { - if (revCommit.hasAll(allFlags)) - break; - - Commit commit = new Commit(); - commit.shortMessage = revCommit.getShortMessage(); - commit.commitId = AbbreviatedObjectId.fromObjectId(revCommit); - commit.authorName = revCommit.getAuthorIdent().getName(); - commit.committerName = revCommit.getCommitterIdent().getName(); - commit.commitDate = revCommit.getAuthorIdent().getWhen(); - - RevCommit parentCommit = getParentCommit(revCommit); - if (revCommit.has(localFlag)) - // Outgoing - commit.direction = RIGHT; - else if (revCommit.has(remoteFlag)) - // Incoming - commit.direction = LEFT; - else - throw new GitCommitsModelDirectionException(); - - commit.children = getChangedObjects(repo, revCommit, - parentCommit, pathFilter, commit.direction); - - if (commit.children != null) - result.add(commit); + try (RevWalk rw = new RevWalk(repo)) { + + final RevFlag localFlag = rw.newFlag("local"); //$NON-NLS-1$ + final RevFlag remoteFlag = rw.newFlag("remote"); //$NON-NLS-1$ + final RevFlagSet allFlags = new RevFlagSet(); + allFlags.add(localFlag); + allFlags.add(remoteFlag); + rw.carry(allFlags); + + RevCommit srcCommit = rw.parseCommit(srcId); + srcCommit.add(localFlag); + rw.markStart(srcCommit); + srcCommit = null; // free not needed resources + + RevCommit dstCommit = rw.parseCommit(dstId); + dstCommit.add(remoteFlag); + rw.markStart(dstCommit); + dstCommit = null; // free not needed resources + + if (pathFilter != null) + rw.setTreeFilter(pathFilter); + + List result = new ArrayList(); + for (RevCommit revCommit : rw) { + if (revCommit.hasAll(allFlags)) + break; + + Commit commit = new Commit(); + commit.shortMessage = revCommit.getShortMessage(); + commit.commitId = AbbreviatedObjectId.fromObjectId(revCommit); + commit.authorName = revCommit.getAuthorIdent().getName(); + commit.committerName = revCommit.getCommitterIdent().getName(); + commit.commitDate = revCommit.getAuthorIdent().getWhen(); + + RevCommit parentCommit = getParentCommit(revCommit); + if (revCommit.has(localFlag)) + // Outgoing + commit.direction = RIGHT; + else if (revCommit.has(remoteFlag)) + // Incoming + commit.direction = LEFT; + else + throw new GitCommitsModelDirectionException(); + + commit.children = getChangedObjects(repo, revCommit, + parentCommit, pathFilter, commit.direction); + + if (commit.children != null) + result.add(commit); + } + rw.dispose(); + return result; } - rw.dispose(); - - return result; } private static RevCommit getParentCommit(RevCommit commit) { diff --git a/org.eclipse.egit.mylyn.ui/src/org/eclipse/egit/internal/mylyn/ui/commit/TaskReferenceFactory.java b/org.eclipse.egit.mylyn.ui/src/org/eclipse/egit/internal/mylyn/ui/commit/TaskReferenceFactory.java index f201fdab9..dafc8967b 100644 --- a/org.eclipse.egit.mylyn.ui/src/org/eclipse/egit/internal/mylyn/ui/commit/TaskReferenceFactory.java +++ b/org.eclipse.egit.mylyn.ui/src/org/eclipse/egit/internal/mylyn/ui/commit/TaskReferenceFactory.java @@ -79,14 +79,13 @@ public class TaskReferenceFactory implements IAdapterFactory { private AbstractTaskReference adaptFromRevCommit(RevCommit commit) { Repository[] repositories = Activator.getDefault().getRepositoryCache().getAllRepositories(); for (Repository r : repositories) { - RevWalk revWalk = new RevWalk(r); String repoUrl = null; String message = null; long timestamp = 0; // try to get repository url and commit message - try { + try (RevWalk revWalk = new RevWalk(r)) { RevCommit revCommit = revWalk.parseCommit(commit); if (revCommit != null) { repoUrl = getRepoUrl(r); @@ -127,8 +126,9 @@ public class TaskReferenceFactory implements IAdapterFactory { GitModelRepository parent = (GitModelRepository) modelCommit.getParent(); Repository repo = parent.getRepository(); AbbreviatedObjectId id = modelCommit.getCachedCommitObj().getId(); - - commit = new RevWalk(repo).lookupCommit(id.toObjectId()); + try (RevWalk rw = new RevWalk(repo)) { + commit = rw.lookupCommit(id.toObjectId()); + } } return commit; } diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java index 36b5c2788..1bf1e586d 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java @@ -366,14 +366,16 @@ public class TestUtil { Set expectedfiles = new HashSet(); for (String path : paths) expectedfiles.add(path); - TreeWalk treeWalk = new TreeWalk(repository); - treeWalk.addTree(repository.resolve("HEAD^{tree}")); - treeWalk.setRecursive(true); - while (treeWalk.next()) { - String path = treeWalk.getPathString(); - if (!expectedfiles.contains(path)) - fail("Repository contains unexpected expected file " + path); - expectedfiles.remove(path); + try (TreeWalk treeWalk = new TreeWalk(repository)) { + treeWalk.addTree(repository.resolve("HEAD^{tree}")); + treeWalk.setRecursive(true); + while (treeWalk.next()) { + String path = treeWalk.getPathString(); + if (!expectedfiles.contains(path)) + fail("Repository contains unexpected expected file " + + path); + expectedfiles.remove(path); + } } if (expectedfiles.size() > 0) { StringBuilder message = new StringBuilder( @@ -401,22 +403,24 @@ public class TestUtil { public static void assertRepositoryContainsFilesWithContent(Repository repository, String... args) throws Exception { HashMap expectedfiles = mkmap(args); - TreeWalk treeWalk = new TreeWalk(repository); - treeWalk.addTree(repository.resolve("HEAD^{tree}")); - treeWalk.setRecursive(true); - while (treeWalk.next()) { - String path = treeWalk.getPathString(); - assertTrue(expectedfiles.containsKey(path)); - ObjectId objectId = treeWalk.getObjectId(0); - byte[] expectedContent = expectedfiles.get(path).getBytes("UTF-8"); - byte[] repoContent = treeWalk.getObjectReader().open(objectId) - .getBytes(); - if (!Arrays.equals(repoContent, expectedContent)) - fail("File " + path + " has repository content " - + new String(repoContent, "UTF-8") - + " instead of expected content " - + new String(expectedContent, "UTF-8")); - expectedfiles.remove(path); + try (TreeWalk treeWalk = new TreeWalk(repository)) { + treeWalk.addTree(repository.resolve("HEAD^{tree}")); + treeWalk.setRecursive(true); + while (treeWalk.next()) { + String path = treeWalk.getPathString(); + assertTrue(expectedfiles.containsKey(path)); + ObjectId objectId = treeWalk.getObjectId(0); + byte[] expectedContent = expectedfiles.get(path) + .getBytes("UTF-8"); + byte[] repoContent = treeWalk.getObjectReader().open(objectId) + .getBytes(); + if (!Arrays.equals(repoContent, expectedContent)) + fail("File " + path + " has repository content " + + new String(repoContent, "UTF-8") + + " instead of expected content " + + new String(expectedContent, "UTF-8")); + expectedfiles.remove(path); + } } if (expectedfiles.size() > 0) { StringBuilder message = new StringBuilder( @@ -510,8 +514,11 @@ public class TestUtil { throws Exception { RevCommit headCommit = null; ObjectId parentId = repository.resolve(Constants.HEAD); - if (parentId != null) - headCommit = new RevWalk(repository).parseCommit(parentId); + if (parentId != null) { + try (RevWalk rw = new RevWalk(repository)) { + headCommit = rw.parseCommit(parentId); + } + } return headCommit; } diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/FetchAndMergeActionTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/FetchAndMergeActionTest.java index 1ef9507f1..22f2ce6ee 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/FetchAndMergeActionTest.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/FetchAndMergeActionTest.java @@ -140,9 +140,10 @@ public class FetchAndMergeActionTest extends LocalRepositoryTestCase { private RevCommit getCommitForHead() throws Exception { Repository repo = lookupRepository(repositoryFile); - RevWalk rw = new RevWalk(repo); - ObjectId id = repo.resolve(repo.getFullBranch()); - return rw.parseCommit(id); + try (RevWalk rw = new RevWalk(repo)) { + ObjectId id = repo.resolve(repo.getFullBranch()); + return rw.parseCommit(id); + } } private void mergeBranch(String branchToMerge, boolean squash) throws Exception { diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/PushActionTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/PushActionTest.java index 520fbfc85..e03dde5f2 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/PushActionTest.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/PushActionTest.java @@ -65,50 +65,52 @@ public class PushActionTest extends LocalRepositoryTestCase { throws Exception, MissingObjectException, IncorrectObjectTypeException, IOException { Repository repo = lookupRepository(remoteRepositoryFile); - RevWalk rw = new RevWalk(repo); - String previous = rw.parseCommit(repo.resolve("HEAD")).name(); + try (RevWalk rw = new RevWalk(repo)) { + String previous = rw.parseCommit(repo.resolve("HEAD")).name(); + touchAndSubmit(null); + SWTBotShell pushDialog = openPushDialog(); + + SWTBotCombo destinationCombo = pushDialog.bot().comboBox(); + String[] items = destinationCombo.items(); + for (int i = 0; i < items.length; i++) { + if (items[i].startsWith(destination)) + destinationCombo.setSelection(i); + } - touchAndSubmit(null); - SWTBotShell pushDialog = openPushDialog(); - - SWTBotCombo destinationCombo = pushDialog.bot().comboBox(); - String[] items = destinationCombo.items(); - for (int i = 0; i < items.length; i++) { - if (items[i].startsWith(destination)) - destinationCombo.setSelection(i); - } - - pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click(); - if (withConfirmPage) pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click(); - pushDialog.bot().button(IDialogConstants.FINISH_LABEL).click(); - SWTBotShell confirm = bot.shell(NLS.bind(UIText.PushResultDialog_title, - destination)); - String result = confirm.bot().tree().getAllItems()[0].getText(); + if (withConfirmPage) + pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click(); + pushDialog.bot().button(IDialogConstants.FINISH_LABEL).click(); + SWTBotShell confirm = bot.shell( + NLS.bind(UIText.PushResultDialog_title, destination)); + String result = confirm.bot().tree().getAllItems()[0].getText(); - assertTrue("Wrong result", result.contains(previous.substring(0, 7))); + assertTrue("Wrong result", + result.contains(previous.substring(0, 7))); - confirm.close(); + confirm.close(); - pushDialog = openPushDialog(); + pushDialog = openPushDialog(); - destinationCombo = pushDialog.bot().comboBox(); - for (int i = 0; i < items.length; i++) { - if (items[i].startsWith(destination)) - destinationCombo.setSelection(i); - } + destinationCombo = pushDialog.bot().comboBox(); + for (int i = 0; i < items.length; i++) { + if (items[i].startsWith(destination)) + destinationCombo.setSelection(i); + } - pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click(); - if (withConfirmPage) pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click(); - pushDialog.bot().button(IDialogConstants.FINISH_LABEL).click(); - confirm = bot.shell(NLS.bind(UIText.PushResultDialog_title, destination)); - result = confirm.bot().tree().getAllItems()[0].getText(); + if (withConfirmPage) + pushDialog.bot().button(IDialogConstants.NEXT_LABEL).click(); + pushDialog.bot().button(IDialogConstants.FINISH_LABEL).click(); + confirm = bot.shell( + NLS.bind(UIText.PushResultDialog_title, destination)); + result = confirm.bot().tree().getAllItems()[0].getText(); - confirm.close(); + confirm.close(); - assertTrue("Wrong result", - result.contains(UIText.PushResultTable_statusUpToDate)); + assertTrue("Wrong result", + result.contains(UIText.PushResultTable_statusUpToDate)); + } } private SWTBotShell openPushDialog() throws Exception { diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/RepositoryActionHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/RepositoryActionHandler.java index ea594e11e..a67991ead 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/RepositoryActionHandler.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/RepositoryActionHandler.java @@ -471,10 +471,9 @@ abstract class RepositoryActionHandler extends AbstractHandler { IResource resource = getSelectedResources()[0]; String path = RepositoryMapping.getMapping(resource.getProject()) .getRepoRelativePath(resource); - RevWalk rw = new RevWalk(repository); - rw.sort(RevSort.COMMIT_TIME_DESC, true); - rw.sort(RevSort.BOUNDARY, true); - try { + try (RevWalk rw = new RevWalk(repository)) { + rw.sort(RevSort.COMMIT_TIME_DESC, true); + rw.sort(RevSort.BOUNDARY, true); if (path.length() > 0) { DiffConfig diffConfig = repository.getConfig().get( DiffConfig.KEY); @@ -502,7 +501,6 @@ abstract class RepositoryActionHandler extends AbstractHandler { } previousCommit = rw.next(); } - } finally { rw.dispose(); } return result; diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/SynchronizeWithMenu.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/SynchronizeWithMenu.java index f1036317e..846e7cefe 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/SynchronizeWithMenu.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/SynchronizeWithMenu.java @@ -222,6 +222,7 @@ public class SynchronizeWithMenu extends ContributionItem implements } catch (IOException e) { Activator.logError(e.getMessage(), e); } finally { + rw.close(); rw.dispose(); } } diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorPage.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorPage.java index fbfae3fe4..58a4682d4 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorPage.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitEditorPage.java @@ -523,8 +523,7 @@ public class CommitEditorPage extends FormPage implements ISchedulingRule { private List loadBranches() { Repository repository = getCommit().getRepository(); RevCommit commit = getCommit().getRevCommit(); - RevWalk revWalk = new RevWalk(repository); - try { + try (RevWalk revWalk = new RevWalk(repository)) { Map refsMap = new HashMap(); refsMap.putAll(repository.getRefDatabase().getRefs( Constants.R_HEADS)); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitHelper.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitHelper.java index f37857a5b..cc7fcaa89 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitHelper.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitHelper.java @@ -100,10 +100,10 @@ public class CommitHelper { private static RevCommit getHeadCommit(Repository repository) { RevCommit headCommit = null; - try { + try (RevWalk rw = new RevWalk(repository)) { ObjectId parentId = repository.resolve(Constants.HEAD); if (parentId != null) - headCommit = new RevWalk(repository).parseCommit(parentId); + headCommit = rw.parseCommit(parentId); } catch (IOException e) { Activator.handleError(UIText.CommitAction_errorRetrievingCommit, e, true); @@ -124,9 +124,9 @@ public class CommitHelper { } private static String getCherryPickOriginalAuthor(Repository mergeRepository) { - try { + try (RevWalk rw = new RevWalk(mergeRepository)) { ObjectId cherryPickHead = mergeRepository.readCherryPickHead(); - PersonIdent author = new RevWalk(mergeRepository).parseCommit( + PersonIdent author = rw.parseCommit( cherryPickHead).getAuthorIdent(); return author.getName() + " <" + author.getEmailAddress() + ">"; //$NON-NLS-1$//$NON-NLS-2$ } catch (IOException e) { diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitSelectionDialog.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitSelectionDialog.java index 8eacdfb44..abbaadbfb 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitSelectionDialog.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/commit/CommitSelectionDialog.java @@ -205,12 +205,13 @@ public class CommitSelectionDialog extends FilteredItemsSelectionDialog { else commitId = repository.resolve(itemsFilter.getPattern()); if (commitId != null) { - RevWalk walk = new RevWalk(repository); - walk.setRetainBody(true); - RevCommit commit = walk.parseCommit(commitId); - contentProvider.add( - new RepositoryCommit(repository, commit), - itemsFilter); + try (RevWalk walk = new RevWalk(repository)) { + walk.setRetainBody(true); + RevCommit commit = walk.parseCommit(commitId); + contentProvider.add( + new RepositoryCommit(repository, commit), + itemsFilter); + } } } catch (RevisionSyntaxException ignored) { // Ignore and advance diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/components/RefContentProposal.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/components/RefContentProposal.java index 6422f7816..d332d6100 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/components/RefContentProposal.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/components/RefContentProposal.java @@ -132,15 +132,18 @@ public class RefContentProposal implements IContentProposal { switch (loader.getType()) { case Constants.OBJ_COMMIT: - RevCommit c = new RevWalk(db).parseCommit(objectId); - appendObjectSummary(sb, UIText.RefContentProposal_commit, c - .getAuthorIdent(), c.getFullMessage()); + try (RevWalk rw = new RevWalk(db)) { + RevCommit c = rw.parseCommit(objectId); + appendObjectSummary(sb, UIText.RefContentProposal_commit, + c.getAuthorIdent(), c.getFullMessage()); + } break; case Constants.OBJ_TAG: - RevWalk walk = new RevWalk(db); - RevTag t = walk.parseTag(objectId); - appendObjectSummary(sb, UIText.RefContentProposal_tag, t - .getTaggerIdent(), t.getFullMessage()); + try (RevWalk rw = new RevWalk(db)) { + RevTag t = rw.parseTag(objectId); + appendObjectSummary(sb, UIText.RefContentProposal_tag, + t.getTaggerIdent(), t.getFullMessage()); + } break; case Constants.OBJ_TREE: sb.append(UIText.RefContentProposal_tree); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/ResetTargetSelectionDialog.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/ResetTargetSelectionDialog.java index 39b034c7e..11a5cdef5 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/ResetTargetSelectionDialog.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/ResetTargetSelectionDialog.java @@ -159,20 +159,26 @@ public class ResetTargetSelectionDialog extends AbstractBranchSelectionDialog { } parsedCommitish = text; getButton(OK).setEnabled(true); - RevWalk rw = new RevWalk(repo); - RevCommit commit = rw.parseCommit(resolved); - sha1.setText(AbbreviatedObjectId.fromObjectId(commit) - .name()); - subject.setText(commit.getShortMessage()); - author.setText(commit.getAuthorIdent().getName() - + " <" //$NON-NLS-1$ - + commit.getAuthorIdent().getEmailAddress() - + "> " + gitDateFormatter.formatDate(commit.getAuthorIdent())); //$NON-NLS-1$ - committer.setText(commit.getCommitterIdent().getName() - + " <" //$NON-NLS-1$ - + commit.getCommitterIdent().getEmailAddress() - + "> " + gitDateFormatter.formatDate(commit.getCommitterIdent())); //$NON-NLS-1$ - rw.dispose(); + try (RevWalk rw = new RevWalk(repo)) { + RevCommit commit = rw.parseCommit(resolved); + sha1.setText(AbbreviatedObjectId + .fromObjectId(commit).name()); + subject.setText(commit.getShortMessage()); + author.setText( + commit.getAuthorIdent().getName() + " <" //$NON-NLS-1$ + + commit.getAuthorIdent() + .getEmailAddress() + + "> " //$NON-NLS-1$ + + gitDateFormatter.formatDate( + commit.getAuthorIdent())); + committer.setText(commit.getCommitterIdent() + .getName() + + " <" //$NON-NLS-1$ + + commit.getCommitterIdent() + .getEmailAddress() + + "> " + gitDateFormatter.formatDate( //$NON-NLS-1$ + commit.getCommitterIdent())); + } } } catch (IOException e1) { setMessage(e1.getMessage(), IMessageProvider.ERROR); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java index ca1ab058b..efab8be75 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/fetch/FetchGerritChangePage.java @@ -667,8 +667,10 @@ public class FetchGerritChangePage extends WizardPage { new URIish(uri), specs, timeout, false).execute(monitor); monitor.worked(1); - return new RevWalk(repository).parseCommit(fetchRes.getAdvertisedRef( - spec.getSource()).getObjectId()); + try (RevWalk rw = new RevWalk(repository)) { + return rw.parseCommit( + fetchRes.getAdvertisedRef(spec.getSource()).getObjectId()); + } } private void createTag(final RefSpec spec, final String textForTag, diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitInfoBuilder.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitInfoBuilder.java index 643ec6d97..d0b1aa4c1 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitInfoBuilder.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitInfoBuilder.java @@ -167,7 +167,7 @@ public class CommitInfoBuilder { d.append(LF); } - try { + try (RevWalk rw = new RevWalk(db)) { List branches = getBranches(commit, allRefs, db); if (!branches.isEmpty()) { d.append(UIText.CommitMessageViewer_branches); @@ -176,7 +176,7 @@ public class CommitInfoBuilder { for (Iterator i = branches.iterator(); i.hasNext();) { Ref head = i.next(); RevCommit p; - p = new RevWalk(db).parseCommit(head.getObjectId()); + p = rw.parseCommit(head.getObjectId()); addLink(d, formatHeadRef(head), styles, p); if (i.hasNext()) { if (count++ <= MAXBRANCHES) { @@ -203,13 +203,13 @@ public class CommitInfoBuilder { if (Activator.getDefault().getPreferenceStore().getBoolean( UIPreferences.HISTORY_SHOW_TAG_SEQUENCE)) { - try { + try (RevWalk rw = new RevWalk(db)) { monitor.setTaskName(UIText.CommitMessageViewer_GettingPreviousTagTaskName); Ref followingTag = getNextTag(false, monitor); if (followingTag != null) { d.append(UIText.CommitMessageViewer_follows); d.append(": "); //$NON-NLS-1$ - RevCommit p = new RevWalk(db).parseCommit(followingTag + RevCommit p = rw.parseCommit(followingTag .getObjectId()); addLink(d, formatTagRef(followingTag), styles, p); d.append(LF); @@ -218,13 +218,13 @@ public class CommitInfoBuilder { Activator.logError(e.getMessage(), e); } - try { + try (RevWalk rw = new RevWalk(db)) { monitor.setTaskName(UIText.CommitMessageViewer_GettingNextTagTaskName); Ref precedingTag = getNextTag(true, monitor); if (precedingTag != null) { d.append(UIText.CommitMessageViewer_precedes); d.append(": "); //$NON-NLS-1$ - RevCommit p = new RevWalk(db).parseCommit(precedingTag + RevCommit p = rw.parseCommit(precedingTag .getObjectId()); addLink(d, formatTagRef(precedingTag), styles, p); d.append(LF); @@ -293,12 +293,9 @@ public class CommitInfoBuilder { Collection allRefs, Repository db) throws MissingObjectException, IncorrectObjectTypeException, IOException { - RevWalk revWalk = new RevWalk(db); - try { + try (RevWalk revWalk = new RevWalk(db)) { revWalk.setRetainBody(false); return RevWalkUtils.findBranchesReachableFrom(commit, revWalk, allRefs); - } finally { - revWalk.dispose(); } } @@ -372,43 +369,48 @@ public class CommitInfoBuilder { throws IOException, OperationCanceledException { if (monitor.isCanceled()) throw new OperationCanceledException(); - RevWalk revWalk = new RevWalk(db); - revWalk.setRetainBody(false); - Map tagsMap = db.getTags(); - Ref tagRef = null; - - for (Ref ref : tagsMap.values()) { - if (monitor.isCanceled()) - throw new OperationCanceledException(); - // both RevCommits must be allocated using same RevWalk instance, - // otherwise isMergedInto returns wrong result! - RevCommit current = revWalk.parseCommit(commit); - // tags can point to any object, we only want tags pointing at - // commits - RevObject any = revWalk.peel(revWalk.parseAny(ref.getObjectId())); - if (!(any instanceof RevCommit)) - continue; - RevCommit newTag = (RevCommit) any; - if (newTag.getId().equals(commit)) - continue; - - // check if newTag matches our criteria - if (isMergedInto(revWalk, newTag, current, searchDescendant)) { + try (RevWalk revWalk = new RevWalk(db)) { + revWalk.setRetainBody(false); + Map tagsMap = db.getTags(); + Ref tagRef = null; + + for (Ref ref : tagsMap.values()) { if (monitor.isCanceled()) throw new OperationCanceledException(); - if (tagRef != null) { - RevCommit oldTag = revWalk - .parseCommit(tagRef.getObjectId()); - - // both oldTag and newTag satisfy search criteria, so taking - // the closest one - if (isMergedInto(revWalk, oldTag, newTag, searchDescendant)) + // both RevCommits must be allocated using same RevWalk + // instance, + // otherwise isMergedInto returns wrong result! + RevCommit current = revWalk.parseCommit(commit); + // tags can point to any object, we only want tags pointing at + // commits + RevObject any = revWalk + .peel(revWalk.parseAny(ref.getObjectId())); + if (!(any instanceof RevCommit)) + continue; + RevCommit newTag = (RevCommit) any; + if (newTag.getId().equals(commit)) + continue; + + // check if newTag matches our criteria + if (isMergedInto(revWalk, newTag, current, searchDescendant)) { + if (monitor.isCanceled()) + throw new OperationCanceledException(); + if (tagRef != null) { + RevCommit oldTag = revWalk + .parseCommit(tagRef.getObjectId()); + + // both oldTag and newTag satisfy search criteria, so + // taking + // the closest one + if (isMergedInto(revWalk, oldTag, newTag, + searchDescendant)) + tagRef = ref; + } else tagRef = ref; - } else - tagRef = ref; + } } + return tagRef; } - return tagRef; } /** diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/GitHistoryPage.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/GitHistoryPage.java index b4dad3798..73d1cbf71 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/GitHistoryPage.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/GitHistoryPage.java @@ -1403,8 +1403,7 @@ public class GitHistoryPage extends HistoryPage implements RefsChangedListener, } private void showHead(Repository repo) { - RevWalk rw = new RevWalk(repo); - try { + try (RevWalk rw = new RevWalk(repo)) { ObjectId head = repo.resolve(Constants.HEAD); if (head == null) return; @@ -1416,8 +1415,7 @@ public class GitHistoryPage extends HistoryPage implements RefsChangedListener, } private void showRef(Ref ref, Repository repo) { - RevWalk rw = new RevWalk(repo); - try { + try (RevWalk rw = new RevWalk(repo)) { RevCommit c = rw.parseCommit(ref.getLeaf().getObjectId()); graph.selectCommit(c); } catch (IOException e) { @@ -1426,8 +1424,7 @@ public class GitHistoryPage extends HistoryPage implements RefsChangedListener, } private void showTag(Ref ref, Repository repo) { - RevWalk rw = new RevWalk(repo); - try { + try (RevWalk rw = new RevWalk(repo)) { RevCommit c = null; RevObject any = rw.parseAny(ref.getLeaf().getObjectId()); if (any instanceof RevCommit) diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/AbstractHistoryCommandHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/AbstractHistoryCommandHandler.java index 9866ca3be..37e3fa663 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/AbstractHistoryCommandHandler.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/AbstractHistoryCommandHandler.java @@ -129,15 +129,16 @@ abstract class AbstractHistoryCommandHandler extends AbstractHandler { Repository repo = getRepository(event); Collection revTags = repo.getTags().values(); List tags = new ArrayList(); - RevWalk walk = new RevWalk(repo); - for (Ref ref : revTags) { - try { - tags.add(walk.parseTag(repo.resolve(ref.getName()))); - } catch (IOException e) { - throw new ExecutionException(e.getMessage(), e); + try (RevWalk walk = new RevWalk(repo)) { + for (Ref ref : revTags) { + try { + tags.add(walk.parseTag(repo.resolve(ref.getName()))); + } catch (IOException e) { + throw new ExecutionException(e.getMessage(), e); + } } + return tags; } - return tags; } protected GitHistoryPage getPage() { diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/CompareWithWorkingTreeHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/CompareWithWorkingTreeHandler.java index 3819c7977..352a0bc30 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/CompareWithWorkingTreeHandler.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/CompareWithWorkingTreeHandler.java @@ -62,8 +62,8 @@ public class CompareWithWorkingTreeHandler extends // TODO can we create a ITypedElement from the local file? Repository repo = getRepository(event); RevCommit leftCommit; - try { - leftCommit = new RevWalk(repo).parseCommit(repo + try (RevWalk rw = new RevWalk(repo)) { + leftCommit = rw.parseCommit(repo .resolve(Constants.HEAD)); } catch (Exception e) { throw new ExecutionException(e.getMessage(), e); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/ShowVersionsHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/ShowVersionsHandler.java index 36847097a..af4b2254a 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/ShowVersionsHandler.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/ShowVersionsHandler.java @@ -129,10 +129,10 @@ public class ShowVersionsHandler extends AbstractHistoryCommandHandler { } if (rev != null) { if (compareMode) - try { + try (RevWalk rw = new RevWalk(repo)) { ITypedElement left = CompareUtils .getFileRevisionTypedElement(gitPath, - new RevWalk(repo).parseCommit(repo + rw.parseCommit(repo .resolve(Constants.HEAD)), repo); ITypedElement right = CompareUtils diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/merge/GitCompareEditorInput.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/merge/GitCompareEditorInput.java index e6fd6a107..be171b86e 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/merge/GitCompareEditorInput.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/merge/GitCompareEditorInput.java @@ -113,15 +113,14 @@ public class GitCompareEditorInput extends CompareEditorInput { protected Object prepareInput(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { // make sure all resources belong to the same repository - RevWalk rw = null; - try { + try (RevWalk rw = new RevWalk(repository)) { monitor.beginTask( UIText.GitCompareEditorInput_CompareResourcesTaskName, IProgressMonitor.UNKNOWN); for (IResource resource : resources) { - RepositoryMapping map = RepositoryMapping.getMapping(resource - .getProject()); + RepositoryMapping map = RepositoryMapping + .getMapping(resource.getProject()); if (repository != null && repository != map.getRepository()) throw new InvocationTargetException( new IllegalStateException( @@ -134,80 +133,81 @@ public class GitCompareEditorInput extends CompareEditorInput { return FOLDER_IMAGE; } }; - diffRoots - .put(new Path(map.getRepoRelativePath(resource)), node); + diffRoots.put(new Path(map.getRepoRelativePath(resource)), + node); repository = map.getRepository(); } if (repository == null) - throw new InvocationTargetException( - new IllegalStateException( - UIText.GitCompareEditorInput_ResourcesInDifferentReposMessagge)); + throw new InvocationTargetException(new IllegalStateException( + UIText.GitCompareEditorInput_ResourcesInDifferentReposMessagge)); if (monitor.isCanceled()) throw new InterruptedException(); - rw = new RevWalk(repository); - final RevCommit baseCommit; try { - baseCommit = rw.parseCommit(repository.resolve(baseVersion)); - } catch (IOException e) { - throw new InvocationTargetException(e); - } - - final RevCommit compareCommit; - if (compareVersion == null) - compareCommit = null; - else try { - compareCommit = rw.parseCommit(repository - .resolve(compareVersion)); + baseCommit = rw + .parseCommit(repository.resolve(baseVersion)); } catch (IOException e) { throw new InvocationTargetException(e); } - if (monitor.isCanceled()) - throw new InterruptedException(); + final RevCommit compareCommit; + if (compareVersion == null) { + compareCommit = null; + } else { + try { + compareCommit = rw.parseCommit( + repository.resolve(compareVersion)); + } catch (IOException e) { + throw new InvocationTargetException(e); + } + } + if (monitor.isCanceled()) + throw new InterruptedException(); - // set the labels - CompareConfiguration config = getCompareConfiguration(); - config.setLeftLabel(compareVersion); - config.setRightLabel(baseVersion); - // set title and icon - if (resources.length == 0) { - Object[] titleParameters = new Object[] { - Activator.getDefault().getRepositoryUtil() - .getRepositoryName(repository), - CompareUtils.truncatedRevision(compareVersion), - CompareUtils.truncatedRevision(baseVersion) }; - setTitle(NLS.bind(UIText.GitCompareEditorInput_EditorTitle, - titleParameters)); - } else if (resources.length == 1) { - Object[] titleParameters = new Object[] { - resources[0].getFullPath().makeRelative().toString(), - CompareUtils.truncatedRevision(compareVersion), - CompareUtils.truncatedRevision(baseVersion) }; - setTitle(NLS.bind( - UIText.GitCompareEditorInput_EditorTitleSingleResource, - titleParameters)); - } else - setTitle(NLS - .bind( - UIText.GitCompareEditorInput_EditorTitleMultipleResources, - CompareUtils.truncatedRevision(compareVersion), - CompareUtils.truncatedRevision(baseVersion))); + // set the labels + CompareConfiguration config = getCompareConfiguration(); + config.setLeftLabel(compareVersion); + config.setRightLabel(baseVersion); + // set title and icon + if (resources.length == 0) { + Object[] titleParameters = new Object[] { + Activator.getDefault().getRepositoryUtil() + .getRepositoryName(repository), + CompareUtils.truncatedRevision(compareVersion), + CompareUtils.truncatedRevision(baseVersion) }; + setTitle(NLS.bind(UIText.GitCompareEditorInput_EditorTitle, + titleParameters)); + } else if (resources.length == 1) { + Object[] titleParameters = new Object[] { + resources[0].getFullPath().makeRelative() + .toString(), + CompareUtils.truncatedRevision(compareVersion), + CompareUtils.truncatedRevision(baseVersion) }; + setTitle(NLS.bind( + UIText.GitCompareEditorInput_EditorTitleSingleResource, + titleParameters)); + } else { + setTitle(NLS + .bind(UIText.GitCompareEditorInput_EditorTitleMultipleResources, + CompareUtils.truncatedRevision( + compareVersion), + CompareUtils.truncatedRevision(baseVersion))); + } - // build the nodes - try { - return buildDiffContainer(baseCommit, compareCommit, monitor); - } catch (IOException e) { - throw new InvocationTargetException(e); + // build the nodes + try { + return buildDiffContainer(baseCommit, compareCommit, + monitor); + } catch (IOException e) { + throw new InvocationTargetException(e); + } + } finally { + monitor.done(); } - } finally { - if (rw != null) - rw.dispose(); - monitor.done(); } } diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/merge/MergeResultDialog.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/merge/MergeResultDialog.java index c893795fd..dbc78c6b7 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/merge/MergeResultDialog.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/merge/MergeResultDialog.java @@ -298,24 +298,25 @@ public class MergeResultDialog extends Dialog { private RepositoryCommit[] getCommits(final ObjectId[] merges) { final List commits = new ArrayList(); - final RevWalk walk = new RevWalk(objectReader); - walk.setRetainBody(true); - for (ObjectId merge : merges) - try { - commits.add(new RepositoryCommit(repository, walk - .parseCommit(merge))); - } catch (IOException e) { - Activator.logError(MessageFormat.format( - UIText.MergeResultDialog_couldNotFindCommit, - merge.name()), e); - } - return commits.toArray(new RepositoryCommit[commits.size()]); + try (final RevWalk walk = new RevWalk(objectReader)) { + walk.setRetainBody(true); + for (ObjectId merge : merges) + try { + commits.add(new RepositoryCommit(repository, + walk.parseCommit(merge))); + } catch (IOException e) { + Activator.logError(MessageFormat.format( + UIText.MergeResultDialog_couldNotFindCommit, + merge.name()), e); + } + return commits.toArray(new RepositoryCommit[commits.size()]); + } } private String getCommitMessage(ObjectId id) { RevCommit commit; - try { - commit = new RevWalk(objectReader).parseCommit(id); + try (RevWalk rw = new RevWalk(objectReader)) { + commit = rw.parseCommit(id); } catch (IOException e) { Activator.logError(UIText.MergeResultDialog_couldNotFindCommit, e); return UIText.MergeResultDialog_couldNotFindCommit; diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/PushBranchPage.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/PushBranchPage.java index b294adfbf..8cce15cf8 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/PushBranchPage.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/PushBranchPage.java @@ -219,12 +219,11 @@ public class PushBranchPage extends WizardPage { commitIcon.getBounds().height)); Label commit = new Label(sourceComposite, SWT.NONE); - RevWalk revWalk = new RevWalk(repository); StringBuilder commitBuilder = new StringBuilder(this.commitToPush .abbreviate(7).name()); StringBuilder commitTooltipBuilder = new StringBuilder( this.commitToPush.getName()); - try { + try (RevWalk revWalk = new RevWalk(repository)) { RevCommit revCommit = revWalk.parseCommit(this.commitToPush); commitBuilder.append(" "); //$NON-NLS-1$ commitBuilder.append(Utils.shortenText(revCommit.getShortMessage(), diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/RefUpdateElement.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/RefUpdateElement.java index 8daab02fa..130f7f0eb 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/RefUpdateElement.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/push/RefUpdateElement.java @@ -167,8 +167,7 @@ class RefUpdateElement extends WorkbenchAdapter { } private RepositoryCommit[] getCommits(Ref end) { - final RevWalk walk = new RevWalk(reader); - try { + try (final RevWalk walk = new RevWalk(reader)) { walk.setRetainBody(true); walk.markStart(walk.parseCommit(update.getNewObjectId())); walk.markUninteresting(walk.parseCommit(end.getObjectId())); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/CreateBranchWizard.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/CreateBranchWizard.java index 35c42fc48..83ce84e9b 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/CreateBranchWizard.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/CreateBranchWizard.java @@ -49,11 +49,11 @@ public class CreateBranchWizard extends Wizard { * a {@link Ref} name or {@link RevCommit} id, or null */ public CreateBranchWizard(Repository repository, String base) { - try { + try (RevWalk rw = new RevWalk(repository)) { if (base == null) { myPage = new CreateBranchPage(repository, (Ref) null); } else if (ObjectId.isId(base)) { - RevCommit commit = new RevWalk(repository).parseCommit(ObjectId + RevCommit commit = rw.parseCommit(ObjectId .fromString(base)); myPage = new CreateBranchPage(repository, commit); } else { @@ -64,8 +64,8 @@ public class CreateBranchWizard extends Wizard { myPage = new CreateBranchPage(repository, currentBranch); } else { // the page only knows some special Refs - RevCommit commit = new RevWalk(repository) - .parseCommit(repository.resolve(base + "^{commit}")); //$NON-NLS-1$ + RevCommit commit = rw.parseCommit( + repository.resolve(base + "^{commit}")); //$NON-NLS-1$ myPage = new CreateBranchPage(repository, commit); } } diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/CreateBranchCommand.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/CreateBranchCommand.java index cbc7fc337..171089f1b 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/CreateBranchCommand.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/CreateBranchCommand.java @@ -40,8 +40,8 @@ public class CreateBranchCommand extends if (node.getType() == RepositoryTreeNodeType.ADDITIONALREF) { Ref ref = (Ref) node.getObject(); - try { - RevCommit baseCommit = new RevWalk(node.getRepository()) + try (RevWalk rw = new RevWalk(node.getRepository())) { + RevCommit baseCommit = rw .parseCommit(ref.getLeaf().getObjectId()); WizardDialog dlg = new WizardDialog( getShell(event), diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/RemoveCommand.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/RemoveCommand.java index 01b53a7e3..ce346a0cf 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/RemoveCommand.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/RemoveCommand.java @@ -274,21 +274,24 @@ public class RemoveCommand extends private boolean isTracked(File file, Repository repo) throws IOException { ObjectId objectId = repo.resolve(Constants.HEAD); RevTree tree; - if (objectId != null) - tree = new RevWalk(repo).parseTree(objectId); - else - tree = null; + try (RevWalk rw = new RevWalk(repo); + TreeWalk treeWalk = new TreeWalk(repo)) { + if (objectId != null) + tree = rw.parseTree(objectId); + else + tree = null; - TreeWalk treeWalk = new TreeWalk(repo); - treeWalk.setRecursive(true); - if (tree != null) - treeWalk.addTree(tree); - else - treeWalk.addTree(new EmptyTreeIterator()); - treeWalk.addTree(new DirCacheIterator(repo.readDirCache())); - treeWalk.setFilter(PathFilterGroup.createFromStrings(Collections.singleton( - Repository.stripWorkDir(repo.getWorkTree(), file)))); - return treeWalk.next(); + treeWalk.setRecursive(true); + if (tree != null) + treeWalk.addTree(tree); + else + treeWalk.addTree(new EmptyTreeIterator()); + treeWalk.addTree(new DirCacheIterator(repo.readDirCache())); + treeWalk.setFilter(PathFilterGroup + .createFromStrings(Collections.singleton(Repository + .stripWorkDir(repo.getWorkTree(), file)))); + return treeWalk.next(); + } } diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/search/CommitSearchQuery.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/search/CommitSearchQuery.java index 4ff7eae2c..5b61ebeaa 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/search/CommitSearchQuery.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/search/CommitSearchQuery.java @@ -198,8 +198,7 @@ public class CommitSearchQuery implements ISearchQuery { private void walkRepository(Repository repository, Pattern pattern, IProgressMonitor monitor) throws IOException { - RevWalk walk = new RevWalk(repository); - try { + try (RevWalk walk = new RevWalk(repository)) { walk.setRetainBody(true); List commits = new LinkedList(); if (this.settings.isAllBranches()) { @@ -230,8 +229,6 @@ public class CommitSearchQuery implements ISearchQuery { } } } - } finally { - walk.dispose(); } } diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/sharing/ExistingOrNewPage.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/sharing/ExistingOrNewPage.java index 1c9baf93f..f7845da00 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/sharing/ExistingOrNewPage.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/sharing/ExistingOrNewPage.java @@ -535,17 +535,17 @@ class ExistingOrNewPage extends WizardPage { ObjectId headCommitId = repo.resolve(Constants.HEAD); if (headCommitId != null) { // Not an empty repo - RevWalk revWalk = new RevWalk(repo); - RevCommit headCommit = revWalk - .parseCommit(headCommitId); - RevTree headTree = headCommit.getTree(); - TreeWalk projectInRepo = TreeWalk.forPath(repo, - repoRelativePath, headTree); - if (projectInRepo != null) { - // the .project file is tracked by this repo - treeItem.setChecked(true); + try (RevWalk revWalk = new RevWalk(repo)) { + RevCommit headCommit = revWalk + .parseCommit(headCommitId); + RevTree headTree = headCommit.getTree(); + TreeWalk projectInRepo = TreeWalk.forPath(repo, + repoRelativePath, headTree); + if (projectInRepo != null) { + // the .project file is tracked by this repo + treeItem.setChecked(true); + } } - revWalk.dispose(); } } repo.close(); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/synchronize/model/GitModelBlob.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/synchronize/model/GitModelBlob.java index 9ddd23920..80487b87d 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/synchronize/model/GitModelBlob.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/synchronize/model/GitModelBlob.java @@ -247,11 +247,10 @@ public class GitModelBlob extends GitModelObject implements ComparisonDataSource baseData; ComparisonDataSource remoteData; - RevWalk rw = new RevWalk(repo); - rw.setRetainBody(true); RevCommit baseCommit = null; RevCommit remoteCommit = null; - try { + try (RevWalk rw = new RevWalk(repo)) { + rw.setRetainBody(true); if (change.getCommitId() != null) baseCommit = rw.parseCommit(change.getCommitId().toObjectId()); if (change.getRemoteCommitId() != null) @@ -259,8 +258,6 @@ public class GitModelBlob extends GitModelObject implements .toObjectId()); } catch (IOException e) { Activator.logError(e.getMessage(), e); - } finally { - rw.dispose(); } if (baseCommit == null && remoteCommit != null) baseCommit = remoteCommit; // prevent from NPE for deleted files -- 2.11.4.GIT