Skip to content

Commit c2ca1d7

Browse files
committed
Allow mailsplit (and hence git-am) to handle mails with CRLF line-endings
It is not that uncommon to have mails with DOS line-ending, notably Thunderbird and web mailers like Gmail (when saving what they call "original" message). So modify mailsplit to convert CRLF line-endings to just LF. Since git-rebase is built on top of git-am, add an option to mailsplit to be used by git-am when it is acting on behalf of git-rebase, to refrain from doing this conversion. And add a test to make sure that rebase still works. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6a2d3f5 commit c2ca1d7

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

builtin-mailsplit.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static int is_from_line(const char *line, int len)
4444
}
4545

4646
static struct strbuf buf = STRBUF_INIT;
47+
static int keep_cr;
4748

4849
/* Called with the first line (potentially partial)
4950
* already in buf[] -- normally that should begin with
@@ -69,6 +70,12 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)
6970
* "From " and having something that looks like a date format.
7071
*/
7172
for (;;) {
73+
if (!keep_cr && buf.len > 1 && buf.buf[buf.len-1] == '\n' &&
74+
buf.buf[buf.len-2] == '\r') {
75+
strbuf_setlen(&buf, buf.len-2);
76+
strbuf_addch(&buf, '\n');
77+
}
78+
7279
if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
7380
die_errno("cannot write output");
7481

@@ -226,6 +233,8 @@ int cmd_mailsplit(int argc, const char **argv, const char *prefix)
226233
nr = strtol(arg+2, NULL, 10);
227234
} else if ( arg[1] == 'b' && !arg[2] ) {
228235
allow_bare = 1;
236+
} else if (!strcmp(arg, "--keep-cr")) {
237+
keep_cr = 1;
229238
} else if ( arg[1] == 'o' && arg[2] ) {
230239
dir = arg+2;
231240
} else if ( arg[1] == '-' && !arg[2] ) {

git-am.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,13 @@ check_patch_format () {
197197
split_patches () {
198198
case "$patch_format" in
199199
mbox)
200-
git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
200+
case "$rebasing" in
201+
'')
202+
keep_cr= ;;
203+
?*)
204+
keep_cr=--keep-cr ;;
205+
esac
206+
git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
201207
clean_abort
202208
;;
203209
stgit-series)

t/t3400-rebase.sh

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
# Copyright (c) 2005 Amos Waterland
44
#
55

6-
test_description='git rebase should not destroy author information
6+
test_description='git rebase assorted tests
77
8-
This test runs git rebase and checks that the author information is not lost.
8+
This test runs git rebase and checks that the author information is not lost
9+
among other things.
910
'
1011
. ./test-lib.sh
1112

@@ -133,4 +134,25 @@ test_expect_success 'rebase -q is quiet' '
133134
test ! -s output.out
134135
'
135136

137+
q_to_cr () {
138+
tr Q '\015'
139+
}
140+
141+
test_expect_success 'Rebase a commit that sprinkles CRs in' '
142+
(
143+
echo "One"
144+
echo "TwoQ"
145+
echo "Three"
146+
echo "FQur"
147+
echo "Five"
148+
) | q_to_cr >CR &&
149+
git add CR &&
150+
test_tick &&
151+
git commit -a -m "A file with a line with CR" &&
152+
git tag file-with-cr &&
153+
git checkout HEAD^0 &&
154+
git rebase --onto HEAD^^ HEAD^ &&
155+
git diff --exit-code file-with-cr:CR HEAD:CR
156+
'
157+
136158
test_done

0 commit comments

Comments
 (0)