format-patch: allow a non-integral version numbers

The `-v<n>` option of `format-patch` can give nothing but an integral iteration number to patches in a series.  Some people, however, prefer to mark a new iteration with only a small fixup with a non integral iteration number (e.g. an "oops, that was wrong" fix-up patch for v4 iteration may be labeled as "v4.1"). Allow `format-patch` to take such a non-integral iteration number. `<n>` can be any string, such as '3.1' or '4rev2'. In the case where it is a non-integral value, the "Range-diff" and "Interdiff" headers will not include the previous version. Signed-off-by: ZheNing Hu <adlternative@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

ZheNing Hu committed Mar 23, 2021 at 11:12 UTC db91988aa102d48af1c1203d8cc2d01240df7365
6 files changed +88 -12
Documentation/git-format-patch.txt
+5
@@ -221,6 +221,11 @@ populated with placeholder text.
221 `--subject-prefix` option) has ` v<n>` appended to it. E.g.
222 `--reroll-count=4` may produce `v4-0001-add-makefile.patch`
223 file that has "Subject: [PATCH v4 1/20] Add makefile" in it.
224 + `<n>` does not have to be an integer (e.g. "--reroll-count=4.4",
225 + or "--reroll-count=4rev2" are allowed), but the downside of
226 + using such a reroll-count is that the range-diff/interdiff
227 + with the previous version does not state exactly which
228 + version the new interation is compared against.
229
230 --to=<email>::
231 Add a `To:` header to the email headers. This is in addition
builtin/log.c
+16 -9
@@ -1662,13 +1662,19 @@ static void print_bases(struct base_tree_info *bases, FILE *file)
1662 oidclr(&bases->base_commit);
1663 }
1664
1665 -static const char *diff_title(struct strbuf *sb, int reroll_count,
1666 - const char *generic, const char *rerolled)
1665 +static const char *diff_title(struct strbuf *sb,
1666 + const char *reroll_count,
1667 + const char *generic,
1668 + const char *rerolled)
1669 {
1668 - if (reroll_count <= 0)
1670 + int v;
1671 +
1672 + /* RFC may be v0, so allow -v1 to diff against v0 */
1673 + if (reroll_count && !strtol_i(reroll_count, 10, &v) &&
1674 + v >= 1)
1675 + strbuf_addf(sb, rerolled, v - 1);
1676 + else
1677 strbuf_addstr(sb, generic);
1670 - else /* RFC may be v0, so allow -v1 to diff against v0 */
1671 - strbuf_addf(sb, rerolled, reroll_count - 1);
1678 return sb->buf;
1679 }
1680
@@ -1717,7 +1723,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1723 struct strbuf buf = STRBUF_INIT;
1724 int use_patch_format = 0;
1725 int quiet = 0;
1720 - int reroll_count = -1;
1726 + const char *reroll_count = NULL;
1727 char *cover_from_description_arg = NULL;
1728 char *branch_name = NULL;
1729 char *base_commit = NULL;
@@ -1751,7 +1757,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1757 N_("use <sfx> instead of '.patch'")),
1758 OPT_INTEGER(0, "start-number", &start_number,
1759 N_("start numbering patches at <n> instead of 1")),
1754 - OPT_INTEGER('v', "reroll-count", &reroll_count,
1760 + OPT_STRING('v', "reroll-count", &reroll_count, N_("reroll-count"),
1761 N_("mark the series as Nth re-roll")),
1762 OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max,
1763 N_("max length of output filename")),
@@ -1862,9 +1868,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1868 if (cover_from_description_arg)
1869 cover_from_description_mode = parse_cover_from_description(cover_from_description_arg);
1870
1865 - if (0 < reroll_count) {
1871 + if (reroll_count) {
1872 struct strbuf sprefix = STRBUF_INIT;
1867 - strbuf_addf(&sprefix, "%s v%d",
1873 +
1874 + strbuf_addf(&sprefix, "%s v%s",
1875 rev.subject_prefix, reroll_count);
1876 rev.reroll_count = reroll_count;
1877 rev.subject_prefix = strbuf_detach(&sprefix, NULL);
log-tree.c
+8 -2
@@ -369,8 +369,14 @@ void fmt_output_subject(struct strbuf *filename,
369 int start_len = filename->len;
370 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
371
372 - if (0 < info->reroll_count)
373 - strbuf_addf(filename, "v%d-", info->reroll_count);
372 + if (info->reroll_count) {
373 + struct strbuf temp = STRBUF_INIT;
374 +
375 + strbuf_addf(&temp, "v%s", info->reroll_count);
376 + format_sanitized_subject(filename, temp.buf, temp.len);
377 + strbuf_addstr(filename, "-");
378 + strbuf_release(&temp);
379 + }
380 strbuf_addf(filename, "%04d-%s", nr, subject);
381
382 if (max_len < filename->len)
revision.h
+1 -1
@@ -235,7 +235,7 @@ struct rev_info {
235 const char *mime_boundary;
236 const char *patch_suffix;
237 int numbered_files;
238 - int reroll_count;
238 + const char *reroll_count;
239 char *message_id;
240 struct ident_split from_ident;
241 struct string_list *ref_message_ids;
t/t3206-range-diff.sh
+24
@@ -521,6 +521,30 @@ test_expect_success 'format-patch --range-diff as commentary' '
521 grep "> 1: .* new message" 0001-*
522 '
523
524 +test_expect_success 'format-patch --range-diff reroll-count with a non-integer' '
525 + git format-patch --range-diff=HEAD~1 -v2.9 HEAD~1 >actual &&
526 + test_when_finished "rm v2.9-0001-*" &&
527 + test_line_count = 1 actual &&
528 + test_i18ngrep "^Range-diff:$" v2.9-0001-* &&
529 + grep "> 1: .* new message" v2.9-0001-*
530 +'
531 +
532 +test_expect_success 'format-patch --range-diff reroll-count with a integer' '
533 + git format-patch --range-diff=HEAD~1 -v2 HEAD~1 >actual &&
534 + test_when_finished "rm v2-0001-*" &&
535 + test_line_count = 1 actual &&
536 + test_i18ngrep "^Range-diff ..* v1:$" v2-0001-* &&
537 + grep "> 1: .* new message" v2-0001-*
538 +'
539 +
540 +test_expect_success 'format-patch --range-diff with v0' '
541 + git format-patch --range-diff=HEAD~1 -v0 HEAD~1 >actual &&
542 + test_when_finished "rm v0-0001-*" &&
543 + test_line_count = 1 actual &&
544 + test_i18ngrep "^Range-diff:$" v0-0001-* &&
545 + grep "> 1: .* new message" v0-0001-*
546 +'
547 +
548 test_expect_success 'range-diff overrides diff.noprefix internally' '
549 git -c diff.noprefix=true range-diff HEAD^...
550 '
t/t4014-format-patch.sh
+34
@@ -386,6 +386,30 @@ test_expect_success 'reroll count (-v)' '
386 ! grep -v "^Subject: \[PATCH v4 [0-3]/3\] " subjects
387 '
388
389 +test_expect_success 'reroll count (-v) with a fractional number' '
390 + rm -fr patches &&
391 + git format-patch -o patches --cover-letter -v4.4 main..side >list &&
392 + ! grep -v "^patches/v4.4-000[0-3]-" list &&
393 + sed -n -e "/^Subject: /p" $(cat list) >subjects &&
394 + ! grep -v "^Subject: \[PATCH v4.4 [0-3]/3\] " subjects
395 +'
396 +
397 +test_expect_success 'reroll (-v) count with a non number' '
398 + rm -fr patches &&
399 + git format-patch -o patches --cover-letter -v4rev2 main..side >list &&
400 + ! grep -v "^patches/v4rev2-000[0-3]-" list &&
401 + sed -n -e "/^Subject: /p" $(cat list) >subjects &&
402 + ! grep -v "^Subject: \[PATCH v4rev2 [0-3]/3\] " subjects
403 +'
404 +
405 +test_expect_success 'reroll (-v) count with a non-pathname character' '
406 + rm -fr patches &&
407 + git format-patch -o patches --cover-letter -v4---..././../--1/.2// main..side >list &&
408 + ! grep -v "patches/v4-\.-\.-\.-1-\.2-000[0-3]-" list &&
409 + sed -n -e "/^Subject: /p" $(cat list) >subjects &&
410 + ! grep -v "^Subject: \[PATCH v4---\.\.\./\./\.\./--1/\.2// [0-3]/3\] " subjects
411 +'
412 +
413 check_threading () {
414 expect="$1" &&
415 shift &&
@@ -2255,6 +2279,16 @@ test_expect_success 'interdiff: reroll-count' '
2279 test_i18ngrep "^Interdiff ..* v1:$" v2-0000-cover-letter.patch
2280 '
2281
2282 +test_expect_success 'interdiff: reroll-count with a non-integer' '
2283 + git format-patch --cover-letter --interdiff=boop~2 -v2.2 -1 boop &&
2284 + test_i18ngrep "^Interdiff:$" v2.2-0000-cover-letter.patch
2285 +'
2286 +
2287 +test_expect_success 'interdiff: reroll-count with a integer' '
2288 + git format-patch --cover-letter --interdiff=boop~2 -v2 -1 boop &&
2289 + test_i18ngrep "^Interdiff ..* v1:$" v2-0000-cover-letter.patch
2290 +'
2291 +
2292 test_expect_success 'interdiff: solo-patch' '
2293 cat >expect <<-\EOF &&
2294 +fleep