1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "parse-options.h"
9 #include "environment.h"
11 #include "object-name.h"
13 #include "string-list.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
)
24 v
= unset
? 0 : DEFAULT_ABBREV
;
27 return error(_("option `%s' expects a numerical value"),
29 v
= strtol(arg
, (char **)&arg
, 10);
31 return error(_("option `%s' expects a numerical value"),
33 if (v
&& v
< MINIMUM_ABBREV
)
36 *(int *)(opt
->value
) = v
;
40 int parse_opt_expiry_date_cb(const struct option
*opt
, const char *arg
,
45 if (parse_expiry_date(arg
, (timestamp_t
*)opt
->value
))
46 die(_("malformed expiration date '%s'"), arg
);
50 int parse_opt_color_flag_cb(const struct option
*opt
, const char *arg
,
56 arg
= unset
? "never" : (const char *)opt
->defval
;
57 value
= git_config_colorbool(NULL
, arg
);
59 return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
61 *(int *)opt
->value
= value
;
65 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
68 int *target
= opt
->value
;
73 /* --no-quiet, --no-verbose */
75 else if (opt
->short_name
== 'v') {
89 int parse_opt_commits(const struct option
*opt
, const char *arg
, int unset
)
92 struct commit
*commit
;
94 BUG_ON_OPT_NEG(unset
);
98 if (repo_get_oid(the_repository
, arg
, &oid
))
99 return error("malformed object name %s", arg
);
100 commit
= lookup_commit_reference(the_repository
, &oid
);
102 return error("no such commit %s", arg
);
103 commit_list_insert(commit
, opt
->value
);
107 int parse_opt_commit(const struct option
*opt
, const char *arg
, int unset
)
109 struct object_id oid
;
110 struct commit
*commit
;
111 struct commit
**target
= opt
->value
;
113 BUG_ON_OPT_NEG(unset
);
117 if (repo_get_oid(the_repository
, arg
, &oid
))
118 return error("malformed object name %s", arg
);
119 commit
= lookup_commit_reference(the_repository
, &oid
);
121 return error("no such commit %s", arg
);
126 int parse_opt_object_name(const struct option
*opt
, const char *arg
, int unset
)
128 struct object_id oid
;
131 oid_array_clear(opt
->value
);
136 if (repo_get_oid(the_repository
, arg
, &oid
))
137 return error(_("malformed object name '%s'"), arg
);
138 oid_array_append(opt
->value
, &oid
);
142 int parse_opt_object_id(const struct option
*opt
, const char *arg
, int unset
)
144 struct object_id oid
;
145 struct object_id
*target
= opt
->value
;
148 oidcpy(target
, null_oid());
153 if (repo_get_oid(the_repository
, arg
, &oid
))
154 return error(_("malformed object name '%s'"), arg
);
159 int parse_opt_tertiary(const struct option
*opt
, const char *arg
, int unset
)
161 int *target
= opt
->value
;
165 *target
= unset
? 2 : 1;
169 static size_t parse_options_count(const struct option
*opt
)
173 for (; opt
&& opt
->type
!= OPTION_END
; opt
++)
178 struct option
*parse_options_dup(const struct option
*o
)
180 struct option no_options
[] = { OPT_END() };
182 return parse_options_concat(o
, no_options
);
185 struct option
*parse_options_concat(const struct option
*a
,
186 const struct option
*b
)
189 size_t a_len
= parse_options_count(a
);
190 size_t b_len
= parse_options_count(b
);
192 ALLOC_ARRAY(ret
, st_add3(a_len
, b_len
, 1));
193 COPY_ARRAY(ret
, a
, a_len
);
194 COPY_ARRAY(ret
+ a_len
, b
, b_len
+ 1); /* + 1 for final OPTION_END */
199 int parse_opt_string_list(const struct option
*opt
, const char *arg
, int unset
)
201 struct string_list
*v
= opt
->value
;
204 string_list_clear(v
, 0);
211 string_list_append(v
, arg
);
215 int parse_opt_strvec(const struct option
*opt
, const char *arg
, int unset
)
217 struct strvec
*v
= opt
->value
;
231 int parse_opt_noop_cb(const struct option
*opt UNUSED
,
232 const char *arg UNUSED
,
239 * Recreates the command-line option in the strbuf.
241 static int recreate_opt(struct strbuf
*sb
, const struct option
*opt
,
242 const char *arg
, int unset
)
246 if (opt
->long_name
) {
247 strbuf_addstr(sb
, unset
? "--no-" : "--");
248 strbuf_addstr(sb
, opt
->long_name
);
250 strbuf_addch(sb
, '=');
251 strbuf_addstr(sb
, arg
);
253 } else if (opt
->short_name
&& !unset
) {
254 strbuf_addch(sb
, '-');
255 strbuf_addch(sb
, opt
->short_name
);
257 strbuf_addstr(sb
, arg
);
265 * For an option opt, recreates the command-line option in opt->value which
266 * must be an char* initialized to NULL. This is useful when we need to pass
267 * the command-line option to another command. Since any previous value will be
268 * overwritten, this callback should only be used for options where the last
271 int parse_opt_passthru(const struct option
*opt
, const char *arg
, int unset
)
273 static struct strbuf sb
= STRBUF_INIT
;
274 char **opt_value
= opt
->value
;
276 if (recreate_opt(&sb
, opt
, arg
, unset
) < 0)
281 *opt_value
= strbuf_detach(&sb
, NULL
);
287 * For an option opt, recreate the command-line option, appending it to
288 * opt->value which must be a strvec. This is useful when we need to pass
289 * the command-line option, which can be specified multiple times, to another
292 int parse_opt_passthru_argv(const struct option
*opt
, const char *arg
, int unset
)
294 static struct strbuf sb
= STRBUF_INIT
;
295 struct strvec
*opt_value
= opt
->value
;
297 if (recreate_opt(&sb
, opt
, arg
, unset
) < 0)
300 strvec_push(opt_value
, sb
.buf
);
305 int parse_opt_tracking_mode(const struct option
*opt
, const char *arg
, int unset
)
308 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_NEVER
;
309 else if (!arg
|| !strcmp(arg
, "direct"))
310 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_EXPLICIT
;
311 else if (!strcmp(arg
, "inherit"))
312 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_INHERIT
;
314 return error(_("option `%s' expects \"%s\" or \"%s\""),
315 "--track", "direct", "inherit");