format-patch: support format.mboxrd with --stdout

mboxrd is a more robust output format when used with --stdout and needs more exposure. Introducing this config knob lets users choose the more robust format for all their --stdout uses. Relying on --pretty=mboxrd and including all of pretty-formats.txt in the `git format-patch' documentation would likely be confusing to users. Furthermore, this setting is useful across multiple invocations. So introduce `format.mboxrd' as a boolean configuration knob that changes the default --pretty=email format to --pretty=mboxrd when (and only when) --stdout is in use. Signed-off-by: Eric Wong <e@80x24.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Wong committed Dec 22, 2022 at 20:16 UTC 4810946f6032a973ad752d06a32437bc9bb35e67
4 files changed +17 -3
Documentation/config/format.txt
+4
@@ -139,3 +139,7 @@ For example,
139 ------------
140 +
141 will only show notes from `refs/notes/bar`.
142 +
143 +format.mboxrd::
144 + A boolean value which enables the robust "mboxrd" format when
145 + `--stdout` is in use to escape "^>+From " lines.
builtin/log.c
+8
@@ -52,6 +52,7 @@ static int decoration_style;
52 static int decoration_given;
53 static int use_mailmap_config = 1;
54 static unsigned int force_in_body_from;
55 +static int stdout_mboxrd;
56 static const char *fmt_patch_subject_prefix = "PATCH";
57 static int fmt_patch_name_max = FORMAT_PATCH_NAME_MAX_DEFAULT;
58 static const char *fmt_pretty;
@@ -1077,6 +1078,10 @@ static int git_format_config(const char *var, const char *value, void *cb)
1078 cover_from_description_mode = parse_cover_from_description(value);
1079 return 0;
1080 }
1081 + if (!strcmp(var, "format.mboxrd")) {
1082 + stdout_mboxrd = git_config_bool(var, value);
1083 + return 0;
1084 + }
1085
1086 return git_log_config(var, value, cb);
1087 }
@@ -2105,6 +2110,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
2110 rev.diffopt.close_file, "--output",
2111 !!output_directory, "--output-directory");
2112
2113 + if (use_stdout && stdout_mboxrd)
2114 + rev.commit_format = CMIT_FMT_MBOXRD;
2115 +
2116 if (use_stdout) {
2117 setup_pager();
2118 } else if (!rev.diffopt.close_file) {
t/t4014-format-patch.sh
+4 -2
@@ -2281,7 +2281,7 @@ test_expect_success 'format-patch --attach cover-letter only is non-multipart' '
2281 test_line_count = 1 output
2282 '
2283
2284 -test_expect_success 'format-patch --pretty=mboxrd' '
2284 +test_expect_success '-c format.mboxrd format-patch' '
2285 sp=" " &&
2286 cat >msg <<-INPUT_END &&
2287 mboxrd should escape the body
@@ -2316,7 +2316,9 @@ test_expect_success 'format-patch --pretty=mboxrd' '
2316 INPUT_END
2317
2318 C=$(git commit-tree HEAD^^{tree} -p HEAD <msg) &&
2319 - git format-patch --pretty=mboxrd --stdout -1 $C~1..$C >patch &&
2319 + git -c format.mboxrd format-patch --stdout -1 $C~1..$C >patch &&
2320 + git format-patch --pretty=mboxrd --stdout -1 $C~1..$C >compat &&
2321 + test_cmp patch compat &&
2322 git grep -h --no-index -A11 \
2323 "^>From could trip up a loose mbox parser" patch >actual &&
2324 test_cmp expect actual
t/t4150-am.sh
+1 -1
@@ -1033,7 +1033,7 @@ test_expect_success 'am --patch-format=mboxrd handles mboxrd' '
1033 >From extra escape for reversibility
1034 INPUT_END
1035 git commit -F msg &&
1036 - git format-patch --pretty=mboxrd --stdout -1 >mboxrd1 &&
1036 + git -c format.mboxrd format-patch --stdout -1 >mboxrd1 &&
1037 grep "^>From could trip up a loose mbox parser" mboxrd1 &&
1038 git checkout -f first &&
1039 git am --patch-format=mboxrd mboxrd1 &&