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. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 20, 2013 at 06:16 UTC 662cc30cd048f2ccd7ba8d1540e0768ae264c0dd
4 files changed +49 -1
cache.h
+9
@@ -963,6 +963,15 @@ struct ident_split {
963 */
964 extern int split_ident_line(struct ident_split *, const char *, int);
965
966 +/*
967 + * Compare split idents for equality or strict ordering. Note that we
968 + * compare only the ident part of the line, ignoring any timestamp.
969 + *
970 + * Because there are two fields, we must choose one as the primary key; we
971 + * currently arbitrarily pick the email.
972 + */
973 +extern int ident_cmp(const struct ident_split *, const struct ident_split *);
974 +
975 struct checkout {
976 const char *base_dir;
977 int base_dir_len;
ident.c
+29
@@ -402,3 +402,32 @@ int git_ident_config(const char *var, const char *value, void *data)
402
403 return 0;
404 }
405 +
406 +static int buf_cmp(const char *a_begin, const char *a_end,
407 + const char *b_begin, const char *b_end)
408 +{
409 + int a_len = a_end - a_begin;
410 + int b_len = b_end - b_begin;
411 + int min = a_len < b_len ? a_len : b_len;
412 + int cmp;
413 +
414 + cmp = memcmp(a_begin, b_begin, min);
415 + if (cmp)
416 + return cmp;
417 +
418 + return a_len - b_len;
419 +}
420 +
421 +int ident_cmp(const struct ident_split *a,
422 + const struct ident_split *b)
423 +{
424 + int cmp;
425 +
426 + cmp = buf_cmp(a->mail_begin, a->mail_end,
427 + b->mail_begin, b->mail_end);
428 + if (cmp)
429 + return cmp;
430 +
431 + return buf_cmp(a->name_begin, a->name_end,
432 + b->name_begin, b->name_end);
433 +}
pretty.c
+1 -1
@@ -432,7 +432,7 @@ void pp_user_info(struct pretty_print_context *pp,
432 map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
433
434 if (pp->fmt == CMIT_FMT_EMAIL) {
435 - if (pp->from_ident) {
435 + if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
436 struct strbuf buf = STRBUF_INIT;
437
438 strbuf_addstr(&buf, "From: ");
t/t4014-format-patch.sh
+10
@@ -1000,6 +1000,16 @@ test_expect_success '--from uses committer ident' '
1000 test_cmp expect patch.head
1001 '
1002
1003 +test_expect_success '--from omits redundant in-body header' '
1004 + git format-patch -1 --stdout --from="A U Thor <author@example.com>" >patch &&
1005 + cat >expect <<-\EOF &&
1006 + From: A U Thor <author@example.com>
1007 +
1008 + EOF
1009 + sed -ne "/^From:/p; /^$/p; /^---$/q" <patch >patch.head &&
1010 + test_cmp expect patch.head
1011 +'
1012 +
1013 test_expect_success 'in-body headers trigger content encoding' '
1014 GIT_AUTHOR_NAME="éxötìc" test_commit exotic &&
1015 test_when_finished "git reset --hard HEAD^" &&