Skip to content

Commit e0d7db7

Browse files
ddevaultgitster
authored andcommitted
format-patch: --rfc honors what --subject-prefix sets
Rather than replacing the configured subject prefix (either through the git config or command line) entirely with "RFC PATCH", this change prepends RFC to whatever subject prefix was already in use. This is useful, for example, when a user is working on a repository that has a subject prefix considered to disambiguate patches: git config format.subjectPrefix 'PATCH my-project' Prior to this change, formatting patches with --rfc would lose the 'my-project' information. The data flow for the subject-prefix was that rev.subject_prefix were to be kept the authoritative version of the subject prefix even while parsing command line options, and sprefix variable was used as a temporary area to futz with it. Now, the parsing code has been refactored to build the subject prefix into the sprefix variable and assigns its value at the end to rev.subject_prefix, which makes the flow easier to grasp. Signed-off-by: Drew DeVault <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5dc72c0 commit e0d7db7

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

Documentation/git-format-patch.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,15 @@ populated with placeholder text.
217217

218218
--subject-prefix=<subject prefix>::
219219
Instead of the standard '[PATCH]' prefix in the subject
220-
line, instead use '[<subject prefix>]'. This
221-
allows for useful naming of a patch series, and can be
222-
combined with the `--numbered` option.
220+
line, instead use '[<subject prefix>]'. This can be used
221+
to name a patch series, and can be combined with the
222+
`--numbered` option.
223+
+
224+
The configuration variable `format.subjectPrefix` may also be used
225+
to configure a subject prefix to apply to a given repository for
226+
all patches. This is often useful on mailing lists which receive
227+
patches for several repositories and can be used to disambiguate
228+
the patches (with a value of e.g. "PATCH my-project").
223229

224230
--filename-max-length=<n>::
225231
Instead of the standard 64 bytes, chomp the generated output
@@ -229,9 +235,9 @@ populated with placeholder text.
229235
variable, or 64 if unconfigured.
230236

231237
--rfc::
232-
Alias for `--subject-prefix="RFC PATCH"`. RFC means "Request For
233-
Comments"; use this when sending an experimental patch for
234-
discussion rather than application.
238+
Prepends "RFC" to the subject prefix (producing "RFC PATCH" by
239+
default). RFC means "Request For Comments"; use this when sending
240+
an experimental patch for discussion rather than application.
235241

236242
-v <n>::
237243
--reroll-count=<n>::

builtin/log.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,19 +1468,16 @@ static int subject_prefix = 0;
14681468
static int subject_prefix_callback(const struct option *opt, const char *arg,
14691469
int unset)
14701470
{
1471+
struct strbuf *sprefix;
1472+
14711473
BUG_ON_OPT_NEG(unset);
1474+
sprefix = opt->value;
14721475
subject_prefix = 1;
1473-
((struct rev_info *)opt->value)->subject_prefix = arg;
1476+
strbuf_reset(sprefix);
1477+
strbuf_addstr(sprefix, arg);
14741478
return 0;
14751479
}
14761480

1477-
static int rfc_callback(const struct option *opt, const char *arg, int unset)
1478-
{
1479-
BUG_ON_OPT_NEG(unset);
1480-
BUG_ON_OPT_ARG(arg);
1481-
return subject_prefix_callback(opt, "RFC PATCH", unset);
1482-
}
1483-
14841481
static int numbered_cmdline_opt = 0;
14851482

