format-patch: add --range-diff option to embed diff in cover letter

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 a range-diff, however, doing so involves manually copy/pasting the diff into the cover letter. Add a --range-diff option to automate this process. The argument to --range-diff specifies the tip of the previous attempt against which to generate the range-diff. For example: git format-patch --cover-letter --range-diff=v1 -3 v2 (At this stage, the previous attempt and the patch series being formatted must share a common base, however, a subsequent enhancement will make it possible to specify an explicit revision range for the previous attempt.) Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 22, 2018 at 05:57 UTC 31e2617a5f8fe1c114e72f058d1c035bbf77cffe
4 files changed +62
Documentation/git-format-patch.txt
+10
@@ -24,6 +24,7 @@ SYNOPSIS
24 [--to=<email>] [--cc=<email>]
25 [--[no-]cover-letter] [--quiet] [--notes[=<ref>]]
26 [--interdiff=<previous>]
27 + [--range-diff=<previous>]
28 [--progress]
29 [<common diff options>]
30 [ <since> | <revision range> ]
@@ -238,6 +239,15 @@ feeding the result to `git send-email`.
239 the series being formatted (for example `git format-patch
240 --cover-letter --interdiff=feature/v1 -3 feature/v2`).
241
242 +--range-diff=<previous>::
243 + As a reviewer aid, insert a range-diff (see linkgit:git-range-diff[1])
244 + into the cover letter showing the differences between the previous
245 + version of the patch series and the series currently being formatted.
246 + `previous` is a single revision naming the tip of the previous
247 + series which shares a common base with the series being formatted (for
248 + example `git format-patch --cover-letter --range-diff=feature/v1 -3
249 + feature/v2`).
250 +
251 --notes[=<ref>]::
252 Append the notes (see linkgit:git-notes[1]) for the commit
253 after the three-dash line.
builtin/log.c
+35
@@ -32,6 +32,7 @@
32 #include "commit-slab.h"
33 #include "repository.h"
34 #include "interdiff.h"
35 +#include "range-diff.h"
36
37 #define MAIL_DEFAULT_WRAP 72
38
@@ -1089,6 +1090,12 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
1090 fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title);
1091 show_interdiff(rev, 0);
1092 }
1093 +
1094 + if (rev->rdiff1) {
1095 + fprintf_ln(rev->diffopt.file, "%s", _("Range-diff:"));
1096 + show_range_diff(rev->rdiff1, rev->rdiff2,
1097 + rev->creation_factor, 1, &rev->diffopt);
1098 + }
1099 }
1100
1101 static const char *clean_message_id(const char *msg_id)
@@ -1438,6 +1445,17 @@ static const char *diff_title(struct strbuf *sb, int reroll_count,
1445 return sb->buf;
1446 }
1447
1448 +static void infer_range_diff_ranges(struct strbuf *r1,
1449 + struct strbuf *r2,
1450 + const char *prev,
1451 + struct commit *head)
1452 +{
1453 + const char *head_oid = oid_to_hex(&head->object.oid);
1454 +
1455 + strbuf_addf(r1, "%s..%s", head_oid, prev);
1456 + strbuf_addf(r2, "%s..%s", prev, head_oid);
1457 +}
1458 +
1459 int cmd_format_patch(int argc, const char **argv, const char *prefix)
1460 {
1461 struct commit *commit;
@@ -1467,6 +1485,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1485 struct progress *progress = NULL;
1486 struct oid_array idiff_prev = OID_ARRAY_INIT;
1487 struct strbuf idiff_title = STRBUF_INIT;
1488 + const char *rdiff_prev = NULL;
1489 + struct strbuf rdiff1 = STRBUF_INIT;
1490 + struct strbuf rdiff2 = STRBUF_INIT;
1491
1492 const struct option builtin_format_patch_options[] = {
1493 { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
@@ -1543,6 +1564,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1564 OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
1565 N_("show changes against <rev> in cover letter or single patch"),
1566 parse_opt_object_name),
1567 + OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
1568 + N_("show changes against <refspec> in cover letter")),
1569 OPT_END()
1570 };
1571
@@ -1775,6 +1798,16 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1798 _("Interdiff against v%d:"));
1799 }
1800
1801 + if (rdiff_prev) {
1802 + if (!cover_letter)
1803 + die(_("--range-diff requires --cover-letter"));
1804 +
1805 + infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev, list[0]);
1806 + rev.rdiff1 = rdiff1.buf;
1807 + rev.rdiff2 = rdiff2.buf;
1808 + rev.creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
1809 + }
1810 +
1811 if (!signature) {
1812 ; /* --no-signature inhibits all signatures */
1813 } else if (signature && signature != git_version_string) {
@@ -1898,6 +1931,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1931 done:
1932 oid_array_clear(&idiff_prev);
1933 strbuf_release(&idiff_title);
1934 + strbuf_release(&rdiff1);
1935 + strbuf_release(&rdiff2);
1936 return 0;
1937 }
1938
revision.h
+5
@@ -218,6 +218,11 @@ struct rev_info {
218 const struct object_id *idiff_oid2;
219 const char *idiff_title;
220
221 + /* range-diff */
222 + const char *rdiff1;
223 + const char *rdiff2;
224 + int creation_factor;
225 +
226 /* commit counts */
227 int count_left;
228 int count_right;
t/t3206-range-diff.sh
+12
@@ -142,4 +142,16 @@ test_expect_success 'changed message' '
142 test_cmp expected actual
143 '
144
145 +for prev in topic
146 +do
147 + test_expect_success "format-patch --range-diff=$prev" '
148 + git format-patch --stdout --cover-letter --range-diff=$prev \
149 + master..unmodified >actual &&
150 + grep "= 1: .* s/5/A" actual &&
151 + grep "= 2: .* s/4/A" actual &&
152 + grep "= 3: .* s/11/B" actual &&
153 + grep "= 4: .* s/12/B" actual
154 + '
155 +done
156 +
157 test_done