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. 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> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Barr committed Aug 8, 2011 at 04:49 UTC 0e8341f29d6541f661cc52f27f96877c0fd469c2
2 files changed +26 -3
git-am.sh
+8 -3
@@ -196,10 +196,15 @@ check_patch_format () {
196 return 0
197 fi
198
199 - # otherwise, check the first few lines of the first patch to try
200 - # to detect its format
199 + # otherwise, check the first few non-blank lines of the first
200 + # patch to try to detect its format
201 {
202 - read l1
202 + # Start from first line containing non-whitespace
203 + l1=
204 + while test -z "$l1"
205 + do
206 + read l1
207 + done
208 read l2
209 read l3
210 case "$l1" in
t/t4150-am.sh
+18
@@ -96,6 +96,13 @@ test_expect_success setup '
96 echo "X-Fake-Field: Line Three" &&
97 git format-patch --stdout first | sed -e "1d"
98 } | append_cr >patch1-crlf.eml &&
99 + {
100 + printf "%255s\\n" ""
101 + echo "X-Fake-Field: Line One" &&
102 + echo "X-Fake-Field: Line Two" &&
103 + echo "X-Fake-Field: Line Three" &&
104 + git format-patch --stdout first | sed -e "1d"
105 + } > patch1-ws.eml &&
106
107 sed -n -e "3,\$p" msg >file &&
108 git add file &&
@@ -167,6 +174,17 @@ test_expect_success 'am applies patch e-mail not in a mbox with CRLF' '
174 test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
175 '
176
177 +test_expect_success 'am applies patch e-mail with preceding whitespace' '
178 + rm -fr .git/rebase-apply &&
179 + git reset --hard &&
180 + git checkout first &&
181 + git am patch1-ws.eml &&
182 + ! test -d .git/rebase-apply &&
183 + git diff --exit-code second &&
184 + test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
185 + test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
186 +'
187 +
188 test_expect_success 'setup: new author and committer' '
189 GIT_AUTHOR_NAME="Another Thor" &&
190 GIT_AUTHOR_EMAIL="a.thor@example.com" &&