Re: [PATCH] git-am: ignore leading whitespace before patch

4 messages, 3 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH] git-am: ignore leading whitespace before patch

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:51:45

David Barr [off-list ref] writes:
Hi Jonathan,
...
quoted
quoted
diff --git a/git-am.sh b/git-am.sh
index 463c741..19b2f0f 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -199,7 +199,11 @@ check_patch_format () {
       # otherwise, check the first few lines of the first patch to try
       # to detect its format
       {
-               read l1
+               # Start from first line containing non-whitespace
+               until [ -n "$l1" ]
+               do
+                       read l1
+               done
...
Do you see any subtle issues in this tiny patch?
I failed to include a test, I'll add at least one to the next version.
I did check that it doesn't break any of the existing git-am tests.
It no longer checks "the first few lines" but can read a lot more, so the
comment that precedes this block is now invalid.

Also we are rather old fashioned and we never say "until [ ... ]" anywhere
in our shell scripts.

	$ git grep -e until -- '*.sh'

Personally to me this is a borderline "Meh", in the sense that I wouldn't
bother to waste too much effort rejecting it, as I do not see downsides
other than these minor points.

Thanks.

[PATCH v2] am: ignore leading whitespace before patch

From: Jonathan Nieder <hidden>
Date: 2016-06-15 22:51:46

From: David Barr <redacted>

Some web-based email clients prepend whitespace to raw message
transcripts to workaround content-sniffing in some browsers.  Adjust
the patch format detection logic to ignore leading whitespace.

So now you can apply patches from GMail with "git am" in three steps:

 1. choose "show original"
 2. tell the browser to "save as" (for example by pressing Ctrl+S)
 3. run "git am" on the saved file

This fixes a regression introduced by v1.6.4-rc0~15^2~2 (git-am
foreign patch support: autodetect some patch formats, 2009-05-27).
GMail support was first introduced to "git am" by v1.5.4-rc0~274^2
(Make mailsplit and mailinfo strip whitespace from the start of the
input, 2007-11-01).

Signed-off-by: David Barr <redacted>
Acked-by: Tay Ray Chuan <redacted>
Signed-off-by: Jonathan Nieder <redacted>
---
Junio C Hamano wrote:
It no longer checks "the first few lines" but can read a lot more, so the
comment that precedes this block is now invalid.

Also we are rather old fashioned and we never say "until [ ... ]" anywhere
in our shell scripts.
Good ideas, thanks.  While at it, let's initialize l1 to protect
against any stray value it might have inherited from the environment.

Looking forward to the promised test, :)
Jonathan

 git-am.sh |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/git-am.sh b/git-am.sh
index 463c741d..c8422dbe 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -196,10 +196,15 @@ check_patch_format () {
 		return 0
 	fi
 
-	# otherwise, check the first few lines of the first patch to try
-	# to detect its format
+	# otherwise, check the first few non-blank lines of the first
+	# patch to try to detect its format
 	{
-		read l1
+		# Start from first line containing non-whitespace
+		l1=
+		while test -z "$l1"
+		do
+			read l1
+		done
 		read l2
 		read l3
 		case "$l1" in
-- 
1.7.6

RE: [PATCH v2] am: ignore leading whitespace before patch

From: David Barr <hidden>
Date: 2016-06-15 22:51:46

Add a test for GMail-style padded email files.

Signed-off-by: David Barr <redacted>
---
 t/t4150-am.sh |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
diff --git a/t/t4150-am.sh b/t/t4150-am.sh
index 151404e..40a5a3e 100755
--- a/t/t4150-am.sh
+++ b/t/t4150-am.sh
@@ -167,6 +167,17 @@ test_expect_success 'am applies patch e-mail not in a mbox with CRLF' '
 	test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
 '
 
+test_expect_success 'am applies patch e-mail with preceding whitespace' '
+	rm -fr .git/rebase-apply &&
+	git reset --hard &&
+	git checkout first &&
+	printf "%256s\\n" "" >patch1-ws.eml &&
+	cat patch1.eml >>patch1-ws.eml &&
+	git am <patch1-ws.eml >output.out 2>&1 &&
+	! test -d .git/rebase-apply &&
+	git diff --exit-code second
+'
+
 test_expect_success 'setup: new author and committer' '
 	GIT_AUTHOR_NAME="Another Thor" &&
 	GIT_AUTHOR_EMAIL="a.thor@example.com" &&
-- 
1.7.6

Re: [PATCH v2] am: ignore leading whitespace before patch

From: David Barr <hidden>
Date: 2016-06-15 22:51:46

*facepalm*

This test already passes:
+       git am <patch1-ws.eml >output.out 2>&1 &&
Alternatively, the following was failing:
+       git am patch1-ws.eml >output.out 2>&1 &&
Note that the email file is passed as an argument rather than a redirect.
--
David Barr
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help