git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] format-patch: print in-body "From" only when needed
@ 2013-09-20 10:16 Jeff King
  2013-09-20 18:17 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff King @ 2013-09-20 10:16 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano

Commit a908047 taught format-patch the "--from" option,
which places the author ident into an in-body from header,
and uses the committer ident in the rfc822 from header.  The
documentation claims that it will omit the in-body header
when it is the same as the rfc822 header, but the code never
implemented that behavior.

This patch completes the feature by comparing the two idents
and doing nothing when they are the same (this is the same
as simply omitting the in-body header, as the two are by
definition indistinguishable in this case). This makes it
reasonable to turn on "--from" all the time (if it matches
your particular workflow), rather than only using it when
exporting other people's patches.

Signed-off-by: Jeff King <peff@peff.net>
---
I had meant for this to be the behavior all along (as shown in the
documentation), and I can't imagine why I didn't implement it along with
the original topic. I never noticed because I didn't turn on "--from"
all the time in my workflow scripts until recently, but instead just
used it manually when sending other people's patches (which I do only
rarely).

 cache.h                 |  9 +++++++++
 ident.c                 | 29 +++++++++++++++++++++++++++++
 pretty.c                |  2 +-
 t/t4014-format-patch.sh | 10 ++++++++++
 4 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/cache.h b/cache.h
