Browse Source

Make ApplyCommand create missing parent directories for new files

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>
stable-2.1
Markus Duft 13 years ago committed by Chris Aniszczyk
parent
commit
3c09e980cb
  1. 9
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A1_sub.patch
  2. 8
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java
  3. 2
      org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java

9
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/A1_sub.patch

@ -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

8
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java

@ -111,6 +111,14 @@ public class ApplyCommandTest extends RepositoryTestCase {
b.getString(0, b.size(), false)); 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 @Test
public void testDeleteD() throws Exception { public void testDeleteD() throws Exception {
ApplyResult result = init("D", true, false); ApplyResult result = init("D", true, false);

2
org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java

@ -167,6 +167,8 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
File f = new File(getRepository().getWorkTree(), path); File f = new File(getRepository().getWorkTree(), path);
if (create) if (create)
try { try {
File parent = f.getParentFile();
FileUtils.mkdirs(parent, true);
FileUtils.createNewFile(f); FileUtils.createNewFile(f);
} catch (IOException e) { } catch (IOException e) {
throw new PatchApplyException(MessageFormat.format( throw new PatchApplyException(MessageFormat.format(

Loading…
Cancel
Save