Skip to content

Commit 55571f7

Browse files
committed
Merge branch 'bg/format-patch-N'
* bg/format-patch-N: Rearrange git-format-patch synopsis to improve clarity. format-patch: Test --[no-]numbered and format.numbered format-patch: Add configuration and off switch for --numbered
2 parents 03edb0a + 6eea60f commit 55571f7

File tree

4 files changed

+139
-5
lines changed

4 files changed

+139
-5
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ fetch.unpackLimit::
432432
pack from a push can make the push operation complete faster,
433433
especially on slow filesystems.
434434

435+
format.numbered::
436+
A boolean which can enable sequence numbers in patch subjects.
437+
Seting this option to "auto" will enable it only if there is
438+
more than one patch. See --numbered option in
439+
gitlink:git-format-patch[1].
440+
435441
format.headers::
436442
Additional email headers to include in a patch to be submitted
437443
by mail. See gitlink:git-format-patch[1].

Documentation/git-format-patch.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ git-format-patch - Prepare patches for e-mail submission
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git-format-patch' [-n | -k] [-o <dir> | --stdout] [--thread]
12+
'git-format-patch' [-k] [-o <dir> | --stdout] [--thread]
1313
[--attach[=<boundary>] | --inline[=<boundary>]]
1414
[-s | --signoff] [<common diff options>]
15+
[-n | --numbered | -N | --no-numbered]
1516
[--start-number <n>] [--numbered-files]
1617
[--in-reply-to=Message-Id] [--suffix=.<sfx>]
1718
[--ignore-if-in-upstream]
@@ -77,6 +78,9 @@ include::diff-options.txt[]
7778
-n|--numbered::
7879
Name output in '[PATCH n/m]' format.
7980

81+
-N|--no-numbered::
82+
Name output in '[PATCH]' format.
83+
8084
--start-number <n>::
8185
Start numbering the patches at <n> instead of 1.
8286

@@ -142,15 +146,16 @@ not add any suffix.
142146

143147
CONFIGURATION
144148
-------------
145-
You can specify extra mail header lines to be added to each
146-
message in the repository configuration. You can also specify
147-
new defaults for the subject prefix and file suffix.
149+
You can specify extra mail header lines to be added to each message
150+
in the repository configuration, new defaults for the subject prefix
151+
and file suffix, and number patches when outputting more than one.
148152

149153
------------
150154
[format]
151155
headers = "Organization: git-foo\n"
152156
subjectprefix = CHANGE
153157
suffix = .txt
158+
numbered = auto
154159
------------
155160

156161

builtin-log.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ static int istitlechar(char c)
273273
static char *extra_headers = NULL;
274274
static int extra_headers_size = 0;
275275
static const char *fmt_patch_suffix = ".patch";
276+
static int numbered = 0;
277+
static int auto_number = 0;
276278

277279
static int git_format_config(const char *var, const char *value)
278280
{
@@ -297,6 +299,15 @@ static int git_format_config(const char *var, const char *value)
297299
if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
298300
return 0;
299301
}
302+
if (!strcmp(var, "format.numbered")) {
303+
if (!strcasecmp(value, "auto")) {
304+
auto_number = 1;
305+
return 0;
306+
}
307+
308+
numbered = git_config_bool(var, value);
309+
return 0;
310+
}
300311

301312
return git_log_config(var, value);
302313
}
@@ -466,7 +477,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
466477
struct rev_info rev;
467478
int nr = 0, total, i, j;
468479
int use_stdout = 0;
469-
int numbered = 0;
470480
int start_number = -1;
471481
int keep_subject = 0;
472482
int numbered_files = 0; /* _just_ numbers */
@@ -503,6 +513,11 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
503513
else if (!strcmp(argv[i], "-n") ||
504514
!strcmp(argv[i], "--numbered"))
505515
numbered = 1;
516+
else if (!strcmp(argv[i], "-N") ||
517+
!strcmp(argv[i], "--no-numbered")) {
518+
numbered = 0;
519+
auto_number = 0;
520+
}
506521
else if (!prefixcmp(argv[i], "--start-number="))
507522
start_number = strtol(argv[i] + 15, NULL, 10);
508523
else if (!strcmp(argv[i], "--numbered-files"))
@@ -642,6 +657,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
642657
list[nr - 1] = commit;
643658
}
644659
total = nr;
660+
if (!keep_subject && auto_number && total > 1)
661+
numbered = 1;
645662
if (numbered)
646663
rev.total = total + start_number - 1;
647664
rev.add_signoff = add_signoff;

t/t4021-format-patch-numbered.sh

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2006 Brian C Gernhardt
4+
#
5+
6+
test_description='Format-patch numbering options'
7+
8+
. ./test-lib.sh
9+
10+
test_expect_success setup '
11+
12+
echo A > file &&
13+
git add file &&
14+
git commit -m First &&
15+
16+
echo B >> file &&
17+
git commit -a -m Second &&
18+
19+
echo C >> file &&
20+
git commit -a -m Third
21+
22+
'
23+
24+
# Each of these gets used multiple times.
25+
26+
test_num_no_numbered() {
27+
cnt=$(grep "^Subject: \[PATCH\]" $1 | wc -l) &&
28+
test $cnt = $2
29+
}
30+
31+
test_single_no_numbered() {
32+
test_num_no_numbered $1 1
33+
}
34+
35+
test_no_numbered() {
36+
test_num_no_numbered $1 2
37+
}
38+
39+
test_single_numbered() {
40+
grep "^Subject: \[PATCH 1/1\]" $1
41+
}
42+
43+
test_numbered() {
44+
grep "^Subject: \[PATCH 1/2\]" $1 &&
45+
grep "^Subject: \[PATCH 2/2\]" $1
46+
}
47+
48+
test_expect_success 'Default: no numbered' '
49+
50+
git format-patch --stdout HEAD~2 >patch0 &&
51+
test_no_numbered patch0
52+
53+
'
54+
55+
test_expect_success 'Use --numbered' '
56+
57+
git format-patch --numbered --stdout HEAD~2 >patch1 &&
58+
test_numbered patch1
59+
60+
'
61+
62+
test_expect_success 'format.numbered = true' '
63+
64+
git config format.numbered true &&
65+
git format-patch --stdout HEAD~2 >patch2 &&
66+
test_numbered patch2
67+
68+
'
69+
70+
test_expect_success 'format.numbered && single patch' '
71+
72+
git format-patch --stdout HEAD^ > patch3 &&
73+
test_single_numbered patch3
74+
75+
'
76+
77+
test_expect_success 'format.numbered && --no-numbered' '
78+
79+
git format-patch --no-numbered --stdout HEAD~2 >patch4 &&
80+
test_no_numbered patch4
81+
82+
'
83+
84+
test_expect_success 'format.numbered = auto' '
85+
86+
git config format.numbered auto
87+
git format-patch --stdout HEAD~2 > patch5 &&
88+
test_numbered patch5
89+
90+
'
91+
92+
test_expect_success 'format.numbered = auto && single patch' '
93+
94+
git format-patch --stdout HEAD^ > patch6 &&
95+
test_single_no_numbered patch6
96+
97+
'
98+
99+
test_expect_success 'format.numbered = auto && --no-numbered' '
100+
101+
git format-patch --no-numbered --stdout HEAD~2 > patch7 &&
102+
test_no_numbered patch7
103+
104+
'
105+
106+
test_done

0 commit comments

Comments
 (0)