diff mbox series

[v2,2/3] format-patch: allow forcing the use of in-body From: header

Message ID 20220829213837.13849-3-gitster@pobox.com (mailing list archive)
State Accepted
Commit 34bc1b1045af6ad813f4de662a453b3c77ba65a7
Headers show
Series format-patch --force-in-body-from | expand

Commit Message

Junio C Hamano Aug. 29, 2022, 9:38 p.m. UTC
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>
---
 Documentation/git-format-patch.txt |  9 +++++++++
 builtin/log.c                      |  5 +++++
 pretty.c                           |  2 ++
 revision.h                         |  1 +
 t/t4014-format-patch.sh            | 13 +++++++++++++
 5 files changed, 30 insertions(+)

Comments

Jeff King Aug. 30, 2022, 8:07 p.m. UTC | #1
On Mon, Aug 29, 2022 at 02:38:36PM -0700, Junio C Hamano wrote:

> +--[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.

I find it a little curious that this option can only be used with
"--from". That makes sense in a way, because this is a special case of
that feature, overriding the "are they the same" check.

But given that the use case is not to send somebody else's patch, but to
duplicate your _own_ ident in both spots, it feels funny that you must
also say "by the way, I am the sender of the email". I.e., you have to
say:

  git format-patch --from='Me <me@example.com>' --force-in-body-from

I guess it is not too bad because just "--from" will do the equivalent
thing (picking "me" from your committer ident). It just feels kind of
clunky that:

  git format-patch --force-in-body-from

will silently ignore the option.

All that said, I don't care _too_ strongly about it. I do suspect the
feature might be better placed in send-email (or possibly in addition).
If you are using send-email, then I think you're not supposed to use
"--from" with format-patch at all, and you have no way of accessing this
feature.

-Peff
Jeff King Aug. 30, 2022, 8:14 p.m. UTC | #2
On Tue, Aug 30, 2022 at 04:07:18PM -0400, Jeff King wrote:

> All that said, I don't care _too_ strongly about it. I do suspect the
> feature might be better placed in send-email (or possibly in addition).
> If you are using send-email, then I think you're not supposed to use
> "--from" with format-patch at all, and you have no way of accessing this
> feature.

This "not supposed to" came from me looking at the documentation. But
this discussion implies that it's maybe not that big a deal:

  https://lore.kernel.org/git/xmqq8twlqwan.fsf@gitster.mtv.corp.google.com/

Apparently we were considering enabling "--from" by default, but I don't
think that ever happened.

-Peff
diff mbox series

Patch

diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
index be797d7a28..7c7f244e57 100644
--- a/Documentation/git-format-patch.txt
+++ b/Documentation/git-format-patch.txt
@@ -275,6 +275,15 @@  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.
+
 --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.
diff --git a/builtin/log.c b/builtin/log.c
index 9b937d59b8..78ccd37bd9 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -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;
@@ -1897,6 +1898,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()
 	};
 
@@ -1940,6 +1943,8 @@  int cmd_format_patch(int argc, const char **argv, const char *prefix)
 			     PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
 			     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);
diff --git a/pretty.c b/pretty.c
index cf418a6b20..b7553e3fe0 100644
--- a/pretty.c
+++ b/pretty.c
@@ -480,6 +480,8 @@  static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
 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;
diff --git a/revision.h b/revision.h
index bb91e7ed91..6e346a60ab 100644
--- a/revision.h
+++ b/revision.h
@@ -221,6 +221,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;
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index fbec8ad2ef..347f7f7f35 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -1400,6 +1400,19 @@  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 'in-body headers trigger content encoding' '
 	test_env GIT_AUTHOR_NAME="éxötìc" test_commit exotic &&
 	test_when_finished "git reset --hard HEAD^" &&