format-patch: print in-body "From" only when needed
PATCH 1/1 format-patch: print in-body "From" only when needed
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.
---
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(-)
cache.h +9 -0
@@ -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;
ident.c +29 -0
@@ -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); +}
pretty.c +1 -1
@@ -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: " );
t/t4014-format-patch.sh +10 -0
@@ -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 <[email protected]>" >patch && + cat >expect <<-\EOF && + From: A U Thor <[email protected]> + + 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^" &&
Signature · 1 line
1.8.3.4.20.geeaee04.dirty
Quoted 7 lines · Jeff King
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.
Quoted 7 lines
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?
Quoted 17 lines · Junio C Hamano
Quoted 7 lines · Jeff King
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.
Quoted 7 lines
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.
Quoted 4 lines
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_identdefault to the value ofGIT_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
---
builtin/log.c +2 -0
@@ -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
Quoted 3 lines · Jeff King
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.
Quoted 15 lines
---
builtin/log.c +2
-0@@ -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