Skip to content

Commit a24be85

Browse files
committed
Merge branch 'mf/format-patch-commit-list-format' into jch
Improve the recently introduced `git format-patch --commit-list-format` (formerly `--cover-letter-format`) option, including a new "modern" preset and better CLI ergonomics. * mf/format-patch-commit-list-format: format-patch: --commit-list-format without prefix format-patch: add preset for --commit-list-format format-patch: wrap generate_commit_list_cover() format.commitListFormat: strip meaning from empty format-patch: rename --cover-letter-format option format-patch: refactor generate_commit_list_cover pretty.c: better die message %(count) and %(total)
2 parents b81d306 + 5b3ddc3 commit a24be85

6 files changed

Lines changed: 80 additions & 53 deletions

File tree

Documentation/config/format.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ format.coverLetter::
104104
format.commitListFormat::
105105
When the `--cover-letter-format` option is not given, `format-patch`
106106
uses the value of this variable to decide how to format the title of
107-
each commit. Default to `shortlog`.
107+
each commit. Defaults to `shortlog`.
108108

109109
format.outputDirectory::
110110
Set a custom directory to store the resulting files instead of the

Documentation/git-format-patch.adoc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ SYNOPSIS
2424
[(--reroll-count|-v) <n>]
2525
[--to=<email>] [--cc=<email>]
2626
[--[no-]cover-letter] [--quiet]
27-
[--cover-letter-format=<format-spec>]
27+
[--commit-list-format=<format-spec>]
2828
[--[no-]encode-email-headers]
2929
[--no-notes | --notes[=<ref>]]
3030
[--interdiff=<previous>]
@@ -323,16 +323,17 @@ feeding the result to `git send-email`.
323323
containing the branch description, shortlog and the overall diffstat. You can
324324
fill in a description in the file before sending it out.
325325
326-
--cover-letter-format=<format-spec>::
327-
Specify the format in which to generate the commit list of the
328-
patch series. This option is available if the user wants to use
329-
an alternative to the default `shortlog` format. The accepted
330-
values for format-spec are "shortlog" or a format string
331-
prefixed with `log:`.
326+
--commit-list-format=<format-spec>::
327+
Specify the format in which to generate the commit list of the patch
328+
series. The accepted values for format-spec are `shortlog`, `modern` or a
329+
format-string prefixed with `log:`.
332330
e.g. `log: %s (%an)`
333-
If defined, defaults to the `format.commitListFormat` configuration
331+
The user is allowed to drop the prefix if the format-string contains a
332+
`%<placeholder>`.
333+
If not given, defaults to the `format.commitListFormat` configuration
334334
variable.
335-
This option is relevant only if a cover letter is generated.
335+
This option implies the use of `--cover-letter` unless
336+
`--no-cover-letter` is given.
336337
337338
--encode-email-headers::
338339
--no-encode-email-headers::

builtin/log.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "progress.h"
4141
#include "commit-slab.h"
4242
#include "advice.h"
43+
#include "utf8.h"
4344

