format-patch: print in-body "From" only when needed
authorJeff King <peff@peff.net>
Fri, 20 Sep 2013 10:16:28 +0000 (06:16 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 20 Sep 2013 18:09:51 +0000 (11:09 -0700)
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>
cache.h
ident.c
pretty.c
t/t4014-format-patch.sh
diff --git a/cache.h b/cache.h
index dd0fb33a1520edf2a243555474d1875818294598..153081cfe28815512eae2d75ed736b5a8c0370fe 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -963,6 +963,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 1c123e685fbfbfcd5289958adc54a6cd48351628..b29f81f83a68258b354c59df9bde40c254060ed9 100644 (file)
--- 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);
+}
index 74563c92b4cdce8e947a2816a8b56355b11439e6..b4e32b74d3622f92b2c5d3b9881e5cdf49d7f375 100644 (file)
--- 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: ");
index 668933bfb2c906ce9f8c74ab498f1986b56aced0..8f272bce84ba605da82dacb04561649f10878df0 100755 (executable)
@@ -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^" &&