Skip to content

Commit 98294e9

Browse files
committed
Merge branch 'jc/format-patch-reroll'
Teach "format-patch" to prefix v4- to its output files for the fourth iteration of a patch series, to make it easier for the submitter to keep separate copies for iterations. * jc/format-patch-reroll: format-patch: give --reroll-count a short synonym -v format-patch: document and test --reroll-count format-patch: add --reroll-count=$N option get_patch_filename(): split into two functions get_patch_filename(): drop "just-numbers" hack get_patch_filename(): simplify function signature builtin/log.c: stop using global patch_suffix builtin/log.c: drop redundant "numbered_files" parameter from make_cover_letter() builtin/log.c: drop unused "numbered" parameter from make_cover_letter()
2 parents 06fb494 + 7952ea6 commit 98294e9

File tree

6 files changed

+84
-33
lines changed

6 files changed

+84
-33
lines changed

Documentation/git-format-patch.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ SYNOPSIS
1818
[--start-number <n>] [--numbered-files]
1919
[--in-reply-to=Message-Id] [--suffix=.<sfx>]
2020
[--ignore-if-in-upstream]
21-
[--subject-prefix=Subject-Prefix]
21+
[--subject-prefix=Subject-Prefix] [(--reroll-count|-v) <n>]
2222
[--to=<email>] [--cc=<email>]
2323
[--cover-letter] [--quiet] [--notes[=<ref>]]
2424
[<common diff options>]
@@ -166,6 +166,15 @@ will want to ensure that threading is disabled for `git send-email`.
166166
allows for useful naming of a patch series, and can be
167167
combined with the `--numbered` option.
168168

169+
-v <n>::
170+
--reroll-count=<n>::
171+
Mark the series as the <n>-th iteration of the topic. The
172+
output filenames have `v<n>` pretended to them, and the
173+
subject prefix ("PATCH" by default, but configurable via the
174+
`--subject-prefix` option) has ` v<n>` appended to it. E.g.
175+
`--reroll-count=4` may produce `v4-0001-add-makefile.patch`
176+
file that has "Subject: [PATCH v4 1/20] Add makefile" in it.
177+
169178
--to=<email>::
170179
Add a `To:` header to the email headers. This is in addition
171180
to any configured headers, and may be used multiple times.