14861483
static int numbered_callback(const struct option *opt, const char *arg,
@@ -1907,6 +1904,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
19071904
struct strbuf rdiff_title = STRBUF_INIT;
19081905
struct strbuf sprefix = STRBUF_INIT;
19091906
int creation_factor = -1;
1907+
int rfc = 0;
19101908

19111909
const struct option builtin_format_patch_options[] = {
19121910
OPT_CALLBACK_F('n', "numbered", &numbered, NULL,
@@ -1930,13 +1928,11 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
19301928
N_("mark the series as Nth re-roll")),
19311929
OPT_INTEGER(0, "filename-max-length", &fmt_patch_name_max,
19321930
N_("max length of output filename")),
1933-
OPT_CALLBACK_F(0, "rfc", &rev, NULL,
1934-
N_("use [RFC PATCH] instead of [PATCH]"),
1935-
PARSE_OPT_NOARG | PARSE_OPT_NONEG, rfc_callback),
1931+
OPT_BOOL(0, "rfc", &rfc, N_("use [RFC PATCH] instead of [PATCH]")),
19361932
OPT_STRING(0, "cover-from-description", &cover_from_description_arg,
19371933
N_("cover-from-description-mode"),
19381934
N_("generate parts of a cover letter based on a branch's description")),
1939-
OPT_CALLBACK_F(0, "subject-prefix", &rev, N_("prefix"),
1935+
OPT_CALLBACK_F(0, "subject-prefix", &sprefix, N_("prefix"),
19401936
N_("use [<prefix>] instead of [PATCH]"),
19411937
PARSE_OPT_NONEG, subject_prefix_callback),
19421938
OPT_CALLBACK_F('o', "output-directory", &output_directory,
@@ -2016,11 +2012,11 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
20162012
rev.max_parents = 1;
20172013
rev.diffopt.flags.recursive = 1;
20182014
rev.diffopt.no_free = 1;
2019-
rev.subject_prefix = fmt_patch_subject_prefix;
20202015
memset(&s_r_opt, 0, sizeof(s_r_opt));
20212016
s_r_opt.def = "HEAD";
20222017
s_r_opt.revarg_opt = REVARG_COMMITTISH;
20232018

2019+
strbuf_addstr(&sprefix, fmt_patch_subject_prefix);
20242020
if (format_no_prefix)
20252021
diff_set_noprefix(&rev.diffopt);
20262022

@@ -2048,13 +2044,16 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
20482044
if (cover_from_description_arg)
20492045
cover_from_description_mode = parse_cover_from_description(cover_from_description_arg);
20502046

2047+
if (rfc)
2048+
strbuf_insertstr(&sprefix, 0, "RFC ");
2049+
20512050
if (reroll_count) {
2052-
strbuf_addf(&sprefix, "%s v%s",
2053-
rev.subject_prefix, reroll_count);
2051+
strbuf_addf(&sprefix, " v%s", reroll_count);
20542052
rev.reroll_count = reroll_count;
2055-
rev.subject_prefix = sprefix.buf;
20562053
}
20572054

2055+
rev.subject_prefix = sprefix.buf;
2056+
20582057
for (i = 0; i < extra_hdr.nr; i++) {
20592058
strbuf_addstr(&buf, extra_hdr.items[i].string);
20602059
strbuf_addch(&buf, '\n');

t/t4014-format-patch.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,27 @@ test_expect_success '--rfc' '
13731373
Subject: [RFC PATCH 1/1] header with . in it
13741374
EOF
13751375
git format-patch -n -1 --stdout --rfc >patch &&
1376-
grep ^Subject: patch >actual &&
1376+
grep "^Subject:" patch >actual &&
1377+
test_cmp expect actual
1378+
'
1379+
1380+
test_expect_success '--rfc does not overwrite prefix' '
1381+
cat >expect <<-\EOF &&
1382+
Subject: [RFC PATCH foobar 1/1] header with . in it
1383+
EOF
1384+
git -c format.subjectPrefix="PATCH foobar" \
1385+
format-patch -n -1 --stdout --rfc >patch &&
1386+
grep "^Subject:" patch >actual &&
1387+
test_cmp expect actual
1388+
'
1389+
1390+
test_expect_success '--rfc is argument order independent' '
1391+
cat >expect <<-\EOF &&
1392+
Subject: [RFC PATCH foobar 1/1] header with . in it
1393+
EOF
1394+
git format-patch -n -1 --stdout --rfc \
1395+
--subject-prefix="PATCH foobar" >patch &&
1396+
grep "^Subject:" patch >actual &&
13771397
test_cmp expect actual
13781398
'
13791399

0 commit comments

Comments
 (0)