]> git.ipfire.org Git - thirdparty/git.git/blob - parse-options-cb.c
Merge branch 'rj/add-i-leak-fix'
[thirdparty/git.git] / parse-options-cb.c
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "branch.h"
4 #include "commit.h"
5 #include "color.h"
6 #include "date.h"
7 #include "environment.h"
8 #include "gettext.h"
9 #include "object-name.h"
10 #include "string-list.h"
11 #include "strvec.h"
12 #include "oid-array.h"
13
14 /*----- some often used options -----*/
15
16 int 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 {
23 if (!*arg)
24 return error(_("option `%s' expects a numerical value"),
25 opt->long_name);
26 v = strtol(arg, (char **)&arg, 10);
27 if (*arg)
28 return error(_("option `%s' expects a numerical value"),
29 opt->long_name);
30 if (v && v < MINIMUM_ABBREV)
31 v = MINIMUM_ABBREV;
32 else if (v > the_hash_algo->hexsz)
33 v = the_hash_algo->hexsz;
34 }
35 *(int *)(opt->value) = v;
36 return 0;
37 }
38
39 int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
40 int unset)
41 {
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;
47 }
48
49 int 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;
56 value = git_config_colorbool(NULL, arg);
57 if (value < 0)
58 return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
59 opt->long_name);
60 *(int *)opt->value = value;
61 return 0;
62 }
63
64 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
65 int unset)
66 {
67 int *target = opt->value;
68
69 BUG_ON_OPT_ARG(arg);
70
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
88 int parse_opt_commits(const struct option *opt, const char *arg, int unset)
89 {
90 struct object_id oid;
91 struct commit *commit;
92
93 BUG_ON_OPT_NEG(unset);
94
95 if (!arg)
96 return -1;
97 if (repo_get_oid(the_repository, arg, &oid))
98 return error("malformed object name %s", arg);
99 commit = lookup_commit_reference(the_repository, &oid);
100 if (!commit)
101 return error("no such commit %s", arg);
102 commit_list_insert(commit, opt->value);
103 return 0;
104 }
105
106 int 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
112 BUG_ON_OPT_NEG(unset);
113
114 if (!arg)
115 return -1;
116 if (repo_get_oid(the_repository, arg, &oid))
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
125 int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
126 {
127 struct object_id oid;
128
129 if (unset) {
130 oid_array_clear(opt->value);
131 return 0;
132 }
133 if (!arg)
134 return -1;
135 if (repo_get_oid(the_repository, arg, &oid))
136 return error(_("malformed object name '%s'"), arg);
137 oid_array_append(opt->value, &oid);
138 return 0;
139 }
140
141 int 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) {
147 oidcpy(target, null_oid());
148 return 0;
149 }
150 if (!arg)
151 return -1;
152 if (repo_get_oid(the_repository, arg, &oid))
153 return error(_("malformed object name '%s'"), arg);
154 *target = oid;
155 return 0;
156 }
157
158 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
159 {
160 int *target = opt->value;
161
162 BUG_ON_OPT_ARG(arg);
163
164 *target = unset ? 2 : 1;
165 return 0;
166 }
167
168 static 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
177 struct option *parse_options_dup(const struct option *o)
178 {
179 struct option no_options[] = { OPT_END() };
180
181 return parse_options_concat(o, no_options);
182 }
183
184 struct option *parse_options_concat(const struct option *a,
185 const struct option *b)
186 {
187 struct option *ret;
188 size_t a_len = parse_options_count(a);
189 size_t b_len = parse_options_count(b);
190
191 ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
192 COPY_ARRAY(ret, a, a_len);
193 COPY_ARRAY(ret + a_len, b, b_len + 1); /* + 1 for final OPTION_END */
194
195 return ret;
196 }
197
198 int 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
210 string_list_append(v, arg);
211 return 0;
212 }
213
214 int parse_opt_strvec(const struct option *opt, const char *arg, int unset)
215 {
216 struct strvec *v = opt->value;
217
218 if (unset) {
219 strvec_clear(v);
220 return 0;
221 }
222
223 if (!arg)
224 return -1;
225
226 strvec_push(v, arg);
227 return 0;
228 }
229
230 int parse_opt_noop_cb(const struct option *opt UNUSED,
231 const char *arg UNUSED,
232 int unset UNUSED)
233 {
234 return 0;
235 }
236
237 /**
238 * Recreates the command-line option in the strbuf.
239 */
240 static int recreate_opt(struct strbuf *sb, const struct option *opt,
241 const char *arg, int unset)
242 {
243 strbuf_reset(sb);
244
245 if (opt->long_name) {
246 strbuf_addstr(sb, unset ? "--no-" : "--");
247 strbuf_addstr(sb, opt->long_name);
248 if (arg) {
249 strbuf_addch(sb, '=');
250 strbuf_addstr(sb, arg);
251 }
252 } else if (opt->short_name && !unset) {
253 strbuf_addch(sb, '-');
254 strbuf_addch(sb, opt->short_name);
255 if (arg)
256 strbuf_addstr(sb, arg);
257 } else
258 return -1;
259
260 return 0;
261 }
262
263 /**
264 * For an option opt, recreates the command-line option in opt->value which
265 * must be an char* initialized to NULL. This is useful when we need to pass
266 * the command-line option to another command. Since any previous value will be
267 * overwritten, this callback should only be used for options where the last
268 * one wins.
269 */
270 int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
271 {
272 static struct strbuf sb = STRBUF_INIT;
273 char **opt_value = opt->value;
274
275 if (recreate_opt(&sb, opt, arg, unset) < 0)
276 return -1;
277
278 free(*opt_value);
279
280 *opt_value = strbuf_detach(&sb, NULL);
281
282 return 0;
283 }
284
285 /**
286 * For an option opt, recreate the command-line option, appending it to
287 * opt->value which must be a strvec. This is useful when we need to pass
288 * the command-line option, which can be specified multiple times, to another
289 * command.
290 */
291 int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
292 {
293 static struct strbuf sb = STRBUF_INIT;
294 struct strvec *opt_value = opt->value;
295
296 if (recreate_opt(&sb, opt, arg, unset) < 0)
297 return -1;
298
299 strvec_push(opt_value, sb.buf);
300
301 return 0;
302 }
303
304 int parse_opt_tracking_mode(const struct option *opt, const char *arg, int unset)
305 {
306 if (unset)
307 *(enum branch_track *)opt->value = BRANCH_TRACK_NEVER;
308 else if (!arg || !strcmp(arg, "direct"))
309 *(enum branch_track *)opt->value = BRANCH_TRACK_EXPLICIT;
310 else if (!strcmp(arg, "inherit"))
311 *(enum branch_track *)opt->value = BRANCH_TRACK_INHERIT;
312 else
313 return error(_("option `%s' expects \"%s\" or \"%s\""),
314 "--track", "direct", "inherit");
315
316 return 0;
317 }