Skip to content

Commit ee6cbf7

Browse files
sunshinecogitster
authored andcommitted
format-patch: allow --interdiff to apply to a lone-patch
When submitting a revised version of a patch or series, it can be helpful (to reviewers) to include a summary of changes since the previous attempt in the form of an interdiff, typically in the cover letter. However, it is occasionally useful, despite making for a noisy read, to insert an interdiff into the commentary section of the lone patch of a 1-patch series. Therefore, extend "git format-patch --interdiff=<prev>" to insert an interdiff into the commentary section of a lone patch rather than requiring a cover letter. The interdiff is indented to avoid confusing git-am and human readers into considering it part of the patch proper. Implementation note: Generating an interdiff for insertion into the commentary section of a patch which itself is currently being generated requires invoking the diffing machinery recursively. However, the machinery does not (presently) support this since it uses global state. Consequently, we need to take care to stash away the state of the in-progress operation while generating the interdiff, and restore it after. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3fcc7a2 commit ee6cbf7

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

Documentation/git-format-patch.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ feeding the result to `git send-email`.
230230
fill in a description in the file before sending it out.
231231

232232
--interdiff=<previous>::
233-
As a reviewer aid, insert an interdiff into the cover letter showing
233+
As a reviewer aid, insert an interdiff into the cover letter,
234+
or as commentary of the lone patch of a 1-patch series, showing
234235
the differences between the previous version of the patch series and
235236
the series currently being formatted. `previous` is a single revision
236237
naming the tip of the previous series which shares a common base with

builtin/log.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
15401540
OPT_BOOL(0, "progress", &show_progress,
15411541
N_("show progress while generating patches")),
15421542
OPT_CALLBACK(0, "interdiff", &idiff_prev, N_("rev"),
1543-
N_("show changes against <rev> in cover letter"),
1543+
N_("show changes against <rev> in cover letter or single patch"),
15441544
parse_opt_object_name),
15451545
OPT_END()
15461546
};
@@ -1765,8 +1765,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
17651765
rev.total = total + start_number - 1;
17661766

17671767
if (idiff_prev.nr) {
1768-
if (!cover_letter)
1769-
die(_("--interdiff requires --cover-letter"));
1768+
if (!cover_letter && total != 1)
1769+
die(_("--interdiff requires --cover-letter or single patch"));
17701770
rev.idiff_oid1 = &idiff_prev.oid[idiff_prev.nr - 1];
17711771
rev.idiff_oid2 = get_commit_tree_oid(list[0]);
17721772
rev.idiff_title = diff_title(&idiff_title, reroll_count,
@@ -1811,6 +1811,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
18111811
print_signature(rev.diffopt.file);
18121812
total++;
18131813
start_number--;
1814+
/* interdiff in cover-letter; omit from patches */
1815+
rev.idiff_oid1 = NULL;
18141816
}
18151817
rev.add_signoff = do_signoff;
18161818

log-tree.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "sequencer.h"
1515
#include "line-log.h"
1616
#include "help.h"
17+
#include "interdiff.h"
1718

1819
static struct decoration name_decoration = { "object names" };
1920
static int decoration_loaded;
@@ -736,6 +737,19 @@ void show_log(struct rev_info *opt)
736737

737738
strbuf_release(&msgbuf);
738739
free(ctx.notes_message);
740+
741+
if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
742+
struct diff_queue_struct dq;
743+
744+
memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
745+
DIFF_QUEUE_CLEAR(&diff_queued_diff);
746+
747+
next_commentary_block(opt, NULL);
748+
fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
749+
show_interdiff(opt, 2);
750+
751+
memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
752+
}
739753
}
740754

741755
int log_tree_diff_flush(struct rev_info *opt)

t/t4014-format-patch.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,7 @@ test_expect_success 'interdiff: cover-letter' '
17301730
EOF
17311731
git format-patch --cover-letter --interdiff=boop~2 -1 boop &&
17321732
test_i18ngrep "^Interdiff:$" 0000-cover-letter.patch &&
1733+
test_i18ngrep ! "^Interdiff:$" 0001-fleep.patch &&
17331734
sed "1,/^@@ /d; /^-- $/q" <0000-cover-letter.patch >actual &&
17341735
test_cmp expect actual
17351736
'
@@ -1739,4 +1740,15 @@ test_expect_success 'interdiff: reroll-count' '
17391740
test_i18ngrep "^Interdiff ..* v1:$" v2-0000-cover-letter.patch
17401741
'
17411742

1743+
test_expect_success 'interdiff: solo-patch' '
1744+
cat >expect <<-\EOF &&
1745+
+fleep
1746+
1747+
EOF
1748+
git format-patch --interdiff=boop~2 -1 boop &&
1749+
test_i18ngrep "^Interdiff:$" 0001-fleep.patch &&
1750+
sed "1,/^ @@ /d; /^$/q" <0001-fleep.patch >actual &&
1751+
test_cmp expect actual
1752+
'
1753+
17421754
test_done

0 commit comments

Comments
 (0)