Skip to content

Commit bfd91b4

Browse files
committed
Merge branch 'pb/trailers-from-command-line'
"git interpret-trailers" learned to take the trailer specifications from the command line that overrides the configured values. * pb/trailers-from-command-line: interpret-trailers: fix documentation typo interpret-trailers: add options for actions trailers: introduce struct new_trailer_item trailers: export action enums and corresponding lookup functions
2 parents 0b96358 + c88bf54 commit bfd91b4

File tree

5 files changed

+274
-53
lines changed

5 files changed

+274
-53
lines changed

Documentation/git-interpret-trailers.txt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,29 @@ OPTIONS
8080
trailer to the input messages. See the description of this
8181
command.
8282

83+
--where <placement>::
84+
--no-where::
85+
Specify where all new trailers will be added. A setting
86+
provided with '--where' overrides all configuration variables
87+
and applies to all '--trailer' options until the next occurrence of
88+
'--where' or '--no-where'.
89+
90+
--if-exists <action>::
91+
--no-if-exists::
92+
Specify what action will be performed when there is already at
93+
least one trailer with the same <token> in the message. A setting
94+
provided with '--if-exists' overrides all configuration variables
95+
and applies to all '--trailer' options until the next occurrence of
96+
'--if-exists' or '--no-if-exists'.
97+
98+
--if-missing <action>::
99+
--no-if-missing::
100+
Specify what action will be performed when there is no other
101+
trailer with the same <token> in the message. A setting
102+
provided with '--if-missing' overrides all configuration variables
103+
and applies to all '--trailer' options until the next occurrence of
104+
'--if-missing' or '--no-if-missing'.
105+
83106
CONFIGURATION VARIABLES
84107
-----------------------
85108

@@ -170,8 +193,8 @@ trailer.<token>.where::
170193
configuration variable and it overrides what is specified by
171194
that option for trailers with the specified <token>.
172195

173-
trailer.<token>.ifexist::
174-
This option takes the same values as the 'trailer.ifexist'
196+
trailer.<token>.ifexists::
197+
This option takes the same values as the 'trailer.ifexists'
175198
configuration variable and it overrides what is specified by
176199
that option for trailers with the specified <token>.
177200

builtin/interpret-trailers.c

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,82 @@ static const char * const git_interpret_trailers_usage[] = {
1616
NULL
1717
};
1818

19+
static enum trailer_where where;
20+
static enum trailer_if_exists if_exists;
21+
static enum trailer_if_missing if_missing;
22+
23+
static int option_parse_where(const struct option *opt,
24+
const char *arg, int unset)
25+
{
26+
return trailer_set_where(&where, arg);
27+
}
28+
29+
static int option_parse_if_exists(const struct option *opt,
30+
const char *arg, int unset)
31+
{
32+
return trailer_set_if_exists(&if_exists, arg);
33+
}
34+
35+
static int option_parse_if_missing(const struct option *opt,
36+
const char *arg, int unset)
37+
{
38+
return trailer_set_if_missing(&if_missing, arg);
39+
}
40+
41+
static void new_trailers_clear(struct list_head *trailers)
42+
{
43+
struct list_head *pos, *tmp;
44+
struct new_trailer_item *item;
45+
46+
list_for_each_safe(pos, tmp, trailers) {
47+
item = list_entry(pos, struct new_trailer_item, list);
48+
list_del(pos);
49+
free(item);
50+
}
51+
}
52+
53+
static int option_parse_trailer(const struct option *opt,
54+
const char *arg, int unset)
55+
{
56+
struct list_head *trailers = opt->value;
57+
struct new_trailer_item *item;
58+
59+
if (unset) {
60+
new_trailers_clear(trailers);
61+
return 0;
62+
}
63+
64+
if (!arg)
65+
return -1;
66+
67+
item = xmalloc(sizeof(*item));
68+
item->text = arg;
69+
item->where = where;
70+
item->if_exists = if_exists;
71+
item->if_missing = if_missing;
72+
list_add_tail(&item->list, trailers);
73+
return 0;
74+
}
75+
1976
int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
2077
{
2178
int in_place = 0;
2279
int trim_empty = 0;
23-
struct string_list trailers = STRING_LIST_INIT_NODUP;
80+
LIST_HEAD(trailers);
2481

2582
struct option options[] = {
2683
OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")),
2784
OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
28-
OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
29-
N_("trailer(s) to add")),
85+
86+
OPT_CALLBACK(0, "where", NULL, N_("action"),
87+
N_("where to place the new trailer"), option_parse_where),
88+
OPT_CALLBACK(0, "if-exists", NULL, N_("action"),
89+
N_("action if trailer already exists"), option_parse_if_exists),
90+
OPT_CALLBACK(0, "if-missing", NULL, N_("action"),
91+
N_("action if trailer is missing"), option_parse_if_missing),
92+
93+
OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
94+
N_("trailer(s) to add"), option_parse_trailer),
3095
OPT_END()
3196
};
3297

