threads / patch / 28000

patchgit-am: ignore leading whitespace before patch

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

## tl;dr

10 messages between Aug 2, 2011 and Aug 8, 2011. Diffs are folded; open one to read it.

replies: 9people: 5as markdown or json

David Barr· Aug 2, 2011, 22:20 UTC · lore

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.

Signed-off-by: David Barr <davidbarr@google.com>
---
 git-am.sh |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
Show changes to git-am.sh +5 −1
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
 		read l2
 		read l3
 		case "$l1" in
-- 
1.7.6
Tay Ray Chuan· Aug 3, 2011, 12:28 UTC · re: David Barr · lore

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

On Wed, Aug 3, 2011 at 6:20 AM, David Barr <davidbarr@google.com> wrote:
Show 6 quoted lines
> 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.
>
> Signed-off-by: David Barr <davidbarr@google.com>
Finally, patches from GMail that play nice with git-am!
  Acked-by: Tay Ray Chuan <rctay89@gmail.com>
-- 
Cheers,
Ray Chuan
Sverre Rabbelier· Aug 3, 2011, 13:21 UTC · re: Tay Ray Chuan · lore

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

Heya,
On Wed, Aug 3, 2011 at 14:28, Tay Ray Chuan <rctay89@gmail.com> wrote:
Show 9 quoted lines
> On Wed, Aug 3, 2011 at 6:20 AM, David Barr <davidbarr@google.com> wrote:
>> 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.
>>
>> Signed-off-by: David Barr <davidbarr@google.com>
>
> Finally, patches from GMail that play nice with git-am!

So how do you get the patches out of gmail? Do you just copy/paste the output of the "Show original" page?

-- 
Cheers,

Sverre Rabbelier
Tay Ray Chuan· Aug 3, 2011, 13:31 UTC · re: Sverre Rabbelier · lore

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

On Wed, Aug 3, 2011 at 9:21 PM, Sverre Rabbelier <srabbelier@gmail.com> wrote:
> So how do you get the patches out of gmail? Do you just copy/paste the
> output of the "Show original" page?

I Ctrl-S from the "Show original" page. Using Chrome, that yields a mail.txt file.

Without this patch, I used to manually remove the first line (all empty whitespace).

-- 
Cheers,
Ray Chuan
Sverre Rabbelier· Aug 3, 2011, 13:33 UTC · re: Tay Ray Chuan · lore

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

Heya,
On Wed, Aug 3, 2011 at 15:31, Tay Ray Chuan <rctay89@gmail.com> wrote:
Show 6 quoted lines
> On Wed, Aug 3, 2011 at 9:21 PM, Sverre Rabbelier <srabbelier@gmail.com> wrote:
>> So how do you get the patches out of gmail? Do you just copy/paste the
>> output of the "Show original" page?
>
> I Ctrl-S from the "Show original" page. Using Chrome, that yields a
> mail.txt file.
Ah, clever hack! Nice :)
-- 
Cheers,

Sverre Rabbelier
David Barr· Aug 6, 2011, 01:56 UTC · re: Tay Ray Chuan · lore

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

Hi Jonathan,
Show 7 quoted lines
> On Wed, Aug 3, 2011 at 6:20 AM, David Barr <davidbarr@google.com> wrote:
>> 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.
>>
>> Signed-off-by: David Barr <davidbarr@google.com>
Show 17 quoted lines
>> 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
>>                read l2
>>                read l3
>>                case "$l1" in
On Wed, Aug 3, 2011 at 10:28 PM, Tay Ray Chuan <rctay89@gmail.com> wrote:
> Finally, patches from GMail that play nice with git-am!
>
>  Acked-by: Tay Ray Chuan <rctay89@gmail.com>

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.

-- David Barr

Junio C Hamano· Aug 6, 2011, 05:00 UTC · re: David Barr · lore

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

David Barr <davidbarr@google.com> writes:
Show 20 quoted lines
> Hi Jonathan,
> ...
>>> 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.
Jonathan Nieder· Aug 8, 2011, 02:49 UTC · re: Junio C Hamano · lore

[PATCH v2] am: ignore leading whitespace before patch

From: David Barr <davidbarr@google.com>

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 <davidbarr@google.com>
Acked-by: Tay Ray Chuan <rctay89@gmail.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Junio C Hamano wrote:
Show 5 quoted lines
> 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(-)
Show changes to git-am.sh +8 −3
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
David Barr· Aug 8, 2011, 05:10 UTC · re: Jonathan Nieder · lore

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

Add a test for GMail-style padded email files.
Signed-off-by: David Barr <davidbarr@google.com>
---
 t/t4150-am.sh |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
Show changes to t/t4150-am.sh +11 −0
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
David Barr· Aug 8, 2011, 05:31 UTC · re: David Barr · lore

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

*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

← back to recent threads