format-patch: move range/inter diff at the end of a single patch output

When running "format-patch" on a multiple patch series, the output coming from "--interdiff" and "--range-diff" options is inserted after the "shortlog" list of commits and the overall diffstat. The idea is that shortlog/diffstat are shorter and with denser information content, which gives a better overview before the readers dive into more details of range/inter diff. When working on a single patch, however, we stuff the inter/range diff output before the actual patch, next to the diffstat. This pushes down the patch text way down with inter/range diff output, distracting readers. Move the inter/range diff output to the very end of the output, after all the patch text is shown. As the inter/range diff is no longer part of the commentary block (i.e., what comes after the log message and "---", but before the patch text), stop producing "---" in the function that generates them. But to separate it out visually (note: this is not needed to help tools like "git apply" that pay attention to the hunk headers to figure out the length of the hunks), add an extra blank line between the end of the patch text and the inter/range diff. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 24, 2024 at 16:02 UTC 2fa04cebfbe57be54b9bc3371a4b70f701d56da9
2 files changed +35 -12
log-tree.c
+5 -6
@@ -684,8 +684,7 @@ static void show_diff_of_diff(struct rev_info *opt)
684 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
685 DIFF_QUEUE_CLEAR(&diff_queued_diff);
686
687 - next_commentary_block(opt, NULL);
688 - fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
687 + fprintf_ln(opt->diffopt.file, "\n%s", opt->idiff_title);
688 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
689 &opt->diffopt);
690
@@ -704,8 +703,7 @@ static void show_diff_of_diff(struct rev_info *opt)
703 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
704 DIFF_QUEUE_CLEAR(&diff_queued_diff);
705
707 - next_commentary_block(opt, NULL);
708 - fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
706 + fprintf_ln(opt->diffopt.file, "\n%s", opt->rdiff_title);
707 /*
708 * Pass minimum required diff-options to range-diff; others
709 * can be added later if deemed desirable.
@@ -903,8 +901,6 @@ void show_log(struct rev_info *opt)
901 strbuf_release(&msgbuf);
902 free(ctx.notes_message);
903 free(ctx.after_subject);
906 -
907 - show_diff_of_diff(opt);
904 }
905
906 int log_tree_diff_flush(struct rev_info *opt)
@@ -1173,9 +1169,12 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
1169 }
1170 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1171 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1172 + if (shown)
1173 + show_diff_of_diff(opt);
1174 opt->loginfo = NULL;
1175 maybe_flush_or_die(opt->diffopt.file, "stdout");
1176 opt->diffopt.no_free = no_free;
1177 +
1178 diff_free(&opt->diffopt);
1179 return shown;
1180 }
t/t4014-format-patch.sh
+30 -6
@@ -2482,13 +2482,37 @@ test_expect_success 'interdiff: reroll-count with a integer' '
2482 '
2483
2484 test_expect_success 'interdiff: solo-patch' '
2485 - cat >expect <<-\EOF &&
2486 - +fleep
2487 -
2488 - EOF
2485 git format-patch --interdiff=boop~2 -1 boop &&
2490 - test_grep "^Interdiff:$" 0001-fleep.patch &&
2491 - sed "1,/^ @@ /d; /^$/q" 0001-fleep.patch >actual &&
2486 +
2487 + # remove up to the last "patch" output line,
2488 + # and remove everything below the signature mark.
2489 + sed -e "1,/^+fleep\$/d" -e "/^-- /,\$d" 0001-fleep.patch >actual &&
2490 +
2491 + # fabricate Interdiff output.
2492 + git diff boop~2 boop >inter &&
2493 + {
2494 + echo &&
2495 + echo "Interdiff:" &&
2496 + sed -e "s/^/ /" inter
2497 + } >expect &&
2498 + test_cmp expect actual
2499 +'
2500 +
2501 +test_expect_success 'range-diff: solo-patch' '
2502 + git format-patch --creation-factor=999 \
2503 + --range-diff=boop~2..boop~1 -1 boop &&
2504 +
2505 + # remove up to the last "patch" output line,
2506 + # and remove everything below the signature mark.
2507 + sed -e "1,/^+fleep\$/d" -e "/^-- /,\$d" 0001-fleep.patch >actual &&
2508 +
2509 + # fabricate range-diff output.
2510 + {
2511 + echo &&
2512 + echo "Range-diff:" &&
2513 + git range-diff --creation-factor=999 \
2514 + boop~2..boop~1 boop~1..boop
2515 + } >expect &&
2516 test_cmp expect actual
2517 '
2518