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