Merge branch 'jc/format-patch-force-in-body-from'

"git format-patch --from=<ident>" can be told to add an in-body
"From:" line even for commits that are authored by the given
<ident> with "--force-in-body-from"option.

* jc/format-patch-force-in-body-from:
  format-patch: learn format.forceInBodyFrom configuration variable
  format-patch: allow forcing the use of in-body From: header
  pretty: separate out the logic to decide the use of in-body from
This commit is contained in:
Junio C Hamano 2022-09-09 12:02:25 -07:00
commit 0e2a4764ed
6 changed files with 73 additions and 1 deletions

View File

@ -15,6 +15,10 @@ format.from::
different. If set to a non-boolean value, format-patch uses that
value instead of your committer identity. Defaults to false.
format.forceInBodyFrom::
Provides the default value for the `--[no-]force-in-body-from`
option to format-patch. Defaults to false.
format.numbered::
A boolean which can enable or disable sequence numbers in patch
subjects. It defaults to "auto" which enables it only if there

View File

@ -275,6 +275,17 @@ header). Note also that `git send-email` already handles this
transformation for you, and this option should not be used if you are
feeding the result to `git send-email`.
--[no-]force-in-body-from::
With the e-mail sender specified via the `--from` option, by
default, an in-body "From:" to identify the real author of
the commit is added at the top of the commit log message if
the sender is different from the author. With this option,
the in-body "From:" is added even when the sender and the
author have the same name and address, which may help if the
mailing list software mangles the sender's identity.
Defaults to the value of the `format.forceInBodyFrom`
configuration variable.
--add-header=<header>::
Add an arbitrary header to the email headers. This is in addition
to any configured headers, and may be used multiple times.

View File

@ -52,6 +52,7 @@ static int default_encode_email_headers = 1;
static int decoration_style;
static int decoration_given;
static int use_mailmap_config = 1;
static unsigned int force_in_body_from;
static const char *fmt_patch_subject_prefix = "PATCH";
static int fmt_patch_name_max = FORMAT_PATCH_NAME_MAX_DEFAULT;
static const char *fmt_pretty;
@ -1058,6 +1059,10 @@ static int git_format_config(const char *var, const char *value, void *cb)
from = NULL;
return 0;
}
if (!strcmp(var, "format.forceinbodyfrom")) {
force_in_body_from = git_config_bool(var, value);
return 0;
}
if (!strcmp(var, "format.notes")) {
int b = git_parse_maybe_bool(value);
if (b < 0)
@ -1949,6 +1954,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
N_("show changes against <refspec> in cover letter or single patch")),
OPT_INTEGER(0, "creation-factor", &creation_factor,
N_("percentage by which creation is weighted")),
OPT_BOOL(0, "force-in-body-from", &force_in_body_from,
N_("show in-body From: even if identical to the e-mail header")),
OPT_END()
};
@ -1992,6 +1999,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
PARSE_OPT_KEEP_DASHDASH);
rev.force_in_body_from = force_in_body_from;
/* Make sure "0000-$sub.patch" gives non-negative length for $sub */
if (fmt_patch_name_max <= strlen("0000-") + strlen(fmt_patch_suffix))
fmt_patch_name_max = strlen("0000-") + strlen(fmt_patch_suffix);

View File

@ -477,6 +477,16 @@ end:
}
}
static int use_in_body_from(const struct pretty_print_context *pp,
const struct ident_split *ident)
{
if (pp->rev && pp->rev->force_in_body_from)
return 1;
if (ident_cmp(pp->from_ident, ident))
return 1;
return 0;
}
void pp_user_info(struct pretty_print_context *pp,
const char *what, struct strbuf *sb,
const char *line, const char *encoding)
@ -503,7 +513,7 @@ void pp_user_info(struct pretty_print_context *pp,
map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
if (cmit_fmt_is_mail(pp->fmt)) {
if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
if (pp->from_ident && use_in_body_from(pp, &ident)) {
struct strbuf buf = STRBUF_INIT;
strbuf_addstr(&buf, "From: ");

View File

@ -229,6 +229,7 @@ struct rev_info {
missing_newline:1,
date_mode_explicit:1,
preserve_subject:1,
force_in_body_from:1,
encode_email_headers:1,
include_header:1;
unsigned int disable_stdin:1;

View File

@ -1400,6 +1400,43 @@ test_expect_success '--from omits redundant in-body header' '
test_cmp expect patch.head
'
test_expect_success 'with --force-in-body-from, redundant in-body from is kept' '
git format-patch --force-in-body-from \
-1 --stdout --from="A U Thor <author@example.com>" >patch &&
cat >expect <<-\EOF &&
From: A U Thor <author@example.com>
From: A U Thor <author@example.com>
EOF
sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
test_cmp expect patch.head
'
test_expect_success 'format.forceInBodyFrom, equivalent to --force-in-body-from' '
git -c format.forceInBodyFrom=yes format-patch \
-1 --stdout --from="A U Thor <author@example.com>" >patch &&
cat >expect <<-\EOF &&
From: A U Thor <author@example.com>
From: A U Thor <author@example.com>
EOF
sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
test_cmp expect patch.head
'
test_expect_success 'format.forceInBodyFrom, equivalent to --force-in-body-from' '
git -c format.forceInBodyFrom=yes format-patch --no-force-in-body-from \
-1 --stdout --from="A U Thor <author@example.com>" >patch &&
cat >expect <<-\EOF &&
From: A U Thor <author@example.com>
EOF
sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
test_cmp expect patch.head
'
test_expect_success 'in-body headers trigger content encoding' '
test_env GIT_AUTHOR_NAME="éxötìc" test_commit exotic &&
test_when_finished "git reset --hard HEAD^" &&