]> source.dussan.org Git - jgit.git/commitdiff
CheckoutTest: Create Git instances in try-with-resource 23/64323/2
authorDavid Pursehouse <david.pursehouse@sonymobile.com>
Thu, 14 Jan 2016 08:49:26 +0000 (17:49 +0900)
committerMatthias Sohn <matthias.sohn@sap.com>
Tue, 19 Jan 2016 16:27:46 +0000 (17:27 +0100)
Change-Id: I49a03f7bee0b61c062ce160674f9aa0cd1bcc8ba
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java

index 939a9f6fdd1631c8668c7eab2d0c1c09de92f9fe..cb36d057e4bef1336e031f395bac8e9117cc8c3b 100644 (file)
@@ -66,27 +66,33 @@ public class CheckoutTest extends CLIRepositoryTestCase {
 
        @Test
        public void testCheckoutSelf() throws Exception {
-               new Git(db).commit().setMessage("initial commit").call();
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
 
-               assertStringArrayEquals("Already on 'master'",
-                               execute("git checkout master"));
+                       assertStringArrayEquals("Already on 'master'",
+                                       execute("git checkout master"));
+               }
        }
 
        @Test
        public void testCheckoutBranch() throws Exception {
-               new Git(db).commit().setMessage("initial commit").call();
-               new Git(db).branchCreate().setName("side").call();
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
+                       git.branchCreate().setName("side").call();
 
-               assertStringArrayEquals("Switched to branch 'side'",
-                               execute("git checkout side"));
+                       assertStringArrayEquals("Switched to branch 'side'",
+                                       execute("git checkout side"));
+               }
        }
 
        @Test
        public void testCheckoutNewBranch() throws Exception {
-               new Git(db).commit().setMessage("initial commit").call();
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
 
-               assertStringArrayEquals("Switched to a new branch 'side'",
-                               execute("git checkout -b side"));
+                       assertStringArrayEquals("Switched to a new branch 'side'",
+                                       execute("git checkout -b side"));
+               }
        }
 
        @Test
@@ -98,11 +104,13 @@ public class CheckoutTest extends CLIRepositoryTestCase {
 
        @Test
        public void testCheckoutNewBranchThatAlreadyExists() throws Exception {
-               new Git(db).commit().setMessage("initial commit").call();
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
 
-               assertStringArrayEquals(
-                               "fatal: A branch named 'master' already exists.",
-                               execute("git checkout -b master"));
+                       assertStringArrayEquals(
+                                       "fatal: A branch named 'master' already exists.",
+                                       execute("git checkout -b master"));
+               }
        }
 
        @Test
