]> source.dussan.org Git - jgit.git/commitdiff
Make ApplyCommand create missing parent directories for new files 73/6673/3
authorMarkus Duft <markus.duft@salomon.at>
Mon, 9 Jul 2012 10:47:52 +0000 (12:47 +0200)
committerChris Aniszczyk <zx@twitter.com>
Mon, 16 Jul 2012 16:04:30 +0000 (09:04 -0700)
Otherwise applying will fail with a FileNotFoundException, because
File.createNewFile() fails with missing parents.

Contains change & according test.

Change-Id: I970522b549b8bb260ca6720da11f12c57ee8a492
Signed-off-by: Chris Aniszczyk <zx@twitter.com>
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A1_sub.patch [new file with mode: 0644]
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java

diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A1_sub.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A1_sub.patch
new file mode 100644 (file)
index 0000000..09867f3
--- /dev/null
@@ -0,0 +1,9 @@
+diff --git a/sub/A1 b/sub/A1
+new file mode 100644
+index 0000000..de98044
+--- /dev/null
++++ b/sub/A1
+@@ -0,0 +1,3 @@
++a
++b
++c
index 16c0a9b4477c20f31296978f609f4201c318888e..3c38edb86e5c0f63e6f0d04251997994691c7d4a 100644 (file)
@@ -111,6 +111,14 @@ public class ApplyCommandTest extends RepositoryTestCase {
                                b.getString(0, b.size(), false));
        }
 
+       @Test
+       public void testAddA1Sub() throws Exception {
+               ApplyResult result = init("A1_sub", false, false);
+               assertEquals(1, result.getUpdatedFiles().size());
+               assertEquals(new File(db.getWorkTree(), "sub/A1"), result
+                               .getUpdatedFiles().get(0));
+       }
+
        @Test
        public void testDeleteD() throws Exception {
                ApplyResult result = init("D", true, false);
index 32abf86edc4636261ca42d43b6ee596dcffafec6..e6070eccf83bd73e801f3b5ad4c9a6d67d283637 100644 (file)
@@ -167,6 +167,8 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
                File f = new File(getRepository().getWorkTree(), path);
                if (create)
                        try {
+                               File parent = f.getParentFile();
+                               FileUtils.mkdirs(parent, true);
                                FileUtils.createNewFile(f);
                        } catch (IOException e) {
                                throw new PatchApplyException(MessageFormat.format(