sequencer: use struct strvec to store merge strategy options
[git/gitster.git] / parse-options-cb.c
blob8eb96c5ca9b6119cc8ba25d2b8db6c259310cb0a
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "branch.h"
4 #include "cache.h"
5 #include "commit.h"
6 #include "color.h"
7 #include "string-list.h"
8 #include "strvec.h"
9 #include "oid-array.h"
11 /*----- some often used options -----*/
13 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
15 int v;
17 if (!arg) {
18 v = unset ? 0 : DEFAULT_ABBREV;
19 } else {
20 if (!*arg)
21 return error(_("option `%s' expects a numerical value"),
22 opt->long_name);
23 v = strtol(arg, (char **)&arg, 10);
24 if (*arg)
25 return error(_("option `%s' expects a numerical value"),
26 opt->long_name);
27 if (v && v < MINIMUM_ABBREV)
28 v = MINIMUM_ABBREV;
29 else if (v > the_hash_algo->hexsz)
30 v = the_hash_algo->hexsz;
32 *(int *)(opt->value) = v;
33 return 0;
36 int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
37 int unset)
39 if (unset)
40 arg = "never";
41 if (parse_expiry_date(arg, (timestamp_t *)opt->value))
42 die(_("malformed expiration date '%s'"), arg);
43 return 0;
46 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
47 int unset)
49 int value;
51 if (!arg)
52 arg = unset ? "never" : (const char *)opt->defval;
53 value = git_config_colorbool(NULL, arg);
54 if (value < 0)
55 return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
56 opt->long_name);
57 *(int *)opt->value = value;
58 return 0;
61 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
62 int unset)
64 int *target = opt->value;
66 BUG_ON_OPT_ARG(arg);
68 if (unset)
69 /* --no-quiet, --no-verbose */
70 *target = 0;
71 else if (opt->short_name == 'v') {
72 if (*target >= 0)
73 (*target)++;
74 else
75 *target = 1;
76 } else {
77 if (*target <= 0)
78 (*target)--;
79 else
80 *target = -1;
82 return 0;
85 int parse_opt_commits(const struct option *opt, const char *arg, int unset)
87 struct object_id oid;
88 struct commit *commit;
90 BUG_ON_OPT_NEG(unset);
92 if (!arg)
93 return -1;
94 if (get_oid(arg, &oid))
95 return error("malformed object name %s", arg);
96 commit = lookup_commit_reference(the_repository, &oid);
97 if (!commit)
98 return error("no such commit %s", arg);
99 commit_list_insert(commit, opt->value);
100 return 0;
103 int parse_opt_commit(const struct option *opt, const char *arg, int unset)
105 struct object_id oid;
106 struct commit *commit;
107 struct commit **target = opt->value;
109 BUG_ON_OPT_NEG(unset);
111 if (!arg)
112 return -1;
113 if (get_oid(arg, &oid))
114 return error("malformed object name %s", arg);
115 commit = lookup_commit_reference(the_repository, &oid);
116 if (!commit)
117 return error("no such commit %s", arg);
118 *target = commit;
119 return 0;
122 int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
124 struct object_id oid;
126 if (unset) {
127 oid_array_clear(opt->value);
128 return 0;
130 if (!arg)
131 return -1;
132 if (get_oid(arg, &oid))
133 return error(_("malformed object name '%s'"), arg);
134 oid_array_append(opt->value, &oid);
135 return 0;
138 int parse_opt_object_id(const struct option *opt, const char *arg, int unset)
140 struct object_id oid;
141 struct object_id *target = opt->value;
143 if (unset) {
144 oidcpy(target, null_oid());
145 return 0;
147 if (!arg)
148 return -1;
149 if (get_oid(arg, &oid))
150 return error(_("malformed object name '%s'"), arg);
151 *target = oid;
152 return 0;
155 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
157 int *target = opt->value;
159 BUG_ON_OPT_ARG(arg);
161 *target = unset ? 2 : 1;
162 return 0;
165 static size_t parse_options_count(const struct option *opt)
167 size_t n = 0;
169 for (; opt && opt->type != OPTION_END; opt++)
170 n++;
171 return n;
174 struct option *parse_options_dup(const struct option *o)
176 struct option no_options[] = { OPT_END() };
178 return parse_options_concat(o, no_options);
181 struct option *parse_options_concat(const struct option *a,
182 const struct option *b)
184 struct option *ret;
185 size_t a_len = parse_options_count(a);
186 size_t b_len = parse_options_count(b);
188 ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
189 COPY_ARRAY(ret, a, a_len);
190 COPY_ARRAY(ret + a_len, b, b_len + 1); /* + 1 for final OPTION_END */
192 return ret;
195 int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
197 struct string_list *v = opt->value;
199 if (unset) {
200 string_list_clear(v, 0);
201 return 0;
204 if (!arg)
205 return -1;
207 string_list_append(v, arg);
208 return 0;
211 int parse_opt_strvec(const struct option *opt, const char *arg, int unset)
213 struct strvec *v = opt->value;
215 if (unset) {
216 strvec_clear(v);
217 return 0;
220 if (!arg)
221 return -1;
223 strvec_push(v, arg);
224 return 0;
227 int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
229 return 0;
233 * Report that the option is unknown, so that other code can handle
234 * it. This can be used as a callback together with
235 * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the
236 * "-h" output even if it's not being handled directly by
237 * parse_options().
239 enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
240 const struct option *opt,
241 const char *arg, int unset)
243 BUG_ON_OPT_ARG(arg);
244 return PARSE_OPT_UNKNOWN;
248 * Recreates the command-line option in the strbuf.
250 static int recreate_opt(struct strbuf *sb, const struct option *opt,
251 const char *arg, int unset)
253 strbuf_reset(sb);
255 if (opt->long_name) {
256 strbuf_addstr(sb, unset ? "--no-" : "--");
257 strbuf_addstr(sb, opt->long_name);
258 if (arg) {
259 strbuf_addch(sb, '=');
260 strbuf_addstr(sb, arg);
262 } else if (opt->short_name && !unset) {
263 strbuf_addch(sb, '-');
264 strbuf_addch(sb, opt->short_name);
265 if (arg)
266 strbuf_addstr(sb, arg);
267 } else
268 return -1;
270 return 0;
274 * For an option opt, recreates the command-line option in opt->value which
275 * must be an char* initialized to NULL. This is useful when we need to pass
276 * the command-line option to another command. Since any previous value will be
277 * overwritten, this callback should only be used for options where the last
278 * one wins.
280 int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
282 static struct strbuf sb = STRBUF_INIT;
283 char **opt_value = opt->value;
285 if (recreate_opt(&sb, opt, arg, unset) < 0)
286 return -1;
288 free(*opt_value);
290 *opt_value = strbuf_detach(&sb, NULL);
292 return 0;
296 * For an option opt, recreate the command-line option, appending it to
297 * opt->value which must be a strvec. This is useful when we need to pass
298 * the command-line option, which can be specified multiple times, to another
299 * command.
301 int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
303 static struct strbuf sb = STRBUF_INIT;
304 struct strvec *opt_value = opt->value;
306 if (recreate_opt(&sb, opt, arg, unset) < 0)
307 return -1;
309 strvec_push(opt_value, sb.buf);
311 return 0;
314 int parse_opt_tracking_mode(const struct option *opt, const char *arg, int unset)
316 if (unset)
317 *(enum branch_track *)opt->value = BRANCH_TRACK_NEVER;
318 else if (!arg || !strcmp(arg, "direct"))
319 *(enum branch_track *)opt->value = BRANCH_TRACK_EXPLICIT;
320 else if (!strcmp(arg, "inherit"))
321 *(enum branch_track *)opt->value = BRANCH_TRACK_INHERIT;
322 else
323 return error(_("option `%s' expects \"%s\" or \"%s\""),
324 "--track", "direct", "inherit");
326 return 0;