]> git.ipfire.org Git - thirdparty/git.git/commitdiff
format-patch: add --interdiff option to embed diff in cover letter
authorEric Sunshine <sunshine@sunshineco.com>
Sun, 22 Jul 2018 09:57:05 +0000 (05:57 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Jul 2018 19:50:06 +0000 (12:50 -0700)
When submitting a revised version of a patch series, it can be helpful
(to reviewers) to include a summary of changes since the previous
attempt in the form of an interdiff, however, doing so involves manually
copy/pasting the diff into the cover letter.

Add an --interdiff option to automate this process. The argument to
--interdiff specifies the tip of the previous attempt against which to
generate the interdiff. For example:

    git format-patch --cover-letter --interdiff=v1 -3 v2

The previous attempt and the patch series being formatted must share a
common base.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-format-patch.txt
Makefile
builtin/log.c
interdiff.c [new file with mode: 0644]
interdiff.h [new file with mode: 0644]
revision.h
t/t4014-format-patch.sh

index b41e1329a7d8439762790e663ac52ec5f487bc8b..a1b1bafee708b11da7959646df4462f039a4ec22 100644 (file)
@@ -23,6 +23,7 @@ SYNOPSIS
                   [(--reroll-count|-v) <n>]
                   [--to=<email>] [--cc=<email>]
                   [--[no-]cover-letter] [--quiet] [--notes[=<ref>]]
+                  [--interdiff=<previous>]
                   [--progress]
                   [<common diff options>]
                   [ <since> | <revision range> ]
@@ -228,6 +229,14 @@ feeding the result to `git send-email`.
        containing the branch description, shortlog and the overall diffstat.  You can
        fill in a description in the file before sending it out.
 
+--interdiff=<previous>::
+       As a reviewer aid, insert an interdiff into the cover letter showing
+       the differences between the previous version of the patch series and
+       the series currently being formatted. `previous` is a single revision
+       naming the tip of the previous series which shares a common base with
+       the series being formatted (for example `git format-patch
+       --cover-letter --interdiff=feature/v1 -3 feature/v2`).
+
 --notes[=<ref>]::
        Append the notes (see linkgit:git-notes[1]) for the commit
        after the three-dash line.
index 08e5c545492c7261dea349d002aef8d3ea1d232f..b2685190e1d9a6c0ad39bf3f5a6bd9a4e84f7273 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -871,6 +871,7 @@ LIB_OBJS += hashmap.o
 LIB_OBJS += help.o
 LIB_OBJS += hex.o
 LIB_OBJS += ident.o
+LIB_OBJS += interdiff.o
 LIB_OBJS += kwset.o
 LIB_OBJS += levenshtein.o
 LIB_OBJS += line-log.o
index 873aabcf40fb123bee28bd6119e1bd0895941e37..1020b784770edde3df46f972e2bb5e5ef675fe22 100644 (file)
@@ -30,6 +30,7 @@
 #include "gpg-interface.h"
 #include "progress.h"
 #include "commit-slab.h"
+#include "interdiff.h"
 
 #define MAIL_DEFAULT_WRAP 72
 
@@ -1082,6 +1083,11 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
        /* We can only do diffstat with a unique reference point */
        if (origin)
                show_diffstat(rev, origin, head);
+
+       if (rev->idiff_oid1) {
+               fprintf_ln(rev->diffopt.file, "%s", _("Interdiff:"));
+               show_interdiff(rev);
+       }
 }
 
 static const char *clean_message_id(const char *msg_id)
@@ -1448,6 +1454,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        struct base_tree_info bases;
        int show_progress = 0;
        struct progress *progress = NULL;
+       struct oid_array idiff_prev = OID_ARRAY_INIT;
 
        const struct option builtin_format_patch_options[] = {
                { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
@@ -1521,6 +1528,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                OPT__QUIET(&quiet, N_("don't print the patch filenames")),
                OPT_BOOL(0, "progress", &show_progress,
                         N_("show progress while generating patches")),
+               OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
+                            N_("show changes against <rev> in cover letter"),
+                            parse_opt_object_name),
                OPT_END()
        };
 
