format-patch: assume --cover-letter for diff in multi-patch series

When we deal with a multi-patch series in git-format-patch(1), if we see `--interdiff` or `--range-diff` but no `--cover-letter`, we return with an error, saying: fatal: --range-diff requires --cover-letter or single patch or: fatal: --interdiff requires --cover-letter or single patch This makes sense because the cover-letter is where we place the diff from the previous version. However, considering that `format-patch` generates a multi-patch as needed, let's adopt a similar "cover as necessary" approach when using `--interdiff` or `--range-diff`. Therefore, relax the requirement for an explicit `--cover-letter` in a multi-patch series when the user says `--iterdiff` or `--range-diff`. Still, if only to return the error, respect "format.coverLetter=no" and `--no-cover-letter`. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Jun 7, 2024 at 22:55 UTC f96c3854494011ccf3862c8327dcd052a8c874fb
3 files changed +31
builtin/log.c
+2
@@ -2255,6 +2255,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
2255 if (cover_letter == -1) {
2256 if (config_cover_letter == COVER_AUTO)
2257 cover_letter = (total > 1);
2258 + else if ((idiff_prev.nr || rdiff_prev) && (total > 1))
2259 + cover_letter = (config_cover_letter != COVER_OFF);
2260 else
2261 cover_letter = (config_cover_letter == COVER_ON);
2262 }
t/t3206-range-diff.sh
+14
@@ -545,6 +545,20 @@ do
545 '
546 done
547
548 +test_expect_success "--range-diff implies --cover-letter for multi-patch series" '
549 + test_when_finished "rm -f v2-000?-*" &&
550 + git format-patch -v2 --range-diff=topic main..unmodified &&
551 + test_grep "^Range-diff against v1:$" v2-0000-cover-letter.patch
552 +'
553 +
554 +test_expect_success "explicit --no-cover-letter defeats implied --cover-letter" '
555 + test_when_finished "rm -f v2-000?-*" &&
556 + test_must_fail git format-patch --no-cover-letter \
557 + -v2 --range-diff=topic main..unmodified &&
558 + test_must_fail git -c format.coverLetter=no format-patch \
559 + -v2 --range-diff=topic main..unmodified
560 +'
561 +
562 test_expect_success 'format-patch --range-diff as commentary' '
563 git format-patch --range-diff=HEAD~1 HEAD~1 >actual &&
564 test_when_finished "rm 0001-*" &&
t/t4014-format-patch.sh
+15
@@ -2445,6 +2445,21 @@ test_expect_success 'interdiff: solo-patch' '
2445 test_cmp expect actual
2446 '
2447
2448 +test_expect_success 'interdiff: multi-patch, implicit --cover-letter' '
2449 + test_when_finished "rm -f v23-0*.patch" &&
2450 + git format-patch --interdiff=boop~2 -2 -v23 &&
2451 + test_grep "^Interdiff against v22:$" v23-0000-cover-letter.patch &&
2452 + test_cmp expect actual
2453 +'
2454 +
2455 +test_expect_success 'interdiff: explicit --no-cover-letter defeats implied --cover-letter' '
2456 + test_when_finished "rm -f v23-0*.patch" &&
2457 + test_must_fail git format-patch --no-cover-letter \
2458 + --interdiff=boop~2 -2 -v23 &&
2459 + test_must_fail git -c format.coverLetter=no format-patch \
2460 + --interdiff=boop~2 -2 -v23
2461 +'
2462 +
2463 test_expect_success 'format-patch does not respect diff.noprefix' '
2464 git -c diff.noprefix format-patch -1 --stdout >actual &&
2465 grep "^--- a/blorp" actual