@@ -120,32 +128,35 @@ public class CheckoutTest extends CLIRepositoryTestCase {
 
        @Test
        public void testCheckoutHead() throws Exception {
-               new Git(db).commit().setMessage("initial commit").call();
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
 
-               assertStringArrayEquals("", execute("git checkout HEAD"));
+                       assertStringArrayEquals("", execute("git checkout HEAD"));
+               }
        }
 
        @Test
        public void testCheckoutExistingBranchWithConflict() throws Exception {
-               Git git = new Git(db);
-               writeTrashFile("a", "Hello world a");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("commit file a").call();
-               git.branchCreate().setName("branch_1").call();
-               git.rm().addFilepattern("a").call();
-               FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
-               writeTrashFile("a/b", "Hello world b");
-               git.add().addFilepattern("a/b").call();
-               git.commit().setMessage("commit folder a").call();
-               git.rm().addFilepattern("a").call();
-               writeTrashFile("a", "New Hello world a");
-               git.add().addFilepattern(".").call();
-
-               String[] execute = execute("git checkout branch_1");
-               assertEquals(
-                               "error: Your local changes to the following files would be overwritten by checkout:",
-                               execute[0]);
-               assertEquals("\ta", execute[1]);
+               try (Git git = new Git(db)) {
+                       writeTrashFile("a", "Hello world a");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("commit file a").call();
+                       git.branchCreate().setName("branch_1").call();
+                       git.rm().addFilepattern("a").call();
+                       FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
+                       writeTrashFile("a/b", "Hello world b");
+                       git.add().addFilepattern("a/b").call();
+                       git.commit().setMessage("commit folder a").call();
+                       git.rm().addFilepattern("a").call();
+                       writeTrashFile("a", "New Hello world a");
+                       git.add().addFilepattern(".").call();
+
+                       String[] execute = execute("git checkout branch_1");
+                       assertEquals(
+                                       "error: Your local changes to the following files would be overwritten by checkout:",
+                                       execute[0]);
+                       assertEquals("\ta", execute[1]);
+               }
        }
 
        /**
@@ -167,41 +178,43 @@ public class CheckoutTest extends CLIRepositoryTestCase {
         */
        @Test
        public void testCheckoutWithMissingWorkingTreeFile() throws Exception {
-               Git git = new Git(db);
-               File fileA = writeTrashFile("a", "Hello world a");
-               writeTrashFile("b", "Hello world b");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("add files a & b").call();
-               Ref branch_1 = git.branchCreate().setName("branch_1").call();
-               writeTrashFile("a", "b");
-               git.add().addFilepattern("a").call();
-               git.commit().setMessage("modify file a").call();
-
-               FileEntry entry = new FileTreeIterator.FileEntry(new File(
-                               db.getWorkTree(), "a"), db.getFS());
-               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
-
-               FileUtils.delete(fileA);
-
-               git.checkout().setName(branch_1.getName()).call();
-
-               entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
-                               db.getFS());
-               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
-               assertEquals("Hello world a", read(fileA));
+               try (Git git = new Git(db)) {
+                       File fileA = writeTrashFile("a", "Hello world a");
+                       writeTrashFile("b", "Hello world b");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("add files a & b").call();
+                       Ref branch_1 = git.branchCreate().setName("branch_1").call();
+                       writeTrashFile("a", "b");
+                       git.add().addFilepattern("a").call();
+                       git.commit().setMessage("modify file a").call();
+
+                       FileEntry entry = new FileTreeIterator.FileEntry(new File(
+                                       db.getWorkTree(), "a"), db.getFS());
+                       assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+
+                       FileUtils.delete(fileA);
+
+                       git.checkout().setName(branch_1.getName()).call();
+
+                       entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
+                                       db.getFS());
+                       assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+                       assertEquals("Hello world a", read(fileA));
+               }
        }
 
        @Test
        public void testCheckoutOrphan() throws Exception {
-               Git git = new Git(db);
-               git.commit().setMessage("initial commit").call();
-
-               assertStringArrayEquals("Switched to a new branch 'new_branch'",
-                               execute("git checkout --orphan new_branch"));
-               assertEquals("refs/heads/new_branch",
-                               db.exactRef("HEAD").getTarget().getName());
-               RevCommit commit = git.commit().setMessage("orphan commit").call();
-               assertEquals(0, commit.getParentCount());
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
+
+                       assertStringArrayEquals("Switched to a new branch 'new_branch'",
+                                       execute("git checkout --orphan new_branch"));
+                       assertEquals("refs/heads/new_branch",
+                                       db.exactRef("HEAD").getTarget().getName());
+                       RevCommit commit = git.commit().setMessage("orphan commit").call();
+                       assertEquals(0, commit.getParentCount());
+               }
        }
 
        /**
@@ -224,33 +237,34 @@ public class CheckoutTest extends CLIRepositoryTestCase {
        @Test
        public void fileModeTestMissingThenFolderWithFileInWorkingTree()
                        throws Exception {
-               Git git = new Git(db);
-               writeTrashFile("b", "Hello world b");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("add file b").call();
-               Ref branch_1 = git.branchCreate().setName("branch_1").call();
-               File folderA = new File(db.getWorkTree(), "a");
-               FileUtils.mkdirs(folderA);
-               writeTrashFile("a/c", "Hello world c");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("add folder a").call();
-
-               FileEntry entry = new FileTreeIterator.FileEntry(new File(
-                               db.getWorkTree(), "a"), db.getFS());
-               assertEquals(FileMode.TREE, entry.getMode());
-
-               FileUtils.delete(folderA, FileUtils.RECURSIVE);
-               writeTrashFile("a", "b");
-
-               entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
-                               db.getFS());
-               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
-
-               git.checkout().setName(branch_1.getName()).call();
-
-               entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
-                               db.getFS());
-               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+               try (Git git = new Git(db)) {
+                       writeTrashFile("b", "Hello world b");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("add file b").call();
+                       Ref branch_1 = git.branchCreate().setName("branch_1").call();
+                       File folderA = new File(db.getWorkTree(), "a");
+                       FileUtils.mkdirs(folderA);
+                       writeTrashFile("a/c", "Hello world c");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("add folder a").call();
+
+                       FileEntry entry = new FileTreeIterator.FileEntry(new File(
+                                       db.getWorkTree(), "a"), db.getFS());
+                       assertEquals(FileMode.TREE, entry.getMode());
+
+                       FileUtils.delete(folderA, FileUtils.RECURSIVE);
+                       writeTrashFile("a", "b");
+
+                       entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
+                                       db.getFS());
+                       assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+
+                       git.checkout().setName(branch_1.getName()).call();
+
+                       entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
+                                       db.getFS());
+                       assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+               }
        }
 
        /**
@@ -272,30 +286,31 @@ public class CheckoutTest extends CLIRepositoryTestCase {
         */
        @Test
        public void fileModeTestFolderWithMissingInWorkingTree() throws Exception {
-               Git git = new Git(db);
-               writeTrashFile("b", "Hello world b");
-               writeTrashFile("a", "b");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("add file b & file a").call();
-               Ref branch_1 = git.branchCreate().setName("branch_1").call();
-               git.rm().addFilepattern("a").call();
-               File folderA = new File(db.getWorkTree(), "a");
-               FileUtils.mkdirs(folderA);
-               writeTrashFile("a/c", "Hello world c");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("add folder a").call();
-
-               FileEntry entry = new FileTreeIterator.FileEntry(new File(
-                               db.getWorkTree(), "a"), db.getFS());
-               assertEquals(FileMode.TREE, entry.getMode());
-
-               FileUtils.delete(folderA, FileUtils.RECURSIVE);
-
-               git.checkout().setName(branch_1.getName()).call();
-
-               entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
-                               db.getFS());
-               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+               try (Git git = new Git(db)) {
+                       writeTrashFile("b", "Hello world b");
+                       writeTrashFile("a", "b");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("add file b & file a").call();
+                       Ref branch_1 = git.branchCreate().setName("branch_1").call();
+                       git.rm().addFilepattern("a").call();
+                       File folderA = new File(db.getWorkTree(), "a");
+                       FileUtils.mkdirs(folderA);
+                       writeTrashFile("a/c", "Hello world c");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("add folder a").call();
+
+                       FileEntry entry = new FileTreeIterator.FileEntry(new File(
+                                       db.getWorkTree(), "a"), db.getFS());
+                       assertEquals(FileMode.TREE, entry.getMode());
+
+                       FileUtils.delete(folderA, FileUtils.RECURSIVE);
+
+                       git.checkout().setName(branch_1.getName()).call();
+
+                       entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
+                                       db.getFS());
+                       assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+               }
        }
 
        /**
@@ -317,32 +332,33 @@ public class CheckoutTest extends CLIRepositoryTestCase {
         */
        @Test
        public void fileModeTestMissingWithFolderInWorkingTree() throws Exception {
-               Git git = new Git(db);
-               writeTrashFile("b", "Hello world b");
-               writeTrashFile("a", "b");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("add file b & file a").call();
-               Ref branch_1 = git.branchCreate().setName("branch_1").call();
-               git.rm().addFilepattern("a").call();
-               git.commit().setMessage("delete file a").call();
-
-               FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
-               writeTrashFile("a/c", "Hello world c");
-
-               FileEntry entry = new FileTreeIterator.FileEntry(new File(
-                               db.getWorkTree(), "a"), db.getFS());
-               assertEquals(FileMode.TREE, entry.getMode());
-
-               CheckoutConflictException exception = null;
-               try {
-                       git.checkout().setName(branch_1.getName()).call();
-               } catch (CheckoutConflictException e) {
-                       exception = e;
+               try (Git git = new Git(db)) {
+                       writeTrashFile("b", "Hello world b");
+                       writeTrashFile("a", "b");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("add file b & file a").call();
+                       Ref branch_1 = git.branchCreate().setName("branch_1").call();
+                       git.rm().addFilepattern("a").call();
+                       git.commit().setMessage("delete file a").call();
+
+                       FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
+                       writeTrashFile("a/c", "Hello world c");
+
+                       FileEntry entry = new FileTreeIterator.FileEntry(new File(
+                                       db.getWorkTree(), "a"), db.getFS());
+                       assertEquals(FileMode.TREE, entry.getMode());
+
+                       CheckoutConflictException exception = null;
+                       try {
+                               git.checkout().setName(branch_1.getName()).call();
+                       } catch (CheckoutConflictException e) {
+                               exception = e;
+                       }
+                       assertNotNull(exception);
+                       assertEquals(2, exception.getConflictingPaths().size());
+                       assertEquals("a", exception.getConflictingPaths().get(0));
+                       assertEquals("a/c", exception.getConflictingPaths().get(1));
                }
-               assertNotNull(exception);
-               assertEquals(2, exception.getConflictingPaths().size());
-               assertEquals("a", exception.getConflictingPaths().get(0));
-               assertEquals("a/c", exception.getConflictingPaths().get(1));
        }
 
        /**
@@ -364,40 +380,41 @@ public class CheckoutTest extends CLIRepositoryTestCase {
        @Test
        public void fileModeTestFolderThenMissingWithFileInWorkingTree()
                        throws Exception {
-               Git git = new Git(db);
-               FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
-               writeTrashFile("a/c", "Hello world c");
-               writeTrashFile("b", "Hello world b");
-               git.add().addFilepattern(".").call();
-               RevCommit commit1 = git.commit().setMessage("add folder a & file b")
-                               .call();
-               Ref branch_1 = git.branchCreate().setName("branch_1").call();
-               git.rm().addFilepattern("a").call();
-               RevCommit commit2 = git.commit().setMessage("delete folder a").call();
-
-               TreeWalk tw = new TreeWalk(db);
-               tw.addTree(commit1.getTree());
-               tw.addTree(commit2.getTree());
-               List<DiffEntry> scan = DiffEntry.scan(tw);
-               assertEquals(1, scan.size());
-               assertEquals(FileMode.MISSING, scan.get(0).getNewMode());
-               assertEquals(FileMode.TREE, scan.get(0).getOldMode());
-
-               writeTrashFile("a", "b");
-
-               FileEntry entry = new FileTreeIterator.FileEntry(new File(
-                               db.getWorkTree(), "a"), db.getFS());
-               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
-
-               CheckoutConflictException exception = null;
-               try {
-                       git.checkout().setName(branch_1.getName()).call();
-               } catch (CheckoutConflictException e) {
-                       exception = e;
+               try (Git git = new Git(db)) {
+                       FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
+                       writeTrashFile("a/c", "Hello world c");
+                       writeTrashFile("b", "Hello world b");
+                       git.add().addFilepattern(".").call();
+                       RevCommit commit1 = git.commit().setMessage("add folder a & file b")
+                                       .call();
+                       Ref branch_1 = git.branchCreate().setName("branch_1").call();
+                       git.rm().addFilepattern("a").call();
+                       RevCommit commit2 = git.commit().setMessage("delete folder a").call();
+
+                       TreeWalk tw = new TreeWalk(db);
+                       tw.addTree(commit1.getTree());
+                       tw.addTree(commit2.getTree());
+                       List<DiffEntry> scan = DiffEntry.scan(tw);
+                       assertEquals(1, scan.size());
+                       assertEquals(FileMode.MISSING, scan.get(0).getNewMode());
+                       assertEquals(FileMode.TREE, scan.get(0).getOldMode());
+
+                       writeTrashFile("a", "b");
+
+                       FileEntry entry = new FileTreeIterator.FileEntry(new File(
+                                       db.getWorkTree(), "a"), db.getFS());
+                       assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+
+                       CheckoutConflictException exception = null;
+                       try {
+                               git.checkout().setName(branch_1.getName()).call();
+                       } catch (CheckoutConflictException e) {
+                               exception = e;
+                       }
+                       assertNotNull(exception);
+                       assertEquals(1, exception.getConflictingPaths().size());
+                       assertEquals("a", exception.getConflictingPaths().get(0));
                }
-               assertNotNull(exception);
-               assertEquals(1, exception.getConflictingPaths().size());
-               assertEquals("a", exception.getConflictingPaths().get(0));
        }
 
        /**
@@ -420,30 +437,31 @@ public class CheckoutTest extends CLIRepositoryTestCase {
        @Test
        public void fileModeTestFolderThenFileWithMissingInWorkingTree()
                        throws Exception {
-               Git git = new Git(db);
-               FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
-               writeTrashFile("a/c", "Hello world c");
-               writeTrashFile("b", "Hello world b");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("add folder a & file b").call();
-               Ref branch_1 = git.branchCreate().setName("branch_1").call();
-               git.rm().addFilepattern("a").call();
-               File fileA = new File(db.getWorkTree(), "a");
-               writeTrashFile("a", "b");
-               git.add().addFilepattern("a").call();
-               git.commit().setMessage("add file a").call();
-
-               FileEntry entry = new FileTreeIterator.FileEntry(new File(
-                               db.getWorkTree(), "a"), db.getFS());
-               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
-
-               FileUtils.delete(fileA);
-
-               git.checkout().setName(branch_1.getName()).call();
-
-               entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
-                               db.getFS());
-               assertEquals(FileMode.TREE, entry.getMode());
+               try (Git git = new Git(db)) {
+                       FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
+                       writeTrashFile("a/c", "Hello world c");
+                       writeTrashFile("b", "Hello world b");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("add folder a & file b").call();
+                       Ref branch_1 = git.branchCreate().setName("branch_1").call();
+                       git.rm().addFilepattern("a").call();
+                       File fileA = new File(db.getWorkTree(), "a");
+                       writeTrashFile("a", "b");
+                       git.add().addFilepattern("a").call();
+                       git.commit().setMessage("add file a").call();
+
+                       FileEntry entry = new FileTreeIterator.FileEntry(new File(
+                                       db.getWorkTree(), "a"), db.getFS());
+                       assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+
+                       FileUtils.delete(fileA);
+
+                       git.checkout().setName(branch_1.getName()).call();
+
+                       entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
+                                       db.getFS());
+                       assertEquals(FileMode.TREE, entry.getMode());
+               }
        }
 
        /**
@@ -464,38 +482,39 @@ public class CheckoutTest extends CLIRepositoryTestCase {
         */
        @Test
        public void fileModeTestFileThenFileWithFolderInIndex() throws Exception {
-               Git git = new Git(db);
-               writeTrashFile("a", "Hello world a");
-               writeTrashFile("b", "Hello world b");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("add files a & b").call();
-               Ref branch_1 = git.branchCreate().setName("branch_1").call();
-               writeTrashFile("a", "b");
-               git.add().addFilepattern("a").call();
-               git.commit().setMessage("add file a").call();
-
-               FileEntry entry = new FileTreeIterator.FileEntry(new File(
-                               db.getWorkTree(), "a"), db.getFS());
-               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
-
-               git.rm().addFilepattern("a").call();
-               FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
-               writeTrashFile("a/c", "Hello world c");
-               git.add().addFilepattern(".").call();
-
-               entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
-                               db.getFS());
-               assertEquals(FileMode.TREE, entry.getMode());
-
-               CheckoutConflictException exception = null;
-               try {
-                       git.checkout().setName(branch_1.getName()).call();
-               } catch (CheckoutConflictException e) {
-                       exception = e;
+               try (Git git = new Git(db)) {
+                       writeTrashFile("a", "Hello world a");
+                       writeTrashFile("b", "Hello world b");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("add files a & b").call();
+                       Ref branch_1 = git.branchCreate().setName("branch_1").call();
+                       writeTrashFile("a", "b");
+                       git.add().addFilepattern("a").call();
+                       git.commit().setMessage("add file a").call();
+
+                       FileEntry entry = new FileTreeIterator.FileEntry(new File(
+                                       db.getWorkTree(), "a"), db.getFS());
+                       assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+
+                       git.rm().addFilepattern("a").call();
+                       FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
+                       writeTrashFile("a/c", "Hello world c");
+                       git.add().addFilepattern(".").call();
+
+                       entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
+                                       db.getFS());
+                       assertEquals(FileMode.TREE, entry.getMode());
+
+                       CheckoutConflictException exception = null;
+                       try {
+                               git.checkout().setName(branch_1.getName()).call();
+                       } catch (CheckoutConflictException e) {
+                               exception = e;
+                       }
+                       assertNotNull(exception);
+                       assertEquals(1, exception.getConflictingPaths().size());
+                       assertEquals("a", exception.getConflictingPaths().get(0));
                }
-               assertNotNull(exception);
-               assertEquals(1, exception.getConflictingPaths().size());
-               assertEquals("a", exception.getConflictingPaths().get(0));
        }
 
        /**
@@ -517,65 +536,67 @@ public class CheckoutTest extends CLIRepositoryTestCase {
         */
        @Test
        public void fileModeTestFileWithFolderInIndex() throws Exception {
-               Git git = new Git(db);
-               writeTrashFile("b", "Hello world b");
-               writeTrashFile("a", "b");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("add file b & file a").call();
-               Ref branch_1 = git.branchCreate().setName("branch_1").call();
-               git.rm().addFilepattern("a").call();
-               writeTrashFile("a", "Hello world a");
-               git.add().addFilepattern("a").call();
-               git.commit().setMessage("add file a").call();
-
-               FileEntry entry = new FileTreeIterator.FileEntry(new File(
-                               db.getWorkTree(), "a"), db.getFS());
-               assertEquals(FileMode.REGULAR_FILE, entry.getMode());
-
-               git.rm().addFilepattern("a").call();
-               FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
-               writeTrashFile("a/c", "Hello world c");
-               git.add().addFilepattern(".").call();
-
-               entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
-                               db.getFS());
-               assertEquals(FileMode.TREE, entry.getMode());
-
-               CheckoutConflictException exception = null;
-               try {
-                       git.checkout().setName(branch_1.getName()).call();
-               } catch (CheckoutConflictException e) {
-                       exception = e;
+               try (Git git = new Git(db)) {
+                       writeTrashFile("b", "Hello world b");
+                       writeTrashFile("a", "b");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("add file b & file a").call();
+                       Ref branch_1 = git.branchCreate().setName("branch_1").call();
+                       git.rm().addFilepattern("a").call();
+                       writeTrashFile("a", "Hello world a");
+                       git.add().addFilepattern("a").call();
+                       git.commit().setMessage("add file a").call();
+
+                       FileEntry entry = new FileTreeIterator.FileEntry(new File(
+                                       db.getWorkTree(), "a"), db.getFS());
+                       assertEquals(FileMode.REGULAR_FILE, entry.getMode());
+
+                       git.rm().addFilepattern("a").call();
+                       FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
+                       writeTrashFile("a/c", "Hello world c");
+                       git.add().addFilepattern(".").call();
+
+                       entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
+                                       db.getFS());
+                       assertEquals(FileMode.TREE, entry.getMode());
+
+                       CheckoutConflictException exception = null;
+                       try {
+                               git.checkout().setName(branch_1.getName()).call();
+                       } catch (CheckoutConflictException e) {
+                               exception = e;
+                       }
+                       assertNotNull(exception);
+                       assertEquals(1, exception.getConflictingPaths().size());
+                       assertEquals("a", exception.getConflictingPaths().get(0));
+
+                       // TODO: ideally we'd like to get two paths from this exception
+                       // assertEquals(2, exception.getConflictingPaths().size());
+                       // assertEquals("a", exception.getConflictingPaths().get(0));
+                       // assertEquals("a/c", exception.getConflictingPaths().get(1));
                }
