1 #include "git-compat-util.h"
2 #include "parse-options.h"
7 #include "environment.h"
9 #include "object-name.h"
10 #include "string-list.h"
12 #include "oid-array.h"
14 /*----- some often used options -----*/
16 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
21 v
= unset
? 0 : DEFAULT_ABBREV
;
24 return error(_("option `%s' expects a numerical value"),
26 v
= strtol(arg
, (char **)&arg
, 10);
28 return error(_("option `%s' expects a numerical value"),
30 if (v
&& v
< MINIMUM_ABBREV
)
32 else if (v
> the_hash_algo
->hexsz
)
33 v
= the_hash_algo
->hexsz
;
35 *(int *)(opt
->value
) = v
;
39 int parse_opt_expiry_date_cb(const struct option
*opt
, const char *arg
,
44 if (parse_expiry_date(arg
, (timestamp_t
*)opt
->value
))
45 die(_("malformed expiration date '%s'"), arg
);
49 int parse_opt_color_flag_cb(const struct option
*opt
, const char *arg
,
55 arg
= unset
? "never" : (const char *)opt
->defval
;
56 value
= git_config_colorbool(NULL
, arg
);
58 return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
60 *(int *)opt
->value
= value
;
64 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
67 int *target
= opt
->value
;
72 /* --no-quiet, --no-verbose */
74 else if (opt
->short_name
== 'v') {
88 int parse_opt_commits(const struct option
*opt
, const char *arg
, int unset
)
91 struct commit
*commit
;
93 BUG_ON_OPT_NEG(unset
);
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
);
101 return error("no such commit %s", arg
);
102 commit_list_insert(commit
, opt
->value
);
106 int parse_opt_commit(const struct option
*opt
, const char *arg
, int unset
)
108 struct object_id oid
;
109 struct commit
*commit
;
110 struct commit
**target
= opt
->value
;
112 BUG_ON_OPT_NEG(unset
);
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
);
120 return error("no such commit %s", arg
);
125 int parse_opt_object_name(const struct option
*opt
, const char *arg
, int unset
)
127 struct object_id oid
;
130 oid_array_clear(opt
->value
);
135 if (repo_get_oid(the_repository
, arg
, &oid
))
136 return error(_("malformed object name '%s'"), arg
);
137 oid_array_append(opt
->value
, &oid
);
141 int parse_opt_object_id(const struct option
*opt
, const char *arg
, int unset
)
143 struct object_id oid
;
144 struct object_id
*target
= opt
->value
;
147 oidcpy(target
, null_oid());
152 if (repo_get_oid(the_repository
, arg
, &oid
))
153 return error(_("malformed object name '%s'"), arg
);
158 int parse_opt_tertiary(const struct option
*opt
, const char *arg
, int unset
)
160 int *target
= opt
->value
;
164 *target
= unset
? 2 : 1;
168 static size_t parse_options_count(const struct option
*opt
)
172 for (; opt
&& opt
->type
!= OPTION_END
; opt
++)
177 struct option
*parse_options_dup(const struct option
*o
)
179 struct option no_options
[] = { OPT_END() };
181 return parse_options_concat(o
, no_options
);
184 struct option
*parse_options_concat(const struct option
*a
,
185 const struct option
*b
)
188 size_t a_len
= parse_options_count(a
);
189 size_t b_len
= parse_options_count(b
);
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 */
198 int parse_opt_string_list(const struct option
*opt
, const char *arg
, int unset
)
200 struct string_list
*v
= opt
->value
;
203 string_list_clear(v
, 0);
210 string_list_append(v
, arg
);
214 int parse_opt_strvec(const struct option
*opt
, const char *arg
, int unset
)
216 struct strvec
*v
= opt
->value
;
230 int parse_opt_noop_cb(const struct option
*opt
, const char *arg
, int unset
)
236 * Recreates the command-line option in the strbuf.
238 static int recreate_opt(struct strbuf
*sb
, const struct option
*opt
,
239 const char *arg
, int unset
)
243 if (opt
->long_name
) {
244 strbuf_addstr(sb
, unset
? "--no-" : "--");
245 strbuf_addstr(sb
, opt
->long_name
);
247 strbuf_addch(sb
, '=');
248 strbuf_addstr(sb
, arg
);
250 } else if (opt
->short_name
&& !unset
) {
251 strbuf_addch(sb
, '-');
252 strbuf_addch(sb
, opt
->short_name
);
254 strbuf_addstr(sb
, arg
);
262 * For an option opt, recreates the command-line option in opt->value which
263 * must be an char* initialized to NULL. This is useful when we need to pass
264 * the command-line option to another command. Since any previous value will be
265 * overwritten, this callback should only be used for options where the last
268 int parse_opt_passthru(const struct option
*opt
, const char *arg
, int unset
)
270 static struct strbuf sb
= STRBUF_INIT
;
271 char **opt_value
= opt
->value
;
273 if (recreate_opt(&sb
, opt
, arg
, unset
) < 0)
278 *opt_value
= strbuf_detach(&sb
, NULL
);
284 * For an option opt, recreate the command-line option, appending it to
285 * opt->value which must be a strvec. This is useful when we need to pass
286 * the command-line option, which can be specified multiple times, to another
289 int parse_opt_passthru_argv(const struct option
*opt
, const char *arg
, int unset
)
291 static struct strbuf sb
= STRBUF_INIT
;
292 struct strvec
*opt_value
= opt
->value
;
294 if (recreate_opt(&sb
, opt
, arg
, unset
) < 0)
297 strvec_push(opt_value
, sb
.buf
);
302 int parse_opt_tracking_mode(const struct option
*opt
, const char *arg
, int unset
)
305 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_NEVER
;
306 else if (!arg
|| !strcmp(arg
, "direct"))
307 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_EXPLICIT
;
308 else if (!strcmp(arg
, "inherit"))
309 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_INHERIT
;
311 return error(_("option `%s' expects \"%s\" or \"%s\""),
312 "--track", "direct", "inherit");