From e81592e07692284aab607e959096030027067695 Mon Sep 17 00:00:00 2001 From: Nadav Cohen Date: Sun, 1 May 2016 19:06:39 -0700 Subject: [PATCH] Allow setting FileMode to executable when applying patches in ApplyCommand git-apply allows modifying file modes in patched files using either "new mode" or "new file mode" headers. This patch adds support for setting files as executables and vice-versa. Change-Id: I24848966b46f686f540a8efa8150b42e0d9c3ad1 Signed-off-by: Nadav Cohen --- .../tst-rsrc/org/eclipse/jgit/diff/M1.patch | 9 ++++++ .../org/eclipse/jgit/diff/M1_PostImage | 3 ++ .../tst-rsrc/org/eclipse/jgit/diff/M2.patch | 10 +++++++ .../org/eclipse/jgit/diff/M2_PostImage | 1 + .../org/eclipse/jgit/diff/M2_PreImage | 3 ++ .../tst-rsrc/org/eclipse/jgit/diff/M3.patch | 10 +++++++ .../org/eclipse/jgit/diff/M3_PostImage | 3 ++ .../org/eclipse/jgit/diff/M3_PreImage | 1 + .../eclipse/jgit/api/ApplyCommandTest.java | 28 +++++++++++++++++++ .../org/eclipse/jgit/api/ApplyCommand.java | 3 ++ 10 files changed, 71 insertions(+) create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M1.patch create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M1_PostImage create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2.patch create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2_PostImage create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2_PreImage create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3.patch create mode 100644 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3_PostImage create mode 100755 org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3_PreImage diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M1.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M1.patch new file mode 100644 index 0000000000..395bd4c503 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M1.patch @@ -0,0 +1,9 @@ +diff --git a/M1 b/M1 +new file mode 100755 +index 0000000..de98044 +--- /dev/null ++++ b/M1 +@@ -0,0 +1,3 @@ ++a ++b ++c diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M1_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M1_PostImage new file mode 100644 index 0000000000..de980441c3 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M1_PostImage @@ -0,0 +1,3 @@ +a +b +c diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2.patch new file mode 100644 index 0000000000..b56ca0828e --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2.patch @@ -0,0 +1,10 @@ +diff --git a/M2 b/M2 +old mode 100644 +new mode 100755 +index 0000000..de98044 +--- a/M2 ++++ b/M2 +@@ -1,3 +1,1 @@ + a +-b +-c diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2_PostImage new file mode 100644 index 0000000000..7898192261 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2_PostImage @@ -0,0 +1 @@ +a diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2_PreImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2_PreImage new file mode 100644 index 0000000000..de980441c3 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M2_PreImage @@ -0,0 +1,3 @@ +a +b +c diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3.patch new file mode 100644 index 0000000000..ee536058ab --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3.patch @@ -0,0 +1,10 @@ +diff --git a/M3 b/M3 +old mode 100755 +new mode 100644 +index 0000000..de98044 +--- a/M3 ++++ b/M3 +@@ -1,1 +1,3 @@ + a ++b ++c diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3_PostImage new file mode 100644 index 0000000000..de980441c3 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3_PostImage @@ -0,0 +1,3 @@ +a +b +c diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3_PreImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3_PreImage new file mode 100755 index 0000000000..7898192261 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/M3_PreImage @@ -0,0 +1 @@ +a diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java index f2b5b3ba95..172807c43d 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java @@ -44,6 +44,7 @@ package org.eclipse.jgit.api; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; @@ -156,6 +157,33 @@ public class ApplyCommandTest extends RepositoryTestCase { b.getString(0, b.size(), false)); } + @Test + public void testAddM1() throws Exception { + ApplyResult result = init("M1", false, true); + assertEquals(1, result.getUpdatedFiles().size()); + assertTrue(result.getUpdatedFiles().get(0).canExecute()); + checkFile(new File(db.getWorkTree(), "M1"), + b.getString(0, b.size(), false)); + } + + @Test + public void testModifyM2() throws Exception { + ApplyResult result = init("M2", true, true); + assertEquals(1, result.getUpdatedFiles().size()); + assertTrue(result.getUpdatedFiles().get(0).canExecute()); + checkFile(new File(db.getWorkTree(), "M2"), + b.getString(0, b.size(), false)); + } + + @Test + public void testModifyM3() throws Exception { + ApplyResult result = init("M3", true, true); + assertEquals(1, result.getUpdatedFiles().size()); + assertFalse(result.getUpdatedFiles().get(0).canExecute()); + checkFile(new File(db.getWorkTree(), "M3"), + b.getString(0, b.size(), false)); + } + @Test public void testModifyX() throws Exception { ApplyResult result = init("X"); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java index 8fbf83954c..d74e991395 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java @@ -58,6 +58,7 @@ import org.eclipse.jgit.api.errors.PatchFormatException; import org.eclipse.jgit.diff.DiffEntry.ChangeType; import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.internal.JGitText; +import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.patch.FileHeader; import org.eclipse.jgit.patch.HunkHeader; @@ -260,6 +261,8 @@ public class ApplyCommand extends GitCommand { FileWriter fw = new FileWriter(f); fw.write(sb.toString()); fw.close(); + + getRepository().getFS().setExecute(f, fh.getNewMode() == FileMode.EXECUTABLE_FILE); } private static boolean isChanged(List ol, List nl) { -- 2.39.5