Skip to content

Commit 31e2617

Browse files
sunshinecogitster
authored andcommitted
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>
1 parent 73a834e commit 31e2617

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

Documentation/git-format-patch.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ SYNOPSIS
2424
[--to=<email>] [--cc=<email>]
2525
[--[no-]cover-letter] [--quiet] [--notes[=<ref>]]
2626
[--interdiff=<previous>]
27+
[--range-diff=<previous>]
2728
[--progress]
2829
[<common diff options>]
2930
[ <since> | <revision range> ]
@@ -238,6 +239,15 @@ feeding the result to `git send-email`.
238239
the series being formatted (for example `git format-patch
239240
--cover-letter --interdiff=feature/v1 -3 feature/v2`).
240241

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+
241251
--notes[=<ref>]::
242252
Append the notes (see linkgit:git-notes[1]) for the commit
243253
after the three-dash line.

builtin/log.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "commit-slab.h"
3333
#include "repository.h"
3434
#include "interdiff.h"
35+
#include "range-diff.h"
3536

3637
#define MAIL_DEFAULT_WRAP 72
3738

@@ -1089,6 +1090,12 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
10891090
fprintf_ln(rev->diffopt.file, "%s", rev->idiff_title);
10901091
show_interdiff(rev, 0);
10911092
}
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+
}
10921099
}
10931100

10941101
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,
14381445
return sb->buf;
14391446
}
14401447

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+
14411459
int cmd_format_patch(int argc, const char **argv, const char *prefix)
14421460
{
14431461
struct commit *commit;
@@ -1467,6 +1485,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
14671485
struct progress *progress = NULL;
14681486
struct oid_array idiff_prev = OID_ARRAY_INIT;
14691487
struct strbuf idiff_title = STRBUF_INIT;
1488+
const char *rdiff_prev = NULL;
1489+
struct strbuf rdiff1 = STRBUF_INIT;
1490+
struct strbuf rdiff2 = STRBUF_INIT;
14701491

14711492
const struct option builtin_format_patch_options[] = {
14721493
{ OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
@@ -1543,6 +1564,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
15431564
OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
15441565
N_("show changes against <rev> in cover letter or single patch"),
15451566
parse_opt_object_name),
1567+
OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
1568+
N_("show changes against <refspec> in cover letter")),
15461569
OPT_END()
15471570
};
15481571

@@ -1775,6 +1798,16 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
17751798
_("Interdiff against v%d:"));
17761799
}
17771800

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+
17781811
if (!signature) {
17791812
; /* --no-signature inhibits all signatures */
17801813
} else if (signature && signature != git_version_string) {
@@ -1898,6 +1931,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
18981931
done:
18991932
oid_array_clear(&idiff_prev);
19001933
strbuf_release(&idiff_title);
1934+
strbuf_release(&rdiff1);
1935+
strbuf_release(&rdiff2);
19011936
return 0;
19021937
}
19031938

revision.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ struct rev_info {
218218
const struct object_id *idiff_oid2;
219219
const char *idiff_title;
220220

221+
/* range-diff */
222+
const char *rdiff1;
223+
const char *rdiff2;
224+
int creation_factor;
225+
221226
/* commit counts */
222227
int count_left;
223228
int count_right;

t/t3206-range-diff.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,16 @@ test_expect_success 'changed message' '
142142
test_cmp expected actual
143143
'
144144

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+
145157
test_done

0 commit comments

Comments
 (0)