Skip to content

Commit 0fcb2ca

Browse files
committed
am: allow individual e-mail files as input
We traditionally allowed a mbox file or a directory name of a maildir (but never an individual file inside a maildir) to be given to "git am". Even though an individual file in a maildir (or more generally, a piece of RFC2822 e-mail) is not a mbox file, it contains enough information to create a commit out of it, so there is no reason to reject one. Running mailsplit on such a file feels stupid, but it does not hurt. This builds on top of a5a6755 (git-am foreign patch support: introduce patch_format, 2009-05-27) that introduced mailbox format detection. The codepath to deal with a mbox requires it to begin with "From " line and also allows it to begin with "From: ", but a random piece of e-mail can and often do begin with any valid RFC2822 header lines. Instead of checking the first line, we extract all the lines up to the first empty line, and make sure they look like e-mail headers. A test is added to t4150 to demonstrate this feature. Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3f55e41 commit 0fcb2ca

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

git-am.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,20 @@ check_patch_format () {
191191
esac
192192
;;
193193
esac
194+
if test -z "$patch_format" &&
195+
test -n "$l1" &&
196+
test -n "$l2" &&
197+
test -n "$l3"
198+
then
199+
# This begins with three non-empty lines. Is this a
200+
# piece of e-mail a-la RFC2822? Grab all the headers,
201+
# discarding the indented remainder of folded lines,
202+
# and see if it looks like that they all begin with the
203+
# header field names...
204+
sed -n -e '/^$/q' -e '/^[ ]/d' -e p "$1" |
205+
egrep -v '^[A-Za-z]+(-[A-Za-z]+)*:' >/dev/null ||
206+
patch_format=mbox
207+
fi
194208
} < "$1" || clean_abort
195209
}
196210

t/t4150-am.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ test_expect_success setup '
7777
git commit -s -F msg &&
7878
git tag second &&
7979
git format-patch --stdout first >patch1 &&
80+
{
81+
echo "X-Fake-Field: Line One" &&
82+
echo "X-Fake-Field: Line Two" &&
83+
echo "X-Fake-Field: Line Three" &&
84+
git format-patch --stdout first | sed -e "1d"
85+
} > patch1.eml &&
8086
sed -n -e "3,\$p" msg >file &&
8187
git add file &&
8288
test_tick &&
@@ -108,6 +114,15 @@ test_expect_success 'am applies patch correctly' '
108114
test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
109115
'
110116

117+
test_expect_success 'am applies patch e-mail not in a mbox' '
118+
git checkout first &&
119+
git am patch1.eml &&
120+
! test -d .git/rebase-apply &&
121+
test -z "$(git diff second)" &&
122+
test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
123+
test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
124+
'
125+
111126
GIT_AUTHOR_NAME="Another Thor"
112127
GIT_AUTHOR_EMAIL="[email protected]"
113128
GIT_COMMITTER_NAME="Co M Miter"

0 commit comments

Comments
 (0)