Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| 2 | #include "parse-options.h" |
Josh Steadmon | d311566 | 2021-12-20 19:30:23 -0800 | [diff] [blame] | 3 | #include "branch.h" |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 4 | #include "cache.h" |
| 5 | #include "commit.h" |
| 6 | #include "color.h" |
| 7 | #include "string-list.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 8 | #include "strvec.h" |
Jeff King | fe299ec | 2020-03-30 10:03:46 -0400 | [diff] [blame] | 9 | #include "oid-array.h" |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 10 | |
| 11 | /*----- some often used options -----*/ |
| 12 | |
| 13 | int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset) |
| 14 | { |
| 15 | int v; |
| 16 | |
| 17 | if (!arg) { |
| 18 | v = unset ? 0 : DEFAULT_ABBREV; |
| 19 | } else { |
Nguyễn Thái Ngọc Duy | f7e68a0 | 2019-05-29 16:11:16 +0700 | [diff] [blame] | 20 | if (!*arg) |
| 21 | return error(_("option `%s' expects a numerical value"), |
| 22 | opt->long_name); |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 23 | v = strtol(arg, (char **)&arg, 10); |
| 24 | if (*arg) |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 25 | return error(_("option `%s' expects a numerical value"), |
| 26 | opt->long_name); |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 27 | if (v && v < MINIMUM_ABBREV) |
| 28 | v = MINIMUM_ABBREV; |
Nguyễn Thái Ngọc Duy | d877418 | 2019-03-24 15:20:04 +0700 | [diff] [blame] | 29 | else if (v > the_hash_algo->hexsz) |
| 30 | v = the_hash_algo->hexsz; |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 31 | } |
| 32 | *(int *)(opt->value) = v; |
| 33 | return 0; |
| 34 | } |
| 35 | |
Junio C Hamano | 27ec394 | 2013-04-25 11:13:49 -0700 | [diff] [blame] | 36 | int parse_opt_expiry_date_cb(const struct option *opt, const char *arg, |
| 37 | int unset) |
| 38 | { |
Junio C Hamano | 8ab5aa4 | 2018-04-21 12:13:13 +0900 | [diff] [blame] | 39 | if (unset) |
| 40 | arg = "never"; |
| 41 | if (parse_expiry_date(arg, (timestamp_t *)opt->value)) |
| 42 | die(_("malformed expiration date '%s'"), arg); |
| 43 | return 0; |
Junio C Hamano | 27ec394 | 2013-04-25 11:13:49 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 46 | int parse_opt_color_flag_cb(const struct option *opt, const char *arg, |
| 47 | int unset) |
| 48 | { |
| 49 | int value; |
| 50 | |
| 51 | if (!arg) |
| 52 | arg = unset ? "never" : (const char *)opt->defval; |
Junio C Hamano | f946b46 | 2011-08-28 21:19:16 -0700 | [diff] [blame] | 53 | value = git_config_colorbool(NULL, arg); |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 54 | if (value < 0) |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 55 | return error(_("option `%s' expects \"always\", \"auto\", or \"never\""), |
| 56 | opt->long_name); |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 57 | *(int *)opt->value = value; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int parse_opt_verbosity_cb(const struct option *opt, const char *arg, |
| 62 | int unset) |
| 63 | { |
| 64 | int *target = opt->value; |
| 65 | |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 66 | BUG_ON_OPT_ARG(arg); |
| 67 | |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 68 | if (unset) |
| 69 | /* --no-quiet, --no-verbose */ |
| 70 | *target = 0; |
| 71 | else if (opt->short_name == 'v') { |
| 72 | if (*target >= 0) |
| 73 | (*target)++; |
| 74 | else |
| 75 | *target = 1; |
| 76 | } else { |
| 77 | if (*target <= 0) |
| 78 | (*target)--; |
| 79 | else |
| 80 | *target = -1; |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
Karthik Nayak | 9d306b5 | 2015-07-07 21:36:14 +0530 | [diff] [blame] | 85 | int parse_opt_commits(const struct option *opt, const char *arg, int unset) |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 86 | { |
brian m. carlson | 9e31eaf | 2017-05-06 22:09:59 +0000 | [diff] [blame] | 87 | struct object_id oid; |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 88 | struct commit *commit; |
| 89 | |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 90 | BUG_ON_OPT_NEG(unset); |
| 91 | |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 92 | if (!arg) |
| 93 | return -1; |
brian m. carlson | 9e31eaf | 2017-05-06 22:09:59 +0000 | [diff] [blame] | 94 | if (get_oid(arg, &oid)) |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 95 | return error("malformed object name %s", arg); |
Stefan Beller | 2122f67 | 2018-06-28 18:21:58 -0700 | [diff] [blame] | 96 | commit = lookup_commit_reference(the_repository, &oid); |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 97 | if (!commit) |
| 98 | return error("no such commit %s", arg); |
| 99 | commit_list_insert(commit, opt->value); |
| 100 | return 0; |
| 101 | } |
| 102 | |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 103 | int parse_opt_commit(const struct option *opt, const char *arg, int unset) |
| 104 | { |
| 105 | struct object_id oid; |
| 106 | struct commit *commit; |
| 107 | struct commit **target = opt->value; |
| 108 | |
Jeff King | 8d2aa8d | 2020-09-30 08:29:02 -0400 | [diff] [blame] | 109 | BUG_ON_OPT_NEG(unset); |
| 110 | |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 111 | if (!arg) |
| 112 | return -1; |
| 113 | if (get_oid(arg, &oid)) |
| 114 | return error("malformed object name %s", arg); |
| 115 | commit = lookup_commit_reference(the_repository, &oid); |
| 116 | if (!commit) |
| 117 | return error("no such commit %s", arg); |
| 118 | *target = commit; |
| 119 | return 0; |
| 120 | } |
| 121 | |
Karthik Nayak | b2172fd | 2015-07-07 21:36:08 +0530 | [diff] [blame] | 122 | int parse_opt_object_name(const struct option *opt, const char *arg, int unset) |
| 123 | { |
brian m. carlson | 9e1d708 | 2017-03-26 16:01:31 +0000 | [diff] [blame] | 124 | struct object_id oid; |
Karthik Nayak | b2172fd | 2015-07-07 21:36:08 +0530 | [diff] [blame] | 125 | |
| 126 | if (unset) { |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 127 | oid_array_clear(opt->value); |
Karthik Nayak | b2172fd | 2015-07-07 21:36:08 +0530 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | if (!arg) |
| 131 | return -1; |
brian m. carlson | 9e1d708 | 2017-03-26 16:01:31 +0000 | [diff] [blame] | 132 | if (get_oid(arg, &oid)) |
Karthik Nayak | b2172fd | 2015-07-07 21:36:08 +0530 | [diff] [blame] | 133 | return error(_("malformed object name '%s'"), arg); |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 134 | oid_array_append(opt->value, &oid); |
Karthik Nayak | b2172fd | 2015-07-07 21:36:08 +0530 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
Phillip Wood | 3389853 | 2019-04-17 15:30:40 +0100 | [diff] [blame] | 138 | int parse_opt_object_id(const struct option *opt, const char *arg, int unset) |
| 139 | { |
| 140 | struct object_id oid; |
| 141 | struct object_id *target = opt->value; |
| 142 | |
| 143 | if (unset) { |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 144 | oidcpy(target, null_oid()); |
Phillip Wood | 3389853 | 2019-04-17 15:30:40 +0100 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | if (!arg) |
| 148 | return -1; |
| 149 | if (get_oid(arg, &oid)) |
| 150 | return error(_("malformed object name '%s'"), arg); |
| 151 | *target = oid; |
| 152 | return 0; |
| 153 | } |
| 154 | |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 155 | int parse_opt_tertiary(const struct option *opt, const char *arg, int unset) |
| 156 | { |
| 157 | int *target = opt->value; |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 158 | |
| 159 | BUG_ON_OPT_ARG(arg); |
| 160 | |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 161 | *target = unset ? 2 : 1; |
| 162 | return 0; |
| 163 | } |
| 164 | |
René Scharfe | f904f90 | 2020-02-09 16:56:47 +0100 | [diff] [blame] | 165 | static size_t parse_options_count(const struct option *opt) |
| 166 | { |
| 167 | size_t n = 0; |
| 168 | |
| 169 | for (; opt && opt->type != OPTION_END; opt++) |
| 170 | n++; |
| 171 | return n; |
| 172 | } |
| 173 | |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 174 | struct option *parse_options_dup(const struct option *o) |
| 175 | { |
René Scharfe | 7a9f8ca | 2020-02-09 16:58:42 +0100 | [diff] [blame] | 176 | struct option no_options[] = { OPT_END() }; |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 177 | |
René Scharfe | 7a9f8ca | 2020-02-09 16:58:42 +0100 | [diff] [blame] | 178 | return parse_options_concat(o, no_options); |
Nguyễn Thái Ngọc Duy | 2087182 | 2019-03-29 17:39:04 +0700 | [diff] [blame] | 179 | } |
| 180 | |
René Scharfe | c840785 | 2020-02-09 16:57:56 +0100 | [diff] [blame] | 181 | struct option *parse_options_concat(const struct option *a, |
| 182 | const struct option *b) |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 183 | { |
Jeff King | 023ff39 | 2016-07-05 16:44:47 -0400 | [diff] [blame] | 184 | struct option *ret; |
René Scharfe | f904f90 | 2020-02-09 16:56:47 +0100 | [diff] [blame] | 185 | size_t a_len = parse_options_count(a); |
| 186 | size_t b_len = parse_options_count(b); |
Jeff King | 023ff39 | 2016-07-05 16:44:47 -0400 | [diff] [blame] | 187 | |
| 188 | ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1)); |
René Scharfe | a277d0a | 2020-02-09 16:55:54 +0100 | [diff] [blame] | 189 | COPY_ARRAY(ret, a, a_len); |
| 190 | COPY_ARRAY(ret + a_len, b, b_len + 1); /* + 1 for final OPTION_END */ |
Jeff King | 023ff39 | 2016-07-05 16:44:47 -0400 | [diff] [blame] | 191 | |
| 192 | return ret; |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | int parse_opt_string_list(const struct option *opt, const char *arg, int unset) |
| 196 | { |
| 197 | struct string_list *v = opt->value; |
| 198 | |
| 199 | if (unset) { |
| 200 | string_list_clear(v, 0); |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | if (!arg) |
| 205 | return -1; |
| 206 | |
Jeff King | 7a7a517 | 2016-06-13 01:39:12 -0400 | [diff] [blame] | 207 | string_list_append(v, arg); |
Dmitry Ivankov | 0687628 | 2011-08-11 15:15:38 +0600 | [diff] [blame] | 208 | return 0; |
| 209 | } |
René Scharfe | 6acec03 | 2011-09-28 19:44:30 +0200 | [diff] [blame] | 210 | |
| 211 | int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset) |
| 212 | { |
| 213 | return 0; |
| 214 | } |
Paul Tan | 6b3ee18 | 2015-06-14 16:41:48 +0800 | [diff] [blame] | 215 | |
| 216 | /** |
Michael Haggerty | ce564eb | 2016-09-05 11:44:52 +0200 | [diff] [blame] | 217 | * Report that the option is unknown, so that other code can handle |
| 218 | * it. This can be used as a callback together with |
| 219 | * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the |
| 220 | * "-h" output even if it's not being handled directly by |
| 221 | * parse_options(). |
| 222 | */ |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 223 | enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx, |
Nguyễn Thái Ngọc Duy | 3ebbe28 | 2019-01-27 07:35:28 +0700 | [diff] [blame] | 224 | const struct option *opt, |
| 225 | const char *arg, int unset) |
Michael Haggerty | ce564eb | 2016-09-05 11:44:52 +0200 | [diff] [blame] | 226 | { |
Nguyễn Thái Ngọc Duy | 3ebbe28 | 2019-01-27 07:35:28 +0700 | [diff] [blame] | 227 | BUG_ON_OPT_ARG(arg); |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 228 | return PARSE_OPT_UNKNOWN; |
Michael Haggerty | ce564eb | 2016-09-05 11:44:52 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
Paul Tan | 6b3ee18 | 2015-06-14 16:41:48 +0800 | [diff] [blame] | 232 | * Recreates the command-line option in the strbuf. |
| 233 | */ |
| 234 | static int recreate_opt(struct strbuf *sb, const struct option *opt, |
| 235 | const char *arg, int unset) |
| 236 | { |
| 237 | strbuf_reset(sb); |
| 238 | |
| 239 | if (opt->long_name) { |
| 240 | strbuf_addstr(sb, unset ? "--no-" : "--"); |
| 241 | strbuf_addstr(sb, opt->long_name); |
| 242 | if (arg) { |
| 243 | strbuf_addch(sb, '='); |
| 244 | strbuf_addstr(sb, arg); |
| 245 | } |
| 246 | } else if (opt->short_name && !unset) { |
| 247 | strbuf_addch(sb, '-'); |
| 248 | strbuf_addch(sb, opt->short_name); |
| 249 | if (arg) |
| 250 | strbuf_addstr(sb, arg); |
| 251 | } else |
| 252 | return -1; |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * For an option opt, recreates the command-line option in opt->value which |
| 259 | * must be an char* initialized to NULL. This is useful when we need to pass |
| 260 | * the command-line option to another command. Since any previous value will be |
| 261 | * overwritten, this callback should only be used for options where the last |
| 262 | * one wins. |
| 263 | */ |
| 264 | int parse_opt_passthru(const struct option *opt, const char *arg, int unset) |
| 265 | { |
| 266 | static struct strbuf sb = STRBUF_INIT; |
| 267 | char **opt_value = opt->value; |
| 268 | |
| 269 | if (recreate_opt(&sb, opt, arg, unset) < 0) |
| 270 | return -1; |
| 271 | |
René Scharfe | 39ea59a | 2016-10-08 16:14:57 +0200 | [diff] [blame] | 272 | free(*opt_value); |
Paul Tan | 6b3ee18 | 2015-06-14 16:41:48 +0800 | [diff] [blame] | 273 | |
| 274 | *opt_value = strbuf_detach(&sb, NULL); |
| 275 | |
| 276 | return 0; |
| 277 | } |
Paul Tan | ffad85c | 2015-06-14 16:41:49 +0800 | [diff] [blame] | 278 | |
| 279 | /** |
| 280 | * For an option opt, recreate the command-line option, appending it to |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 281 | * opt->value which must be a strvec. This is useful when we need to pass |
Paul Tan | ffad85c | 2015-06-14 16:41:49 +0800 | [diff] [blame] | 282 | * the command-line option, which can be specified multiple times, to another |
| 283 | * command. |
| 284 | */ |
| 285 | int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset) |
| 286 | { |
| 287 | static struct strbuf sb = STRBUF_INIT; |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 288 | struct strvec *opt_value = opt->value; |
Paul Tan | ffad85c | 2015-06-14 16:41:49 +0800 | [diff] [blame] | 289 | |
| 290 | if (recreate_opt(&sb, opt, arg, unset) < 0) |
| 291 | return -1; |
| 292 | |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 293 | strvec_push(opt_value, sb.buf); |
Paul Tan | ffad85c | 2015-06-14 16:41:49 +0800 | [diff] [blame] | 294 | |
| 295 | return 0; |
| 296 | } |
Josh Steadmon | d311566 | 2021-12-20 19:30:23 -0800 | [diff] [blame] | 297 | |
| 298 | int parse_opt_tracking_mode(const struct option *opt, const char *arg, int unset) |
| 299 | { |
| 300 | if (unset) |
| 301 | *(enum branch_track *)opt->value = BRANCH_TRACK_NEVER; |
| 302 | else if (!arg || !strcmp(arg, "direct")) |
| 303 | *(enum branch_track *)opt->value = BRANCH_TRACK_EXPLICIT; |
| 304 | else if (!strcmp(arg, "inherit")) |
| 305 | *(enum branch_track *)opt->value = BRANCH_TRACK_INHERIT; |
| 306 | else |
| 307 | return error(_("option `%s' expects \"%s\" or \"%s\""), |
| 308 | "--track", "direct", "inherit"); |
| 309 | |
| 310 | return 0; |
| 311 | } |