From 7f84e40f31c692df57c75719bb1733dbb8d0eb40 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Thu, 14 Jan 2016 17:49:26 +0900 Subject: [PATCH] CheckoutTest: Create Git instances in try-with-resource Change-Id: I49a03f7bee0b61c062ce160674f9aa0cd1bcc8ba Signed-off-by: David Pursehouse --- .../org/eclipse/jgit/pgm/CheckoutTest.java | 523 +++++++++--------- 1 file changed, 272 insertions(+), 251 deletions(-) diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java index 939a9f6fd..cb36d057e 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java @@ -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 void testCheckoutNonExistingBranch() throws Exception { @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 void testCheckoutUnresolvedHead() throws Exception { @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(); + 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]); + 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 void testCheckoutExistingBranchWithConflict() throws Exception { */ @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(); + 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()); + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - FileUtils.delete(fileA); + FileUtils.delete(fileA); - git.checkout().setName(branch_1.getName()).call(); + 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)); + 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(); + 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()); + 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 void testCheckoutOrphan() throws Exception { @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(); + 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()); + 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"); + 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()); + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - git.checkout().setName(branch_1.getName()).call(); + git.checkout().setName(branch_1.getName()).call(); - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + } } /** @@ -272,30 +286,31 @@ public void fileModeTestMissingThenFolderWithFileInWorkingTree() */ @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(); + 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()); + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.TREE, entry.getMode()); - FileUtils.delete(folderA, FileUtils.RECURSIVE); + FileUtils.delete(folderA, FileUtils.RECURSIVE); - git.checkout().setName(branch_1.getName()).call(); + git.checkout().setName(branch_1.getName()).call(); - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + } } /** @@ -317,32 +332,33 @@ public void fileModeTestFolderWithMissingInWorkingTree() throws Exception { */ @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(); + 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"); + 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()); + 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; + 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 void fileModeTestMissingWithFolderInWorkingTree() throws Exception { @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(); + 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 scan = DiffEntry.scan(tw); - assertEquals(1, scan.size()); - assertEquals(FileMode.MISSING, scan.get(0).getNewMode()); - assertEquals(FileMode.TREE, scan.get(0).getOldMode()); + TreeWalk tw = new TreeWalk(db); + tw.addTree(commit1.getTree()); + tw.addTree(commit2.getTree()); + List 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"); + writeTrashFile("a", "b"); - FileEntry entry = new FileTreeIterator.FileEntry(new File( - db.getWorkTree(), "a"), db.getFS()); - assertEquals(FileMode.REGULAR_FILE, entry.getMode()); + 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; + 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 void fileModeTestFolderThenMissingWithFileInWorkingTree() @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(); + 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()); + FileEntry entry = new FileTreeIterator.FileEntry(new File( + db.getWorkTree(), "a"), db.getFS()); + assertEquals(FileMode.REGULAR_FILE, entry.getMode()); - FileUtils.delete(fileA); + FileUtils.delete(fileA); - git.checkout().setName(branch_1.getName()).call(); + git.checkout().setName(branch_1.getName()).call(); - entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), - db.getFS()); - assertEquals(FileMode.TREE, entry.getMode()); + entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"), + db.getFS()); + assertEquals(FileMode.TREE, entry.getMode()); + } } /** @@ -464,38 +482,39 @@ public void fileModeTestFolderThenFileWithMissingInWorkingTree() */ @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(); + 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()); + 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(); + 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()); + 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; + 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 void fileModeTestFileThenFileWithFolderInIndex() throws Exception { */ @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(); + 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()); + 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(); + 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()); + 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; + 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)); + } } }