index a47b9c0..bfea954 100644
--- a/cache.h
+++ b/cache.h
@@ -953,6 +953,15 @@ struct ident_split {
  */
 extern int split_ident_line(struct ident_split *, const char *, int);
 
+/*
+ * Compare split idents for equality or strict ordering. Note that we
+ * compare only the ident part of the line, ignoring any timestamp.
+ *
+ * Because there are two fields, we must choose one as the primary key; we
+ * currently arbitrarily pick the email.
+ */
+extern int ident_cmp(const struct ident_split *, const struct ident_split *);
+
 struct checkout {
 	const char *base_dir;
 	int base_dir_len;
diff --git a/ident.c b/ident.c
index 1c123e6..b29f81f 100644
--- a/ident.c
+++ b/ident.c
@@ -402,3 +402,32 @@ int git_ident_config(const char *var, const char *value, void *data)
 
 	return 0;
 }
+
+static int buf_cmp(const char *a_begin, const char *a_end,
+		   const char *b_begin, const char *b_end)
+{
+	int a_len = a_end - a_begin;
+	int b_len = b_end - b_begin;
+	int min = a_len < b_len ? a_len : b_len;
+	int cmp;
+
+	cmp = memcmp(a_begin, b_begin, min);
+	if (cmp)
+		return cmp;
+
+	return a_len - b_len;
+}
+
+int ident_cmp(const struct ident_split *a,
+	      const struct ident_split *b)
+{
+	int cmp;
+
+	cmp = buf_cmp(a->mail_begin, a->mail_end,
+		      b->mail_begin, b->mail_end);
+	if (cmp)
+		return cmp;
+
+	return buf_cmp(a->name_begin, a->name_end,
+		       b->name_begin, b->name_end);
+}
diff --git a/pretty.c b/pretty.c
index 74563c9..b4e32b7 100644
--- a/pretty.c
+++ b/pretty.c
@@ -432,7 +432,7 @@ void pp_user_info(struct pretty_print_context *pp,
 		map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
 
 	if (pp->fmt == CMIT_FMT_EMAIL) {
-		if (pp->from_ident) {
+		if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
 			struct strbuf buf = STRBUF_INIT;
 
 			strbuf_addstr(&buf, "From: ");
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index 668933b..8f272bc 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -1000,6 +1000,16 @@ test_expect_success '--from uses committer ident' '
 	test_cmp expect patch.head
 '
 
+test_expect_success '--from omits redundant in-body header' '
+	git format-patch -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' '
 	GIT_AUTHOR_NAME="éxötìc" test_commit exotic &&
 	test_when_finished "git reset --hard HEAD^" &&
-- 
1.8.3.4.20.geeaee04.dirty

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] format-patch: print in-body "From" only when needed
  2013-09-20 10:16 [PATCH] format-patch: print in-body "From" only when needed Jeff King
@ 2013-09-20 18:17 ` Junio C Hamano
  2013-09-20 19:12   ` Jeff King
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2013-09-20 18:17 UTC (permalink / raw)
  To: Jeff King; +Cc: git

Jeff King <peff@peff.net> writes:

> Commit a908047 taught format-patch the "--from" option,
> which places the author ident into an in-body from header,
> and uses the committer ident in the rfc822 from header.  The
> documentation claims that it will omit the in-body header
> when it is the same as the rfc822 header, but the code never
> implemented that behavior.
>
> This patch completes the feature by comparing the two idents
> and doing nothing when they are the same (this is the same
> as simply omitting the in-body header, as the two are by
> definition indistinguishable in this case). This makes it
> reasonable to turn on "--from" all the time (if it matches
> your particular workflow), rather than only using it when
> exporting other people's patches.

This fix makes 100% sense to me under the assumption that the
"--from" option is a good idea, but then it makes me wonder why we
need the option in the first place.  What would break if we made
pp->from_ident default to the value of GIT_COMMITTER_IDENT?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] format-patch: print in-body "From" only when needed
  2013-09-20 18:17 ` Junio C Hamano
@ 2013-09-20 19:12   ` Jeff King
  2013-09-20 21:06     ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff King @ 2013-09-20 19:12 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Fri, Sep 20, 2013 at 11:17:45AM -0700, Junio C Hamano wrote:

> Jeff King <peff@peff.net> writes:
> 
> > Commit a908047 taught format-patch the "--from" option,
> > which places the author ident into an in-body from header,
> > and uses the committer ident in the rfc822 from header.  The
> > documentation claims that it will omit the in-body header
> > when it is the same as the rfc822 header, but the code never
> > implemented that behavior.
> >
> > This patch completes the feature by comparing the two idents
> > and doing nothing when they are the same (this is the same
> > as simply omitting the in-body header, as the two are by
> > definition indistinguishable in this case). This makes it
> > reasonable to turn on "--from" all the time (if it matches
> > your particular workflow), rather than only using it when
> > exporting other people's patches.
> 
> This fix makes 100% sense to me under the assumption that the
> "--from" option is a good idea, but then it makes me wonder why we
> need the option in the first place.  What would break if we made
> pp->from_ident default to the value of GIT_COMMITTER_IDENT?

Anything consuming format-patch output that does not understand in-body
headers would be broken.

I think rebase would be safe, because it uses git-am under the hood, so
in theory it is a noop.

send-email would have to learn to parse the in-body header, for two
reasons:

  1. We cannot get rid of send-email's in-body header writing, because
     we would want to handle patches generated (or munged) outside of
     format-patch, and because send-email has its own "--from" option
     and sendemail.from configuration.

     If that config matches the committer, it should be a noop. If it
     doesn't, we have we have two cases:

       a. There is no in-body header. We promote the rfc822 header to an
          in-body one, and add our configured "from" as the rfc822
          header.

       b. There is an in-body header. We leave it intact, but throw away
          the current rfc822 header and replace it with our configured
          header.

  2. send-email does header magic like auto-adding cc's, and suppressing
     addresses found in other headers. It would need (at least) to pick
     out the author from the in-body header to cc.

The patch below turns the feature on all the time. There are test
breakages in in t9001, t3901, t4014, and t4013. The last 3 I think are
just cosmetic. t9001 has a ton of breakages around the header
suppressions, but I didn't analyze which ones were just "this should now
read 'committer' instead of 'author'" and which ones represented real
breakage.

So I think it should be possible to fix our internal consumers of
format-patch output to handle this. And certainly there are some
external consumers that would be made more happy (i.e., anything that is
planning to actually send the output as an email). But I'm a little wary
of external consumers that might get confused by it. I think we'd want
to flip it at a major version boundary, at least.

-Peff

---
diff --git a/builtin/log.c b/builtin/log.c
index 77d0f5f..77564fd 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -1235,6 +1235,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		rev.no_inline = 1;
 	}
 
+	from = xstrdup(git_committer_info(IDENT_NO_DATE));
+
 	/*
 	 * Parse the arguments before setup_revisions(), or something
 	 * like "git format-patch -o a123 HEAD^.." may fail; a123 is

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] format-patch: print in-body "From" only when needed
  2013-09-20 19:12   ` Jeff King
@ 2013-09-20 21:06     ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2013-09-20 21:06 UTC (permalink / raw)
  To: Jeff King; +Cc: git

Jeff King <peff@peff.net> writes:

> So I think it should be possible to fix our internal consumers 
> ... I'm a little wary
> of external consumers that might get confused by it.

Yeah, thanks for a good summary of analysis.  I agree that it would
be doable, but it is dubious if it is worth it.

>
> ---
> diff --git a/builtin/log.c b/builtin/log.c
> index 77d0f5f..77564fd 100644
> --- a/builtin/log.c
> +++ b/builtin/log.c
> @@ -1235,6 +1235,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
>  		rev.no_inline = 1;
>  	}
>  
> +	from = xstrdup(git_committer_info(IDENT_NO_DATE));
> +
>  	/*
>  	 * Parse the arguments before setup_revisions(), or something
>  	 * like "git format-patch -o a123 HEAD^.." may fail; a123 is

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-09-20 21:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-09-20 10:16 [PATCH] format-patch: print in-body "From" only when needed Jeff King
2013-09-20 18:17 ` Junio C Hamano
2013-09-20 19:12   ` Jeff King
2013-09-20 21:06     ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).