4445
#include "commit-reach.h"
4546
#include "range-diff.h"
@@ -1055,17 +1056,8 @@ static int git_format_config(const char *var, const char *value,
10551056
return 0;
10561057
}
10571058
if (!strcmp(var, "format.commitlistformat")) {
1058-
struct strbuf tmp = STRBUF_INIT;
1059-
strbuf_init(&tmp, 0);
1060-
if (value)
1061-
strbuf_addstr(&tmp, value);
1062-
else
1063-
strbuf_addstr(&tmp, "log:[%(count)/%(total)] %s");
1064-
10651059
FREE_AND_NULL(cfg->fmt_cover_letter_commit_list);
1066-
git_config_string(&cfg->fmt_cover_letter_commit_list, var, tmp.buf);
1067-
strbuf_release(&tmp);
1068-
return 0;
1060+
return git_config_string(&cfg->fmt_cover_letter_commit_list, var, value);
10691061
}
10701062
if (!strcmp(var, "format.outputdirectory")) {
10711063
FREE_AND_NULL(cfg->config_output_directory);
@@ -1373,22 +1365,26 @@ static void generate_commit_list_cover(FILE *cover_file, const char *format,
13731365
struct commit **list, int n)
13741366
{
13751367
struct strbuf commit_line = STRBUF_INIT;
1368+
struct strbuf wrapped_line = STRBUF_INIT;
13761369
struct pretty_print_context ctx = {0};
13771370
struct rev_info rev = REV_INFO_INIT;
13781371

1379-
strbuf_init(&commit_line, 0);
13801372
rev.total = n;
13811373
ctx.rev = &rev;
1382-
for (int i = n - 1; i >= 0; i--) {
1383-
rev.nr = n - i;
1384-
repo_format_commit_message(the_repository, list[i], format,
1374+
for (int i = 1; i <= n; i++) {
1375+
rev.nr = i;
1376+
repo_format_commit_message(the_repository, list[n - i], format,
13851377
&commit_line, &ctx);
1386-
fprintf(cover_file, "%s\n", commit_line.buf);
1378+
strbuf_add_wrapped_text(&wrapped_line, commit_line.buf, 0, 0,
1379+
MAIL_DEFAULT_WRAP);
1380+
fprintf(cover_file, "%s\n", wrapped_line.buf);
13871381
strbuf_reset(&commit_line);
1382+
strbuf_reset(&wrapped_line);
13881383
}
13891384
fprintf(cover_file, "\n");
13901385

13911386
strbuf_release(&commit_line);
1387+
strbuf_release(&wrapped_line);
13921388
}
13931389

13941390
static void make_cover_letter(struct rev_info *rev, int use_separate_file,
@@ -1449,6 +1445,11 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
14491445
generate_commit_list_cover(rev->diffopt.file, format, list, nr);
14501446
else if (!strcmp(format, "shortlog"))
14511447
generate_shortlog_cover_letter(&log, rev, list, nr);
1448+
else if (!strcmp(format, "modern"))
1449+
generate_commit_list_cover(rev->diffopt.file, "[%(count)/%(total)] %s",
1450+
list, nr);
1451+
else if (strchr(format, '%'))
1452+
generate_commit_list_cover(rev->diffopt.file, format, list, nr);
14521453
else
14531454
die(_("'%s' is not a valid format string"), format);
14541455

@@ -2015,7 +2016,7 @@ int cmd_format_patch(int argc,
20152016
N_("print patches to standard out")),
20162017
OPT_BOOL(0, "cover-letter", &cover_letter,
20172018
N_("generate a cover letter")),
2018-
OPT_STRING(0, "cover-letter-format", &cover_letter_fmt, N_("format-spec"),
2019+
OPT_STRING(0, "commit-list-format", &cover_letter_fmt, N_("format-spec"),
20192020
N_("format spec used for the commit list in the cover letter")),
20202021
OPT_BOOL(0, "numbered-files", &just_numbers,
20212022
N_("use simple number sequence for output file names")),
@@ -2359,6 +2360,8 @@ int cmd_format_patch(int argc,
23592360
cover_letter_fmt = cfg.fmt_cover_letter_commit_list;
23602361
if (!cover_letter_fmt)
23612362
cover_letter_fmt = "shortlog";
2363+
} else if (cover_letter == -1) {
2364+
cover_letter = 1;
23622365
}
23632366

23642367
if (cover_letter == -1) {

pretty.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,15 +1551,15 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
15511551

15521552
if (starts_with(placeholder, "(count)")) {
15531553
if (!c->pretty_ctx->rev)
1554-
die(_("this format specifier can't be used with this command"));
1554+
die(_("%s is not supported by this command"), "%(count)");
15551555
strbuf_addf(sb, "%0*d", decimal_width(c->pretty_ctx->rev->total),
15561556
c->pretty_ctx->rev->nr);
15571557
return 7;
15581558
}
15591559

15601560
if (starts_with(placeholder, "(total)")) {
15611561
if (!c->pretty_ctx->rev)
1562-
die(_("this format specifier can't be used with this command"));
1562+
die(_("%s is not supported by this command"), "%(total)");
15631563
strbuf_addf(sb, "%d", c->pretty_ctx->rev->total);
15641564
return 7;
15651565
}

t/t4014-format-patch.sh

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -383,49 +383,73 @@ test_expect_success 'filename limit applies only to basename' '
383383
test_expect_success 'cover letter with subject, author and count' '
384384
rm -rf patches &&
385385
test_when_finished "git reset --hard HEAD~1" &&
386-
test_when_finished "rm -rf patches result test_file" &&
386+
test_when_finished "rm -rf patches test_file" &&
387387
touch test_file &&
388388
git add test_file &&
389389
git commit -m "This is a subject" &&
390-
git format-patch --cover-letter \
391-
--cover-letter-format="log:[%(count)/%(total)] %s (%an)" -o patches HEAD~1 &&
392-
grep "^\[1/1\] This is a subject (A U Thor)$" patches/0000-cover-letter.patch >result &&
393-
test_line_count = 1 result
390+
git format-patch --commit-list-format="log:[%(count)/%(total)] %s (%an)" \
391+
-o patches HEAD~1 &&
392+
test_grep "^\[1/1\] This is a subject (A U Thor)$" patches/0000-cover-letter.patch
394393
'
395394

396-
test_expected_success 'cover letter with author and count' '
395+
test_expect_success 'cover letter with custom format no prefix' '
396+
rm -rf patches &&
397397
test_when_finished "git reset --hard HEAD~1" &&
398-
test_when_finished "rm -rf patches result test_file" &&
398+
test_when_finished "rm -rf patches test_file" &&
399399
touch test_file &&
400400
git add test_file &&
401401
git commit -m "This is a subject" &&
402-
git format-patch --cover-letter \
403-
--cover-letter-format="log:[%(count)/%(total)] %an" -o patches HEAD~1 &&
404-
grep "^\[1/1\] A U Thor$" patches/0000-cover-letter.patch >result &&
405-
test_line_count = 1 result
402+
git format-patch --commit-list-format="[%(count)/%(total)] %s (%an)" \
403+
-o patches HEAD~1 &&
404+
test_grep "^\[1/1\] This is a subject (A U Thor)$" patches/0000-cover-letter.patch
406405
'
407406

408-
test_expect_success 'cover letter shortlog' '
407+
test_expect_success 'cover letter fail when no prefix and no placeholder' '
408+
rm -rf patches &&
409409
test_when_finished "git reset --hard HEAD~1" &&
410-
test_when_finished "rm -rf patches result test_file" &&
410+
test_when_finished "rm -rf patches test_file err" &&
411411
touch test_file &&
412412
git add test_file &&
413413
git commit -m "This is a subject" &&
414-
git format-patch --cover-letter --cover-letter-format=shortlog \
415-
-o patches HEAD~1 &&
416-
sed -n -e "/^A U Thor/p;" patches/0000-cover-letter.patch >result &&
417-
test_line_count = 1 result
414+
test_must_fail git format-patch --commit-list-format="this should fail" \
415+
-o patches HEAD~1 2>err &&
416+
test_grep "is not a valid format string" err
418417
'
419418

420-
test_expect_success 'cover letter no format' '
419+
test_expect_success 'cover letter modern format' '
420+
test_when_finished "git reset --hard HEAD~1" &&
421+
test_when_finished "rm -rf patches test_file" &&
422+
touch test_file &&
423+
git add test_file &&
424+
git commit -m "This is a subject" &&
425+
git format-patch --commit-list-format="modern" -o patches HEAD~1 &&
426+
test_grep "^\[1/1\] This is a subject$" patches/0000-cover-letter.patch
427+
'
428+
429+
test_expect_success 'cover letter shortlog format' '
430+
test_when_finished "git reset --hard HEAD~1" &&
431+
test_when_finished "rm -rf expect patches result test_file" &&
432+
cat >expect <<-"EOF" &&
433+
A U Thor (1):
434+
This is a subject
435+
EOF
436+
touch test_file &&
437+
git add test_file &&
438+
git commit -m "This is a subject" &&
439+
git format-patch --commit-list-format=shortlog -o patches HEAD~1 &&
440+
grep -E -A 1 "^A U Thor \([[:digit:]]+\):$" patches/0000-cover-letter.patch >result &&
441+
cat result &&
442+
test_cmp expect result
443+
'
444+
445+
test_expect_success 'no cover letter but with format specified' '
421446
test_when_finished "git reset --hard HEAD~1" &&
422447
test_when_finished "rm -rf patches result test_file" &&
423448
touch test_file &&
424449
git add test_file &&
425450
git commit -m "This is a subject" &&
426-
git format-patch --cover-letter -o patches HEAD~1 &&
427-
sed -n -e "/^A U Thor/p;" patches/0000-cover-letter.patch >result &&
428-
test_line_count = 1 result
451+
git format-patch --no-cover-letter --commit-list-format="[%(count)] %s" -o patches HEAD~1 &&
452+
test_path_is_missing patches/0000-cover-letter.patch
429453
'
430454

431455
test_expect_success 'cover letter config with count, subject and author' '
@@ -450,14 +474,14 @@ test_expect_success 'cover letter config with count and author' '
450474
test_line_count = 2 result
451475
'
452476

453-
test_expect_success 'cover letter config commitlistformat set but no format' '
477+
test_expect_success 'cover letter config commitlistformat set to modern' '
454478
test_when_finished "rm -rf patches result" &&
455479
test_when_finished "git config unset format.coverletter" &&
456480
test_when_finished "git config unset format.commitlistformat" &&
457481
git config set format.coverletter true &&
458-
printf "\tcommitlistformat" >> .git/config &&
482+
git config set format.commitlistformat modern &&
459483
git format-patch -o patches HEAD~2 &&
460-
grep -E "^[[[:digit:]]+/[[:digit:]]+] .*" patches/0000-cover-letter.patch >result &&
484+
grep -E "^[[[:digit:]]+/[[:digit:]]+] .*$" patches/0000-cover-letter.patch >result &&
461485
test_line_count = 2 result
462486
'
463487

t/t9902-completion.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2775,7 +2775,6 @@ test_expect_success PERL 'send-email' '
27752775
test_completion "git send-email --cov" <<-\EOF &&
27762776
--cover-from-description=Z
27772777
--cover-letter Z
2778-
--cover-letter-format=Z
27792778
EOF
27802779
test_completion "git send-email --val" <<-\EOF &&
27812780
--validate Z

0 commit comments

Comments
 (0)