aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Stocker2012-07-08 08:48:39 (EDT)
committerRobin Stocker2012-07-08 09:15:38 (EDT)
commita216624ef7628bf3746746f48f0c725fd55d1eaf (patch)
tree5fa76afb7a2695916abbf4cf7e60b95f1e48e66b
parentc0b4b79296966f53d3dd20913860942f9476dd20 (diff)
downloadjgit-a216624ef7628bf3746746f48f0c725fd55d1eaf.zip
jgit-a216624ef7628bf3746746f48f0c725fd55d1eaf.tar.gz
jgit-a216624ef7628bf3746746f48f0c725fd55d1eaf.tar.bz2
Create parent dir if necessary on checkoutrefs/changes/66/6666/1
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>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java15
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java7
2 files changed, 20 insertions, 2 deletions
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 9060cd5..dc9303a 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
@@ -220,6 +220,21 @@ public class CheckoutCommandTest extends RepositoryTestCase {
}
@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 {
CheckoutCommand co = git.checkout();
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 03df65d..479fbd0 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.RevTree;
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 @@ public class CheckoutCommand extends GitCommand<Ref> {
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(