Skip to content

Commit 0e2a476

Browse files
committed
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
2 parents 428dce9 + d5fc07d commit 0e2a476

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

Documentation/config/format.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ format.from::
1515
different. If set to a non-boolean value, format-patch uses that
1616
value instead of your committer identity. Defaults to false.
1717

18+
format.forceInBodyFrom::
19+
Provides the default value for the `--[no-]force-in-body-from`
20+
option to format-patch. Defaults to false.
21+
1822
format.numbered::
1923
A boolean which can enable or disable sequence numbers in patch
2024
subjects. It defaults to "auto" which enables it only if there

Documentation/git-format-patch.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ header). Note also that `git send-email` already handles this
275275
transformation for you, and this option should not be used if you are
276276
feeding the result to `git send-email`.
277277

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+
Defaults to the value of the `format.forceInBodyFrom`
287+
configuration variable.
288+
278289
--add-header=<header>::
279290
Add an arbitrary header to the email headers. This is in addition
280291
to any configured headers, and may be used multiple times.

builtin/log.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static int default_encode_email_headers = 1;
5252
static int decoration_style;
5353
static int decoration_given;
5454
static int use_mailmap_config = 1;
55+
static unsigned int force_in_body_from;
5556
static const char *fmt_patch_subject_prefix = "PATCH";
5657
static int fmt_patch_name_max = FORMAT_PATCH_NAME_MAX_DEFAULT;
5758
static const char *fmt_pretty;
@@ -1058,6 +1059,10 @@ static int git_format_config(const char *var, const char *value, void *cb)
10581059
from = NULL;
10591060
return 0;
10601061
}
1062+
if (!strcmp(var, "format.forceinbodyfrom")) {
1063+
force_in_body_from = git_config_bool(var, value);
1064+
return 0;
1065+
}
10611066
if (!strcmp(var, "format.notes")) {
10621067
int b = git_parse_maybe_bool(value);
10631068
if (b < 0)
@@ -1949,6 +1954,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
19491954
N_("show changes against <refspec> in cover letter or single patch")),
19501955
OPT_INTEGER(0, "creation-factor", &creation_factor,
19511956
N_("percentage by which creation is weighted")),
1957+
OPT_BOOL(0, "force-in-body-from", &force_in_body_from,
1958+
N_("show in-body From: even if identical to the e-mail header")),
19521959
OPT_END()
19531960
};
19541961

@@ -1992,6 +1999,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
19921999
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT |
19932000
PARSE_OPT_KEEP_DASHDASH);
19942001

2002+
rev.force_in_body_from = force_in_body_from;
2003+
19952004
/* Make sure "0000-$sub.patch" gives non-negative length for $sub */
19962005
if (fmt_patch_name_max <= strlen("0000-") + strlen(fmt_patch_suffix))
19972006
fmt_patch_name_max = strlen("0000-") + strlen(fmt_patch_suffix);

pretty.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,16 @@ static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
477477
}
478478
}
479479

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;
488+
}
489+
480490
void pp_user_info(struct pretty_print_context *pp,
481491
const char *what, struct strbuf *sb,
482492
const char *line, const char *encoding)
@@ -503,7 +513,7 @@ void pp_user_info(struct pretty_print_context *pp,
503513
map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
504514

505515
if (cmit_fmt_is_mail(pp->fmt)) {
506-
if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
516+
if (pp->from_ident && use_in_body_from(pp, &ident)) {
507517
struct strbuf buf = STRBUF_INIT;
508518

509519
strbuf_addstr(&buf, "From: ");

revision.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ struct rev_info {
229229
missing_newline:1,
230230
date_mode_explicit:1,
231231
preserve_subject:1,
232+
force_in_body_from:1,
232233
encode_email_headers:1,
233234
include_header:1;
234235
unsigned int disable_stdin:1;

t/t4014-format-patch.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,43 @@ test_expect_success '--from omits redundant in-body header' '
14001400
test_cmp expect patch.head
14011401
'
14021402

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 <[email protected]>" >patch &&
1406+
cat >expect <<-\EOF &&
1407+
From: A U Thor <[email protected]>
1408+
1409+
From: A U Thor <[email protected]>
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 'format.forceInBodyFrom, equivalent to --force-in-body-from' '
1417+
git -c format.forceInBodyFrom=yes format-patch \
1418+
-1 --stdout --from="A U Thor <[email protected]>" >patch &&
1419+
cat >expect <<-\EOF &&
1420+
From: A U Thor <[email protected]>
1421+
1422+
From: A U Thor <[email protected]>
1423+
1424+
EOF
1425+
sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
1426+
test_cmp expect patch.head
1427+
'
1428+
1429+
test_expect_success 'format.forceInBodyFrom, equivalent to --force-in-body-from' '
1430+
git -c format.forceInBodyFrom=yes format-patch --no-force-in-body-from \
1431+
-1 --stdout --from="A U Thor <[email protected]>" >patch &&
1432+
cat >expect <<-\EOF &&
1433+
From: A U Thor <[email protected]>
1434+
1435+
EOF
1436+
sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
1437+
test_cmp expect patch.head
1438+
'
1439+
14031440
test_expect_success 'in-body headers trigger content encoding' '
14041441
test_env GIT_AUTHOR_NAME="éxötìc" test_commit exotic &&
14051442
test_when_finished "git reset --hard HEAD^" &&

0 commit comments

Comments
 (0)