format-patch: extend --range-diff to accept revision range

When submitting a revised a patch series, the --range-diff option embeds a range-diff in the cover letter showing changes since the previous version of the patch series. The argument to --range-diff is a simple revision naming the tip of the previous series, which works fine if the previous and current versions of the patch series share a common base. However, it fails if the revision ranges of the old and new versions of the series are disjoint. To address this shortcoming, extend --range-diff to also accept an explicit revision range for the previous series. For example: git format-patch --cover-letter --range-diff=v1~3..v1 -3 v2 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 2e6fd71a52f5bde1c4036b8b0c586ae446f620f8
3 files changed +19 -7
Documentation/git-format-patch.txt
+5 -3
@@ -243,10 +243,12 @@ feeding the result to `git send-email`.
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
246 + `previous` can be a single revision naming the tip of the previous
247 + series if it 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`).
249 + feature/v2`), or a revision range if the two versions of the series are
250 + disjoint (for example `git format-patch --cover-letter
251 + --range-diff=feature/v1~3..feature/v1 -3 feature/v2`).
252
253 --notes[=<ref>]::
254 Append the notes (see linkgit:git-notes[1]) for the commit
builtin/log.c
+13 -3
@@ -1448,12 +1448,21 @@ static const char *diff_title(struct strbuf *sb, int reroll_count,
1448 static void infer_range_diff_ranges(struct strbuf *r1,
1449 struct strbuf *r2,
1450 const char *prev,
1451 + struct commit *origin,
1452 struct commit *head)
1453 {
1454 const char *head_oid = oid_to_hex(&head->object.oid);
1455
1455 - strbuf_addf(r1, "%s..%s", head_oid, prev);
1456 - strbuf_addf(r2, "%s..%s", prev, head_oid);
1456 + if (!strstr(prev, "..")) {
1457 + strbuf_addf(r1, "%s..%s", head_oid, prev);
1458 + strbuf_addf(r2, "%s..%s", prev, head_oid);
1459 + } else if (!origin) {
1460 + die(_("failed to infer range-diff ranges"));
1461 + } else {
1462 + strbuf_addstr(r1, prev);
1463 + strbuf_addf(r2, "%s..%s",
1464 + oid_to_hex(&origin->object.oid), head_oid);
1465 + }
1466 }
1467
1468 int cmd_format_patch(int argc, const char **argv, const char *prefix)
@@ -1802,7 +1811,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1811 if (!cover_letter)
1812 die(_("--range-diff requires --cover-letter"));
1813
1805 - infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev, list[0]);
1814 + infer_range_diff_ranges(&rdiff1, &rdiff2, rdiff_prev,
1815 + origin, list[0]);
1816 rev.rdiff1 = rdiff1.buf;
1817 rev.rdiff2 = rdiff2.buf;
1818 rev.creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
t/t3206-range-diff.sh
+1 -1
@@ -142,7 +142,7 @@ test_expect_success 'changed message' '
142 test_cmp expected actual
143 '
144
145 -for prev in topic
145 +for prev in topic master..topic
146 do
147 test_expect_success "format-patch --range-diff=$prev" '
148 git format-patch --stdout --cover-letter --range-diff=$prev \