]> git.ipfire.org Git - thirdparty/git.git/blame - parse-options-cb.c
treewide: be explicit about dependence on gettext.h
[thirdparty/git.git] / parse-options-cb.c
CommitLineData
06876284
DI
1#include "git-compat-util.h"
2#include "parse-options.h"
d3115660 3#include "branch.h"
06876284
DI
4#include "cache.h"
5#include "commit.h"
6#include "color.h"
f394e093 7#include "gettext.h"
06876284 8#include "string-list.h"
dbbcd44f 9#include "strvec.h"
fe299ec5 10#include "oid-array.h"
06876284
DI
11
12/*----- some often used options -----*/
13
14int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
15{
16 int v;
17
18 if (!arg) {
19 v = unset ? 0 : DEFAULT_ABBREV;
20 } else {
f7e68a08
NTND
21 if (!*arg)
22 return error(_("option `%s' expects a numerical value"),
23 opt->long_name);
06876284
DI
24 v = strtol(arg, (char **)&arg, 10);
25 if (*arg)
9440b831
NTND
26 return error(_("option `%s' expects a numerical value"),
27 opt->long_name);
06876284
DI
28 if (v && v < MINIMUM_ABBREV)
29 v = MINIMUM_ABBREV;
d8774183
NTND
30 else if (v > the_hash_algo->hexsz)
31 v = the_hash_algo->hexsz;
06876284
DI
32 }
33 *(int *)(opt->value) = v;
34 return 0;
35}
36
27ec394a
JH
37int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
38 int unset)
39{
8ab5aa4b
JH
40 if (unset)
41 arg = "never";
42 if (parse_expiry_date(arg, (timestamp_t *)opt->value))
43 die(_("malformed expiration date '%s'"), arg);
44 return 0;
27ec394a
JH
45}
46
06876284
DI
47int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
48 int unset)
49{
50 int value;
51
52 if (!arg)
53 arg = unset ? "never" : (const char *)opt->defval;
f946b465 54 value = git_config_colorbool(NULL, arg);
06876284 55 if (value < 0)
9440b831
NTND
56 return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
57 opt->long_name);
06876284
DI
58 *(int *)opt->value = value;
59 return 0;
60}
61
62int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
63 int unset)
64{
65 int *target = opt->value;
66
517fe807
JK
67 BUG_ON_OPT_ARG(arg);
68
06876284
DI
69 if (unset)
70 /* --no-quiet, --no-verbose */
71 *target = 0;
72 else if (opt->short_name == 'v') {
73 if (*target >= 0)
74 (*target)++;
75 else
76 *target = 1;
77 } else {
78 if (*target <= 0)
79 (*target)--;
80 else
81 *target = -1;
82 }
83 return 0;
84}
85
9d306b5a 86int parse_opt_commits(const struct option *opt, const char *arg, int unset)
06876284 87{
9e31eafe 88 struct object_id oid;
06876284
DI
89 struct commit *commit;
90
517fe807
JK
91 BUG_ON_OPT_NEG(unset);
92
06876284
DI
93 if (!arg)
94 return -1;
9e31eafe 95 if (get_oid(arg, &oid))
06876284 96 return error("malformed object name %s", arg);
2122f675 97 commit = lookup_commit_reference(the_repository, &oid);
06876284
DI
98 if (!commit)
99 return error("no such commit %s", arg);
100 commit_list_insert(commit, opt->value);
101 return 0;
102}
103
7d3488eb
PW
104int parse_opt_commit(const struct option *opt, const char *arg, int unset)
105{
106 struct object_id oid;
107 struct commit *commit;
108 struct commit **target = opt->value;
109
8d2aa8df
JK
110 BUG_ON_OPT_NEG(unset);
111
7d3488eb
PW
112 if (!arg)
113 return -1;
114 if (get_oid(arg, &oid))
115 return error("malformed object name %s", arg);
116 commit = lookup_commit_reference(the_repository, &oid);
117 if (!commit)
118 return error("no such commit %s", arg);
119 *target = commit;
120 return 0;
121}
122
b2172fdf
KN
123int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
124{
9e1d7087 125 struct object_id oid;
b2172fdf
KN
126
127 if (unset) {
910650d2 128 oid_array_clear(opt->value);
b2172fdf
KN
129 return 0;
130 }
131 if (!arg)
132 return -1;
9e1d7087 133 if (get_oid(arg, &oid))
b2172fdf 134 return error(_("malformed object name '%s'"), arg);
910650d2 135 oid_array_append(opt->value, &oid);
b2172fdf
KN
136 return 0;
137}
138
33898531
PW
139int parse_opt_object_id(const struct option *opt, const char *arg, int unset)
140{
141 struct object_id oid;
142 struct object_id *target = opt->value;
143
144 if (unset) {
14228447 145 oidcpy(target, null_oid());
33898531
PW
146 return 0;
147 }
148 if (!arg)
149 return -1;
150 if (get_oid(arg, &oid))
151 return error(_("malformed object name '%s'"), arg);
152 *target = oid;
153 return 0;
154}
155
06876284
DI
156int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
157{
158 int *target = opt->value;
517fe807
JK
159
160 BUG_ON_OPT_ARG(arg);
161
06876284
DI
162 *target = unset ? 2 : 1;
163 return 0;
164}
165
f904f902
RS
166static size_t parse_options_count(const struct option *opt)
167{
168 size_t n = 0;
169
170 for (; opt && opt->type != OPTION_END; opt++)
171 n++;
172 return n;
173}
174
20871822
NTND
175struct option *parse_options_dup(const struct option *o)
176{
7a9f8ca8
RS
177 struct option no_options[] = { OPT_END() };
178
179 return parse_options_concat(o, no_options);
20871822
NTND
180}
181
c8407857
RS
182struct option *parse_options_concat(const struct option *a,
183 const struct option *b)
06876284 184{
023ff39b 185 struct option *ret;
f904f902
RS
186 size_t a_len = parse_options_count(a);
187 size_t b_len = parse_options_count(b);
023ff39b
JK
188
189 ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
a277d0a6
RS
190 COPY_ARRAY(ret, a, a_len);
191 COPY_ARRAY(ret + a_len, b, b_len + 1); /* + 1 for final OPTION_END */
023ff39b
JK
192
193 return ret;
06876284
DI
194}
195
196int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
197{
198 struct string_list *v = opt->value;
199
200 if (unset) {
201 string_list_clear(v, 0);
202 return 0;
203 }
204
205 if (!arg)
206 return -1;
207
7a7a517a 208 string_list_append(v, arg);
06876284
DI
209 return 0;
210}
6acec038
RS
211
212int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
213{
214 return 0;
215}
6b3ee18d 216
ce564eb1
MH
217/**
218 * Report that the option is unknown, so that other code can handle
219 * it. This can be used as a callback together with
220 * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the
221 * "-h" output even if it's not being handled directly by
222 * parse_options().
223 */
f41179f1 224enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
3ebbe289
NTND
225 const struct option *opt,
226 const char *arg, int unset)
ce564eb1 227{
3ebbe289 228 BUG_ON_OPT_ARG(arg);
f41179f1 229 return PARSE_OPT_UNKNOWN;
ce564eb1
MH
230}
231
6b3ee18d
PT
232/**
233 * Recreates the command-line option in the strbuf.
234 */
235static int recreate_opt(struct strbuf *sb, const struct option *opt,
236 const char *arg, int unset)
237{
238 strbuf_reset(sb);
239
240 if (opt->long_name) {
241 strbuf_addstr(sb, unset ? "--no-" : "--");
242 strbuf_addstr(sb, opt->long_name);
243 if (arg) {
244 strbuf_addch(sb, '=');
245 strbuf_addstr(sb, arg);
246 }
247 } else if (opt->short_name && !unset) {
248 strbuf_addch(sb, '-');
249 strbuf_addch(sb, opt->short_name);
250 if (arg)
251 strbuf_addstr(sb, arg);
252 } else
253 return -1;
254
255 return 0;
256}
257
258/**
259 * For an option opt, recreates the command-line option in opt->value which
260 * must be an char* initialized to NULL. This is useful when we need to pass
261 * the command-line option to another command. Since any previous value will be
262 * overwritten, this callback should only be used for options where the last
263 * one wins.
264 */
265int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
266{
267 static struct strbuf sb = STRBUF_INIT;
268 char **opt_value = opt->value;
269
270 if (recreate_opt(&sb, opt, arg, unset) < 0)
271 return -1;
272
39ea59a2 273 free(*opt_value);
6b3ee18d
PT
274
275 *opt_value = strbuf_detach(&sb, NULL);
276
277 return 0;
278}
ffad85c5
PT
279
280/**
281 * For an option opt, recreate the command-line option, appending it to
c972bf4c 282 * opt->value which must be a strvec. This is useful when we need to pass
ffad85c5
PT
283 * the command-line option, which can be specified multiple times, to another
284 * command.
285 */
286int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
287{
288 static struct strbuf sb = STRBUF_INIT;
c972bf4c 289 struct strvec *opt_value = opt->value;
ffad85c5
PT
290
291 if (recreate_opt(&sb, opt, arg, unset) < 0)
292 return -1;
293
c972bf4c 294 strvec_push(opt_value, sb.buf);
ffad85c5
PT
295
296 return 0;
297}
d3115660
JS
298
299int parse_opt_tracking_mode(const struct option *opt, const char *arg, int unset)
300{
301 if (unset)
302 *(enum branch_track *)opt->value = BRANCH_TRACK_NEVER;
303 else if (!arg || !strcmp(arg, "direct"))
304 *(enum branch_track *)opt->value = BRANCH_TRACK_EXPLICIT;
305 else if (!strcmp(arg, "inherit"))
306 *(enum branch_track *)opt->value = BRANCH_TRACK_INHERIT;
307 else
308 return error(_("option `%s' expects \"%s\" or \"%s\""),
309 "--track", "direct", "inherit");
310
311 return 0;
312}