@@ -43,7 +108,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
43108
process_trailers(NULL, in_place, trim_empty, &trailers);
44109
}
45110

46-
string_list_clear(&trailers, 0);
111+
new_trailers_clear(&trailers);
47112

48113
return 0;
49114
}

t/t7513-interpret-trailers.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,36 @@ test_expect_success 'using "where = before"' '
681681
test_cmp expected actual
682682
'
683683

684+
test_expect_success 'overriding configuration with "--where after"' '
685+
git config trailer.ack.where "before" &&
686+
cat complex_message_body >expected &&
687+
sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
688+
Fixes: Z
689+
Acked-by= Z
690+
Acked-by= Peff
691+
Reviewed-by: Z
692+
Signed-off-by: Z
693+
EOF
694+
git interpret-trailers --where after --trailer "ack: Peff" \
695+
complex_message >actual &&
696+
test_cmp expected actual
697+
'
698+
699+
test_expect_success 'using "where = before" with "--no-where"' '
700+
cat complex_message_body >expected &&
701+
sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
702+
Bug #42
703+
Fixes: Z
704+
Acked-by= Peff
705+
Acked-by= Z
706+
Reviewed-by: Z
707+
Signed-off-by: Z
708+
EOF
709+
git interpret-trailers --where after --no-where --trailer "ack: Peff" \
710+
--trailer "bug: 42" complex_message >actual &&
711+
test_cmp expected actual
712+
'
713+
684714
test_expect_success 'using "where = after"' '
685715
git config trailer.ack.where "after" &&
686716
cat complex_message_body >expected &&
@@ -947,6 +977,23 @@ test_expect_success 'using "ifExists = add" with "where = after"' '
947977
test_cmp expected actual
948978
'
949979

980+
test_expect_success 'overriding configuration with "--if-exists replace"' '
981+
git config trailer.fix.key "Fixes: " &&
982+
git config trailer.fix.ifExists "add" &&
983+
cat complex_message_body >expected &&
984+
sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
985+
Bug #42
986+
Acked-by= Z
987+
Reviewed-by:
988+
Signed-off-by: Z
989+
Fixes: 22
990+
EOF
991+
git interpret-trailers --if-exists replace --trailer "review:" \
992+
--trailer "fix=53" --trailer "fix=22" --trailer "bug: 42" \
993+
<complex_message >actual &&
994+
test_cmp expected actual
995+
'
996+
950997
test_expect_success 'using "ifExists = replace"' '
951998
git config trailer.fix.key "Fixes: " &&
952999
git config trailer.fix.ifExists "replace" &&
@@ -1026,6 +1073,25 @@ test_expect_success 'the default is "ifMissing = add"' '
10261073
test_cmp expected actual
10271074
'
10281075

1076+
test_expect_success 'overriding configuration with "--if-missing doNothing"' '
1077+
git config trailer.ifmissing "add" &&
1078+
cat complex_message_body >expected &&
1079+
sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
1080+
Fixes: Z
1081+
Acked-by= Z
1082+
Acked-by= Junio
1083+
Acked-by= Peff
1084+
Reviewed-by:
1085+
Signed-off-by: Z
1086+
EOF
1087+
git interpret-trailers --if-missing doNothing \
1088+
--trailer "review:" --trailer "fix=53" \
1089+
--trailer "cc=Linus" --trailer "ack: Junio" \
1090+
--trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
1091+
<complex_message >actual &&
1092+
test_cmp expected actual
1093+
'
1094+
10291095
test_expect_success 'when default "ifMissing" is "doNothing"' '
10301096
git config trailer.ifmissing "doNothing" &&
10311097
cat complex_message_body >expected &&

0 commit comments

Comments
 (0)