Skip to contentSkip to conversation

git-am: ignore leading whitespace before patch

PATCH In review David Barr 5 participants 10 messages Jun 15, 2016

lore
Share & tools
lore

David Barr

author
#0 lore raw

PATCH 1/1 git-am: ignore leading whitespace before patch

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.

---
 git-am.sh |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
git-am.sh +5 -1
index 463c741..19b2f0f 100755
@@ -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
Signature · 1 line
1.7.6
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · David Barr

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.

Finally, patches from GMail that play nice with git-am!

Signature · 2 lines
Cheers,
Ray Chuan
#2 lore raw

Heya,

Quoted 9 lines · Tay Ray Chuan
Quoted 6 lines · David Barr

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.

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?

Signature · 3 lines
Cheers,

Sverre Rabbelier
#3 lore raw
Quoted 2 lines · Sverre Rabbelier

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).

Signature · 2 lines
Cheers,
Ray Chuan
#4 lore raw

Heya,

Quoted 6 lines · Tay Ray Chuan
Quoted 2 lines · Sverre Rabbelier

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 :)

Signature · 3 lines
Cheers,

Sverre Rabbelier
author
#5 lore raw
In reply to Tay Ray Chuan

Hi Jonathan,

Quoted 25 lines · David Barr
Quoted 24 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.

git-am.sh +5 -1
index 463c741..19b2f0f 100755
@@ -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
Quoted 3 lines · Tay Ray Chuan

Finally, patches from GMail that play nice with git-am!

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

#6 lore raw
Quoted 12 lines · David Barr

Hi Jonathan,
...

Quoted 10 lines
Quoted 10 lines
git-am.sh +1 -1
index 463c741..19b2f0f 100755
@@ -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
Quoted 8 lines
Quoted 4 lines

git-am.sh check_patch_format () { · line 206

Quoted 4 lines
git-am.sh +4 -0
+               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.

#7 lore raw

PATCH v2 1/1 am: ignore leading whitespace before patch

From: David Barr <[email protected]>

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).

---

Quoted 5 lines · Junio C Hamano

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(-)
git-am.sh +8 -3
index 463c741d..c8422dbe 100755
@@ -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
Signature · 1 line
1.7.6
author
#8 lore raw

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

Add a test for GMail-style padded email files.

---
 t/t4150-am.sh |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
t/t4150-am.sh +11 -0
index 151404e..40a5a3e 100755
@@ -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="[email protected]" &&
Signature · 1 line
1.7.6
author
#9 lore raw

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

*facepalm*

This test already passes:

Quoted 1 line

+       git am <patch1-ws.eml >output.out 2>&1 &&

Alternatively, the following was failing:

Quoted 1 line

+       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