format-patch: allow forcing the use of in-body From: header

Users may be authoring and committing their commits under the same e-mail address they use to send their patches from, in which case they shouldn't need to use the in-body From: line in their outgoing e-mails. At the receiving end, "git am" will use the address on the "From:" header of the incoming e-mail and all should be well. Some mailing lists, however, mangle the From: address from what the original sender had; in such a situation, the user may want to add the in-body "From:" header even for their own patches. "git format-patch --[no-]force-in-body-from" was invented for such users. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 29, 2022 at 14:38 UTC 34bc1b1045af6ad813f4de662a453b3c77ba65a7
5 files changed +30
Documentation/git-format-patch.txt
+9
@@ -275,6 +275,15 @@ header). Note also that `git send-email` already handles this
275 transformation for you, and this option should not be used if you are
276 feeding the result to `git send-email`.
277
278 +--[no-]force-in-body-from::
279 + With the e-mail sender specified via the `--from` option, by
280 + default, an in-body "From:" to identify the real author of
281 + the commit is added at the top of the commit log message if
282 + the sender is different from the author. With this option,
283 + the in-body "From:" is added even when the sender and the
284 + author have the same name and address, which may help if the
285 + mailing list software mangles the sender's identity.
286 +
287 --add-header=<header>::
288 Add an arbitrary header to the email headers. This is in addition
289 to any configured headers, and may be used multiple times.
builtin/log.c
+5
@@ -52,6 +52,7 @@ static int default_encode_email_headers = 1;
52 static int decoration_style;
53 static int decoration_given;
54 static int use_mailmap_config = 1;
55 +static unsigned int force_in_body_from;
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;
@@ -1897,6 +1898,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1898 N_("show changes against <refspec> in cover letter or single patch")),
1899 OPT_INTEGER(0, "creation-factor", &creation_factor,
1900 N_("percentage by which creation is weighted")),
1901 + OPT_BOOL(0, "force-in-body-from", &force_in_body_from,
1902 + N_("show in-body From: even if identical to the e-mail header")),
1903 OPT_END()
1904 };
1905
@@ -1940,6 +1943,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1943 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
1944 PARSE_OPT_KEEP_DASHDASH);
1945
1946 + rev.force_in_body_from = force_in_body_from;
1947 +
1948 /* Make sure "0000-$sub.patch" gives non-negative length for $sub */
1949 if (fmt_patch_name_max <= strlen("0000-") + strlen(fmt_patch_suffix))
1950 fmt_patch_name_max = strlen("0000-") + strlen(fmt_patch_suffix);
pretty.c
+2
@@ -480,6 +480,8 @@ end:
480 static int use_in_body_from(const struct pretty_print_context *pp,
481 const struct ident_split *ident)
482 {
483 + if (pp->rev && pp->rev->force_in_body_from)
484 + return 1;
485 if (ident_cmp(pp->from_ident, ident))
486 return 1;
487 return 0;
revision.h
+1
@@ -221,6 +221,7 @@ struct rev_info {
221 missing_newline:1,
222 date_mode_explicit:1,
223 preserve_subject:1,
224 + force_in_body_from:1,
225 encode_email_headers:1,
226 include_header:1;
227 unsigned int disable_stdin:1;
t/t4014-format-patch.sh
+13
@@ -1400,6 +1400,19 @@ test_expect_success '--from omits redundant in-body header' '
1400 test_cmp expect patch.head
1401 '
1402
1403 +test_expect_success 'with --force-in-body-from, redundant in-body from is kept' '
1404 + git format-patch --force-in-body-from \
1405 + -1 --stdout --from="A U Thor <author@example.com>" >patch &&
1406 + cat >expect <<-\EOF &&
1407 + From: A U Thor <author@example.com>
1408 +
1409 + From: A U Thor <author@example.com>
1410 +
1411 + EOF
1412 + sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
1413 + test_cmp expect patch.head
1414 +'
1415 +
1416 test_expect_success 'in-body headers trigger content encoding' '
1417 test_env GIT_AUTHOR_NAME="éxötìc" test_commit exotic &&
1418 test_when_finished "git reset --hard HEAD^" &&