builtin/log.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ static int reopen_stdout(struct commit *commit, const char *subject,
678678
struct rev_info *rev, int quiet)
679679
{
680680
struct strbuf filename = STRBUF_INIT;
681-
int suffix_len = strlen(fmt_patch_suffix) + 1;
681+
int suffix_len = strlen(rev->patch_suffix) + 1;
682682

683683
if (output_directory) {
684684
strbuf_addstr(&filename, output_directory);
@@ -689,7 +689,12 @@ static int reopen_stdout(struct commit *commit, const char *subject,
689689
strbuf_addch(&filename, '/');
690690
}
691691

692-
get_patch_filename(commit, subject, rev->nr, fmt_patch_suffix, &filename);
692+
if (rev->numbered_files)
693+
strbuf_addf(&filename, "%d", rev->nr);
694+
else if (commit)
695+
fmt_output_commit(&filename, commit, rev);
696+
else
697+
fmt_output_subject(&filename, subject, rev);
693698

694699
if (!quiet)
695700
fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
@@ -773,7 +778,6 @@ static void add_branch_description(struct strbuf *buf, const char *branch_name)
773778
}
774779

775780
static void make_cover_letter(struct rev_info *rev, int use_stdout,
776-
int numbered, int numbered_files,
777781
struct commit *origin,
778782
int nr, struct commit **list, struct commit *head,
779783
const char *branch_name,
@@ -796,7 +800,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
796800
committer = git_committer_info(0);
797801

798802
if (!use_stdout &&
799-
reopen_stdout(NULL, numbered_files ? NULL : "cover-letter", rev, quiet))
803+
reopen_stdout(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
800804
return;
801805

802806
log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
@@ -1060,7 +1064,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10601064
int nr = 0, total, i;
10611065
int use_stdout = 0;
10621066
int start_number = -1;
1063-
int numbered_files = 0; /* _just_ numbers */
1067+
int just_numbers = 0;
10641068
int ignore_if_in_upstream = 0;
10651069
int cover_letter = 0;
10661070
int boundary_count = 0;
@@ -1072,6 +1076,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10721076
struct strbuf buf = STRBUF_INIT;
10731077
int use_patch_format = 0;
10741078
int quiet = 0;
1079+
int reroll_count = -1;
10751080
char *branch_name = NULL;
10761081
const struct option builtin_format_patch_options[] = {
10771082
{ OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
@@ -1085,12 +1090,14 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10851090
N_("print patches to standard out")),
10861091
OPT_BOOLEAN(0, "cover-letter", &cover_letter,
10871092
N_("generate a cover letter")),
1088-
OPT_BOOLEAN(0, "numbered-files", &numbered_files,
1093+
OPT_BOOLEAN(0, "numbered-files", &just_numbers,
10891094
N_("use simple number sequence for output file names")),
10901095
OPT_STRING(0, "suffix", &fmt_patch_suffix, N_("sfx"),
10911096
N_("use <sfx> instead of '.patch'")),
10921097
OPT_INTEGER(0, "start-number", &start_number,
10931098
N_("start numbering patches at <n> instead of 1")),
1099+
OPT_INTEGER('v', "reroll-count", &reroll_count,
1100+
N_("mark the series as Nth re-roll")),
10941101
{ OPTION_CALLBACK, 0, "subject-prefix", &rev, N_("prefix"),
10951102
N_("Use [<prefix>] instead of [PATCH]"),
10961103
PARSE_OPT_NONEG, subject_prefix_callback },
@@ -1164,6 +1171,14 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
11641171
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
11651172
PARSE_OPT_KEEP_DASHDASH);
11661173

1174+
if (0 < reroll_count) {
1175+
struct strbuf sprefix = STRBUF_INIT;
1176+
strbuf_addf(&sprefix, "%s v%d",
1177+
rev.subject_prefix, reroll_count);
1178+
rev.reroll_count = reroll_count;
1179+
rev.subject_prefix = strbuf_detach(&sprefix, NULL);
1180+
}
1181+
11671182
if (do_signoff) {
11681183
const char *committer;
11691184
const char *endpos;
@@ -1354,12 +1369,12 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
13541369
const char *msgid = clean_message_id(in_reply_to);
13551370
string_list_append(rev.ref_message_ids, msgid);
13561371
}
1357-
rev.numbered_files = numbered_files;
1372+
rev.numbered_files = just_numbers;
13581373
rev.patch_suffix = fmt_patch_suffix;
13591374
if (cover_letter) {
13601375
if (thread)
13611376
gen_message_id(&rev, "cover");
1362-
make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1377+
make_cover_letter(&rev, use_stdout,
13631378
origin, nr, list, head, branch_name, quiet);
13641379
total++;
13651380
start_number--;
@@ -1406,7 +1421,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
14061421
}
14071422

14081423
if (!use_stdout &&
1409-
reopen_stdout(numbered_files ? NULL : commit, NULL, &rev, quiet))
1424+
reopen_stdout(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
14101425
die(_("Failed to create output files"));
14111426
shown = log_tree_commit(&rev, commit);
14121427
free(commit->buffer);

log-tree.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -299,26 +299,34 @@ static unsigned int digits_in_number(unsigned int number)
299299
return result;
300300
}
301301

302-
void get_patch_filename(struct commit *commit, const char *subject, int nr,
303-
const char *suffix, struct strbuf *buf)
302+
void fmt_output_subject(struct strbuf *filename,
303+
const char *subject,
304+
struct rev_info *info)
304305
{
305-
int suffix_len = strlen(suffix) + 1;
306-
int start_len = buf->len;
307-
308-
strbuf_addf(buf, commit || subject ? "%04d-" : "%d", nr);
309-
if (commit || subject) {
310-
int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
311-
struct pretty_print_context ctx = {0};
312-
313-
if (subject)
314-
strbuf_addstr(buf, subject);
315-
else if (commit)
316-
format_commit_message(commit, "%f", buf, &ctx);
317-
318-
if (max_len < buf->len)
319-
strbuf_setlen(buf, max_len);
320-
strbuf_addstr(buf, suffix);
321-
}
306+
const char *suffix = info->patch_suffix;
307+
int nr = info->nr;
308+
int start_len = filename->len;
309+
int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
310+
311+
if (0 < info->reroll_count)
312+
strbuf_addf(filename, "v%d-", info->reroll_count);
313+
strbuf_addf(filename, "%04d-%s", nr, subject);
314+
315+
if (max_len < filename->len)
316+
strbuf_setlen(filename, max_len);
317+
strbuf_addstr(filename, suffix);
318+
}
319+
320+
void fmt_output_commit(struct strbuf *filename,
321+
struct commit *commit,
322+
struct rev_info *info)
323+
{
324+
struct pretty_print_context ctx = {0};
325+
struct strbuf subject = STRBUF_INIT;
326+
327+
format_commit_message(commit, "%f", &subject, &ctx);
328+
fmt_output_subject(filename, subject.buf, info);
329+
strbuf_release(&subject);
322330
}
323331

324332
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
@@ -387,8 +395,10 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
387395
mime_boundary_leader, opt->mime_boundary);
388396
extra_headers = subject_buffer;
389397

390-
get_patch_filename(opt->numbered_files ? NULL : commit, NULL,
391-
opt->nr, opt->patch_suffix, &filename);
398+
if (opt->numbered_files)
399+
strbuf_addf(&filename, "%d", opt->nr);
400+
else
401+
fmt_output_commit(&filename, commit, opt);
392402
snprintf(buffer, sizeof(buffer) - 1,
393403
"\n--%s%s\n"
394404
"Content-Type: text/x-patch;"

log-tree.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
2121
void load_ref_decorations(int flags);
2222

2323
#define FORMAT_PATCH_NAME_MAX 64
24-
void get_patch_filename(struct commit *commit, const char *subject, int nr,
25-
const char *suffix, struct strbuf *buf);
24+
void fmt_output_commit(struct strbuf *, struct commit *, struct rev_info *);
25+
void fmt_output_subject(struct strbuf *, const char *subject, struct rev_info *);
2626

2727
#endif

revision.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ struct rev_info {
135135
const char *mime_boundary;
136136
const char *patch_suffix;
137137
int numbered_files;
138+
int reroll_count;
138139
char *message_id;
139140
struct string_list *ref_message_ids;
140141
const char *add_signoff;

t/t4014-format-patch.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,22 @@ test_expect_success 'multiple files' '
271271
ls patches/0001-Side-changes-1.patch patches/0002-Side-changes-2.patch patches/0003-Side-changes-3-with-n-backslash-n-in-it.patch
272272
'
273273

274+
test_expect_success 'reroll count' '
275+
rm -fr patches &&
276+
git format-patch -o patches --cover-letter --reroll-count 4 master..side >list &&
277+
! grep -v "^patches/v4-000[0-3]-" list &&
278+
sed -n -e "/^Subject: /p" $(cat list) >subjects &&
279+
! grep -v "^Subject: \[PATCH v4 [0-3]/3\] " subjects
280+
'
281+
282+
test_expect_success 'reroll count (-v)' '
283+
rm -fr patches &&
284+
git format-patch -o patches --cover-letter -v4 master..side >list &&
285+
! grep -v "^patches/v4-000[0-3]-" list &&
286+
sed -n -e "/^Subject: /p" $(cat list) >subjects &&
287+
! grep -v "^Subject: \[PATCH v4 [0-3]/3\] " subjects
288+
'
289+
274290
check_threading () {
275291
expect="$1" &&
276292
shift &&

0 commit comments

Comments
 (0)