Browse Source

Create parent dir if necessary on checkout

An example where this is necessary is when a whole directory was deleted
and checkout is used to restore a file which was in that directory.

Bug: 372133
Change-Id: I1d45e0a5d2525fe1fdfbf08c9c5c166dd909e9fd
Signed-off-by: Robin Stocker <robin@nibor.org>
stable-2.1
Robin Stocker 13 years ago
parent
commit
a216624ef7
  1. 15
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java
  2. 7
      org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java

15
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java

@ -219,6 +219,21 @@ public class CheckoutCommandTest extends RepositoryTestCase {
indexState(db2, CONTENT)); 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 @Test
public void testDetachedHeadOnCheckout() throws JGitInternalException, public void testDetachedHeadOnCheckout() throws JGitInternalException,
IOException, GitAPIException { IOException, GitAPIException {

7
org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java

@ -78,6 +78,7 @@ import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup; import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.eclipse.jgit.util.FileUtils;
/** /**
* Checkout a branch to the working tree * Checkout a branch to the working tree
@ -297,9 +298,11 @@ public class CheckoutCommand extends GitCommand<Ref> {
public void apply(DirCacheEntry ent) { public void apply(DirCacheEntry ent) {
ent.setObjectId(blobId); ent.setObjectId(blobId);
ent.setFileMode(mode); ent.setFileMode(mode);
File file = new File(workTree, ent.getPathString());
File parentDir = file.getParentFile();
try { try {
DirCacheCheckout.checkoutEntry(repo, new File( FileUtils.mkdirs(parentDir, true);
workTree, ent.getPathString()), ent, r); DirCacheCheckout.checkoutEntry(repo, file, ent, r);
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException( throw new JGitInternalException(
MessageFormat.format( MessageFormat.format(

Loading…
Cancel
Save