compat/fsmonitor: fix socket path in networked SHA256 repos
[git/gitster.git] / parse-options-cb.c
blob3fb7ce68cac11452b9d60c1455b5813326551a72
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "parse-options.h"
5 #include "branch.h"
6 #include "commit.h"
7 #include "color.h"
8 #include "date.h"
9 #include "environment.h"
10 #include "gettext.h"
11 #include "object-name.h"
12 #include "setup.h"
13 #include "string-list.h"
14 #include "strvec.h"
15 #include "oid-array.h"
17 /*----- some often used options -----*/
19 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
21 int v;
23 if (!arg) {
24 v = unset ? 0 : DEFAULT_ABBREV;
25 } else {
26 if (!*arg)
27 return error(_("option `%s' expects a numerical value"),
28 opt->long_name);
29 v = strtol(arg, (char **)&arg, 10);
30 if (*arg)
31 return error(_("option `%s' expects a numerical value"),
32 opt->long_name);
33 if (v && v < MINIMUM_ABBREV)
34 v = MINIMUM_ABBREV;
35 else if (startup_info->have_repository && v > the_hash_algo->hexsz)
36 v = the_hash_algo->hexsz;
38 *(int *)(opt->value) = v;
39 return 0;
42 int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
43 int unset)
45 if (unset)
46 arg = "never";
47 if (parse_expiry_date(arg, (timestamp_t *)opt->value))
48 die(_("malformed expiration date '%s'"), arg);
49 return 0;
52 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
53 int unset)
55 int value;
57 if (!arg)
58 arg = unset ? "never" : (const char *)opt->defval;
59 value = git_config_colorbool(NULL, arg);
60 if (value < 0)
61 return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
62 opt->long_name);
63 *(int *)opt->value = value;
64 return 0;
67 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
68 int unset)
70 int *target = opt->value;
72 BUG_ON_OPT_ARG(arg);
74 if (unset)
75 /* --no-quiet, --no-verbose */
76 *target = 0;
77 else if (opt->short_name == 'v') {
78 if (*target >= 0)
79 (*target)++;
80 else
81 *target = 1;
82 } else {
83 if (*target <= 0)
84 (*target)--;
85 else
86 *target = -1;
88 return 0;
91 int parse_opt_commits(const struct option *opt, const char *arg, int unset)
93 struct object_id oid;
94 struct commit *commit;
96 BUG_ON_OPT_NEG(unset);
98 if (!arg)
99 return -1;
100 if (repo_get_oid(the_repository, arg, &oid))
101 return error("malformed object name %s", arg);
102 commit = lookup_commit_reference(the_repository, &oid);
103 if (!commit)
104 return error("no such commit %s", arg);
105 commit_list_insert(commit, opt->value);
106 return 0;
109 int parse_opt_commit(const struct option *opt, const char *arg, int unset)
111 struct object_id oid;
112 struct commit *commit;
113 struct commit **target = opt->value;
115 BUG_ON_OPT_NEG(unset);
117 if (!arg)
118 return -1;
119 if (repo_get_oid(the_repository, arg, &oid))
120 return error("malformed object name %s", arg);
121 commit = lookup_commit_reference(the_repository, &oid);
122 if (!commit)
123 return error("no such commit %s", arg);
124 *target = commit;
125 return 0;
128 int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
130 struct object_id oid;
132 if (unset) {
133 oid_array_clear(opt->value);
134 return 0;
136 if (!arg)
137 return -1;
138 if (repo_get_oid(the_repository, arg, &oid))
139 return error(_("malformed object name '%s'"), arg);
140 oid_array_append(opt->value, &oid);
141 return 0;
144 int parse_opt_object_id(const struct option *opt, const char *arg, int unset)
146 struct object_id oid;
147 struct object_id *target = opt->value;
149 if (unset) {
150 oidcpy(target, null_oid());
151 return 0;
153 if (!arg)
154 return -1;
155 if (repo_get_oid(the_repository, arg, &oid))
156 return error(_("malformed object name '%s'"), arg);
157 *target = oid;
158 return 0;
161 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
163 int *target = opt->value;
165 BUG_ON_OPT_ARG(arg);
167 *target = unset ? 2 : 1;
168 return 0;
171 static size_t parse_options_count(const struct option *opt)
173 size_t n = 0;
175 for (; opt && opt->type != OPTION_END; opt++)
176 n++;
177 return n;
180 struct option *parse_options_dup(const struct option *o)
182 struct option no_options[] = { OPT_END() };
184 return parse_options_concat(o, no_options);
187 struct option *parse_options_concat(const struct option *a,
188 const struct option *b)
190 struct option *ret;
191 size_t a_len = parse_options_count(a);
192 size_t b_len = parse_options_count(b);
194 ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
195 COPY_ARRAY(ret, a, a_len);
196 COPY_ARRAY(ret + a_len, b, b_len + 1); /* + 1 for final OPTION_END */
198 return ret;
201 int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
203 struct string_list *v = opt->value;
205 if (unset) {
206 string_list_clear(v, 0);
207 return 0;
210 if (!arg)
211 return -1;
213 string_list_append(v, arg);
214 return 0;
217 int parse_opt_strvec(const struct option *opt, const char *arg, int unset)
219 struct strvec *v = opt->value;
221 if (unset) {
222 strvec_clear(v);
223 return 0;
226 if (!arg)
227 return -1;
229 strvec_push(v, arg);
230 return 0;
233 int parse_opt_noop_cb(const struct option *opt UNUSED,
234 const char *arg UNUSED,
235 int unset UNUSED)
237 return 0;
241 * Recreates the command-line option in the strbuf.
243 static int recreate_opt(struct strbuf *sb, const struct option *opt,
244 const char *arg, int unset)
246 strbuf_reset(sb);
248 if (opt->long_name) {
249 strbuf_addstr(sb, unset ? "--no-" : "--");
250 strbuf_addstr(sb, opt->long_name);
251 if (arg) {
252 strbuf_addch(sb, '=');
253 strbuf_addstr(sb, arg);
255 } else if (opt->short_name && !unset) {
256 strbuf_addch(sb, '-');
257 strbuf_addch(sb, opt->short_name);
258 if (arg)
259 strbuf_addstr(sb, arg);
260 } else
261 return -1;
263 return 0;
267 * For an option opt, recreates the command-line option in opt->value which
268 * must be an char* initialized to NULL. This is useful when we need to pass
269 * the command-line option to another command. Since any previous value will be
270 * overwritten, this callback should only be used for options where the last
271 * one wins.
273 int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
275 static struct strbuf sb = STRBUF_INIT;
276 char **opt_value = opt->value;
278 if (recreate_opt(&sb, opt, arg, unset) < 0)
279 return -1;
281 free(*opt_value);
283 *opt_value = strbuf_detach(&sb, NULL);
285 return 0;
289 * For an option opt, recreate the command-line option, appending it to
290 * opt->value which must be a strvec. This is useful when we need to pass
291 * the command-line option, which can be specified multiple times, to another
292 * command.
294 int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
296 static struct strbuf sb = STRBUF_INIT;
297 struct strvec *opt_value = opt->value;
299 if (recreate_opt(&sb, opt, arg, unset) < 0)
300 return -1;
302 strvec_push(opt_value, sb.buf);
304 return 0;
307 int parse_opt_tracking_mode(const struct option *opt, const char *arg, int unset)
309 if (unset)
310 *(enum branch_track *)opt->value = BRANCH_TRACK_NEVER;
311 else if (!arg || !strcmp(arg, "direct"))
312 *(enum branch_track *)opt->value = BRANCH_TRACK_EXPLICIT;
313 else if (!strcmp(arg, "inherit"))
314 *(enum branch_track *)opt->value = BRANCH_TRACK_INHERIT;
315 else
316 return error(_("option `%s' expects \"%s\" or \"%s\""),
317 "--track", "direct", "inherit");
319 return 0;