Skip to content

Commit 0e8341f

Browse files
barrbraingitster
authored andcommitted
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>
1 parent e7a85be commit 0e8341f

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

git-am.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,15 @@ check_patch_format () {
196196
return 0
197197
fi
198198

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
201201
{
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
203208
read l2
204209
read l3
205210
case "$l1" in

t/t4150-am.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ test_expect_success setup '
9696
echo "X-Fake-Field: Line Three" &&
9797
git format-patch --stdout first | sed -e "1d"
9898
} | 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 &&
99106
100107
sed -n -e "3,\$p" msg >file &&
101108
git add file &&
@@ -167,6 +174,17 @@ test_expect_success 'am applies patch e-mail not in a mbox with CRLF' '
167174
test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
168175
'
169176

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+
170188
test_expect_success 'setup: new author and committer' '
171189
GIT_AUTHOR_NAME="Another Thor" &&
172190
GIT_AUTHOR_EMAIL="a.thor@example.com" &&

0 commit comments

Comments
 (0)