Skip to content

Commit d9925d1

Browse files
Eric Wonggitster
authored andcommitted
am: support --patch-format=mboxrd
Combined with "git format-patch --pretty=mboxrd", this should allow us to round-trip commit messages with embedded mbox "From " lines without corruption. Signed-off-by: Eric Wong <e@80x24.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent c88098d commit d9925d1

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

Documentation/git-am.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ default. You can use `--no-utf8` to override this.
116116
By default the command will try to detect the patch format
117117
automatically. This option allows the user to bypass the automatic
118118
detection and specify the patch format that the patch(es) should be
119-
interpreted as. Valid formats are mbox, stgit, stgit-series and hg.
119+
interpreted as. Valid formats are mbox, mboxrd,
120+
stgit, stgit-series and hg.
120121

121122
-i::
122123
--interactive::

builtin/am.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ enum patch_format {
7070
PATCH_FORMAT_MBOX,
7171
PATCH_FORMAT_STGIT,
7272
PATCH_FORMAT_STGIT_SERIES,
73-
PATCH_FORMAT_HG
73+
PATCH_FORMAT_HG,
74+
PATCH_FORMAT_MBOXRD
7475
};
7576

7677
enum keep_type {
@@ -712,7 +713,8 @@ static int detect_patch_format(const char **paths)
712713
* Splits out individual email patches from `paths`, where each path is either
713714
* a mbox file or a Maildir. Returns 0 on success, -1 on failure.
714715
*/
715-
static int split_mail_mbox(struct am_state *state, const char **paths, int keep_cr)
716+
static int split_mail_mbox(struct am_state *state, const char **paths,
717+
int keep_cr, int mboxrd)
716718
{
717719
struct child_process cp = CHILD_PROCESS_INIT;
718720
struct strbuf last = STRBUF_INIT;
@@ -724,6 +726,8 @@ static int split_mail_mbox(struct am_state *state, const char **paths, int keep_
724726
argv_array_push(&cp.args, "-b");
725727
if (keep_cr)
726728
argv_array_push(&cp.args, "--keep-cr");
729+
if (mboxrd)
730+
argv_array_push(&cp.args, "--mboxrd");
727731
argv_array_push(&cp.args, "--");
728732
argv_array_pushv(&cp.args, paths);
729733

@@ -965,13 +969,15 @@ static int split_mail(struct am_state *state, enum patch_format patch_format,
965969

966970
switch (patch_format) {
967971
case PATCH_FORMAT_MBOX:
968-
return split_mail_mbox(state, paths, keep_cr);
972+
return split_mail_mbox(state, paths, keep_cr, 0);
969973
case PATCH_FORMAT_STGIT:
970974
return split_mail_conv(stgit_patch_to_mail, state, paths, keep_cr);
971975
case PATCH_FORMAT_STGIT_SERIES:
972976
return split_mail_stgit_series(state, paths, keep_cr);
973977
case PATCH_FORMAT_HG:
974978
return split_mail_conv(hg_patch_to_mail, state, paths, keep_cr);
979+
case PATCH_FORMAT_MBOXRD:
980+
return split_mail_mbox(state, paths, keep_cr, 1);
975981
default:
976982
die("BUG: invalid patch_format");
977983
}
@@ -2201,6 +2207,8 @@ static int parse_opt_patchformat(const struct option *opt, const char *arg, int
22012207
*opt_value = PATCH_FORMAT_STGIT_SERIES;
22022208
else if (!strcmp(arg, "hg"))
22032209
*opt_value = PATCH_FORMAT_HG;
2210+
else if (!strcmp(arg, "mboxrd"))
2211+
*opt_value = PATCH_FORMAT_MBOXRD;
22042212
else
22052213
return error(_("Invalid value for --patch-format: %s"), arg);
22062214
return 0;

t/t4150-am.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,4 +957,24 @@ test_expect_success 'am -s unexpected trailer block' '
957957
test_cmp expect actual
958958
'
959959

960+
test_expect_success 'am --patch-format=mboxrd handles mboxrd' '
961+
rm -fr .git/rebase-apply &&
962+
git checkout -f first &&
963+
echo mboxrd >>file &&
964+
git add file &&
965+
cat >msg <<-\INPUT_END &&
966+
mboxrd should escape the body
967+
968+
From could trip up a loose mbox parser
969+
>From extra escape for reversibility
970+
INPUT_END
971+
git commit -F msg &&
972+
git format-patch --pretty=mboxrd --stdout -1 >mboxrd1 &&
973+
grep "^>From could trip up a loose mbox parser" mboxrd1 &&
974+
git checkout -f first &&
975+
git am --patch-format=mboxrd mboxrd1 &&
976+
git cat-file commit HEAD | tail -n4 >out &&
977+
test_cmp msg out
978+
'
979+
960980
test_done

0 commit comments

Comments
 (0)