cache.h: remove expand_user_path()
[git/debian.git] / parse-options-cb.c
blobfbf4b010195a141bb847facf77aaf3c501dc7e9e
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 "gettext.h"
8 #include "string-list.h"
9 #include "strvec.h"
10 #include "oid-array.h"
12 /*----- some often used options -----*/
14 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
16 int v;
18 if (!arg) {
19 v = unset ? 0 : DEFAULT_ABBREV;
20 } else {
21 if (!*arg)
22 return error(_("option `%s' expects a numerical value"),
23 opt->long_name);
24 v = strtol(arg, (char **)&arg, 10);
25 if (*arg)
26 return error(_("option `%s' expects a numerical value"),
27 opt->long_name);
28 if (v && v < MINIMUM_ABBREV)
29 v = MINIMUM_ABBREV;
30 else if (v > the_hash_algo->hexsz)
31 v = the_hash_algo->hexsz;
33 *(int *)(opt->value) = v;
34 return 0;
37 int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
38 int unset)
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;
47 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
48 int unset)
50 int value;
52 if (!arg)
53 arg = unset ? "never" : (const char *)opt->defval;
54 value = git_config_colorbool(NULL, arg);
55 if (value < 0)
56 return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
57 opt->long_name);
58 *(int *)opt->value = value;
59 return 0;
62 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
63 int unset)
65 int *target = opt->value;
67 BUG_ON_OPT_ARG(arg);
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;
83 return 0;
86 int parse_opt_commits(const struct option *opt, const char *arg, int unset)
88 struct object_id oid;
89 struct commit *commit;
91 BUG_ON_OPT_NEG(unset);
93 if (!arg)
94 return -1;
95 if (get_oid(arg, &oid))
96 return error("malformed object name %s", arg);
97 commit = lookup_commit_reference(the_repository, &oid);
98 if (!commit)
99 return error("no such commit %s", arg);
100 commit_list_insert(commit, opt->value);
101 return 0;
104 int parse_opt_commit(const struct option *opt, const char *arg, int unset)
106 struct object_id oid;
107 struct commit *commit;
108 struct commit **target = opt->value;
110 BUG_ON_OPT_NEG(unset);
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;
123 int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
125 struct object_id oid;
127 if (unset) {
128 oid_array_clear(opt->value);
129 return 0;
131 if (!arg)
132 return -1;
133 if (get_oid(arg, &oid))
134 return error(_("malformed object name '%s'"), arg);
135 oid_array_append(opt->value, &oid);
136 return 0;
139 int parse_opt_object_id(const struct option *opt, const char *arg, int unset)
141 struct object_id oid;
142 struct object_id *target = opt->value;
144 if (unset) {
145 oidcpy(target, null_oid());
146 return 0;
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;
156 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
158 int *target = opt->value;
160 BUG_ON_OPT_ARG(arg);
162 *target = unset ? 2 : 1;
163 return 0;
166 static size_t parse_options_count(const struct option *opt)
168 size_t n = 0;
170 for (; opt && opt->type != OPTION_END; opt++)
171 n++;
172 return n;
175 struct option *parse_options_dup(const struct option *o)
177 struct option no_options[] = { OPT_END() };
179 return parse_options_concat(o, no_options);
182 struct option *parse_options_concat(const struct option *a,
183 const struct option *b)
185 struct option *ret;
186 size_t a_len = parse_options_count(a);
187 size_t b_len = parse_options_count(b);
189 ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
190 COPY_ARRAY(ret, a, a_len);
191 COPY_ARRAY(ret + a_len, b, b_len + 1); /* + 1 for final OPTION_END */
193 return ret;
196 int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
198 struct string_list *v = opt->value;
200 if (unset) {
201 string_list_clear(v, 0);
202 return 0;
205 if (!arg)
206 return -1;
208 string_list_append(v, arg);
209 return 0;
212 int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
214 return 0;
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().
224 enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
225 const struct option *opt,
226 const char *arg, int unset)
228 BUG_ON_OPT_ARG(arg);
229 return PARSE_OPT_UNKNOWN;
233 * Recreates the command-line option in the strbuf.
235 static int recreate_opt(struct strbuf *sb, const struct option *opt,
236 const char *arg, int unset)
238 strbuf_reset(sb);
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);
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;
255 return 0;
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.
265 int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
267 static struct strbuf sb = STRBUF_INIT;
268 char **opt_value = opt->value;
270 if (recreate_opt(&sb, opt, arg, unset) < 0)
271 return -1;
273 free(*opt_value);
275 *opt_value = strbuf_detach(&sb, NULL);
277 return 0;
281 * For an option opt, recreate the command-line option, appending it to
282 * opt->value which must be a strvec. This is useful when we need to pass
283 * the command-line option, which can be specified multiple times, to another
284 * command.
286 int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
288 static struct strbuf sb = STRBUF_INIT;
289 struct strvec *opt_value = opt->value;
291 if (recreate_opt(&sb, opt, arg, unset) < 0)
292 return -1;
294 strvec_push(opt_value, sb.buf);
296 return 0;
299 int parse_opt_tracking_mode(const struct option *opt, const char *arg, int unset)
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");
311 return 0;