diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java index 9060cd530..dc9303aec 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java @@ -219,6 +219,21 @@ public void testCheckoutRemoteTrackingWithoutLocalBranch() throws Exception { indexState(db2, CONTENT)); } + @Test + public void testCheckoutOfFileWithInexistentParentDir() throws Exception { + File a = writeTrashFile("dir/a.txt", "A"); + writeTrashFile("dir/b.txt", "A"); + git.add().addFilepattern("dir/a.txt").addFilepattern("dir/b.txt") + .call(); + git.commit().setMessage("Added dir").call(); + + File dir = new File(db.getWorkTree(), "dir"); + FileUtils.delete(dir, FileUtils.RECURSIVE); + + git.checkout().addPath("dir/a.txt").call(); + assertTrue(a.exists()); + } + @Test public void testDetachedHeadOnCheckout() throws JGitInternalException, IOException, GitAPIException { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java index 03df65d8a..479fbd047 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java @@ -78,6 +78,7 @@ import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.filter.PathFilterGroup; +import org.eclipse.jgit.util.FileUtils; /** * Checkout a branch to the working tree @@ -297,9 +298,11 @@ protected CheckoutCommand checkoutPaths() throws IOException, public void apply(DirCacheEntry ent) { ent.setObjectId(blobId); ent.setFileMode(mode); + File file = new File(workTree, ent.getPathString()); + File parentDir = file.getParentFile(); try { - DirCacheCheckout.checkoutEntry(repo, new File( - workTree, ent.getPathString()), ent, r); + FileUtils.mkdirs(parentDir, true); + DirCacheCheckout.checkoutEntry(repo, file, ent, r); } catch (IOException e) { throw new JGitInternalException( MessageFormat.format(