From 5ca9aaa17277c2d7f33508afcdc8f748fada8881 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 12:25:20 +0900 Subject: [PATCH 01/16] RepoCommandTest: Open Git instances in try-with-resource Change-Id: I171e84eeb7862e74761ba6c961f14c86beaba9e7 Signed-off-by: David Pursehouse --- .../eclipse/jgit/gitrepo/RepoCommandTest.java | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java index 524d0b8e7..77ef1a646 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java @@ -82,38 +82,42 @@ public class RepoCommandTest extends RepositoryTestCase { super.setUp(); defaultDb = createWorkRepository(); - Git git = new Git(defaultDb); - JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "branch world"); - git.add().addFilepattern("hello.txt").call(); - oldCommitId = git.commit().setMessage("Initial commit").call().getId(); - git.checkout().setName(BRANCH).setCreateBranch(true).call(); - git.checkout().setName("master").call(); - git.tag().setName(TAG).call(); - JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "master world"); - git.add().addFilepattern("hello.txt").call(); - git.commit().setMessage("Second commit").call(); - addRepoToClose(defaultDb); + try (Git git = new Git(defaultDb)) { + JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "branch world"); + git.add().addFilepattern("hello.txt").call(); + oldCommitId = git.commit().setMessage("Initial commit").call().getId(); + git.checkout().setName(BRANCH).setCreateBranch(true).call(); + git.checkout().setName("master").call(); + git.tag().setName(TAG).call(); + JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "master world"); + git.add().addFilepattern("hello.txt").call(); + git.commit().setMessage("Second commit").call(); + addRepoToClose(defaultDb); + } notDefaultDb = createWorkRepository(); - git = new Git(notDefaultDb); - JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello"); - git.add().addFilepattern("world.txt").call(); - git.commit().setMessage("Initial commit").call(); - addRepoToClose(notDefaultDb); + try (Git git = new Git(notDefaultDb)) { + JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello"); + git.add().addFilepattern("world.txt").call(); + git.commit().setMessage("Initial commit").call(); + addRepoToClose(notDefaultDb); + } groupADb = createWorkRepository(); - git = new Git(groupADb); - JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world"); - git.add().addFilepattern("a.txt").call(); - git.commit().setMessage("Initial commit").call(); - addRepoToClose(groupADb); + try (Git git = new Git(groupADb)) { + JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world"); + git.add().addFilepattern("a.txt").call(); + git.commit().setMessage("Initial commit").call(); + addRepoToClose(groupADb); + } groupBDb = createWorkRepository(); - git = new Git(groupBDb); - JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world"); - git.add().addFilepattern("b.txt").call(); - git.commit().setMessage("Initial commit").call(); - addRepoToClose(groupBDb); + try (Git git = new Git(groupBDb)) { + JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world"); + git.add().addFilepattern("b.txt").call(); + git.commit().setMessage("Initial commit").call(); + addRepoToClose(groupBDb); + } resolveRelativeUris(); } From 77887b49f93707248c996473db1d8672e33235b0 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 12:26:58 +0900 Subject: [PATCH 02/16] ReflogTest: Open Git instances in try-with-resource Change-Id: I950b6f16148cfe11de729b04904f88d6e4c28b9a Signed-off-by: David Pursehouse --- .../tst/org/eclipse/jgit/pgm/ReflogTest.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java index ce808326b..7330ee97b 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ReflogTest.java @@ -57,24 +57,27 @@ public class ReflogTest extends CLIRepositoryTestCase { @Test public void testSingleCommit() throws Exception { - new Git(db).commit().setMessage("initial commit").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("initial commit").call(); - assertEquals("6fd41be HEAD@{0}: commit (initial): initial commit", - execute("git reflog")[0]); + assertEquals("6fd41be HEAD@{0}: commit (initial): initial commit", + execute("git reflog")[0]); + } } @Test public void testBranch() throws Exception { - Git git = new Git(db); - git.commit().setMessage("first commit").call(); - git.checkout().setCreateBranch(true).setName("side").call(); - writeTrashFile("file", "side content"); - git.add().addFilepattern("file").call(); - git.commit().setMessage("side commit").call(); + try (Git git = new Git(db)) { + git.commit().setMessage("first commit").call(); + git.checkout().setCreateBranch(true).setName("side").call(); + writeTrashFile("file", "side content"); + git.add().addFilepattern("file").call(); + git.commit().setMessage("side commit").call(); - assertArrayEquals(new String[] { - "38890c7 side@{0}: commit: side commit", - "d216986 side@{1}: branch: Created from commit first commit", - "" }, execute("git reflog refs/heads/side")); + assertArrayEquals(new String[] { + "38890c7 side@{0}: commit: side commit", + "d216986 side@{1}: branch: Created from commit first commit", + "" }, execute("git reflog refs/heads/side")); + } } } \ No newline at end of file From 952f381981b5d537f354d967e413a1b5b162c002 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 12:29:44 +0900 Subject: [PATCH 03/16] ReflogResolveTest: Open Git instances in try-with-resource Change-Id: I11ee38bfcf4951bf05a1632df08b1d074d38338b Signed-off-by: David Pursehouse --- .../eclipse/jgit/lib/ReflogResolveTest.java | 182 +++++++++--------- 1 file changed, 94 insertions(+), 88 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogResolveTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogResolveTest.java index 75d74fca7..7db9f60fd 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogResolveTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogResolveTest.java @@ -60,117 +60,123 @@ public class ReflogResolveTest extends RepositoryTestCase { @Test public void resolveMasterCommits() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - RevCommit c1 = git.commit().setMessage("create file").call(); - writeTrashFile("file.txt", "content2"); - git.add().addFilepattern("file.txt").call(); - RevCommit c2 = git.commit().setMessage("edit file").call(); - - assertEquals(c2, db.resolve("master@{0}")); - assertEquals(c1, db.resolve("master@{1}")); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + RevCommit c1 = git.commit().setMessage("create file").call(); + writeTrashFile("file.txt", "content2"); + git.add().addFilepattern("file.txt").call(); + RevCommit c2 = git.commit().setMessage("edit file").call(); + + assertEquals(c2, db.resolve("master@{0}")); + assertEquals(c1, db.resolve("master@{1}")); + } } @Test public void resolveUnnamedCurrentBranchCommits() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - RevCommit c1 = git.commit().setMessage("create file").call(); - writeTrashFile("file.txt", "content2"); - git.add().addFilepattern("file.txt").call(); - RevCommit c2 = git.commit().setMessage("edit file").call(); - - assertEquals(c2, db.resolve("master@{0}")); - assertEquals(c1, db.resolve("master@{1}")); - - git.checkout().setCreateBranch(true).setName("newbranch") - .setStartPoint(c1).call(); - - // same as current branch, e.g. master - assertEquals(c1, db.resolve("@{0}")); - try { + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + RevCommit c1 = git.commit().setMessage("create file").call(); + writeTrashFile("file.txt", "content2"); + git.add().addFilepattern("file.txt").call(); + RevCommit c2 = git.commit().setMessage("edit file").call(); + + assertEquals(c2, db.resolve("master@{0}")); + assertEquals(c1, db.resolve("master@{1}")); + + git.checkout().setCreateBranch(true).setName("newbranch") + .setStartPoint(c1).call(); + + // same as current branch, e.g. master + assertEquals(c1, db.resolve("@{0}")); + try { + assertEquals(c1, db.resolve("@{1}")); + fail(); // Looking at wrong ref, e.g HEAD + } catch (RevisionSyntaxException e) { + assertNotNull(e); + } + + // detached head, read HEAD reflog + git.checkout().setName(c2.getName()).call(); + assertEquals(c2, db.resolve("@{0}")); assertEquals(c1, db.resolve("@{1}")); - fail(); // Looking at wrong ref, e.g HEAD - } catch (RevisionSyntaxException e) { - assertNotNull(e); + assertEquals(c2, db.resolve("@{2}")); } - - // detached head, read HEAD reflog - git.checkout().setName(c2.getName()).call(); - assertEquals(c2, db.resolve("@{0}")); - assertEquals(c1, db.resolve("@{1}")); - assertEquals(c2, db.resolve("@{2}")); } @Test public void resolveReflogParent() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - RevCommit c1 = git.commit().setMessage("create file").call(); - writeTrashFile("file.txt", "content2"); - git.add().addFilepattern("file.txt").call(); - git.commit().setMessage("edit file").call(); - - assertEquals(c1, db.resolve("master@{0}~1")); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + RevCommit c1 = git.commit().setMessage("create file").call(); + writeTrashFile("file.txt", "content2"); + git.add().addFilepattern("file.txt").call(); + git.commit().setMessage("edit file").call(); + + assertEquals(c1, db.resolve("master@{0}~1")); + } } @Test public void resolveNonExistingBranch() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - git.commit().setMessage("create file").call(); - assertNull(db.resolve("notabranch@{7}")); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + git.commit().setMessage("create file").call(); + assertNull(db.resolve("notabranch@{7}")); + } } @Test public void resolvePreviousBranch() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - RevCommit c1 = git.commit().setMessage("create file").call(); - writeTrashFile("file.txt", "content2"); - git.add().addFilepattern("file.txt").call(); - RevCommit c2 = git.commit().setMessage("edit file").call(); - - git.checkout().setCreateBranch(true).setName("newbranch") - .setStartPoint(c1).call(); - - git.checkout().setName(c1.getName()).call(); - - git.checkout().setName("master").call(); - - assertEquals(c1.getName(), db.simplify("@{-1}")); - assertEquals("newbranch", db.simplify("@{-2}")); - assertEquals("master", db.simplify("@{-3}")); - - // chained expression - try { - // Cannot refer to reflog of detached head - db.resolve("@{-1}@{0}"); - fail(); - } catch (RevisionSyntaxException e) { - // good + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + RevCommit c1 = git.commit().setMessage("create file").call(); + writeTrashFile("file.txt", "content2"); + git.add().addFilepattern("file.txt").call(); + RevCommit c2 = git.commit().setMessage("edit file").call(); + + git.checkout().setCreateBranch(true).setName("newbranch") + .setStartPoint(c1).call(); + + git.checkout().setName(c1.getName()).call(); + + git.checkout().setName("master").call(); + + assertEquals(c1.getName(), db.simplify("@{-1}")); + assertEquals("newbranch", db.simplify("@{-2}")); + assertEquals("master", db.simplify("@{-3}")); + + // chained expression + try { + // Cannot refer to reflog of detached head + db.resolve("@{-1}@{0}"); + fail(); + } catch (RevisionSyntaxException e) { + // good + } + assertEquals(c1.getName(), db.resolve("@{-2}@{0}").getName()); + + assertEquals(c2.getName(), db.resolve("@{-3}@{0}").getName()); } - assertEquals(c1.getName(), db.resolve("@{-2}@{0}").getName()); - - assertEquals(c2.getName(), db.resolve("@{-3}@{0}").getName()); } @Test public void resolveDate() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - git.commit().setMessage("create file").call(); - try { - db.resolve("master@{yesterday}"); - fail("Exception not thrown"); - } catch (RevisionSyntaxException e) { - assertNotNull(e); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + git.commit().setMessage("create file").call(); + try { + db.resolve("master@{yesterday}"); + fail("Exception not thrown"); + } catch (RevisionSyntaxException e) { + assertNotNull(e); + } } } } \ No newline at end of file From 615280ba0789994b548cf847e9d149b96ea57aab Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 12:30:46 +0900 Subject: [PATCH 04/16] RefDirectoryTest: Fix warning about member variable hiding The parameter name 'totalWork' was hiding a class member variable of the same name. Change-Id: I646525e82900e23ffabfc756bcf5052ef873656a Signed-off-by: David Pursehouse --- .../eclipse/jgit/internal/storage/file/RefDirectoryTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefDirectoryTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefDirectoryTest.java index 52e181bc8..ef5dfcd8e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefDirectoryTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefDirectoryTest.java @@ -1444,8 +1444,8 @@ public class RefDirectoryTest extends LocalDiskRepositoryTestCase { // empty } - public void beginTask(String title, int totalWork) { - this.totalWork = totalWork; + public void beginTask(String title, int total) { + this.totalWork = total; lastWork = 0; } From 767d5f4e59fc9901d5348154c78db5d90a04bfca Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 12:39:06 +0900 Subject: [PATCH 05/16] Update .mailmap - List names alphabetically by Firstname Surname - Replace tabs with spaces - Add missing name Change-Id: Ib320dad316e0f6e3e95363c4d6a64744032db857 Signed-off-by: David Pursehouse --- .mailmap | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.mailmap b/.mailmap index 8701b1919..8f11a1c49 100644 --- a/.mailmap +++ b/.mailmap @@ -1,5 +1,6 @@ -Shawn Pearce Shawn O. Pearce -Shawn Pearce Shawn Pearce -Shawn Pearce Shawn O. Pearce -Saša Živkov Sasa Zivkov -Saša Živkov Saša Živkov +Roberto Tyley roberto +Saša Živkov Sasa Zivkov +Saša Živkov Saša Živkov +Shawn Pearce Shawn O. Pearce +Shawn Pearce Shawn Pearce +Shawn Pearce Shawn O. Pearce From 964da41d52258e88d4f79b0d483a7ee5505c7f0e Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 13:43:56 +0900 Subject: [PATCH 06/16] ReflogConfigTest: refactor commit method to avoid variable hiding The author and committer parameters were hiding class members of the same name. Since the passed in PersonIdent instances were being created from the class members anyway, remove the parameters and instead create the PersonIdent instances inline in the method. Change-Id: I66b057df388835d57f332fdcbadb8a9f4e1094a4 Signed-off-by: David Pursehouse --- .../org/eclipse/jgit/lib/ReflogConfigTest.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java index 7b6d7d4b0..df1a52dc0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReflogConfigTest.java @@ -70,8 +70,7 @@ public class ReflogConfigTest extends RepositoryTestCase { // do one commit and check that reflog size is 0: no reflogs should be // written - commit("A Commit\n", new PersonIdent(author, commitTime, tz), - new PersonIdent(committer, commitTime, tz)); + commit("A Commit\n", commitTime, tz); commitTime += 60 * 1000; assertTrue( "Reflog for HEAD still contain no entry", @@ -83,8 +82,7 @@ public class ReflogConfigTest extends RepositoryTestCase { assertTrue(cfg.get(CoreConfig.KEY).isLogAllRefUpdates()); // do one commit and check that reflog size is increased to 1 - commit("A Commit\n", new PersonIdent(author, commitTime, tz), - new PersonIdent(committer, commitTime, tz)); + commit("A Commit\n", commitTime, tz); commitTime += 60 * 1000; assertTrue( "Reflog for HEAD should contain one entry", @@ -96,18 +94,17 @@ public class ReflogConfigTest extends RepositoryTestCase { assertFalse(cfg.get(CoreConfig.KEY).isLogAllRefUpdates()); // do one commit and check that reflog size is 2 - commit("A Commit\n", new PersonIdent(author, commitTime, tz), - new PersonIdent(committer, commitTime, tz)); + commit("A Commit\n", commitTime, tz); assertTrue( "Reflog for HEAD should contain two entries", db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 2); } - private void commit(String commitMsg, PersonIdent author, - PersonIdent committer) throws IOException { + private void commit(String commitMsg, long commitTime, int tz) + throws IOException { final CommitBuilder commit = new CommitBuilder(); - commit.setAuthor(author); - commit.setCommitter(committer); + commit.setAuthor(new PersonIdent(author, commitTime, tz)); + commit.setCommitter(new PersonIdent(committer, commitTime, tz)); commit.setMessage(commitMsg); ObjectId id; try (ObjectInserter inserter = db.newObjectInserter()) { From 3adea9ac7fb916f1cd3aebea5dacac92da749da6 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 13:48:54 +0900 Subject: [PATCH 07/16] RecursiveMergerTest: Open TreeWalk and BufferedReader in try-with-resource Change-Id: I381d535eb4ed7535ba8541c5320f81ce11d5b173 Signed-off-by: David Pursehouse --- .../jgit/merge/RecursiveMergerTest.java | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/RecursiveMergerTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/RecursiveMergerTest.java index 7ef6448e5..0e7109c58 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/RecursiveMergerTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/RecursiveMergerTest.java @@ -872,32 +872,31 @@ public class RecursiveMergerTest extends RepositoryTestCase { private String contentAsString(Repository r, ObjectId treeId, String path) throws MissingObjectException, IOException { - TreeWalk tw = new TreeWalk(r); - tw.addTree(treeId); - tw.setFilter(PathFilter.create(path)); - tw.setRecursive(true); - if (!tw.next()) - return null; - AnyObjectId blobId = tw.getObjectId(0); + AnyObjectId blobId; + try (TreeWalk tw = new TreeWalk(r)) { + tw.addTree(treeId); + tw.setFilter(PathFilter.create(path)); + tw.setRecursive(true); + if (!tw.next()) { + return null; + } + blobId = tw.getObjectId(0); + } StringBuilder result = new StringBuilder(); - BufferedReader br = null; ObjectReader or = r.newObjectReader(); - try { - br = new BufferedReader(new InputStreamReader(or.open(blobId) - .openStream())); + try (BufferedReader br = new BufferedReader( + new InputStreamReader(or.open(blobId).openStream()))) { String line; boolean first = true; while ((line = br.readLine()) != null) { - if (!first) + if (!first) { result.append('\n'); + } result.append(line); first = false; } return result.toString(); - } finally { - if (br != null) - br.close(); } } } From 83f1f8fc7c46a34d60fe90b683a1fa3a06e27d2e Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 13:50:54 +0900 Subject: [PATCH 08/16] RacyGitTests: Open NameConflictTreeWalk in try-with-resource Change-Id: Ic17b05fd7d056761b352168de97efb607a289276 Signed-off-by: David Pursehouse --- .../org/eclipse/jgit/lib/RacyGitTests.java | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java index 99d6801f3..aa46d1a2b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java @@ -82,38 +82,42 @@ public class RacyGitTests extends RepositoryTestCase { } FileTreeIteratorWithTimeControl fileIt = new FileTreeIteratorWithTimeControl( db, modTimes); - NameConflictTreeWalk tw = new NameConflictTreeWalk(db); - tw.addTree(fileIt); - tw.setRecursive(true); - FileTreeIterator t; - long t0 = 0; - for (int i = 0; i < 10; i++) { - assertTrue(tw.next()); - t = tw.getTree(0, FileTreeIterator.class); - if (i == 0) - t0 = t.getEntryLastModified(); - else - assertEquals(t0, t.getEntryLastModified()); - } - long t1 = 0; - for (int i = 0; i < 10; i++) { - assertTrue(tw.next()); - t = tw.getTree(0, FileTreeIterator.class); - if (i == 0) { - t1 = t.getEntryLastModified(); - assertTrue(t1 > t0); - } else - assertEquals(t1, t.getEntryLastModified()); - } - long t2 = 0; - for (int i = 0; i < 10; i++) { - assertTrue(tw.next()); - t = tw.getTree(0, FileTreeIterator.class); - if (i == 0) { - t2 = t.getEntryLastModified(); - assertTrue(t2 > t1); - } else - assertEquals(t2, t.getEntryLastModified()); + try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) { + tw.addTree(fileIt); + tw.setRecursive(true); + FileTreeIterator t; + long t0 = 0; + for (int i = 0; i < 10; i++) { + assertTrue(tw.next()); + t = tw.getTree(0, FileTreeIterator.class); + if (i == 0) { + t0 = t.getEntryLastModified(); + } else { + assertEquals(t0, t.getEntryLastModified()); + } + } + long t1 = 0; + for (int i = 0; i < 10; i++) { + assertTrue(tw.next()); + t = tw.getTree(0, FileTreeIterator.class); + if (i == 0) { + t1 = t.getEntryLastModified(); + assertTrue(t1 > t0); + } else { + assertEquals(t1, t.getEntryLastModified()); + } + } + long t2 = 0; + for (int i = 0; i < 10; i++) { + assertTrue(tw.next()); + t = tw.getTree(0, FileTreeIterator.class); + if (i == 0) { + t2 = t.getEntryLastModified(); + assertTrue(t2 > t1); + } else { + assertEquals(t2, t.getEntryLastModified()); + } + } } } From f3c250bd083871a11ea692ddd8a930a34e8db92c Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 13:55:40 +0900 Subject: [PATCH 09/16] PushCommandTest: Open Git instances in try-with-resource Change-Id: I3a8e28a4097e868a34ee1b23256c7f28d570cd75 Signed-off-by: David Pursehouse --- .../org/eclipse/jgit/api/PushCommandTest.java | 371 +++++++++--------- 1 file changed, 184 insertions(+), 187 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java index 1fcfef9c7..2a325405e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java @@ -90,26 +90,27 @@ public class PushCommandTest extends RepositoryTestCase { remoteConfig.update(config); config.save(); - Git git1 = new Git(db); - // create some refs via commits and tag - RevCommit commit = git1.commit().setMessage("initial commit").call(); - Ref tagRef = git1.tag().setName("tag").call(); - - try { - db2.resolve(commit.getId().getName() + "^{commit}"); - fail("id shouldn't exist yet"); - } catch (MissingObjectException e) { - // we should get here + try (Git git1 = new Git(db)) { + // create some refs via commits and tag + RevCommit commit = git1.commit().setMessage("initial commit").call(); + Ref tagRef = git1.tag().setName("tag").call(); + + try { + db2.resolve(commit.getId().getName() + "^{commit}"); + fail("id shouldn't exist yet"); + } catch (MissingObjectException e) { + // we should get here + } + + RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x"); + git1.push().setRemote("test").setRefSpecs(spec) + .call(); + + assertEquals(commit.getId(), + db2.resolve(commit.getId().getName() + "^{commit}")); + assertEquals(tagRef.getObjectId(), + db2.resolve(tagRef.getObjectId().getName())); } - - RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x"); - git1.push().setRemote("test").setRefSpecs(spec) - .call(); - - assertEquals(commit.getId(), - db2.resolve(commit.getId().getName() + "^{commit}")); - assertEquals(tagRef.getObjectId(), - db2.resolve(tagRef.getObjectId().getName())); } @Test @@ -132,15 +133,16 @@ public class PushCommandTest extends RepositoryTestCase { + hookOutput.toPath() + "\"\ncat - >>\"" + hookOutput.toPath() + "\"\nexit 0"); - Git git1 = new Git(db); - // create some refs via commits and tag - RevCommit commit = git1.commit().setMessage("initial commit").call(); + try (Git git1 = new Git(db)) { + // create some refs via commits and tag + RevCommit commit = git1.commit().setMessage("initial commit").call(); - RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x"); - git1.push().setRemote("test").setRefSpecs(spec).call(); - assertEquals("1:test, 2:" + uri + ", 3:\n" + "refs/heads/master " - + commit.getName() + " refs/heads/x " - + ObjectId.zeroId().name(), read(hookOutput)); + RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x"); + git1.push().setRemote("test").setRefSpecs(spec).call(); + assertEquals("1:test, 2:" + uri + ", 3:\n" + "refs/heads/master " + + commit.getName() + " refs/heads/x " + + ObjectId.zeroId().name(), read(hookOutput)); + } } private File writeHookFile(final String name, final String data) @@ -160,45 +162,45 @@ public class PushCommandTest extends RepositoryTestCase { String branch = "refs/heads/master"; String trackingBranch = "refs/remotes/" + remote + "/master"; - Git git = new Git(db); + try (Git git = new Git(db)) { + RevCommit commit1 = git.commit().setMessage("Initial commit") + .call(); - RevCommit commit1 = git.commit().setMessage("Initial commit") - .call(); + RefUpdate branchRefUpdate = db.updateRef(branch); + branchRefUpdate.setNewObjectId(commit1.getId()); + branchRefUpdate.update(); - RefUpdate branchRefUpdate = db.updateRef(branch); - branchRefUpdate.setNewObjectId(commit1.getId()); - branchRefUpdate.update(); + RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch); + trackingBranchRefUpdate.setNewObjectId(commit1.getId()); + trackingBranchRefUpdate.update(); - RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch); - trackingBranchRefUpdate.setNewObjectId(commit1.getId()); - trackingBranchRefUpdate.update(); - - final StoredConfig config = db.getConfig(); - RemoteConfig remoteConfig = new RemoteConfig(config, remote); - URIish uri = new URIish(db2.getDirectory().toURI().toURL()); - remoteConfig.addURI(uri); - remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" - + remote + "/*")); - remoteConfig.update(config); - config.save(); + final StoredConfig config = db.getConfig(); + RemoteConfig remoteConfig = new RemoteConfig(config, remote); + URIish uri = new URIish(db2.getDirectory().toURI().toURL()); + remoteConfig.addURI(uri); + remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + + remote + "/*")); + remoteConfig.update(config); + config.save(); - RevCommit commit2 = git.commit().setMessage("Commit to push").call(); + RevCommit commit2 = git.commit().setMessage("Commit to push").call(); - RefSpec spec = new RefSpec(branch + ":" + branch); - Iterable resultIterable = git.push().setRemote(remote) - .setRefSpecs(spec).call(); + RefSpec spec = new RefSpec(branch + ":" + branch); + Iterable resultIterable = git.push().setRemote(remote) + .setRefSpecs(spec).call(); - PushResult result = resultIterable.iterator().next(); - TrackingRefUpdate trackingRefUpdate = result - .getTrackingRefUpdate(trackingBranch); + PushResult result = resultIterable.iterator().next(); + TrackingRefUpdate trackingRefUpdate = result + .getTrackingRefUpdate(trackingBranch); - assertNotNull(trackingRefUpdate); - assertEquals(trackingBranch, trackingRefUpdate.getLocalName()); - assertEquals(branch, trackingRefUpdate.getRemoteName()); - assertEquals(commit2.getId(), trackingRefUpdate.getNewObjectId()); - assertEquals(commit2.getId(), db.resolve(trackingBranch)); - assertEquals(commit2.getId(), db2.resolve(branch)); + assertNotNull(trackingRefUpdate); + assertEquals(trackingBranch, trackingRefUpdate.getLocalName()); + assertEquals(branch, trackingRefUpdate.getRemoteName()); + assertEquals(commit2.getId(), trackingRefUpdate.getNewObjectId()); + assertEquals(commit2.getId(), db.resolve(trackingBranch)); + assertEquals(commit2.getId(), db2.resolve(branch)); + } } /** @@ -208,40 +210,38 @@ public class PushCommandTest extends RepositoryTestCase { */ @Test public void testPushRefUpdate() throws Exception { - Git git = new Git(db); - Git git2 = new Git(createBareRepository()); - - final StoredConfig config = git.getRepository().getConfig(); - RemoteConfig remoteConfig = new RemoteConfig(config, "test"); - URIish uri = new URIish(git2.getRepository().getDirectory().toURI() - .toURL()); - remoteConfig.addURI(uri); - remoteConfig.addPushRefSpec(new RefSpec("+refs/heads/*:refs/heads/*")); - remoteConfig.update(config); - config.save(); - - writeTrashFile("f", "content of f"); - git.add().addFilepattern("f").call(); - RevCommit commit = git.commit().setMessage("adding f").call(); - - assertEquals(null, git2.getRepository().resolve("refs/heads/master")); - git.push().setRemote("test").call(); - assertEquals(commit.getId(), - git2.getRepository().resolve("refs/heads/master")); - - git.branchCreate().setName("refs/heads/test").call(); - git.checkout().setName("refs/heads/test").call(); - - - for (int i = 0; i < 6; i++) { - writeTrashFile("f" + i, "content of f" + i); - git.add().addFilepattern("f" + i).call(); - commit = git.commit().setMessage("adding f" + i).call(); + try (Git git = new Git(db); + Git git2 = new Git(createBareRepository())) { + final StoredConfig config = git.getRepository().getConfig(); + RemoteConfig remoteConfig = new RemoteConfig(config, "test"); + URIish uri = new URIish(git2.getRepository().getDirectory().toURI() + .toURL()); + remoteConfig.addURI(uri); + remoteConfig.addPushRefSpec(new RefSpec("+refs/heads/*:refs/heads/*")); + remoteConfig.update(config); + config.save(); + + writeTrashFile("f", "content of f"); + git.add().addFilepattern("f").call(); + RevCommit commit = git.commit().setMessage("adding f").call(); + + assertEquals(null, git2.getRepository().resolve("refs/heads/master")); git.push().setRemote("test").call(); - git2.getRepository().getAllRefs(); - assertEquals("failed to update on attempt " + i, commit.getId(), - git2.getRepository().resolve("refs/heads/test")); - + assertEquals(commit.getId(), + git2.getRepository().resolve("refs/heads/master")); + + git.branchCreate().setName("refs/heads/test").call(); + git.checkout().setName("refs/heads/test").call(); + + for (int i = 0; i < 6; i++) { + writeTrashFile("f" + i, "content of f" + i); + git.add().addFilepattern("f" + i).call(); + commit = git.commit().setMessage("adding f" + i).call(); + git.push().setRemote("test").call(); + git2.getRepository().getAllRefs(); + assertEquals("failed to update on attempt " + i, commit.getId(), + git2.getRepository().resolve("refs/heads/test")); + } } } @@ -252,28 +252,26 @@ public class PushCommandTest extends RepositoryTestCase { */ @Test public void testPushWithRefSpecFromConfig() throws Exception { - Git git = new Git(db); - Git git2 = new Git(createBareRepository()); - - final StoredConfig config = git.getRepository().getConfig(); - RemoteConfig remoteConfig = new RemoteConfig(config, "test"); - URIish uri = new URIish(git2.getRepository().getDirectory().toURI() - .toURL()); - remoteConfig.addURI(uri); - remoteConfig.addPushRefSpec(new RefSpec("HEAD:refs/heads/newbranch")); - remoteConfig.update(config); - config.save(); - - writeTrashFile("f", "content of f"); - git.add().addFilepattern("f").call(); - RevCommit commit = git.commit().setMessage("adding f").call(); - - assertEquals(null, git2.getRepository().resolve("refs/heads/master")); - git.push().setRemote("test").call(); - assertEquals(commit.getId(), - git2.getRepository().resolve("refs/heads/newbranch")); - - + try (Git git = new Git(db); + Git git2 = new Git(createBareRepository())) { + final StoredConfig config = git.getRepository().getConfig(); + RemoteConfig remoteConfig = new RemoteConfig(config, "test"); + URIish uri = new URIish(git2.getRepository().getDirectory().toURI() + .toURL()); + remoteConfig.addURI(uri); + remoteConfig.addPushRefSpec(new RefSpec("HEAD:refs/heads/newbranch")); + remoteConfig.update(config); + config.save(); + + writeTrashFile("f", "content of f"); + git.add().addFilepattern("f").call(); + RevCommit commit = git.commit().setMessage("adding f").call(); + + assertEquals(null, git2.getRepository().resolve("refs/heads/master")); + git.push().setRemote("test").call(); + assertEquals(commit.getId(), + git2.getRepository().resolve("refs/heads/newbranch")); + } } /** @@ -283,38 +281,37 @@ public class PushCommandTest extends RepositoryTestCase { */ @Test public void testPushWithoutPushRefSpec() throws Exception { - Git git = new Git(db); - Git git2 = new Git(createBareRepository()); - - final StoredConfig config = git.getRepository().getConfig(); - RemoteConfig remoteConfig = new RemoteConfig(config, "test"); - URIish uri = new URIish(git2.getRepository().getDirectory().toURI() - .toURL()); - remoteConfig.addURI(uri); - remoteConfig.addFetchRefSpec(new RefSpec( - "+refs/heads/*:refs/remotes/origin/*")); - remoteConfig.update(config); - config.save(); - - writeTrashFile("f", "content of f"); - git.add().addFilepattern("f").call(); - RevCommit commit = git.commit().setMessage("adding f").call(); - - git.checkout().setName("not-pushed").setCreateBranch(true).call(); - git.checkout().setName("branchtopush").setCreateBranch(true).call(); - - assertEquals(null, - git2.getRepository().resolve("refs/heads/branchtopush")); - assertEquals(null, git2.getRepository() - .resolve("refs/heads/not-pushed")); - assertEquals(null, git2.getRepository().resolve("refs/heads/master")); - git.push().setRemote("test").call(); - assertEquals(commit.getId(), - git2.getRepository().resolve("refs/heads/branchtopush")); - assertEquals(null, git2.getRepository() - .resolve("refs/heads/not-pushed")); - assertEquals(null, git2.getRepository().resolve("refs/heads/master")); - + try (Git git = new Git(db); + Git git2 = new Git(createBareRepository())) { + final StoredConfig config = git.getRepository().getConfig(); + RemoteConfig remoteConfig = new RemoteConfig(config, "test"); + URIish uri = new URIish(git2.getRepository().getDirectory().toURI() + .toURL()); + remoteConfig.addURI(uri); + remoteConfig.addFetchRefSpec(new RefSpec( + "+refs/heads/*:refs/remotes/origin/*")); + remoteConfig.update(config); + config.save(); + + writeTrashFile("f", "content of f"); + git.add().addFilepattern("f").call(); + RevCommit commit = git.commit().setMessage("adding f").call(); + + git.checkout().setName("not-pushed").setCreateBranch(true).call(); + git.checkout().setName("branchtopush").setCreateBranch(true).call(); + + assertEquals(null, + git2.getRepository().resolve("refs/heads/branchtopush")); + assertEquals(null, git2.getRepository() + .resolve("refs/heads/not-pushed")); + assertEquals(null, git2.getRepository().resolve("refs/heads/master")); + git.push().setRemote("test").call(); + assertEquals(commit.getId(), + git2.getRepository().resolve("refs/heads/branchtopush")); + assertEquals(null, git2.getRepository() + .resolve("refs/heads/not-pushed")); + assertEquals(null, git2.getRepository().resolve("refs/heads/master")); + } } /** @@ -335,51 +332,51 @@ public class PushCommandTest extends RepositoryTestCase { remoteConfig.update(config); config.save(); - Git git1 = new Git(db); - Git git2 = new Git(db2); - - // push master (with a new commit) to the remote - git1.commit().setMessage("initial commit").call(); + try (Git git1 = new Git(db); + Git git2 = new Git(db2)) { + // push master (with a new commit) to the remote + git1.commit().setMessage("initial commit").call(); - RefSpec spec = new RefSpec("refs/heads/*:refs/heads/*"); - git1.push().setRemote("test").setRefSpecs(spec).call(); - - // create an unrelated ref and a commit on our remote - git2.branchCreate().setName("refs/heads/other").call(); - git2.checkout().setName("refs/heads/other").call(); - - writeTrashFile("a", "content of a"); - git2.add().addFilepattern("a").call(); - RevCommit commit2 = git2.commit().setMessage("adding a").call(); - - // run a gc to ensure we have a bitmap index - Properties res = git1.gc().setExpire(null).call(); - assertEquals(7, res.size()); - - // create another commit so we have something else to push - writeTrashFile("b", "content of b"); - git1.add().addFilepattern("b").call(); - RevCommit commit3 = git1.commit().setMessage("adding b").call(); - - try { - // Re-run the push. Failure may happen here. + RefSpec spec = new RefSpec("refs/heads/*:refs/heads/*"); git1.push().setRemote("test").setRefSpecs(spec).call(); - } catch (TransportException e) { - assertTrue("should be caused by a MissingObjectException", e - .getCause().getCause() instanceof MissingObjectException); - fail("caught MissingObjectException for a change we don't have"); - } - // Remote will have both a and b. Master will have only b - try { - db.resolve(commit2.getId().getName() + "^{commit}"); - fail("id shouldn't exist locally"); - } catch (MissingObjectException e) { - // we should get here + // create an unrelated ref and a commit on our remote + git2.branchCreate().setName("refs/heads/other").call(); + git2.checkout().setName("refs/heads/other").call(); + + writeTrashFile("a", "content of a"); + git2.add().addFilepattern("a").call(); + RevCommit commit2 = git2.commit().setMessage("adding a").call(); + + // run a gc to ensure we have a bitmap index + Properties res = git1.gc().setExpire(null).call(); + assertEquals(7, res.size()); + + // create another commit so we have something else to push + writeTrashFile("b", "content of b"); + git1.add().addFilepattern("b").call(); + RevCommit commit3 = git1.commit().setMessage("adding b").call(); + + try { + // Re-run the push. Failure may happen here. + git1.push().setRemote("test").setRefSpecs(spec).call(); + } catch (TransportException e) { + assertTrue("should be caused by a MissingObjectException", e + .getCause().getCause() instanceof MissingObjectException); + fail("caught MissingObjectException for a change we don't have"); + } + + // Remote will have both a and b. Master will have only b + try { + db.resolve(commit2.getId().getName() + "^{commit}"); + fail("id shouldn't exist locally"); + } catch (MissingObjectException e) { + // we should get here + } + assertEquals(commit2.getId(), + db2.resolve(commit2.getId().getName() + "^{commit}")); + assertEquals(commit3.getId(), + db2.resolve(commit3.getId().getName() + "^{commit}")); } - assertEquals(commit2.getId(), - db2.resolve(commit2.getId().getName() + "^{commit}")); - assertEquals(commit3.getId(), - db2.resolve(commit3.getId().getName() + "^{commit}")); } } From ad9b326e3f42d49470c2ad687e4ebc9617a85089 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 13:57:34 +0900 Subject: [PATCH 10/16] PullCommandWithRebaseTest: Open RevWalk in try-with-resource Change-Id: I16f4d219e8b103d30149b2a94d3484424f3de84b Signed-off-by: David Pursehouse --- .../jgit/api/PullCommandWithRebaseTest.java | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PullCommandWithRebaseTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PullCommandWithRebaseTest.java index 9ad845b2a..b405f6ad7 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PullCommandWithRebaseTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PullCommandWithRebaseTest.java @@ -273,26 +273,27 @@ public class PullCommandWithRebaseTest extends RepositoryTestCase { // Get the HEAD and HEAD~1 commits Repository targetRepo = target.getRepository(); - RevWalk revWalk = new RevWalk(targetRepo); - ObjectId headId = targetRepo.resolve(Constants.HEAD); - RevCommit root = revWalk.parseCommit(headId); - revWalk.markStart(root); - // HEAD - RevCommit head = revWalk.next(); - // HEAD~1 - RevCommit beforeHead = revWalk.next(); - - // verify the commit message on the HEAD commit - assertEquals(TARGET_COMMIT_MESSAGE, head.getFullMessage()); - // verify the commit just before HEAD - assertEquals(SOURCE_COMMIT_MESSAGE, beforeHead.getFullMessage()); - - // verify file states - assertFileContentsEqual(sourceFile, SOURCE_FILE_CONTENTS); - assertFileContentsEqual(newFile, NEW_FILE_CONTENTS); - // verify repository state - assertEquals(RepositoryState.SAFE, target - .getRepository().getRepositoryState()); + try (RevWalk revWalk = new RevWalk(targetRepo)) { + ObjectId headId = targetRepo.resolve(Constants.HEAD); + RevCommit root = revWalk.parseCommit(headId); + revWalk.markStart(root); + // HEAD + RevCommit head = revWalk.next(); + // HEAD~1 + RevCommit beforeHead = revWalk.next(); + + // verify the commit message on the HEAD commit + assertEquals(TARGET_COMMIT_MESSAGE, head.getFullMessage()); + // verify the commit just before HEAD + assertEquals(SOURCE_COMMIT_MESSAGE, beforeHead.getFullMessage()); + + // verify file states + assertFileContentsEqual(sourceFile, SOURCE_FILE_CONTENTS); + assertFileContentsEqual(newFile, NEW_FILE_CONTENTS); + // verify repository state + assertEquals(RepositoryState.SAFE, target + .getRepository().getRepositoryState()); + } } @Override From a3923f6b95dcbda701602edd1fa0406dc44d4843 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 14:01:30 +0900 Subject: [PATCH 11/16] PostOrderTreeWalkTest: Open TreeWalk in try-with-resource Also remove unnecessary nesting in test methods. Change-Id: Id59f8403c0a7b38ebb6b3a24814257cd59ea575d Signed-off-by: David Pursehouse --- .../jgit/treewalk/PostOrderTreeWalkTest.java | 162 +++++++++--------- 1 file changed, 81 insertions(+), 81 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/PostOrderTreeWalkTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/PostOrderTreeWalkTest.java index acbbb39aa..6ad47c295 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/PostOrderTreeWalkTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/PostOrderTreeWalkTest.java @@ -60,124 +60,124 @@ import org.junit.Test; public class PostOrderTreeWalkTest extends RepositoryTestCase { @Test public void testInitialize_NoPostOrder() throws Exception { - final TreeWalk tw = new TreeWalk(db); - assertFalse(tw.isPostOrderTraversal()); + try (final TreeWalk tw = new TreeWalk(db)) { + assertFalse(tw.isPostOrderTraversal()); + } } @Test public void testInitialize_TogglePostOrder() throws Exception { - final TreeWalk tw = new TreeWalk(db); - assertFalse(tw.isPostOrderTraversal()); - tw.setPostOrderTraversal(true); - assertTrue(tw.isPostOrderTraversal()); - tw.setPostOrderTraversal(false); - assertFalse(tw.isPostOrderTraversal()); + try (final TreeWalk tw = new TreeWalk(db)) { + assertFalse(tw.isPostOrderTraversal()); + tw.setPostOrderTraversal(true); + assertTrue(tw.isPostOrderTraversal()); + tw.setPostOrderTraversal(false); + assertFalse(tw.isPostOrderTraversal()); + } } @Test public void testResetDoesNotAffectPostOrder() throws Exception { - final TreeWalk tw = new TreeWalk(db); - tw.setPostOrderTraversal(true); - assertTrue(tw.isPostOrderTraversal()); - tw.reset(); - assertTrue(tw.isPostOrderTraversal()); - - tw.setPostOrderTraversal(false); - assertFalse(tw.isPostOrderTraversal()); - tw.reset(); - assertFalse(tw.isPostOrderTraversal()); + try (final TreeWalk tw = new TreeWalk(db)) { + tw.setPostOrderTraversal(true); + assertTrue(tw.isPostOrderTraversal()); + tw.reset(); + assertTrue(tw.isPostOrderTraversal()); + + tw.setPostOrderTraversal(false); + assertFalse(tw.isPostOrderTraversal()); + tw.reset(); + assertFalse(tw.isPostOrderTraversal()); + } } @Test public void testNoPostOrder() throws Exception { final DirCache tree = db.readDirCache(); - { - final DirCacheBuilder b = tree.builder(); - - b.add(makeFile("a")); - b.add(makeFile("b/c")); - b.add(makeFile("b/d")); - b.add(makeFile("q")); - - b.finish(); - assertEquals(4, tree.getEntryCount()); + final DirCacheBuilder b = tree.builder(); + + b.add(makeFile("a")); + b.add(makeFile("b/c")); + b.add(makeFile("b/d")); + b.add(makeFile("q")); + + b.finish(); + assertEquals(4, tree.getEntryCount()); + + try (final TreeWalk tw = new TreeWalk(db)) { + tw.setPostOrderTraversal(false); + tw.addTree(new DirCacheIterator(tree)); + + assertModes("a", REGULAR_FILE, tw); + assertModes("b", TREE, tw); + assertTrue(tw.isSubtree()); + assertFalse(tw.isPostChildren()); + tw.enterSubtree(); + assertModes("b/c", REGULAR_FILE, tw); + assertModes("b/d", REGULAR_FILE, tw); + assertModes("q", REGULAR_FILE, tw); } - - final TreeWalk tw = new TreeWalk(db); - tw.setPostOrderTraversal(false); - tw.addTree(new DirCacheIterator(tree)); - - assertModes("a", REGULAR_FILE, tw); - assertModes("b", TREE, tw); - assertTrue(tw.isSubtree()); - assertFalse(tw.isPostChildren()); - tw.enterSubtree(); - assertModes("b/c", REGULAR_FILE, tw); - assertModes("b/d", REGULAR_FILE, tw); - assertModes("q", REGULAR_FILE, tw); } @Test public void testWithPostOrder_EnterSubtree() throws Exception { final DirCache tree = db.readDirCache(); - { - final DirCacheBuilder b = tree.builder(); + final DirCacheBuilder b = tree.builder(); - b.add(makeFile("a")); - b.add(makeFile("b/c")); - b.add(makeFile("b/d")); - b.add(makeFile("q")); + b.add(makeFile("a")); + b.add(makeFile("b/c")); + b.add(makeFile("b/d")); + b.add(makeFile("q")); - b.finish(); - assertEquals(4, tree.getEntryCount()); - } + b.finish(); + assertEquals(4, tree.getEntryCount()); - final TreeWalk tw = new TreeWalk(db); - tw.setPostOrderTraversal(true); - tw.addTree(new DirCacheIterator(tree)); + try (final TreeWalk tw = new TreeWalk(db)) { + tw.setPostOrderTraversal(true); + tw.addTree(new DirCacheIterator(tree)); - assertModes("a", REGULAR_FILE, tw); + assertModes("a", REGULAR_FILE, tw); - assertModes("b", TREE, tw); - assertTrue(tw.isSubtree()); - assertFalse(tw.isPostChildren()); - tw.enterSubtree(); - assertModes("b/c", REGULAR_FILE, tw); - assertModes("b/d", REGULAR_FILE, tw); + assertModes("b", TREE, tw); + assertTrue(tw.isSubtree()); + assertFalse(tw.isPostChildren()); + tw.enterSubtree(); + assertModes("b/c", REGULAR_FILE, tw); + assertModes("b/d", REGULAR_FILE, tw); - assertModes("b", TREE, tw); - assertTrue(tw.isSubtree()); - assertTrue(tw.isPostChildren()); + assertModes("b", TREE, tw); + assertTrue(tw.isSubtree()); + assertTrue(tw.isPostChildren()); - assertModes("q", REGULAR_FILE, tw); + assertModes("q", REGULAR_FILE, tw); + } } @Test public void testWithPostOrder_NoEnterSubtree() throws Exception { final DirCache tree = db.readDirCache(); - { - final DirCacheBuilder b = tree.builder(); + final DirCacheBuilder b = tree.builder(); - b.add(makeFile("a")); - b.add(makeFile("b/c")); - b.add(makeFile("b/d")); - b.add(makeFile("q")); + b.add(makeFile("a")); + b.add(makeFile("b/c")); + b.add(makeFile("b/d")); + b.add(makeFile("q")); - b.finish(); - assertEquals(4, tree.getEntryCount()); - } + b.finish(); + assertEquals(4, tree.getEntryCount()); - final TreeWalk tw = new TreeWalk(db); - tw.setPostOrderTraversal(true); - tw.addTree(new DirCacheIterator(tree)); + try (final TreeWalk tw = new TreeWalk(db)) { + tw.setPostOrderTraversal(true); + tw.addTree(new DirCacheIterator(tree)); - assertModes("a", REGULAR_FILE, tw); + assertModes("a", REGULAR_FILE, tw); - assertModes("b", TREE, tw); - assertTrue(tw.isSubtree()); - assertFalse(tw.isPostChildren()); + assertModes("b", TREE, tw); + assertTrue(tw.isSubtree()); + assertFalse(tw.isPostChildren()); - assertModes("q", REGULAR_FILE, tw); + assertModes("q", REGULAR_FILE, tw); + } } private DirCacheEntry makeFile(final String path) throws Exception { From f7a3643fb8c977739af928f2aafc9eeb01a83abc Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 14:02:49 +0900 Subject: [PATCH 12/16] PathSuffixFilterTest: Open TreeWalk in try-with-resource Change-Id: If0ee71f09a5464e27f0496dac364f8f9bb015eb6 Signed-off-by: David Pursehouse --- .../treewalk/filter/PathSuffixFilterTest.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java index d871c5ec1..3885c4168 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java @@ -113,15 +113,16 @@ public class PathSuffixFilterTest extends RepositoryTestCase { private List getMatchingPaths(String suffixFilter, final ObjectId treeId, boolean recursiveWalk) throws IOException { - final TreeWalk tw = new TreeWalk(db); - tw.setFilter(PathSuffixFilter.create(suffixFilter)); - tw.setRecursive(recursiveWalk); - tw.addTree(treeId); - - List paths = new ArrayList(); - while (tw.next()) - paths.add(tw.getPathString()); - return paths; + try (final TreeWalk tw = new TreeWalk(db)) { + tw.setFilter(PathSuffixFilter.create(suffixFilter)); + tw.setRecursive(recursiveWalk); + tw.addTree(treeId); + + List paths = new ArrayList(); + while (tw.next()) + paths.add(tw.getPathString()); + return paths; + } } } From 53b6bb9cca12aa872a6839f3ab9d129d2708735a Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 14:07:24 +0900 Subject: [PATCH 13/16] PatchIdDiffFormatterTest: Open Git and PatchIdDiffFormatter in try-with-resource Change-Id: I39e898e52c3d9dffaba9dabf11c085503fbc1acf Signed-off-by: David Pursehouse --- .../jgit/diff/PatchIdDiffFormatterTest.java | 82 ++++++++++--------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/PatchIdDiffFormatterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/PatchIdDiffFormatterTest.java index ce283ae4f..024aaa3f5 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/PatchIdDiffFormatterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/PatchIdDiffFormatterTest.java @@ -61,21 +61,22 @@ public class PatchIdDiffFormatterTest extends RepositoryTestCase { File folder = new File(db.getDirectory().getParent(), "folder"); folder.mkdir(); write(new File(folder, "folder.txt"), "folder"); - Git git = new Git(db); - git.add().addFilepattern(".").call(); - git.commit().setMessage("Initial commit").call(); - write(new File(folder, "folder.txt"), "folder change"); + try (Git git = new Git(db); + PatchIdDiffFormatter df = new PatchIdDiffFormatter()) { + git.add().addFilepattern(".").call(); + git.commit().setMessage("Initial commit").call(); + write(new File(folder, "folder.txt"), "folder change"); - PatchIdDiffFormatter df = new PatchIdDiffFormatter(); - df.setRepository(db); - df.setPathFilter(PathFilter.create("folder")); - DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache()); - FileTreeIterator newTree = new FileTreeIterator(db); - df.format(oldTree, newTree); - df.flush(); + df.setRepository(db); + df.setPathFilter(PathFilter.create("folder")); + DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache()); + FileTreeIterator newTree = new FileTreeIterator(db); + df.format(oldTree, newTree); + df.flush(); - assertEquals("1ff64e0f9333e9b81967c3e8d7a81362b14d5441", df - .getCalulatedPatchId().name()); + assertEquals("1ff64e0f9333e9b81967c3e8d7a81362b14d5441", df + .getCalulatedPatchId().name()); + } } @Test @@ -84,37 +85,40 @@ public class PatchIdDiffFormatterTest extends RepositoryTestCase { File folder = new File(db.getDirectory().getParent(), "folder"); folder.mkdir(); write(new File(folder, "folder.txt"), "\n\n\n\nfolder"); - Git git = new Git(db); - git.add().addFilepattern(".").call(); - git.commit().setMessage("Initial commit").call(); - write(new File(folder, "folder.txt"), "\n\n\n\nfolder change"); + try (Git git = new Git(db)) { + git.add().addFilepattern(".").call(); + git.commit().setMessage("Initial commit").call(); + write(new File(folder, "folder.txt"), "\n\n\n\nfolder change"); - PatchIdDiffFormatter df = new PatchIdDiffFormatter(); - df.setRepository(db); - df.setPathFilter(PathFilter.create("folder")); - DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache()); - FileTreeIterator newTree = new FileTreeIterator(db); - df.format(oldTree, newTree); - df.flush(); + try (PatchIdDiffFormatter df = new PatchIdDiffFormatter()) { + df.setRepository(db); + df.setPathFilter(PathFilter.create("folder")); + DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache()); + FileTreeIterator newTree = new FileTreeIterator(db); + df.format(oldTree, newTree); + df.flush(); - assertEquals("08fca5ac531383eb1da8bf6b6f7cf44411281407", df - .getCalulatedPatchId().name()); + assertEquals("08fca5ac531383eb1da8bf6b6f7cf44411281407", df + .getCalulatedPatchId().name()); + } - write(new File(folder, "folder.txt"), "a\n\n\n\nfolder"); - git.add().addFilepattern(".").call(); - git.commit().setMessage("Initial commit").call(); - write(new File(folder, "folder.txt"), "a\n\n\n\nfolder change"); + write(new File(folder, "folder.txt"), "a\n\n\n\nfolder"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("Initial commit").call(); + write(new File(folder, "folder.txt"), "a\n\n\n\nfolder change"); - df = new PatchIdDiffFormatter(); - df.setRepository(db); - df.setPathFilter(PathFilter.create("folder")); - oldTree = new DirCacheIterator(db.readDirCache()); - newTree = new FileTreeIterator(db); - df.format(oldTree, newTree); - df.flush(); + try (PatchIdDiffFormatter df = new PatchIdDiffFormatter()) { + df.setRepository(db); + df.setPathFilter(PathFilter.create("folder")); + DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache()); + FileTreeIterator newTree = new FileTreeIterator(db); + df.format(oldTree, newTree); + df.flush(); - assertEquals("08fca5ac531383eb1da8bf6b6f7cf44411281407", df - .getCalulatedPatchId().name()); + assertEquals("08fca5ac531383eb1da8bf6b6f7cf44411281407", df + .getCalulatedPatchId().name()); + } + } } } From 101ac16e8d5874da9dacb99ecd72aa286128d6c9 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 14:09:02 +0900 Subject: [PATCH 14/16] PackWriterTest: Open RevWalk in try-with-resource Change-Id: Ic8ed941d096718e81c7ffeb166bcf7faaa2ff57d Signed-off-by: David Pursehouse --- .../jgit/internal/storage/file/PackWriterTest.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackWriterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackWriterTest.java index 01d6ee68e..63a307285 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackWriterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackWriterTest.java @@ -342,12 +342,13 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327"), ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"), ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3") }; - final RevWalk parser = new RevWalk(db); - final RevObject forcedOrderRevs[] = new RevObject[forcedOrder.length]; - for (int i = 0; i < forcedOrder.length; i++) - forcedOrderRevs[i] = parser.parseAny(forcedOrder[i]); + try (final RevWalk parser = new RevWalk(db)) { + final RevObject forcedOrderRevs[] = new RevObject[forcedOrder.length]; + for (int i = 0; i < forcedOrder.length; i++) + forcedOrderRevs[i] = parser.parseAny(forcedOrder[i]); - createVerifyOpenPack(Arrays.asList(forcedOrderRevs)); + createVerifyOpenPack(Arrays.asList(forcedOrderRevs)); + } assertEquals(forcedOrder.length, writer.getObjectCount()); verifyObjectsOrder(forcedOrder); From 35ba741cd4d2371606a20f55e275bcf41c600aea Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 14:10:12 +0900 Subject: [PATCH 15/16] ObjectDirectoryTest: Fix warnings about variable hiding The variable and parameter named 'db' were hiding class members with the same name. Change-Id: I27017afdc5f49c38c6f5be494e7a21239ea601a7 Signed-off-by: David Pursehouse --- .../jgit/internal/storage/file/ObjectDirectoryTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ObjectDirectoryTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ObjectDirectoryTest.java index 3226f42e0..923f28389 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ObjectDirectoryTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ObjectDirectoryTest.java @@ -62,18 +62,18 @@ public class ObjectDirectoryTest extends RepositoryTestCase { throws Exception { ExecutorService e = Executors.newCachedThreadPool(); for (int i=0; i < 100; ++i) { - ObjectDirectory db = createBareRepository().getObjectDatabase(); - for (Future f : e.invokeAll(blobInsertersForTheSameFanOutDir(db))) { + ObjectDirectory dir = createBareRepository().getObjectDatabase(); + for (Future f : e.invokeAll(blobInsertersForTheSameFanOutDir(dir))) { f.get(); } } } private Collection> blobInsertersForTheSameFanOutDir( - final ObjectDirectory db) { + final ObjectDirectory dir) { Callable callable = new Callable() { public ObjectId call() throws Exception { - return db.newInserter().insert(Constants.OBJ_BLOB, new byte[0]); + return dir.newInserter().insert(Constants.OBJ_BLOB, new byte[0]); } }; return Collections.nCopies(4, callable); From 50e21322de4c366da8c7f662b566b9ab1d06a960 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 12 Feb 2016 14:12:25 +0900 Subject: [PATCH 16/16] NoteMapTest: Open TreeWalk instances in try-with-resource Change-Id: I70da0140fe087e7e69c28e9ddd125495d916ec1b Signed-off-by: David Pursehouse --- .../org/eclipse/jgit/notes/NoteMapTest.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java index 4539a013f..47b08a7e1 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java @@ -403,10 +403,12 @@ public class NoteMapTest extends RepositoryTestCase { } RevCommit n = commitNoteMap(map); - TreeWalk tw = new TreeWalk(reader); - tw.reset(n.getTree()); - while (tw.next()) - assertFalse("no fan-out subtree", tw.isSubtree()); + try (TreeWalk tw = new TreeWalk(reader)) { + tw.reset(n.getTree()); + while (tw.next()) { + assertFalse("no fan-out subtree", tw.isSubtree()); + } + } for (int i = 254; i < 256; i++) { idBuf.setByte(Constants.OBJECT_ID_LENGTH - 1, i); @@ -418,13 +420,15 @@ public class NoteMapTest extends RepositoryTestCase { // The 00 bucket is fully split. String path = fanout(38, idBuf.name()); - tw = TreeWalk.forPath(reader, path, n.getTree()); - assertNotNull("has " + path, tw); + try (TreeWalk tw = TreeWalk.forPath(reader, path, n.getTree())) { + assertNotNull("has " + path, tw); + } // The other bucket is not. path = fanout(2, data1.name()); - tw = TreeWalk.forPath(reader, path, n.getTree()); - assertNotNull("has " + path, tw); + try (TreeWalk tw = TreeWalk.forPath(reader, path, n.getTree())) { + assertNotNull("has " + path, tw); + } } @Test