format-patch: --commit-list-format without prefix

Having to prefix a custom format-string with "log:" when passed from the
CLI can be annoying. It would be great if this prefix wasn't required.

Teach make_cover_letter() to accept custom format-strings without the
"log:" prefix if a placeholder is detected.

Note that both here and in "git log --format" the check is done naively
by just checking for the presence of a '%'.

Signed-off-by: Mirko Faina <mroik@delayed.space>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Mirko Faina 2026-03-23 17:57:35 +01:00 committed by Junio C Hamano
parent d022dc77ab
commit 36c16a5b7f
3 changed files with 29 additions and 1 deletions

View File

@ -326,8 +326,10 @@ feeding the result to `git send-email`.
--commit-list-format=<format-spec>::
Specify the format in which to generate the commit list of the patch
series. The accepted values for format-spec are `shortlog`, `modern` or a
format string prefixed with `log:`.
format-string prefixed with `log:`.
e.g. `log: %s (%an)`
The user is allowed to drop the prefix if the format-string contains a
`%<placeholder>`.
If not given, defaults to the `format.commitListFormat` configuration
variable.
This option implies the use of `--cover-letter` unless

View File

@ -1448,6 +1448,8 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
else if (!strcmp(format, "modern"))
generate_commit_list_cover(rev->diffopt.file, "[%(count)/%(total)] %s",
list, nr);
else if (strchr(format, '%'))
generate_commit_list_cover(rev->diffopt.file, format, list, nr);
else
die(_("'%s' is not a valid format string"), format);


View File

@ -392,6 +392,30 @@ test_expect_success 'cover letter with subject, author and count' '
test_grep "^\[1/1\] This is a subject (A U Thor)$" patches/0000-cover-letter.patch
'

test_expect_success 'cover letter with custom format no prefix' '
rm -rf patches &&
test_when_finished "git reset --hard HEAD~1" &&
test_when_finished "rm -rf patches test_file" &&
touch test_file &&
git add test_file &&
git commit -m "This is a subject" &&
git format-patch --commit-list-format="[%(count)/%(total)] %s (%an)" \
-o patches HEAD~1 &&
test_grep "^\[1/1\] This is a subject (A U Thor)$" patches/0000-cover-letter.patch
'

test_expect_success 'cover letter fail when no prefix and no placeholder' '
rm -rf patches &&
test_when_finished "git reset --hard HEAD~1" &&
test_when_finished "rm -rf patches test_file err" &&
touch test_file &&
git add test_file &&
git commit -m "This is a subject" &&
test_must_fail git format-patch --commit-list-format="this should fail" \
-o patches HEAD~1 2>err &&
test_grep "is not a valid format string" err
'

test_expect_success 'cover letter modern format' '
test_when_finished "git reset --hard HEAD~1" &&
test_when_finished "rm -rf patches test_file" &&