@@ -1706,7 +1716,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                if (rev.pending.nr == 2) {
                        struct object_array_entry *o = rev.pending.objects;
                        if (oidcmp(&o[0].item->oid, &o[1].item->oid) == 0)
-                               return 0;
+                               goto done;
                }
                get_patch_ids(&rev, &ids);
        }
@@ -1730,7 +1740,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        }
        if (nr == 0)
                /* nothing to do */
-               return 0;
+               goto done;
        total = nr;
        if (cover_letter == -1) {
                if (config_cover_letter == COVER_AUTO)
@@ -1743,6 +1753,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        if (numbered)
                rev.total = total + start_number - 1;
 
+       if (idiff_prev.nr) {
+               if (!cover_letter)
+                       die(_("--interdiff requires --cover-letter"));
+               rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1];
+               rev.idiff_oid2 = get_commit_tree_oid(list[0]);
+       }
+
        if (!signature) {
                ; /* --no-signature inhibits all signatures */
        } else if (signature && signature != git_version_string) {
@@ -1860,6 +1877,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        string_list_clear(&extra_hdr, 0);
        if (ignore_if_in_upstream)
                free_patch_ids(&ids);
+
+done:
+       oid_array_clear(&idiff_prev);
        return 0;
 }
 
diff --git a/interdiff.c b/interdiff.c
new file mode 100644 (file)
index 0000000..d0fac10
--- /dev/null
@@ -0,0 +1,17 @@
+#include "cache.h"
+#include "commit.h"
+#include "revision.h"
+#include "interdiff.h"
+
+void show_interdiff(struct rev_info *rev)
+{
+       struct diff_options opts;
+
+       memcpy(&opts, &rev->diffopt, sizeof(opts));
+       opts.output_format = DIFF_FORMAT_PATCH;
+       diff_setup_done(&opts);
+
+       diff_tree_oid(rev->idiff_oid1, rev->idiff_oid2, "", &opts);
+       diffcore_std(&opts);
+       diff_flush(&opts);
+}
diff --git a/interdiff.h b/interdiff.h
new file mode 100644 (file)
index 0000000..793c014
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef INTERDIFF_H
+#define INTERDIFF_H
+
+struct rev_info;
+
+void show_interdiff(struct rev_info *);
+
+#endif
index bf2239f87689d9486c4b888bed5cba6affa21953..61931fbac5bd86479889699a47d3afab2fb24ec5 100644 (file)
@@ -212,6 +212,10 @@ struct rev_info {
        /* notes-specific options: which refs to show */
        struct display_notes_opt notes_opt;
 
+       /* interdiff */
+       const struct object_id *idiff_oid1;
+       const struct object_id *idiff_oid2;
+
        /* commit counts */
        int count_left;
        int count_right;
index 53880da7bbe85521112ced3ff94ead05587c7e81..57b46322aadbf7576acec417c5ae06028c8d9899 100755 (executable)
@@ -1717,4 +1717,21 @@ test_expect_success 'format-patch --pretty=mboxrd' '
        test_cmp expect actual
 '
 
+test_expect_success 'interdiff: setup' '
+       git checkout -b boop master &&
+       test_commit fnorp blorp &&
+       test_commit fleep blorp
+'
+
+test_expect_success 'interdiff: cover-letter' '
+       sed "y/q/ /" >expect <<-\EOF &&
+       +fleep
+       --q
+       EOF
+       git format-patch --cover-letter --interdiff=boop~2 -1 boop &&
+       test_i18ngrep "^Interdiff:$" 0000-cover-letter.patch &&
+       sed "1,/^@@ /d; /^-- $/q" <0000-cover-letter.patch >actual &&
+       test_cmp expect actual
+'
+
 test_done