Skip to content

Commit f54477a

Browse files
committed
Merge branch 'mf/format-patch-commit-list-format' into mf/format-patch-commit-list-format-doc
* 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 docs/pretty-formats: add %(count) and %(total) format-patch: rename --cover-letter-format option format-patch: refactor generate_commit_list_cover pretty.c: better die message %(count) and %(total) docs: add usage for the cover-letter fmt feature format-patch: add commitListFormat config format-patch: add ability to use alt cover format format-patch: move cover letter summary generation pretty.c: add %(count) and %(total) placeholders
2 parents 67006b9 + 36c16a5 commit f54477a

6 files changed

Lines changed: 243 additions & 14 deletions

File tree

Documentation/config/format.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ format.coverLetter::
101101
generate a cover-letter only when there's more than one patch.
102102
Default is false.
103103

104+
format.commitListFormat::
105+
When the `--cover-letter-format` option is not given, `format-patch`
106+
uses the value of this variable to decide how to format the title of
107+
each commit. Defaults to `shortlog`.
108+
104109
format.outputDirectory::
105110
Set a custom directory to store the resulting files instead of the
106111
current working directory. All directory components will be created.

Documentation/git-format-patch.adoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ SYNOPSIS
2424
[(--reroll-count|-v) <n>]
2525
[--to=<email>] [--cc=<email>]
2626
[--[no-]cover-letter] [--quiet]
27+
[--commit-list-format=<format-spec>]
2728
[--[no-]encode-email-headers]
2829
[--no-notes | --notes[=<ref>]]
2930
[--interdiff=<previous>]
@@ -322,6 +323,18 @@ feeding the result to `git send-email`.
322323
containing the branch description, shortlog and the overall diffstat. You can
323324
fill in a description in the file before sending it out.
324325
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:`.
330+
e.g. `log: %s (%an)`
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
334+
variable.
335+
This option implies the use of `--cover-letter` unless
336+
`--no-cover-letter` is given.
337+
325338
--encode-email-headers::
326339
--no-encode-email-headers::
327340
Encode email headers that have non-ASCII characters with
@@ -453,6 +466,7 @@ with configuration variables.
453466
signOff = true
454467
outputDirectory = <directory>
455468
coverLetter = auto
469+
commitListFormat = shortlog
456470
coverFromDescription = auto
457471
------------
458472

Documentation/pretty-formats.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ The placeholders are:
253253
linkgit:git-rev-list[1])
254254
+%d+:: ref names, like the --decorate option of linkgit:git-log[1]
255255
+%D+:: ref names without the " (", ")" wrapping.
256+
+%(count)+:: the number of a patch within a patch series. Used only in
257+
`--commit-list-format` in `format-patch`
258+
+%(total)+:: the total number of patches in a patch series. Used only in
259+
`--commit-list-format` in `format-patch`
256260
++%(decorate++`[:<option>,...]`++)++::
257261
ref names with custom decorations. The `decorate` string may be followed by a
258262
colon and zero or more comma-separated options. Option values may contain

builtin/log.c

Lines changed: 80 additions & 14 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"
@@ -886,6 +887,7 @@ struct format_config {
886887
char *signature;
887888
char *signature_file;
888889
enum cover_setting config_cover_letter;
890+
char *fmt_cover_letter_commit_list;
889891
char *config_output_directory;
890892
enum cover_from_description cover_from_description_mode;
891893
int show_notes;
@@ -930,6 +932,7 @@ static void format_config_release(struct format_config *cfg)
930932
string_list_clear(&cfg->extra_cc, 0);
931933
strbuf_release(&cfg->sprefix);
932934
free(cfg->fmt_patch_suffix);
935+
free(cfg->fmt_cover_letter_commit_list);
933936
}
934937

935938
static enum cover_from_description parse_cover_from_description(const char *arg)
@@ -1052,6 +1055,10 @@ static int git_format_config(const char *var, const char *value,
10521055
cfg->config_cover_letter = git_config_bool(var, value) ? COVER_ON : COVER_OFF;
10531056
return 0;
10541057
}
1058+
if (!strcmp(var, "format.commitlistformat")) {
1059+
FREE_AND_NULL(cfg->fmt_cover_letter_commit_list);
1060+
return git_config_string(&cfg->fmt_cover_letter_commit_list, var, value);
1061+
}
10551062
if (!strcmp(var, "format.outputdirectory")) {
10561063
FREE_AND_NULL(cfg->config_output_directory);
10571064
return git_config_string(&cfg->config_output_directory, var, value);
@@ -1335,13 +1342,59 @@ static void get_notes_args(struct strvec *arg, struct rev_info *rev)
13351342
}
13361343
}
13371344

1345+
static void generate_shortlog_cover_letter(struct shortlog *log,
1346+
struct rev_info *rev,
1347+
struct commit **list,
1348+
int nr)
1349+
{
1350+
shortlog_init(log);
1351+
log->wrap_lines = 1;
1352+
log->wrap = MAIL_DEFAULT_WRAP;
1353+
log->in1 = 2;
1354+
log->in2 = 4;
1355+
log->file = rev->diffopt.file;
1356+
log->groups = SHORTLOG_GROUP_AUTHOR;
1357+
shortlog_finish_setup(log);
1358+
for (int i = 0; i < nr; i++)
1359+
shortlog_add_commit(log, list[i]);
1360+
1361+
shortlog_output(log);
1362+
}
1363+
1364+
static void generate_commit_list_cover(FILE *cover_file, const char *format,
1365+
struct commit **list, int n)
1366+
{
1367+
struct strbuf commit_line = STRBUF_INIT;
1368+
struct strbuf wrapped_line = STRBUF_INIT;
1369+
struct pretty_print_context ctx = {0};
1370+
struct rev_info rev = REV_INFO_INIT;
1371+
1372+
rev.total = n;
1373+
ctx.rev = &rev;
1374+
for (int i = 1; i <= n; i++) {
1375+
rev.nr = i;
1376+
repo_format_commit_message(the_repository, list[n - i], format,
1377+
&commit_line, &ctx);
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);
1381+
strbuf_reset(&commit_line);
1382+
strbuf_reset(&wrapped_line);
1383+
}
1384+
fprintf(cover_file, "\n");
1385+
1386+
strbuf_release(&commit_line);
1387+
strbuf_release(&wrapped_line);
1388+
}
1389+
13381390
static void make_cover_letter(struct rev_info *rev, int use_separate_file,
13391391
struct commit *origin,
13401392
int nr, struct commit **list,
13411393
const char *description_file,
13421394
const char *branch_name,
13431395
int quiet,
1344-
const struct format_config *cfg)
1396+
const struct format_config *cfg,
1397+
const char *format)
13451398
{
13461399
const char *from;
13471400
struct shortlog log;
@@ -1388,18 +1441,17 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
13881441
free(pp.after_subject);
13891442
strbuf_release(&sb);
13901443

1391-
shortlog_init(&log);
1392-
log.wrap_lines = 1;
1393-
log.wrap = MAIL_DEFAULT_WRAP;
1394-
log.in1 = 2;
1395-
log.in2 = 4;
1396-
log.file = rev->diffopt.file;
1397-
log.groups = SHORTLOG_GROUP_AUTHOR;
1398-
shortlog_finish_setup(&log);
1399-
for (i = 0; i < nr; i++)
1400-
shortlog_add_commit(&log, list[i]);
1401-
1402-
shortlog_output(&log);
1444+
if (skip_prefix(format, "log:", &format))
1445+
generate_commit_list_cover(rev->diffopt.file, format, list, nr);
1446+
else if (!strcmp(format, "shortlog"))
1447+
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);
1453+
else
1454+
die(_("'%s' is not a valid format string"), format);
14031455

14041456
/* We can only do diffstat with a unique reference point */
14051457
if (origin)
@@ -1917,6 +1969,7 @@ int cmd_format_patch(int argc,
19171969
int just_numbers = 0;
19181970
int ignore_if_in_upstream = 0;
19191971
int cover_letter = -1;
1972+
const char *cover_letter_fmt = NULL;
19201973
int boundary_count = 0;
19211974
int no_binary_diff = 0;
19221975
int zero_commit = 0;
@@ -1963,6 +2016,8 @@ int cmd_format_patch(int argc,
19632016
N_("print patches to standard out")),
19642017
OPT_BOOL(0, "cover-letter", &cover_letter,
19652018
N_("generate a cover letter")),
2019+
OPT_STRING(0, "commit-list-format", &cover_letter_fmt, N_("format-spec"),
2020+
N_("format spec used for the commit list in the cover letter")),
19662021
OPT_BOOL(0, "numbered-files", &just_numbers,
19672022
N_("use simple number sequence for output file names")),
19682023
OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
@@ -2300,6 +2355,15 @@ int cmd_format_patch(int argc,
23002355
/* nothing to do */
23012356
goto done;
23022357
total = list.nr;
2358+
2359+
if (!cover_letter_fmt) {
2360+
cover_letter_fmt = cfg.fmt_cover_letter_commit_list;
2361+
if (!cover_letter_fmt)
2362+
cover_letter_fmt = "shortlog";
2363+
} else if (cover_letter == -1) {
2364+
cover_letter = 1;
2365+
}
2366+
23032367
if (cover_letter == -1) {
23042368
if (cfg.config_cover_letter == COVER_AUTO)
23052369
cover_letter = (total > 1);
@@ -2386,12 +2450,14 @@ int cmd_format_patch(int argc,
23862450
}
23872451
rev.numbered_files = just_numbers;
23882452
rev.patch_suffix = fmt_patch_suffix;
2453+
23892454
if (cover_letter) {
23902455
if (cfg.thread)
23912456
gen_message_id(&rev, "cover");
23922457
make_cover_letter(&rev, !!output_directory,
23932458
origin, list.nr, list.items,
2394-
description_file, branch_name, quiet, &cfg);
2459+
description_file, branch_name, quiet, &cfg,
2460+
cover_letter_fmt);
23952461
print_bases(&bases, rev.diffopt.file);
23962462
print_signature(signature, rev.diffopt.file);
23972463
total++;

pretty.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,21 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
15491549
if (!commit->object.parsed)
15501550
parse_object(the_repository, &commit->object.oid);
15511551

1552+
if (starts_with(placeholder, "(count)")) {
1553+
if (!c->pretty_ctx->rev)
1554+
die(_("%s is not supported by this command"), "%(count)");
1555+
strbuf_addf(sb, "%0*d", decimal_width(c->pretty_ctx->rev->total),
1556+
c->pretty_ctx->rev->nr);
1557+
return 7;
1558+
}
1559+
1560+
if (starts_with(placeholder, "(total)")) {
1561+
if (!c->pretty_ctx->rev)
1562+
die(_("%s is not supported by this command"), "%(total)");
1563+
strbuf_addf(sb, "%d", c->pretty_ctx->rev->total);
1564+
return 7;
1565+
}
1566+
15521567
switch (placeholder[0]) {
15531568
case 'H': /* commit hash */
15541569
strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));

t/t4014-format-patch.sh

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,131 @@ test_expect_success 'filename limit applies only to basename' '
380380
done
381381
'
382382

383+
test_expect_success 'cover letter with subject, author and count' '
384+
rm -rf patches &&
385+
test_when_finished "git reset --hard HEAD~1" &&
386+
test_when_finished "rm -rf patches test_file" &&
387+
touch test_file &&
388+
git add test_file &&
389+
git commit -m "This is a subject" &&
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
393+
'
394+
395+
test_expect_success 'cover letter with custom format no prefix' '
396+
rm -rf patches &&
397+
test_when_finished "git reset --hard HEAD~1" &&
398+
test_when_finished "rm -rf patches test_file" &&
399+
touch test_file &&
400+
git add test_file &&
401+
git commit -m "This is a subject" &&
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
405+
'
406+
407+
test_expect_success 'cover letter fail when no prefix and no placeholder' '
408+
rm -rf patches &&
409+
test_when_finished "git reset --hard HEAD~1" &&
410+
test_when_finished "rm -rf patches test_file err" &&
411+
touch test_file &&
412+
git add test_file &&
413+
git commit -m "This is a subject" &&
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
417+
'
418+
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' '
446+
test_when_finished "git reset --hard HEAD~1" &&
447+
test_when_finished "rm -rf patches result test_file" &&
448+
touch test_file &&
449+
git add test_file &&
450+
git commit -m "This is a subject" &&
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
453+
'
454+
455+
test_expect_success 'cover letter config with count, subject and author' '
456+
test_when_finished "rm -rf patches result" &&
457+
test_when_finished "git config unset format.coverletter" &&
458+
test_when_finished "git config unset format.commitlistformat" &&
459+
git config set format.coverletter true &&
460+
git config set format.commitlistformat "log:[%(count)/%(total)] %s (%an)" &&
461+
git format-patch -o patches HEAD~2 &&
462+
grep -E "^[[[:digit:]]+/[[:digit:]]+] .* \(A U Thor\)" patches/0000-cover-letter.patch >result &&
463+
test_line_count = 2 result
464+
'
465+
466+
test_expect_success 'cover letter config with count and author' '
467+
test_when_finished "rm -rf patches result" &&
468+
test_when_finished "git config unset format.coverletter" &&
469+
test_when_finished "git config unset format.commitlistformat" &&
470+
git config set format.coverletter true &&
471+
git config set format.commitlistformat "log:[%(count)/%(total)] (%an)" &&
472+
git format-patch -o patches HEAD~2 &&
473+
grep -E "^[[[:digit:]]+/[[:digit:]]+] \(A U Thor\)" patches/0000-cover-letter.patch >result &&
474+
test_line_count = 2 result
475+
'
476+
477+
test_expect_success 'cover letter config commitlistformat set to modern' '
478+
test_when_finished "rm -rf patches result" &&
479+
test_when_finished "git config unset format.coverletter" &&
480+
test_when_finished "git config unset format.commitlistformat" &&
481+
git config set format.coverletter true &&
482+
git config set format.commitlistformat modern &&
483+
git format-patch -o patches HEAD~2 &&
484+
grep -E "^[[[:digit:]]+/[[:digit:]]+] .*$" patches/0000-cover-letter.patch >result &&
485+
test_line_count = 2 result
486+
'
487+
488+
test_expect_success 'cover letter config commitlistformat set to shortlog' '
489+
test_when_finished "rm -rf patches result" &&
490+
test_when_finished "git config unset format.coverletter" &&
491+
test_when_finished "git config unset format.commitlistformat" &&
492+
git config set format.coverletter true &&
493+
git config set format.commitlistformat shortlog &&
494+
git format-patch -o patches HEAD~2 &&
495+
grep -E "^A U Thor \([[:digit:]]+\)" patches/0000-cover-letter.patch >result &&
496+
test_line_count = 1 result
497+
'
498+
499+
test_expect_success 'cover letter config commitlistformat not set' '
500+
test_when_finished "rm -rf patches result" &&
501+
test_when_finished "git config unset format.coverletter" &&
502+
git config set format.coverletter true &&
503+
git format-patch -o patches HEAD~2 &&
504+
grep -E "^A U Thor \([[:digit:]]+\)" patches/0000-cover-letter.patch >result &&
505+
test_line_count = 1 result
506+
'
507+
383508
test_expect_success 'reroll count' '
384509
rm -fr patches &&
385510
git format-patch -o patches --cover-letter --reroll-count 4 main..side >list &&

0 commit comments

Comments
 (0)