-               assertNotNull(exception);
-               assertEquals(1, exception.getConflictingPaths().size());
-               assertEquals("a", exception.getConflictingPaths().get(0));
-
-               // TODO: ideally we'd like to get two paths from this exception
-               // assertEquals(2, exception.getConflictingPaths().size());
-               // assertEquals("a", exception.getConflictingPaths().get(0));
-               // assertEquals("a/c", exception.getConflictingPaths().get(1));
        }
 
        @Test
        public void testCheckoutPath() throws Exception {
-               Git git = new Git(db);
-               writeTrashFile("a", "Hello world a");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("commit file a").call();
-               git.branchCreate().setName("branch_1").call();
-               git.checkout().setName("branch_1").call();
-               File b = writeTrashFile("b", "Hello world b");
-               git.add().addFilepattern("b").call();
-               git.commit().setMessage("commit file b").call();
-               File a = writeTrashFile("a", "New Hello world a");
-               git.add().addFilepattern(".").call();
-               git.commit().setMessage("modified a").call();
-               assertArrayEquals(new String[] { "" },
-                               execute("git checkout HEAD~2 -- a"));
-               assertEquals("Hello world a", read(a));
-               assertArrayEquals(new String[] { "* branch_1", "  master", "" },
-                               execute("git branch"));
-               assertEquals("Hello world b", read(b));
+               try (Git git = new Git(db)) {
+                       writeTrashFile("a", "Hello world a");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("commit file a").call();
+                       git.branchCreate().setName("branch_1").call();
+                       git.checkout().setName("branch_1").call();
+                       File b = writeTrashFile("b", "Hello world b");
+                       git.add().addFilepattern("b").call();
+                       git.commit().setMessage("commit file b").call();
+                       File a = writeTrashFile("a", "New Hello world a");
+                       git.add().addFilepattern(".").call();
+                       git.commit().setMessage("modified a").call();
+                       assertArrayEquals(new String[] { "" },
+                                       execute("git checkout HEAD~2 -- a"));
+                       assertEquals("Hello world a", read(a));
+                       assertArrayEquals(new String[] { "* branch_1", "  master", "" },
+                                       execute("git branch"));
+                       assertEquals("Hello world b", read(b));
+               }
        }
 }