| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #include "builtin.h" |
| 3 | #include "abspath.h" |
| 4 | #include "advice.h" |
| 5 | #include "config.h" |
| 6 | #include "color.h" |
| 7 | #include "date.h" |
| 8 | #include "editor.h" |
| 9 | #include "environment.h" |
| 10 | #include "gettext.h" |
| 11 | #include "ident.h" |
| 12 | #include "parse-options.h" |
| 13 | #include "urlmatch.h" |
| 14 | #include "path.h" |
| 15 | #include "quote.h" |
| 16 | #include "setup.h" |
| 17 | #include "strbuf.h" |
| 18 | #include "worktree.h" |
| 19 | |
| 20 | static const char *const builtin_config_usage[] = { |
| 21 | N_("git config list [<file-option>] [<display-option>] [--includes]"), |
| 22 | N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp] [--value=<pattern>] [--fixed-value] [--default=<default>] [--url=<url>] <name>"), |
| 23 | N_("git config set [<file-option>] [--type=<type>] [--all] [--value=<pattern>] [--fixed-value] <name> <value>"), |
| 24 | N_("git config unset [<file-option>] [--all] [--value=<pattern>] [--fixed-value] <name>"), |
| 25 | N_("git config rename-section [<file-option>] <old-name> <new-name>"), |
| 26 | N_("git config remove-section [<file-option>] <name>"), |
| 27 | N_("git config edit [<file-option>]"), |
| 28 | N_("git config [<file-option>] --get-colorbool <name> [<stdout-is-tty>]"), |
| 29 | NULL |
| 30 | }; |
| 31 | |
| 32 | static const char *const builtin_config_list_usage[] = { |
| 33 | N_("git config list [<file-option>] [<display-option>] [--includes]"), |
| 34 | NULL |
| 35 | }; |
| 36 | |
| 37 | static const char *const builtin_config_get_usage[] = { |
| 38 | N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<pattern>] [--fixed-value] [--default=<default>] <name>"), |
| 39 | NULL |
| 40 | }; |
| 41 | |
| 42 | static const char *const builtin_config_set_usage[] = { |
| 43 | N_("git config set [<file-option>] [--type=<type>] [--comment=<message>] [--all] [--value=<pattern>] [--fixed-value] <name> <value>"), |
| 44 | NULL |
| 45 | }; |
| 46 | |
| 47 | static const char *const builtin_config_unset_usage[] = { |
| 48 | N_("git config unset [<file-option>] [--all] [--value=<pattern>] [--fixed-value] <name>"), |
| 49 | NULL |
| 50 | }; |
| 51 | |
| 52 | static const char *const builtin_config_rename_section_usage[] = { |
| 53 | N_("git config rename-section [<file-option>] <old-name> <new-name>"), |
| 54 | NULL |
| 55 | }; |
| 56 | |
| 57 | static const char *const builtin_config_remove_section_usage[] = { |
| 58 | N_("git config remove-section [<file-option>] <name>"), |
| 59 | NULL |
| 60 | }; |
| 61 | |
| 62 | static const char *const builtin_config_edit_usage[] = { |
| 63 | N_("git config edit [<file-option>]"), |
| 64 | NULL |
| 65 | }; |
| 66 | |
| 67 | #define CONFIG_LOCATION_OPTIONS(opts) \ |
| 68 | OPT_GROUP(N_("Config file location")), \ |
| 69 | OPT_BOOL(0, "global", &opts.use_global_config, N_("use global config file")), \ |
| 70 | OPT_BOOL(0, "system", &opts.use_system_config, N_("use system config file")), \ |
| 71 | OPT_BOOL(0, "local", &opts.use_local_config, N_("use repository config file")), \ |
| 72 | OPT_BOOL(0, "worktree", &opts.use_worktree_config, N_("use per-worktree config file")), \ |
| 73 | OPT_STRING('f', "file", &opts.source.file, N_("file"), N_("use given config file")), \ |
| 74 | OPT_STRING(0, "blob", &opts.source.blob, N_("blob-id"), N_("read config from given blob object")) |
| 75 | |
| 76 | struct config_location_options { |
| 77 | struct git_config_source source; |
| 78 | struct config_options options; |
| 79 | char *file_to_free; |
| 80 | int use_global_config; |
| 81 | int use_system_config; |
| 82 | int use_local_config; |
| 83 | int use_worktree_config; |
| 84 | int respect_includes_opt; |
| 85 | }; |
| 86 | #define CONFIG_LOCATION_OPTIONS_INIT { \ |
| 87 | .respect_includes_opt = -1, \ |
| 88 | } |
| 89 | |
| 90 | enum config_type { |
| 91 | TYPE_NONE = 0, |
| 92 | TYPE_BOOL, |
| 93 | TYPE_INT, |
| 94 | TYPE_BOOL_OR_INT, |
| 95 | TYPE_PATH, |
| 96 | TYPE_EXPIRY_DATE, |
| 97 | TYPE_COLOR, |
| 98 | TYPE_BOOL_OR_STR, |
| 99 | }; |
| 100 | |
| 101 | #define CONFIG_TYPE_OPTIONS(type) \ |
| 102 | OPT_GROUP(N_("Type")), \ |
| 103 | OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), \ |
| 104 | OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), \ |
| 105 | OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), \ |
| 106 | OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), \ |
| 107 | OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), \ |
| 108 | OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), \ |
| 109 | OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE) |
| 110 | |
| 111 | #define CONFIG_DISPLAY_OPTIONS(opts) \ |
| 112 | OPT_GROUP(N_("Display options")), \ |
| 113 | OPT_BOOL('z', "null", &opts.end_nul, N_("terminate values with NUL byte")), \ |
| 114 | OPT_BOOL(0, "name-only", &opts.omit_values, N_("show variable names only")), \ |
| 115 | OPT_BOOL(0, "show-origin", &opts.show_origin, N_("show origin of config (file, standard input, blob, command line)")), \ |
| 116 | OPT_BOOL(0, "show-scope", &opts.show_scope, N_("show scope of config (worktree, local, global, system, command)")), \ |
| 117 | OPT_BOOL(0, "show-names", &opts.show_keys, N_("show config keys in addition to their values")), \ |
| 118 | CONFIG_TYPE_OPTIONS(opts.type) |
| 119 | |
| 120 | struct config_display_options { |
| 121 | int end_nul; |
| 122 | int omit_values; |
| 123 | int show_origin; |
| 124 | int show_scope; |
| 125 | int show_keys; |
| 126 | enum config_type type; |
| 127 | char *default_value; |
| 128 | /* Populated via `display_options_init()`. */ |
| 129 | int term; |
| 130 | int delim; |
| 131 | int key_delim; |
| 132 | }; |
| 133 | #define CONFIG_DISPLAY_OPTIONS_INIT { \ |
| 134 | .term = '\n', \ |
| 135 | .delim = '=', \ |
| 136 | .key_delim = ' ', \ |
| 137 | .type = TYPE_NONE, \ |
| 138 | } |
| 139 | |
| 140 | #define OPT_CALLBACK_VALUE(s, l, v, h, i) { \ |
| 141 | .type = OPTION_CALLBACK, \ |
| 142 | .short_name = (s), \ |
| 143 | .long_name = (l), \ |
| 144 | .value = (v), \ |
| 145 | .help = (h), \ |
| 146 | .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, \ |
| 147 | .callback = option_parse_type, \ |
| 148 | .defval = (i), \ |
| 149 | } |
| 150 | |
| 151 | static int option_parse_type(const struct option *opt, const char *arg, |
| 152 | int unset) |
| 153 | { |
| 154 | int new_type, *to_type; |
| 155 | |
| 156 | if (unset) { |
| 157 | *((int *) opt->value) = 0; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * To support '--<type>' style flags, begin with new_type equal to |
| 163 | * opt->defval. |
| 164 | */ |
| 165 | new_type = opt->defval; |
| 166 | if (!new_type) { |
| 167 | if (!strcmp(arg, "bool")) |
| 168 | new_type = TYPE_BOOL; |
| 169 | else if (!strcmp(arg, "int")) |
| 170 | new_type = TYPE_INT; |
| 171 | else if (!strcmp(arg, "bool-or-int")) |
| 172 | new_type = TYPE_BOOL_OR_INT; |
| 173 | else if (!strcmp(arg, "bool-or-str")) |
| 174 | new_type = TYPE_BOOL_OR_STR; |
| 175 | else if (!strcmp(arg, "path")) |
| 176 | new_type = TYPE_PATH; |
| 177 | else if (!strcmp(arg, "expiry-date")) |
| 178 | new_type = TYPE_EXPIRY_DATE; |
| 179 | else if (!strcmp(arg, "color")) |
| 180 | new_type = TYPE_COLOR; |
| 181 | else |
| 182 | die(_("unrecognized --type argument, %s"), arg); |
| 183 | } |
| 184 | |
| 185 | to_type = opt->value; |
| 186 | if (*to_type && *to_type != new_type) { |
| 187 | /* |
| 188 | * Complain when there is a new type not equal to the old type. |
| 189 | * This allows for combinations like '--int --type=int' and |
| 190 | * '--type=int --type=int', but disallows ones like '--type=bool |
| 191 | * --int' and '--type=bool |
| 192 | * --type=int'. |
| 193 | */ |
| 194 | error(_("only one type at a time")); |
| 195 | exit(129); |
| 196 | } |
| 197 | *to_type = new_type; |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static void check_argc(int argc, int min, int max) |
| 203 | { |
| 204 | if (argc >= min && argc <= max) |
| 205 | return; |
| 206 | if (min == max) |
| 207 | error(_("wrong number of arguments, should be %d"), min); |
| 208 | else |
| 209 | error(_("wrong number of arguments, should be from %d to %d"), |
| 210 | min, max); |
| 211 | exit(129); |
| 212 | } |
| 213 | |
| 214 | static NORETURN void die_missing_set_value(const char *arg) |
| 215 | { |
| 216 | const char *last_dot = strrchr(arg, '.'); |
| 217 | const char *eq = last_dot ? strchr(last_dot + 1, '=') : NULL; |
| 218 | char *prefix = eq ? xstrndup(arg, eq - arg) : NULL; |
| 219 | |
| 220 | if (prefix && git_config_key_is_valid(prefix)) { |
| 221 | error(_("missing value to set to the variable '%s'"), arg); |
| 222 | advise(_("did you mean \"git config set %s %s\"?"), |
| 223 | prefix, eq + 1); |
| 224 | } else if (git_config_key_is_valid(arg)) { |
| 225 | error(_("missing value to set to the variable '%s'"), arg); |
| 226 | } else { |
| 227 | error(_("missing value to set to a variable with an invalid name '%s'"), |
| 228 | arg); |
| 229 | } |
| 230 | free(prefix); |
| 231 | exit(129); |
| 232 | } |
| 233 | |
| 234 | static void show_config_origin(const struct config_display_options *opts, |
| 235 | const struct key_value_info *kvi, |
| 236 | struct strbuf *buf) |
| 237 | { |
| 238 | const char term = opts->end_nul ? '\0' : '\t'; |
| 239 | |
| 240 | strbuf_addstr(buf, config_origin_type_name(kvi->origin_type)); |
| 241 | strbuf_addch(buf, ':'); |
| 242 | if (opts->end_nul) |
| 243 | strbuf_addstr(buf, kvi->filename ? kvi->filename : ""); |
| 244 | else |
| 245 | quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0); |
| 246 | strbuf_addch(buf, term); |
| 247 | } |
| 248 | |
| 249 | static void show_config_scope(const struct config_display_options *opts, |
| 250 | const struct key_value_info *kvi, |
| 251 | struct strbuf *buf) |
| 252 | { |
| 253 | const char term = opts->end_nul ? '\0' : '\t'; |
| 254 | const char *scope = config_scope_name(kvi->scope); |
| 255 | |
| 256 | strbuf_addstr(buf, N_(scope)); |
| 257 | strbuf_addch(buf, term); |
| 258 | } |
| 259 | |
| 260 | struct strbuf_list { |
| 261 | struct strbuf *items; |
| 262 | int nr; |
| 263 | int alloc; |
| 264 | }; |
| 265 | |
| 266 | static int format_config_int64(struct strbuf *buf, |
| 267 | const char *key_, |
| 268 | const char *value_, |
| 269 | const struct key_value_info *kvi, |
| 270 | int gently) |
| 271 | { |
| 272 | int64_t v = 0; |
| 273 | if (gently) { |
| 274 | if (!git_parse_int64(value_, &v)) |
| 275 | return -1; |
| 276 | } else { |
| 277 | /* may die() */ |
| 278 | v = git_config_int64(key_, value_ ? value_ : "", kvi); |
| 279 | } |
| 280 | |
| 281 | strbuf_addf(buf, "%"PRId64, v); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int format_config_bool(struct strbuf *buf, |
| 286 | const char *key_, |
| 287 | const char *value_, |
| 288 | int gently) |
| 289 | { |
| 290 | int v = 0; |
| 291 | if (gently) { |
| 292 | if ((v = git_parse_maybe_bool(value_)) < 0) |
| 293 | return -1; |
| 294 | } else { |
| 295 | /* may die() */ |
| 296 | v = git_config_bool(key_, value_); |
| 297 | } |
| 298 | |
| 299 | strbuf_addstr(buf, v ? "true" : "false"); |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int format_config_bool_or_int(struct strbuf *buf, |
| 304 | const char *key_, |
| 305 | const char *value_, |
| 306 | const struct key_value_info *kvi, |
| 307 | int gently) |
| 308 | { |
| 309 | int v, is_bool = 0; |
| 310 | |
| 311 | if (gently) { |
| 312 | v = git_parse_maybe_bool_text(value_); |
| 313 | |
| 314 | if (v >= 0) |
| 315 | is_bool = 1; |
| 316 | else if (!git_parse_int(value_, &v)) |
| 317 | return -1; |
| 318 | } else { |
| 319 | v = git_config_bool_or_int(key_, value_, kvi, |
| 320 | &is_bool); |
| 321 | } |
| 322 | |
| 323 | if (is_bool) |
| 324 | strbuf_addstr(buf, v ? "true" : "false"); |
| 325 | else |
| 326 | strbuf_addf(buf, "%d", v); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /* This mode is always gentle. */ |
| 332 | static int format_config_bool_or_str(struct strbuf *buf, |
| 333 | const char *value_) |
| 334 | { |
| 335 | int v = git_parse_maybe_bool(value_); |
| 336 | if (v < 0) |
| 337 | strbuf_addstr(buf, value_); |
| 338 | else |
| 339 | strbuf_addstr(buf, v ? "true" : "false"); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int format_config_path(struct strbuf *buf, |
| 344 | const char *key_, |
| 345 | const char *value_, |
| 346 | int gently) |
| 347 | { |
| 348 | char *v; |
| 349 | |
| 350 | if (git_config_pathname(&v, key_, value_) < 0) |
| 351 | return -1; |
| 352 | |
| 353 | if (v) |
| 354 | strbuf_addstr(buf, v); |
| 355 | else |
| 356 | return gently ? -1 : 1; /* :(optional)no-such-file */ |
| 357 | |
| 358 | free(v); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static int format_config_expiry_date(struct strbuf *buf, |
| 363 | const char *key_, |
| 364 | const char *value_, |
| 365 | int quietly) |
| 366 | { |
| 367 | timestamp_t t; |
| 368 | if (quietly) { |
| 369 | if (parse_expiry_date(value_, &t)) |
| 370 | return -1; |
| 371 | } else if (git_config_expiry_date(&t, key_, value_) < 0) { |
| 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | strbuf_addf(buf, "%"PRItime, t); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int format_config_color(struct strbuf *buf, |
| 380 | const char *key_, |
| 381 | const char *value_, |
| 382 | int gently) |
| 383 | { |
| 384 | char v[COLOR_MAXLEN]; |
| 385 | |
| 386 | if (gently) { |
| 387 | if (color_parse_quietly(value_, v) < 0) |
| 388 | return -1; |
| 389 | } else if (git_config_color(v, key_, value_) < 0) { |
| 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | strbuf_addstr(buf, v); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Format the configuration key-value pair (`key_`, `value_`) and |
| 399 | * append it into strbuf `buf`. Returns a negative value on failure, |
| 400 | * 0 on success, 1 on a missing optional value (i.e., telling the |
| 401 | * caller to pretend that <key_,value_> did not exist). |
| 402 | * |
| 403 | * Note: 'gently' is currently ignored, but will be implemented in |
| 404 | * a future change. |
| 405 | */ |
| 406 | static int format_config(const struct config_display_options *opts, |
| 407 | struct strbuf *buf, const char *key_, |
| 408 | const char *value_, const struct key_value_info *kvi, |
| 409 | int gently) |
| 410 | { |
| 411 | int res = 0; |
| 412 | if (opts->show_scope) |
| 413 | show_config_scope(opts, kvi, buf); |
| 414 | if (opts->show_origin) |
| 415 | show_config_origin(opts, kvi, buf); |
| 416 | if (opts->show_keys) |
| 417 | strbuf_addstr(buf, key_); |
| 418 | |
| 419 | if (opts->omit_values) |
| 420 | goto terminator; |
| 421 | |
| 422 | if (opts->show_keys) |
| 423 | strbuf_addch(buf, opts->key_delim); |
| 424 | |
| 425 | switch (opts->type) { |
| 426 | case TYPE_INT: |
| 427 | res = format_config_int64(buf, key_, value_, kvi, gently); |
| 428 | break; |
| 429 | |
| 430 | case TYPE_BOOL: |
| 431 | res = format_config_bool(buf, key_, value_, gently); |
| 432 | break; |
| 433 | |
| 434 | case TYPE_BOOL_OR_INT: |
| 435 | res = format_config_bool_or_int(buf, key_, value_, kvi, gently); |
| 436 | break; |
| 437 | |
| 438 | case TYPE_BOOL_OR_STR: |
| 439 | res = format_config_bool_or_str(buf, value_); |
| 440 | break; |
| 441 | |
| 442 | case TYPE_PATH: |
| 443 | res = format_config_path(buf, key_, value_, gently); |
| 444 | break; |
| 445 | |
| 446 | case TYPE_EXPIRY_DATE: |
| 447 | res = format_config_expiry_date(buf, key_, value_, gently); |
| 448 | break; |
| 449 | |
| 450 | case TYPE_COLOR: |
| 451 | res = format_config_color(buf, key_, value_, gently); |
| 452 | break; |
| 453 | |
| 454 | case TYPE_NONE: |
| 455 | if (value_) { |
| 456 | strbuf_addstr(buf, value_); |
| 457 | } else { |
| 458 | /* Just show the key name; back out delimiter */ |
| 459 | if (opts->show_keys) |
| 460 | strbuf_setlen(buf, buf->len - 1); |
| 461 | } |
| 462 | break; |
| 463 | |
| 464 | default: |
| 465 | BUG("undefined type %d", opts->type); |
| 466 | } |
| 467 | |
| 468 | terminator: |
| 469 | strbuf_addch(buf, opts->term); |
| 470 | return res; |
| 471 | } |
| 472 | |
| 473 | static int show_all_config(const char *key_, const char *value_, |
| 474 | const struct config_context *ctx, |
| 475 | void *cb) |
| 476 | { |
| 477 | const struct config_display_options *opts = cb; |
| 478 | const struct key_value_info *kvi = ctx->kvi; |
| 479 | struct strbuf formatted = STRBUF_INIT; |
| 480 | |
| 481 | if (format_config(opts, &formatted, key_, value_, kvi, 1) >= 0) |
| 482 | fwrite(formatted.buf, 1, formatted.len, stdout); |
| 483 | |
| 484 | strbuf_release(&formatted); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | #define GET_VALUE_ALL (1 << 0) |
| 489 | #define GET_VALUE_KEY_REGEXP (1 << 1) |
| 490 | |
| 491 | struct collect_config_data { |
| 492 | const struct config_display_options *display_opts; |
| 493 | struct strbuf_list *values; |
| 494 | const char *value_pattern; |
| 495 | const char *key; |
| 496 | regex_t *regexp; |
| 497 | regex_t *key_regexp; |
| 498 | int do_not_match; |
| 499 | unsigned get_value_flags; |
| 500 | unsigned flags; |
| 501 | }; |
| 502 | |
| 503 | static int collect_config(const char *key_, const char *value_, |
| 504 | const struct config_context *ctx, void *cb) |
| 505 | { |
| 506 | struct collect_config_data *data = cb; |
| 507 | struct strbuf_list *values = data->values; |
| 508 | const struct key_value_info *kvi = ctx->kvi; |
| 509 | int status; |
| 510 | |
| 511 | if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) && |
| 512 | strcmp(key_, data->key)) |
| 513 | return 0; |
| 514 | if ((data->get_value_flags & GET_VALUE_KEY_REGEXP) && |
| 515 | regexec(data->key_regexp, key_, 0, NULL, 0)) |
| 516 | return 0; |
| 517 | if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) && |
| 518 | strcmp(data->value_pattern, (value_?value_:""))) |
| 519 | return 0; |
| 520 | if (data->regexp && |
| 521 | (data->do_not_match ^ !!regexec(data->regexp, (value_?value_:""), 0, NULL, 0))) |
| 522 | return 0; |
| 523 | |
| 524 | ALLOC_GROW(values->items, values->nr + 1, values->alloc); |
| 525 | strbuf_init(&values->items[values->nr], 0); |
| 526 | |
| 527 | status = format_config(data->display_opts, &values->items[values->nr++], |
| 528 | key_, value_, kvi, 0); |
| 529 | if (status < 0) |
| 530 | return status; |
| 531 | if (status) { |
| 532 | strbuf_release(&values->items[--values->nr]); |
| 533 | status = 0; |
| 534 | } |
| 535 | return status; |
| 536 | } |
| 537 | |
| 538 | static int get_value(const struct config_location_options *opts, |
| 539 | const struct config_display_options *display_opts, |
| 540 | const char *key_, const char *regex_, |
| 541 | unsigned get_value_flags, unsigned flags) |
| 542 | { |
| 543 | int ret = CONFIG_GENERIC_ERROR; |
| 544 | struct strbuf_list values = {NULL}; |
| 545 | struct collect_config_data data = { |
| 546 | .display_opts = display_opts, |
| 547 | .values = &values, |
| 548 | .get_value_flags = get_value_flags, |
| 549 | .flags = flags, |
| 550 | }; |
| 551 | char *key = NULL; |
| 552 | int i; |
| 553 | |
| 554 | if (get_value_flags & GET_VALUE_KEY_REGEXP) { |
| 555 | char *tl; |
| 556 | |
| 557 | /* |
| 558 | * NEEDSWORK: this naive pattern lowercasing obviously does not |
| 559 | * work for more complex patterns like "^[^.]*Foo.*bar". |
| 560 | * Perhaps we should deprecate this altogether someday. |
| 561 | */ |
| 562 | |
| 563 | key = xstrdup(key_); |
| 564 | for (tl = key + strlen(key) - 1; |
| 565 | tl >= key && *tl != '.'; |
| 566 | tl--) |
| 567 | *tl = tolower(*tl); |
| 568 | for (tl = key; *tl && *tl != '.'; tl++) |
| 569 | *tl = tolower(*tl); |
| 570 | |
| 571 | data.key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
| 572 | if (regcomp(data.key_regexp, key, REG_EXTENDED)) { |
| 573 | error(_("invalid key pattern: %s"), key_); |
| 574 | FREE_AND_NULL(data.key_regexp); |
| 575 | ret = CONFIG_INVALID_PATTERN; |
| 576 | goto free_strings; |
| 577 | } |
| 578 | } else { |
| 579 | if (git_config_parse_key(key_, &key, NULL)) { |
| 580 | ret = CONFIG_INVALID_KEY; |
| 581 | goto free_strings; |
| 582 | } |
| 583 | |
| 584 | data.key = key; |
| 585 | } |
| 586 | |
| 587 | if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE)) |
| 588 | data.value_pattern = regex_; |
| 589 | else if (regex_) { |
| 590 | if (regex_[0] == '!') { |
| 591 | data.do_not_match = 1; |
| 592 | regex_++; |
| 593 | } |
| 594 | |
| 595 | data.regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
| 596 | if (regcomp(data.regexp, regex_, REG_EXTENDED)) { |
| 597 | error(_("invalid pattern: %s"), regex_); |
| 598 | FREE_AND_NULL(data.regexp); |
| 599 | ret = CONFIG_INVALID_PATTERN; |
| 600 | goto free_strings; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | config_with_options(collect_config, &data, |
| 605 | &opts->source, the_repository, |
| 606 | &opts->options); |
| 607 | |
| 608 | if (!values.nr && display_opts->default_value) { |
| 609 | struct key_value_info kvi = KVI_INIT; |
| 610 | struct strbuf *item; |
| 611 | int status; |
| 612 | |
| 613 | kvi_from_param(&kvi); |
| 614 | ALLOC_GROW(values.items, values.nr + 1, values.alloc); |
| 615 | item = &values.items[values.nr++]; |
| 616 | strbuf_init(item, 0); |
| 617 | |
| 618 | status = format_config(display_opts, item, key_, |
| 619 | display_opts->default_value, &kvi, 0); |
| 620 | if (status < 0) |
| 621 | die(_("failed to format default config value: %s"), |
| 622 | display_opts->default_value); |
| 623 | if (status) { |
| 624 | /* default was a missing optional value */ |
| 625 | values.nr--; |
| 626 | strbuf_release(item); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | ret = !values.nr; |
| 631 | |
| 632 | for (i = 0; i < values.nr; i++) { |
| 633 | struct strbuf *buf = values.items + i; |
| 634 | if ((get_value_flags & GET_VALUE_ALL) || i == values.nr - 1) |
| 635 | fwrite(buf->buf, 1, buf->len, stdout); |
| 636 | strbuf_release(buf); |
| 637 | } |
| 638 | free(values.items); |
| 639 | |
| 640 | free_strings: |
| 641 | free(key); |
| 642 | if (data.key_regexp) { |
| 643 | regfree(data.key_regexp); |
| 644 | free(data.key_regexp); |
| 645 | } |
| 646 | if (data.regexp) { |
| 647 | regfree(data.regexp); |
| 648 | free(data.regexp); |
| 649 | } |
| 650 | |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | static char *normalize_value(const char *key, const char *value, |
| 655 | int type, struct key_value_info *kvi) |
| 656 | { |
| 657 | if (!value) |
| 658 | return NULL; |
| 659 | |
| 660 | if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE) |
| 661 | /* |
| 662 | * We don't do normalization for TYPE_PATH here: If |
| 663 | * the path is like ~/foobar/, we prefer to store |
| 664 | * "~/foobar/" in the config file, and to expand the ~ |
| 665 | * when retrieving the value. |
| 666 | * Also don't do normalization for expiry dates. |
| 667 | */ |
| 668 | return xstrdup(value); |
| 669 | if (type == TYPE_INT) |
| 670 | return xstrfmt("%"PRId64, git_config_int64(key, value, kvi)); |
| 671 | if (type == TYPE_BOOL) |
| 672 | return xstrdup(git_config_bool(key, value) ? "true" : "false"); |
| 673 | if (type == TYPE_BOOL_OR_INT) { |
| 674 | int is_bool, v; |
| 675 | v = git_config_bool_or_int(key, value, kvi, &is_bool); |
| 676 | if (!is_bool) |
| 677 | return xstrfmt("%d", v); |
| 678 | else |
| 679 | return xstrdup(v ? "true" : "false"); |
| 680 | } |
| 681 | if (type == TYPE_BOOL_OR_STR) { |
| 682 | int v = git_parse_maybe_bool(value); |
| 683 | if (v < 0) |
| 684 | return xstrdup(value); |
| 685 | else |
| 686 | return xstrdup(v ? "true" : "false"); |
| 687 | } |
| 688 | if (type == TYPE_COLOR) { |
| 689 | char v[COLOR_MAXLEN]; |
| 690 | if (git_config_color(v, key, value)) |
| 691 | die(_("cannot parse color '%s'"), value); |
| 692 | |
| 693 | /* |
| 694 | * The contents of `v` now contain an ANSI escape |
| 695 | * sequence, not suitable for including within a |
| 696 | * configuration file. Treat the above as a |
| 697 | * "sanity-check", and return the given value, which we |
| 698 | * know is representable as valid color code. |
| 699 | */ |
| 700 | return xstrdup(value); |
| 701 | } |
| 702 | |
| 703 | BUG("cannot normalize type %d", type); |
| 704 | } |
| 705 | |
| 706 | struct get_color_config_data { |
| 707 | int get_color_found; |
| 708 | const char *get_color_slot; |
| 709 | char parsed_color[COLOR_MAXLEN]; |
| 710 | }; |
| 711 | |
| 712 | static int git_get_color_config(const char *var, const char *value, |
| 713 | const struct config_context *ctx UNUSED, |
| 714 | void *cb) |
| 715 | { |
| 716 | struct get_color_config_data *data = cb; |
| 717 | |
| 718 | if (!strcmp(var, data->get_color_slot)) { |
| 719 | if (!value) |
| 720 | config_error_nonbool(var); |
| 721 | if (color_parse(value, data->parsed_color) < 0) |
| 722 | return -1; |
| 723 | data->get_color_found = 1; |
| 724 | } |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int get_color(const struct config_location_options *opts, |
| 729 | const char *var, const char *def_color) |
| 730 | { |
| 731 | struct get_color_config_data data = { |
| 732 | .get_color_slot = var, |
| 733 | .parsed_color[0] = '\0', |
| 734 | }; |
| 735 | int ret; |
| 736 | |
| 737 | config_with_options(git_get_color_config, &data, |
| 738 | &opts->source, the_repository, |
| 739 | &opts->options); |
| 740 | |
| 741 | if (!data.get_color_found && def_color) { |
| 742 | if (color_parse(def_color, data.parsed_color) < 0) { |
| 743 | ret = error(_("unable to parse default color value")); |
| 744 | goto out; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | ret = 0; |
| 749 | |
| 750 | out: |
| 751 | fputs(data.parsed_color, stdout); |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | struct get_colorbool_config_data { |
| 756 | enum git_colorbool get_colorbool_found; |
| 757 | enum git_colorbool get_diff_color_found; |
| 758 | enum git_colorbool get_color_ui_found; |
| 759 | const char *get_colorbool_slot; |
| 760 | }; |
| 761 | |
| 762 | static int git_get_colorbool_config(const char *var, const char *value, |
| 763 | const struct config_context *ctx UNUSED, |
| 764 | void *cb) |
| 765 | { |
| 766 | struct get_colorbool_config_data *data = cb; |
| 767 | |
| 768 | if (!strcmp(var, data->get_colorbool_slot)) |
| 769 | data->get_colorbool_found = git_config_colorbool(var, value); |
| 770 | else if (!strcmp(var, "diff.color")) |
| 771 | data->get_diff_color_found = git_config_colorbool(var, value); |
| 772 | else if (!strcmp(var, "color.ui")) |
| 773 | data->get_color_ui_found = git_config_colorbool(var, value); |
| 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | static int get_colorbool(const struct config_location_options *opts, |
| 778 | const char *var, int print) |
| 779 | { |
| 780 | struct get_colorbool_config_data data = { |
| 781 | .get_colorbool_slot = var, |
| 782 | .get_colorbool_found = GIT_COLOR_UNKNOWN, |
| 783 | .get_diff_color_found = GIT_COLOR_UNKNOWN, |
| 784 | .get_color_ui_found = GIT_COLOR_UNKNOWN, |
| 785 | }; |
| 786 | bool result; |
| 787 | |
| 788 | config_with_options(git_get_colorbool_config, &data, |
| 789 | &opts->source, the_repository, |
| 790 | &opts->options); |
| 791 | |
| 792 | if (data.get_colorbool_found == GIT_COLOR_UNKNOWN) { |
| 793 | if (!strcmp(data.get_colorbool_slot, "color.diff")) |
| 794 | data.get_colorbool_found = data.get_diff_color_found; |
| 795 | if (data.get_colorbool_found == GIT_COLOR_UNKNOWN) |
| 796 | data.get_colorbool_found = data.get_color_ui_found; |
| 797 | } |
| 798 | |
| 799 | if (data.get_colorbool_found == GIT_COLOR_UNKNOWN) |
| 800 | /* default value if none found in config */ |
| 801 | data.get_colorbool_found = GIT_COLOR_AUTO; |
| 802 | |
| 803 | result = want_color(data.get_colorbool_found); |
| 804 | |
| 805 | if (print) { |
| 806 | printf("%s\n", result ? "true" : "false"); |
| 807 | return 0; |
| 808 | } else |
| 809 | return result ? 0 : 1; |
| 810 | } |
| 811 | |
| 812 | static void check_write(const struct git_config_source *source) |
| 813 | { |
| 814 | if (!source->file && !startup_info->have_repository) |
| 815 | die(_("not in a git directory")); |
| 816 | |
| 817 | if (source->use_stdin) |
| 818 | die(_("writing to stdin is not supported")); |
| 819 | |
| 820 | if (source->blob) |
| 821 | die(_("writing config blobs is not supported")); |
| 822 | } |
| 823 | |
| 824 | struct urlmatch_current_candidate_value { |
| 825 | char value_is_null; |
| 826 | struct strbuf value; |
| 827 | struct key_value_info kvi; |
| 828 | }; |
| 829 | |
| 830 | static int urlmatch_collect_fn(const char *var, const char *value, |
| 831 | const struct config_context *ctx, |
| 832 | void *cb) |
| 833 | { |
| 834 | struct string_list *values = cb; |
| 835 | struct string_list_item *item = string_list_insert(values, var); |
| 836 | struct urlmatch_current_candidate_value *matched = item->util; |
| 837 | const struct key_value_info *kvi = ctx->kvi; |
| 838 | |
| 839 | if (!matched) { |
| 840 | matched = xmalloc(sizeof(*matched)); |
| 841 | strbuf_init(&matched->value, 0); |
| 842 | item->util = matched; |
| 843 | } else { |
| 844 | strbuf_reset(&matched->value); |
| 845 | } |
| 846 | matched->kvi = *kvi; |
| 847 | |
| 848 | if (value) { |
| 849 | strbuf_addstr(&matched->value, value); |
| 850 | matched->value_is_null = 0; |
| 851 | } else { |
| 852 | matched->value_is_null = 1; |
| 853 | } |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | static int get_urlmatch(const struct config_location_options *opts, |
| 858 | const struct config_display_options *_display_opts, |
| 859 | const char *var, const char *url) |
| 860 | { |
| 861 | int ret; |
| 862 | char *section; |
| 863 | char *section_tail; |
| 864 | struct config_display_options display_opts = *_display_opts; |
| 865 | struct string_list_item *item; |
| 866 | struct urlmatch_config config = URLMATCH_CONFIG_INIT; |
| 867 | struct string_list values = STRING_LIST_INIT_DUP; |
| 868 | |
| 869 | config.collect_fn = urlmatch_collect_fn; |
| 870 | config.cascade_fn = NULL; |
| 871 | config.cb = &values; |
| 872 | |
| 873 | if (!url_normalize(url, &config.url)) |
| 874 | die("%s", config.url.err); |
| 875 | |
| 876 | config.section = section = xstrdup_tolower(var); |
| 877 | section_tail = strchr(section, '.'); |
| 878 | if (section_tail) { |
| 879 | *section_tail = '\0'; |
| 880 | config.key = section_tail + 1; |
| 881 | display_opts.show_keys = 0; |
| 882 | } else { |
| 883 | config.key = NULL; |
| 884 | display_opts.show_keys = 1; |
| 885 | } |
| 886 | |
| 887 | config_with_options(urlmatch_config_entry, &config, |
| 888 | &opts->source, the_repository, |
| 889 | &opts->options); |
| 890 | |
| 891 | ret = !values.nr; |
| 892 | |
| 893 | for_each_string_list_item(item, &values) { |
| 894 | struct urlmatch_current_candidate_value *matched = item->util; |
| 895 | struct strbuf buf = STRBUF_INIT; |
| 896 | int status; |
| 897 | |
| 898 | status = format_config(&display_opts, &buf, item->string, |
| 899 | matched->value_is_null ? NULL : matched->value.buf, |
| 900 | &matched->kvi, 0); |
| 901 | if (!status) |
| 902 | fwrite(buf.buf, 1, buf.len, stdout); |
| 903 | strbuf_release(&buf); |
| 904 | |
| 905 | strbuf_release(&matched->value); |
| 906 | } |
| 907 | urlmatch_config_release(&config); |
| 908 | string_list_clear(&values, 1); |
| 909 | free(config.url.url); |
| 910 | |
| 911 | free(section); |
| 912 | return ret; |
| 913 | } |
| 914 | |
| 915 | static char *default_user_config(void) |
| 916 | { |
| 917 | struct strbuf buf = STRBUF_INIT; |
| 918 | strbuf_addf(&buf, |
| 919 | _("# This is Git's per-user configuration file.\n" |
| 920 | "[user]\n" |
| 921 | "# Please adapt and uncomment the following lines:\n" |
| 922 | "# name = %s\n" |
| 923 | "# email = %s\n"), |
| 924 | ident_default_name(), |
| 925 | ident_default_email()); |
| 926 | return strbuf_detach(&buf, NULL); |
| 927 | } |
| 928 | |
| 929 | static void location_options_init(struct config_location_options *opts, |
| 930 | const char *prefix) |
| 931 | { |
| 932 | if (!opts->source.file) |
| 933 | opts->source.file = opts->file_to_free = |
| 934 | xstrdup_or_null(getenv(CONFIG_ENVIRONMENT)); |
| 935 | |
| 936 | if (opts->use_global_config + opts->use_system_config + |
| 937 | opts->use_local_config + opts->use_worktree_config + |
| 938 | !!opts->source.file + !!opts->source.blob > 1) { |
| 939 | error(_("only one config file at a time")); |
| 940 | exit(129); |
| 941 | } |
| 942 | |
| 943 | if (!startup_info->have_repository) { |
| 944 | if (opts->use_local_config) |
| 945 | die(_("--local can only be used inside a git repository")); |
| 946 | if (opts->source.blob) |
| 947 | die(_("--blob can only be used inside a git repository")); |
| 948 | if (opts->use_worktree_config) |
| 949 | die(_("--worktree can only be used inside a git repository")); |
| 950 | } |
| 951 | |
| 952 | if (opts->source.file && |
| 953 | !strcmp(opts->source.file, "-")) { |
| 954 | opts->source.file = NULL; |
| 955 | opts->source.use_stdin = 1; |
| 956 | opts->source.scope = CONFIG_SCOPE_COMMAND; |
| 957 | } |
| 958 | |
| 959 | if (opts->use_global_config) { |
| 960 | opts->source.file = opts->file_to_free = git_global_config(); |
| 961 | if (!opts->source.file) |
| 962 | /* |
| 963 | * It is unknown if HOME/.gitconfig exists, so |
| 964 | * we do not know if we should write to XDG |
| 965 | * location; error out even if XDG_CONFIG_HOME |
| 966 | * is set and points at a sane location. |
| 967 | */ |
| 968 | die(_("$HOME not set")); |
| 969 | opts->source.scope = CONFIG_SCOPE_GLOBAL; |
| 970 | } else if (opts->use_system_config) { |
| 971 | opts->source.file = opts->file_to_free = git_system_config(); |
| 972 | opts->source.scope = CONFIG_SCOPE_SYSTEM; |
| 973 | } else if (opts->use_local_config) { |
| 974 | opts->source.file = opts->file_to_free = repo_git_path(the_repository, "config"); |
| 975 | opts->source.scope = CONFIG_SCOPE_LOCAL; |
| 976 | } else if (opts->use_worktree_config) { |
| 977 | struct worktree **worktrees = get_worktrees(the_repository); |
| 978 | if (the_repository->repository_format_worktree_config) |
| 979 | opts->source.file = opts->file_to_free = |
| 980 | repo_git_path(the_repository, "config.worktree"); |
| 981 | else if (worktrees[0] && worktrees[1]) |
| 982 | die(_("--worktree cannot be used with multiple " |
| 983 | "working trees unless the config\n" |
| 984 | "extension worktreeConfig is enabled. " |
| 985 | "Please read \"CONFIGURATION FILE\"\n" |
| 986 | "section in \"git help worktree\" for details")); |
| 987 | else |
| 988 | opts->source.file = opts->file_to_free = |
| 989 | repo_git_path(the_repository, "config"); |
| 990 | opts->source.scope = CONFIG_SCOPE_LOCAL; |
| 991 | free_worktrees(worktrees); |
| 992 | } else if (opts->source.file) { |
| 993 | if (!is_absolute_path(opts->source.file) && prefix) |
| 994 | opts->source.file = opts->file_to_free = |
| 995 | prefix_filename(prefix, opts->source.file); |
| 996 | opts->source.scope = CONFIG_SCOPE_COMMAND; |
| 997 | } else if (opts->source.blob) { |
| 998 | opts->source.scope = CONFIG_SCOPE_COMMAND; |
| 999 | } |
| 1000 | |
| 1001 | if (opts->respect_includes_opt == -1) |
| 1002 | opts->options.respect_includes = !opts->source.file; |
| 1003 | else |
| 1004 | opts->options.respect_includes = opts->respect_includes_opt; |
| 1005 | if (startup_info->have_repository) { |
| 1006 | opts->options.commondir = repo_get_common_dir(the_repository); |
| 1007 | opts->options.git_dir = repo_get_git_dir(the_repository); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | static void location_options_release(struct config_location_options *opts) |
| 1012 | { |
| 1013 | free(opts->file_to_free); |
| 1014 | } |
| 1015 | |
| 1016 | static void display_options_init(struct config_display_options *opts) |
| 1017 | { |
| 1018 | if (opts->end_nul) { |
| 1019 | opts->term = '\0'; |
| 1020 | opts->delim = '\n'; |
| 1021 | opts->key_delim = '\n'; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | static void display_options_init_list(struct config_display_options *opts) |
| 1026 | { |
| 1027 | opts->show_keys = 1; |
| 1028 | |
| 1029 | if (opts->end_nul) { |
| 1030 | display_options_init(opts); |
| 1031 | } else { |
| 1032 | opts->term = '\n'; |
| 1033 | opts->delim = ' '; |
| 1034 | opts->key_delim = '='; |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | static int cmd_config_list(int argc, const char **argv, const char *prefix, |
| 1039 | struct repository *repo UNUSED) |
| 1040 | { |
| 1041 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
| 1042 | struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; |
| 1043 | struct option opts[] = { |
| 1044 | CONFIG_LOCATION_OPTIONS(location_opts), |
| 1045 | CONFIG_DISPLAY_OPTIONS(display_opts), |
| 1046 | OPT_GROUP(N_("Other")), |
| 1047 | OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, |
| 1048 | N_("respect include directives on lookup")), |
| 1049 | OPT_END(), |
| 1050 | }; |
| 1051 | |
| 1052 | argc = parse_options(argc, argv, prefix, opts, builtin_config_list_usage, 0); |
| 1053 | check_argc(argc, 0, 0); |
| 1054 | |
| 1055 | location_options_init(&location_opts, prefix); |
| 1056 | display_options_init_list(&display_opts); |
| 1057 | |
| 1058 | setup_auto_pager("config", 1); |
| 1059 | |
| 1060 | if (config_with_options(show_all_config, &display_opts, |
| 1061 | &location_opts.source, the_repository, |
| 1062 | &location_opts.options) < 0) { |
| 1063 | if (location_opts.source.file) |
| 1064 | die_errno(_("unable to read config file '%s'"), |
| 1065 | location_opts.source.file); |
| 1066 | else |
| 1067 | die(_("error processing config file(s)")); |
| 1068 | } |
| 1069 | |
| 1070 | location_options_release(&location_opts); |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | static int cmd_config_get(int argc, const char **argv, const char *prefix, |
| 1075 | struct repository *repo UNUSED) |
| 1076 | { |
| 1077 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
| 1078 | struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; |
| 1079 | const char *value_pattern = NULL, *url = NULL; |
| 1080 | int flags = 0; |
| 1081 | unsigned get_value_flags = 0; |
| 1082 | struct option opts[] = { |
| 1083 | CONFIG_LOCATION_OPTIONS(location_opts), |
| 1084 | OPT_GROUP(N_("Filter options")), |
| 1085 | OPT_BIT(0, "all", &get_value_flags, N_("return all values for multi-valued config options"), GET_VALUE_ALL), |
| 1086 | OPT_BIT(0, "regexp", &get_value_flags, N_("interpret the name as a regular expression"), GET_VALUE_KEY_REGEXP), |
| 1087 | OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")), |
| 1088 | OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), |
| 1089 | OPT_STRING(0, "url", &url, N_("URL"), N_("show config matching the given URL")), |
| 1090 | CONFIG_DISPLAY_OPTIONS(display_opts), |
| 1091 | OPT_GROUP(N_("Other")), |
| 1092 | OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, |
| 1093 | N_("respect include directives on lookup")), |
| 1094 | OPT_STRING(0, "default", &display_opts.default_value, |
| 1095 | N_("value"), N_("use default value when missing entry")), |
| 1096 | OPT_END(), |
| 1097 | }; |
| 1098 | int ret; |
| 1099 | |
| 1100 | argc = parse_options(argc, argv, prefix, opts, builtin_config_get_usage, |
| 1101 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1102 | check_argc(argc, 1, 1); |
| 1103 | |
| 1104 | if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern) |
| 1105 | die(_("--fixed-value only applies with 'value-pattern'")); |
| 1106 | if (display_opts.default_value && |
| 1107 | ((get_value_flags & GET_VALUE_ALL) || url)) |
| 1108 | die(_("--default= cannot be used with --all or --url=")); |
| 1109 | if (url && ((get_value_flags & GET_VALUE_ALL) || |
| 1110 | (get_value_flags & GET_VALUE_KEY_REGEXP) || |
| 1111 | value_pattern)) |
| 1112 | die(_("--url= cannot be used with --all, --regexp or --value")); |
| 1113 | |
| 1114 | location_options_init(&location_opts, prefix); |
| 1115 | display_options_init(&display_opts); |
| 1116 | |
| 1117 | if (display_opts.type != TYPE_COLOR) |
| 1118 | setup_auto_pager("config", 1); |
| 1119 | |
| 1120 | if (url) |
| 1121 | ret = get_urlmatch(&location_opts, &display_opts, argv[0], url); |
| 1122 | else if (display_opts.type == TYPE_COLOR && !strlen(argv[0]) && display_opts.default_value) |
| 1123 | ret = get_color(&location_opts, "", display_opts.default_value); |
| 1124 | else |
| 1125 | ret = get_value(&location_opts, &display_opts, argv[0], value_pattern, |
| 1126 | get_value_flags, flags); |
| 1127 | |
| 1128 | location_options_release(&location_opts); |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
| 1132 | static int cmd_config_set(int argc, const char **argv, const char *prefix, |
| 1133 | struct repository *repo UNUSED) |
| 1134 | { |
| 1135 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
| 1136 | const char *value_pattern = NULL, *comment_arg = NULL; |
| 1137 | char *comment = NULL; |
| 1138 | int flags = 0, append = 0, type = 0; |
| 1139 | struct option opts[] = { |
| 1140 | CONFIG_LOCATION_OPTIONS(location_opts), |
| 1141 | CONFIG_TYPE_OPTIONS(type), |
| 1142 | OPT_GROUP(N_("Filter")), |
| 1143 | OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE), |
| 1144 | OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")), |
| 1145 | OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), |
| 1146 | OPT_GROUP(N_("Other")), |
| 1147 | OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")), |
| 1148 | OPT_BOOL(0, "append", &append, N_("add a new line without altering any existing values")), |
| 1149 | OPT_END(), |
| 1150 | }; |
| 1151 | struct key_value_info default_kvi = KVI_INIT; |
| 1152 | char *value; |
| 1153 | int ret; |
| 1154 | |
| 1155 | argc = parse_options(argc, argv, prefix, opts, builtin_config_set_usage, |
| 1156 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1157 | if (argc == 1) |
| 1158 | die_missing_set_value(argv[0]); |
| 1159 | check_argc(argc, 2, 2); |
| 1160 | |
| 1161 | if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern) |
| 1162 | die(_("--fixed-value only applies with --value=<pattern>")); |
| 1163 | if (append && value_pattern) |
| 1164 | die(_("--append cannot be used with --value=<pattern>")); |
| 1165 | if (append) |
| 1166 | value_pattern = CONFIG_REGEX_NONE; |
| 1167 | |
| 1168 | comment = git_config_prepare_comment_string(comment_arg); |
| 1169 | |
| 1170 | location_options_init(&location_opts, prefix); |
| 1171 | check_write(&location_opts.source); |
| 1172 | |
| 1173 | value = normalize_value(argv[0], argv[1], type, &default_kvi); |
| 1174 | |
| 1175 | if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) { |
| 1176 | ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file, |
| 1177 | argv[0], value, value_pattern, |
| 1178 | comment, flags); |
| 1179 | } else { |
| 1180 | ret = repo_config_set_in_file_gently(the_repository, location_opts.source.file, |
| 1181 | argv[0], comment, value); |
| 1182 | if (ret == CONFIG_NOTHING_SET) |
| 1183 | error(_("cannot overwrite multiple values with a single value\n" |
| 1184 | " Use --value=<pattern>, --append or --all to change %s."), argv[0]); |
| 1185 | } |
| 1186 | |
| 1187 | location_options_release(&location_opts); |
| 1188 | free(comment); |
| 1189 | free(value); |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | static int cmd_config_unset(int argc, const char **argv, const char *prefix, |
| 1194 | struct repository *repo UNUSED) |
| 1195 | { |
| 1196 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
| 1197 | const char *value_pattern = NULL; |
| 1198 | int flags = 0; |
| 1199 | struct option opts[] = { |
| 1200 | CONFIG_LOCATION_OPTIONS(location_opts), |
| 1201 | OPT_GROUP(N_("Filter")), |
| 1202 | OPT_BIT(0, "all", &flags, N_("unset all multi-valued config options"), CONFIG_FLAGS_MULTI_REPLACE), |
| 1203 | OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("unset multi-valued config options with matching values")), |
| 1204 | OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), |
| 1205 | OPT_END(), |
| 1206 | }; |
| 1207 | int ret; |
| 1208 | |
| 1209 | argc = parse_options(argc, argv, prefix, opts, builtin_config_unset_usage, |
| 1210 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1211 | check_argc(argc, 1, 1); |
| 1212 | |
| 1213 | if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern) |
| 1214 | die(_("--fixed-value only applies with 'value-pattern'")); |
| 1215 | |
| 1216 | location_options_init(&location_opts, prefix); |
| 1217 | check_write(&location_opts.source); |
| 1218 | |
| 1219 | if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) |
| 1220 | ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file, |
| 1221 | argv[0], NULL, value_pattern, |
| 1222 | NULL, flags); |
| 1223 | else |
| 1224 | ret = repo_config_set_in_file_gently(the_repository, location_opts.source.file, argv[0], |
| 1225 | NULL, NULL); |
| 1226 | |
| 1227 | location_options_release(&location_opts); |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
| 1231 | static int cmd_config_rename_section(int argc, const char **argv, const char *prefix, |
| 1232 | struct repository *repo UNUSED) |
| 1233 | { |
| 1234 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
| 1235 | struct option opts[] = { |
| 1236 | CONFIG_LOCATION_OPTIONS(location_opts), |
| 1237 | OPT_END(), |
| 1238 | }; |
| 1239 | int ret; |
| 1240 | |
| 1241 | argc = parse_options(argc, argv, prefix, opts, builtin_config_rename_section_usage, |
| 1242 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1243 | check_argc(argc, 2, 2); |
| 1244 | |
| 1245 | location_options_init(&location_opts, prefix); |
| 1246 | check_write(&location_opts.source); |
| 1247 | |
| 1248 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, |
| 1249 | argv[0], argv[1]); |
| 1250 | if (ret < 0) |
| 1251 | goto out; |
| 1252 | else if (!ret) |
| 1253 | die(_("no such section: %s"), argv[0]); |
| 1254 | ret = 0; |
| 1255 | |
| 1256 | out: |
| 1257 | location_options_release(&location_opts); |
| 1258 | return ret; |
| 1259 | } |
| 1260 | |
| 1261 | static int cmd_config_remove_section(int argc, const char **argv, const char *prefix, |
| 1262 | struct repository *repo UNUSED) |
| 1263 | { |
| 1264 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
| 1265 | struct option opts[] = { |
| 1266 | CONFIG_LOCATION_OPTIONS(location_opts), |
| 1267 | OPT_END(), |
| 1268 | }; |
| 1269 | int ret; |
| 1270 | |
| 1271 | argc = parse_options(argc, argv, prefix, opts, builtin_config_remove_section_usage, |
| 1272 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1273 | check_argc(argc, 1, 1); |
| 1274 | |
| 1275 | location_options_init(&location_opts, prefix); |
| 1276 | check_write(&location_opts.source); |
| 1277 | |
| 1278 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, |
| 1279 | argv[0], NULL); |
| 1280 | if (ret < 0) |
| 1281 | goto out; |
| 1282 | else if (!ret) |
| 1283 | die(_("no such section: %s"), argv[0]); |
| 1284 | ret = 0; |
| 1285 | |
| 1286 | out: |
| 1287 | location_options_release(&location_opts); |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
| 1291 | static int show_editor(struct config_location_options *opts) |
| 1292 | { |
| 1293 | char *config_file; |
| 1294 | |
| 1295 | if (!opts->source.file && !startup_info->have_repository) |
| 1296 | die(_("not in a git directory")); |
| 1297 | if (opts->source.use_stdin) |
| 1298 | die(_("editing stdin is not supported")); |
| 1299 | if (opts->source.blob) |
| 1300 | die(_("editing blobs is not supported")); |
| 1301 | repo_config(the_repository, git_default_config, NULL); |
| 1302 | config_file = opts->source.file ? |
| 1303 | xstrdup(opts->source.file) : |
| 1304 | repo_git_path(the_repository, "config"); |
| 1305 | if (opts->use_global_config) { |
| 1306 | int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666); |
| 1307 | if (fd >= 0) { |
| 1308 | char *content = default_user_config(); |
| 1309 | write_str_in_full(fd, content); |
| 1310 | free(content); |
| 1311 | close(fd); |
| 1312 | } |
| 1313 | else if (errno != EEXIST) |
| 1314 | die_errno(_("cannot create configuration file %s"), config_file); |
| 1315 | } |
| 1316 | launch_editor(config_file, NULL, NULL); |
| 1317 | free(config_file); |
| 1318 | |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | static int cmd_config_edit(int argc, const char **argv, const char *prefix, |
| 1323 | struct repository *repo UNUSED) |
| 1324 | { |
| 1325 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
| 1326 | struct option opts[] = { |
| 1327 | CONFIG_LOCATION_OPTIONS(location_opts), |
| 1328 | OPT_END(), |
| 1329 | }; |
| 1330 | int ret; |
| 1331 | |
| 1332 | argc = parse_options(argc, argv, prefix, opts, builtin_config_edit_usage, 0); |
| 1333 | check_argc(argc, 0, 0); |
| 1334 | |
| 1335 | location_options_init(&location_opts, prefix); |
| 1336 | check_write(&location_opts.source); |
| 1337 | |
| 1338 | ret = show_editor(&location_opts); |
| 1339 | location_options_release(&location_opts); |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
| 1343 | static int cmd_config_actions(int argc, const char **argv, const char *prefix) |
| 1344 | { |
| 1345 | enum { |
| 1346 | ACTION_GET = (1<<0), |
| 1347 | ACTION_GET_ALL = (1<<1), |
| 1348 | ACTION_GET_REGEXP = (1<<2), |
| 1349 | ACTION_REPLACE_ALL = (1<<3), |
| 1350 | ACTION_ADD = (1<<4), |
| 1351 | ACTION_UNSET = (1<<5), |
| 1352 | ACTION_UNSET_ALL = (1<<6), |
| 1353 | ACTION_RENAME_SECTION = (1<<7), |
| 1354 | ACTION_REMOVE_SECTION = (1<<8), |
| 1355 | ACTION_LIST = (1<<9), |
| 1356 | ACTION_EDIT = (1<<10), |
| 1357 | ACTION_SET = (1<<11), |
| 1358 | ACTION_SET_ALL = (1<<12), |
| 1359 | ACTION_GET_COLOR = (1<<13), |
| 1360 | ACTION_GET_COLORBOOL = (1<<14), |
| 1361 | ACTION_GET_URLMATCH = (1<<15), |
| 1362 | }; |
| 1363 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
| 1364 | struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; |
| 1365 | const char *comment_arg = NULL; |
| 1366 | int actions = 0; |
| 1367 | unsigned flags = 0; |
| 1368 | struct option opts[] = { |
| 1369 | CONFIG_LOCATION_OPTIONS(location_opts), |
| 1370 | OPT_GROUP(N_("Action")), |
| 1371 | OPT_CMDMODE(0, "get", &actions, N_("get value: name [<value-pattern>]"), ACTION_GET), |
| 1372 | OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL), |
| 1373 | OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP), |
| 1374 | OPT_CMDMODE(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH), |
| 1375 | OPT_CMDMODE(0, "replace-all", &actions, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL), |
| 1376 | OPT_CMDMODE(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD), |
| 1377 | OPT_CMDMODE(0, "unset", &actions, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET), |
| 1378 | OPT_CMDMODE(0, "unset-all", &actions, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL), |
| 1379 | OPT_CMDMODE(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), |
| 1380 | OPT_CMDMODE(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), |
| 1381 | OPT_CMDMODE('l', "list", &actions, N_("list all"), ACTION_LIST), |
| 1382 | OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), |
| 1383 | OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR), |
| 1384 | OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL), |
| 1385 | CONFIG_DISPLAY_OPTIONS(display_opts), |
| 1386 | OPT_GROUP(N_("Other")), |
| 1387 | OPT_STRING(0, "default", &display_opts.default_value, |
| 1388 | N_("value"), N_("with --get, use default value when missing entry")), |
| 1389 | OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")), |
| 1390 | OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), |
| 1391 | OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, |
| 1392 | N_("respect include directives on lookup")), |
| 1393 | OPT_END(), |
| 1394 | }; |
| 1395 | char *value = NULL, *comment = NULL; |
| 1396 | int ret = 0; |
| 1397 | int actions_implicit; |
| 1398 | struct key_value_info default_kvi = KVI_INIT; |
| 1399 | |
| 1400 | argc = parse_options(argc, argv, prefix, opts, |
| 1401 | builtin_config_usage, |
| 1402 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1403 | |
| 1404 | location_options_init(&location_opts, prefix); |
| 1405 | display_options_init(&display_opts); |
| 1406 | |
| 1407 | if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && display_opts.type) { |
| 1408 | error(_("--get-color and variable type are incoherent")); |
| 1409 | exit(129); |
| 1410 | } |
| 1411 | |
| 1412 | actions_implicit = (actions == 0); |
| 1413 | if (actions_implicit) |
| 1414 | switch (argc) { |
| 1415 | case 1: actions = ACTION_GET; break; |
| 1416 | case 2: actions = ACTION_SET; break; |
| 1417 | case 3: actions = ACTION_SET_ALL; break; |
| 1418 | default: |
| 1419 | error(_("no action specified")); |
| 1420 | exit(129); |
| 1421 | } |
| 1422 | if (actions_implicit && argc == 1) { |
| 1423 | const char *last_dot = strrchr(argv[0], '.'); |
| 1424 | if (last_dot && strchr(last_dot + 1, '=')) |
| 1425 | die_missing_set_value(argv[0]); |
| 1426 | } |
| 1427 | if (display_opts.omit_values && |
| 1428 | !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) { |
| 1429 | error(_("--name-only is only applicable to --list or --get-regexp")); |
| 1430 | exit(129); |
| 1431 | } |
| 1432 | |
| 1433 | if (display_opts.show_origin && !(actions & |
| 1434 | (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) { |
| 1435 | error(_("--show-origin is only applicable to --get, --get-all, " |
| 1436 | "--get-regexp, and --list")); |
| 1437 | exit(129); |
| 1438 | } |
| 1439 | |
| 1440 | if (display_opts.default_value && !(actions & ACTION_GET)) { |
| 1441 | error(_("--default is only applicable to --get")); |
| 1442 | exit(129); |
| 1443 | } |
| 1444 | |
| 1445 | if (comment_arg && |
| 1446 | !(actions & (ACTION_ADD|ACTION_SET|ACTION_SET_ALL|ACTION_REPLACE_ALL))) { |
| 1447 | error(_("--comment is only applicable to add/set/replace operations")); |
| 1448 | exit(129); |
| 1449 | } |
| 1450 | |
| 1451 | /* check usage of --fixed-value */ |
| 1452 | if (flags & CONFIG_FLAGS_FIXED_VALUE) { |
| 1453 | int allowed_usage = 0; |
| 1454 | |
| 1455 | switch (actions) { |
| 1456 | /* git config --get <name> <value-pattern> */ |
| 1457 | case ACTION_GET: |
| 1458 | /* git config --get-all <name> <value-pattern> */ |
| 1459 | case ACTION_GET_ALL: |
| 1460 | /* git config --get-regexp <name-pattern> <value-pattern> */ |
| 1461 | case ACTION_GET_REGEXP: |
| 1462 | /* git config --unset <name> <value-pattern> */ |
| 1463 | case ACTION_UNSET: |
| 1464 | /* git config --unset-all <name> <value-pattern> */ |
| 1465 | case ACTION_UNSET_ALL: |
| 1466 | allowed_usage = argc > 1 && !!argv[1]; |
| 1467 | break; |
| 1468 | |
| 1469 | /* git config <name> <value> <value-pattern> */ |
| 1470 | case ACTION_SET_ALL: |
| 1471 | /* git config --replace-all <name> <value> <value-pattern> */ |
| 1472 | case ACTION_REPLACE_ALL: |
| 1473 | allowed_usage = argc > 2 && !!argv[2]; |
| 1474 | break; |
| 1475 | |
| 1476 | /* other options don't allow --fixed-value */ |
| 1477 | } |
| 1478 | |
| 1479 | if (!allowed_usage) { |
| 1480 | error(_("--fixed-value only applies with 'value-pattern'")); |
| 1481 | exit(129); |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | comment = git_config_prepare_comment_string(comment_arg); |
| 1486 | |
| 1487 | /* |
| 1488 | * The following actions may produce more than one line of output and |
| 1489 | * should therefore be paged. |
| 1490 | */ |
| 1491 | if (actions & (ACTION_LIST | ACTION_GET_ALL | ACTION_GET_REGEXP | ACTION_GET_URLMATCH)) |
| 1492 | setup_auto_pager("config", 1); |
| 1493 | |
| 1494 | if (actions == ACTION_LIST) { |
| 1495 | check_argc(argc, 0, 0); |
| 1496 | display_options_init_list(&display_opts); |
| 1497 | if (config_with_options(show_all_config, &display_opts, |
| 1498 | &location_opts.source, the_repository, |
| 1499 | &location_opts.options) < 0) { |
| 1500 | if (location_opts.source.file) |
| 1501 | die_errno(_("unable to read config file '%s'"), |
| 1502 | location_opts.source.file); |
| 1503 | else |
| 1504 | die(_("error processing config file(s)")); |
| 1505 | } |
| 1506 | } |
| 1507 | else if (actions == ACTION_EDIT) { |
| 1508 | ret = show_editor(&location_opts); |
| 1509 | } |
| 1510 | else if (actions == ACTION_SET) { |
| 1511 | check_write(&location_opts.source); |
| 1512 | check_argc(argc, 2, 2); |
| 1513 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); |
| 1514 | ret = repo_config_set_in_file_gently(the_repository, location_opts.source.file, argv[0], comment, value); |
| 1515 | if (ret == CONFIG_NOTHING_SET) |
| 1516 | error(_("cannot overwrite multiple values with a single value\n" |
| 1517 | " Use a regexp, --add or --replace-all to change %s."), argv[0]); |
| 1518 | } |
| 1519 | else if (actions == ACTION_SET_ALL) { |
| 1520 | check_write(&location_opts.source); |
| 1521 | check_argc(argc, 2, 3); |
| 1522 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); |
| 1523 | ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file, |
| 1524 | argv[0], value, argv[2], |
| 1525 | comment, flags); |
| 1526 | } |
| 1527 | else if (actions == ACTION_ADD) { |
| 1528 | check_write(&location_opts.source); |
| 1529 | check_argc(argc, 2, 2); |
| 1530 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); |
| 1531 | ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file, |
| 1532 | argv[0], value, |
| 1533 | CONFIG_REGEX_NONE, |
| 1534 | comment, flags); |
| 1535 | } |
| 1536 | else if (actions == ACTION_REPLACE_ALL) { |
| 1537 | check_write(&location_opts.source); |
| 1538 | check_argc(argc, 2, 3); |
| 1539 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); |
| 1540 | ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file, |
| 1541 | argv[0], value, argv[2], |
| 1542 | comment, flags | CONFIG_FLAGS_MULTI_REPLACE); |
| 1543 | } |
| 1544 | else if (actions == ACTION_GET) { |
| 1545 | check_argc(argc, 1, 2); |
| 1546 | ret = get_value(&location_opts, &display_opts, argv[0], argv[1], |
| 1547 | 0, flags); |
| 1548 | } |
| 1549 | else if (actions == ACTION_GET_ALL) { |
| 1550 | check_argc(argc, 1, 2); |
| 1551 | ret = get_value(&location_opts, &display_opts, argv[0], argv[1], |
| 1552 | GET_VALUE_ALL, flags); |
| 1553 | } |
| 1554 | else if (actions == ACTION_GET_REGEXP) { |
| 1555 | display_opts.show_keys = 1; |
| 1556 | check_argc(argc, 1, 2); |
| 1557 | ret = get_value(&location_opts, &display_opts, argv[0], argv[1], |
| 1558 | GET_VALUE_ALL|GET_VALUE_KEY_REGEXP, flags); |
| 1559 | } |
| 1560 | else if (actions == ACTION_GET_URLMATCH) { |
| 1561 | check_argc(argc, 2, 2); |
| 1562 | ret = get_urlmatch(&location_opts, &display_opts, argv[0], argv[1]); |
| 1563 | } |
| 1564 | else if (actions == ACTION_UNSET) { |
| 1565 | check_write(&location_opts.source); |
| 1566 | check_argc(argc, 1, 2); |
| 1567 | if (argc == 2) |
| 1568 | ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file, |
| 1569 | argv[0], NULL, argv[1], |
| 1570 | NULL, flags); |
| 1571 | else |
| 1572 | ret = repo_config_set_in_file_gently(the_repository, location_opts.source.file, |
| 1573 | argv[0], NULL, NULL); |
| 1574 | } |
| 1575 | else if (actions == ACTION_UNSET_ALL) { |
| 1576 | check_write(&location_opts.source); |
| 1577 | check_argc(argc, 1, 2); |
| 1578 | ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file, |
| 1579 | argv[0], NULL, argv[1], |
| 1580 | NULL, flags | CONFIG_FLAGS_MULTI_REPLACE); |
| 1581 | } |
| 1582 | else if (actions == ACTION_RENAME_SECTION) { |
| 1583 | check_write(&location_opts.source); |
| 1584 | check_argc(argc, 2, 2); |
| 1585 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, |
| 1586 | argv[0], argv[1]); |
| 1587 | if (ret < 0) |
| 1588 | goto out; |
| 1589 | else if (!ret) |
| 1590 | die(_("no such section: %s"), argv[0]); |
| 1591 | else |
| 1592 | ret = 0; |
| 1593 | } |
| 1594 | else if (actions == ACTION_REMOVE_SECTION) { |
| 1595 | check_write(&location_opts.source); |
| 1596 | check_argc(argc, 1, 1); |
| 1597 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, |
| 1598 | argv[0], NULL); |
| 1599 | if (ret < 0) |
| 1600 | goto out; |
| 1601 | else if (!ret) |
| 1602 | die(_("no such section: %s"), argv[0]); |
| 1603 | else |
| 1604 | ret = 0; |
| 1605 | } |
| 1606 | else if (actions == ACTION_GET_COLOR) { |
| 1607 | check_argc(argc, 1, 2); |
| 1608 | ret = get_color(&location_opts, argv[0], argv[1]); |
| 1609 | } |
| 1610 | else if (actions == ACTION_GET_COLORBOOL) { |
| 1611 | check_argc(argc, 1, 2); |
| 1612 | if (argc == 2) |
| 1613 | color_stdout_is_tty = git_config_bool("command line", argv[1]); |
| 1614 | ret = get_colorbool(&location_opts, argv[0], argc == 2); |
| 1615 | } |
| 1616 | |
| 1617 | out: |
| 1618 | location_options_release(&location_opts); |
| 1619 | free(comment); |
| 1620 | free(value); |
| 1621 | return ret; |
| 1622 | } |
| 1623 | |
| 1624 | int cmd_config(int argc, |
| 1625 | const char **argv, |
| 1626 | const char *prefix, |
| 1627 | struct repository *repo) |
| 1628 | { |
| 1629 | parse_opt_subcommand_fn *subcommand = NULL; |
| 1630 | struct option subcommand_opts[] = { |
| 1631 | OPT_SUBCOMMAND("list", &subcommand, cmd_config_list), |
| 1632 | OPT_SUBCOMMAND("get", &subcommand, cmd_config_get), |
| 1633 | OPT_SUBCOMMAND("set", &subcommand, cmd_config_set), |
| 1634 | OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset), |
| 1635 | OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section), |
| 1636 | OPT_SUBCOMMAND("remove-section", &subcommand, cmd_config_remove_section), |
| 1637 | OPT_SUBCOMMAND("edit", &subcommand, cmd_config_edit), |
| 1638 | OPT_END(), |
| 1639 | }; |
| 1640 | |
| 1641 | /* |
| 1642 | * This is somewhat hacky: we first parse the command line while |
| 1643 | * keeping all args intact in order to determine whether a subcommand |
| 1644 | * has been specified. If so, we re-parse it a second time, but this |
| 1645 | * time we drop KEEP_ARGV0. This is so that we don't munge the command |
| 1646 | * line in case no subcommand was given, which would otherwise confuse |
| 1647 | * us when parsing the legacy-style modes that don't use subcommands. |
| 1648 | */ |
| 1649 | argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage, |
| 1650 | PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT); |
| 1651 | if (subcommand) { |
| 1652 | argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage, |
| 1653 | PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT); |
| 1654 | return subcommand(argc, argv, prefix, repo); |
| 1655 | } |
| 1656 | |
| 1657 | return cmd_config_actions(argc, argv, prefix); |
| 1658 | } |