Skip to contentSkip to conversation

Teach `git apply` to accept Subversion-generated diffs

PATCH In review Johannes Schindelin 2 participants 4 messages Feb 15, 2018

lore
Share & tools
lore
#0 lore raw

cover Teach `git apply` to accept Subversion-generated diffs

They are accepted already, almost. With one exception: the ---/+++ lines contain not only the file names, but continue with a TAB and then the revision (or "working copy" or "nonexistent") in parenthesis.

We already handle these lines, except in the case of /dev/null. Let's handle the case gracefully where the diff contains a "--- /dev/null" line with a trailing comment.

This contribution came in via Pull Request to Git for Windows, from a first time contributor! Yes!

Johannes Schindelin (1):
apply: demonstrate a problem applying svn diffs

Tatyana Krasnukha (1):
apply: handle Subversion diffs with /dev/null gracefully

 apply.c                          |  2 +-
 t/t4135-apply-weird-filenames.sh | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

base-commit: b2e45c695d09f6a31ce09347ae0a5d2cdfe9dd4e
Published-As: https://github.com/dscho/git/releases/tag/apply-svn-diff-v1
Fetch-It-Via: git fetch https://github.com/dscho/git apply-svn-diff-v1

Signature · 1 line
2.16.1.windows.1
#1 lore raw

PATCH 1/2 apply: demonstrate a problem applying svn diffs

Subversion generates diffs that contain funny ---/+++ lines containing more than just the file names. Example:

	--- a/trunk/README	(revision 4711)
	+++ /dev/null	(nonexistent)

Let's add a test case demonstrating that apply cannot handle the /dev/null line (although it can handle the trunk/README line just fine).

Reported in https://github.com/git-for-windows/git/issues/1489

---
 t/t4135-apply-weird-filenames.sh | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
t/t4135-apply-weird-filenames.sh +17 -0
index 27cb0009fb1..b14b8085786 100755
@@ -89,4 +89,21 @@ test_expect_success 'traditional, whitespace-damaged, colon in timezone' '
 	test_cmp expected "post image.txt"
 '
 
+cat >diff-from-svn <<\EOF
+Index: Makefile
+===================================================================
+diff --git a/branches/Makefile
+deleted file mode 100644
+--- a/branches/Makefile	(revision 13)
++++ /dev/null	(nonexistent)
+@@ +1 0,0 @@
+-
+EOF
+
+test_expect_failure 'apply handles a diff generated by Subversion' '
+	>Makefile &&
+	git apply -p2 diff-from-svn &&
+	test_path_is_missing Makefile
+'
+
 test_done
Signature · 1 line
2.16.1.windows.1
#2 lore raw

PATCH 2/2 apply: handle Subversion diffs with /dev/null gracefully

In reply to Johannes Schindelin

From: Tatyana Krasnukha <[email protected]>

Subversion generates diffs that can contain lines like this one:

--- /dev/null (nonexistent)

Let's teach Git's apply machinery to handle such a line gracefully.

This fixes https://github.com/git-for-windows/git/isues/1489

---
 apply.c                          | 2 +-
 t/t4135-apply-weird-filenames.sh | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
apply.c +1 -1
index f8b67bfee2c..107aa4c216e 100644
@@ -950,7 +950,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 		}
 		free(another);
 	} else {
-		if (!starts_with(line, "/dev/null\n"))
+		if (!is_dev_null(line))
 			return error(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
 	}
 
t/t4135-apply-weird-filenames.sh +1 -1
index b14b8085786..c7c688fcc4b 100755
@@ -100,7 +100,7 @@ deleted file mode 100644
 -
 EOF
 
-test_expect_failure 'apply handles a diff generated by Subversion' '
+test_expect_success 'apply handles a diff generated by Subversion' '
 	>Makefile &&
 	git apply -p2 diff-from-svn &&
 	test_path_is_missing Makefile
Signature · 1 line
2.16.1.windows.1
#3 lore raw

Re: [PATCH 2/2] apply: handle Subversion diffs with /dev/null gracefully

apply.c static int gitdiff_verify_name(struct apply_state *state, · line 955

Quoted 5 lines · Johannes Schindelin
apply.c +1 -1
 	} else {
-		if (!starts_with(line, "/dev/null\n"))
+		if (!is_dev_null(line))
 			return error(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
 	}

Yup. This seems to be the last explicit/manual check with the string "/dev/null" (instead of using is_dev_null(), which is how it should be and already is done in codepaths that guesses -p value and decides if it is a creation or a deletion patch).

Looks good. Will queue.

Quoted 13 lines
t/t4135-apply-weird-filenames.sh +1 -1
index b14b8085786..c7c688fcc4b 100755
@@ -100,7 +100,7 @@ deleted file mode 100644
 -
 EOF
 
-test_expect_failure 'apply handles a diff generated by Subversion' '
+test_expect_success 'apply handles a diff generated by Subversion' '
 	>Makefile &&
 	git apply -p2 diff-from-svn &&
 	test_path_is_missing Makefile