builtin / config.con commit wt-status.h: drop stdio.h include (7a06fb0)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "config.h"
   4#include "color.h"
   5#include "parse-options.h"
   6#include "urlmatch.h"
   7#include "quote.h"
   8#include "worktree.h"
   9
  10static const char *const builtin_config_usage[] = {
  11        N_("git config [<options>]"),
  12        NULL
  13};
  14
  15static char *key;
  16static regex_t *key_regexp;
  17static regex_t *regexp;
  18static int show_keys;
  19static int omit_values;
  20static int use_key_regexp;
  21static int do_all;
  22static int do_not_match;
  23static char delim = '=';
  24static char key_delim = ' ';
  25static char term = '\n';
  26
  27static int use_global_config, use_system_config, use_local_config;
  28static int use_worktree_config;
  29static struct git_config_source given_config_source;
  30static int actions, type;
  31static char *default_value;
  32static int end_null;
  33static int respect_includes_opt = -1;
  34static struct config_options config_options;
  35static int show_origin;
  36
  37#define ACTION_GET (1<<0)
  38#define ACTION_GET_ALL (1<<1)
  39#define ACTION_GET_REGEXP (1<<2)
  40#define ACTION_REPLACE_ALL (1<<3)
  41#define ACTION_ADD (1<<4)
  42#define ACTION_UNSET (1<<5)
  43#define ACTION_UNSET_ALL (1<<6)
  44#define ACTION_RENAME_SECTION (1<<7)
  45#define ACTION_REMOVE_SECTION (1<<8)
  46#define ACTION_LIST (1<<9)
  47#define ACTION_EDIT (1<<10)
  48#define ACTION_SET (1<<11)
  49#define ACTION_SET_ALL (1<<12)
  50#define ACTION_GET_COLOR (1<<13)
  51#define ACTION_GET_COLORBOOL (1<<14)
  52#define ACTION_GET_URLMATCH (1<<15)
  53
  54/*
  55 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
  56 * one line of output and which should therefore be paged.
  57 */
  58#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
  59                        ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
  60
  61#define TYPE_BOOL               1
  62#define TYPE_INT                2
  63#define TYPE_BOOL_OR_INT        3
  64#define TYPE_PATH               4
  65#define TYPE_EXPIRY_DATE        5
  66#define TYPE_COLOR              6
  67
  68#define OPT_CALLBACK_VALUE(s, l, v, h, i) \
  69        { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
  70        PARSE_OPT_NONEG, option_parse_type, (i) }
  71
  72static NORETURN void usage_builtin_config(void);
  73
  74static int option_parse_type(const struct option *opt, const char *arg,
  75                             int unset)
  76{
  77        int new_type, *to_type;
  78
  79        if (unset) {
  80                *((int *) opt->value) = 0;
  81                return 0;
  82        }
  83
  84        /*
  85         * To support '--<type>' style flags, begin with new_type equal to
  86         * opt->defval.
  87         */
  88        new_type = opt->defval;
  89        if (!new_type) {
  90                if (!strcmp(arg, "bool"))
  91                        new_type = TYPE_BOOL;
  92                else if (!strcmp(arg, "int"))
  93                        new_type = TYPE_INT;
  94                else if (!strcmp(arg, "bool-or-int"))
  95                        new_type = TYPE_BOOL_OR_INT;
  96                else if (!strcmp(arg, "path"))
  97                        new_type = TYPE_PATH;
  98                else if (!strcmp(arg, "expiry-date"))
  99                        new_type = TYPE_EXPIRY_DATE;
 100                else if (!strcmp(arg, "color"))
 101                        new_type = TYPE_COLOR;
 102                else
 103                        die(_("unrecognized --type argument, %s"), arg);
 104        }
 105
 106        to_type = opt->value;
 107        if (*to_type && *to_type != new_type) {
 108                /*
 109                 * Complain when there is a new type not equal to the old type.
 110                 * This allows for combinations like '--int --type=int' and
 111                 * '--type=int --type=int', but disallows ones like '--type=bool
 112                 * --int' and '--type=bool
 113                 * --type=int'.
 114                 */
 115                error(_("only one type at a time"));
 116                usage_builtin_config();
 117        }
 118        *to_type = new_type;
 119
 120        return 0;
 121}
 122
 123static struct option builtin_config_options[] = {
 124        OPT_GROUP(N_("Config file location")),
 125        OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
 126        OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
 127        OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
 128        OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
 129        OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
 130        OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
 131        OPT_GROUP(N_("Action")),
 132        OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
 133        OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
 134        OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
 135        OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
 136        OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
 137        OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
 138        OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
 139        OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
 140        OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
 141        OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
 142        OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
 143        OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
 144        OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
 145        OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
 146        OPT_GROUP(N_("Type")),
 147        OPT_CALLBACK('t', "type", &type, "", N_("value is given this type"), option_parse_type),
 148        OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
 149        OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
 150        OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
 151        OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
 152        OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
 153        OPT_GROUP(N_("Other")),
 154        OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
 155        OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
 156        OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
 157        OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
 158        OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
 159        OPT_END(),
 160};
 161
 162static NORETURN void usage_builtin_config(void)
 163{
 164        usage_with_options(builtin_config_usage, builtin_config_options);
 165}
 166
 167static void check_argc(int argc, int min, int max)
 168{
 169        if (argc >= min && argc <= max)
 170                return;
 171        if (min == max)
 172                error(_("wrong number of arguments, should be %d"), min);
 173        else
 174                error(_("wrong number of arguments, should be from %d to %d"),
 175                      min, max);
 176        usage_builtin_config();
 177}
 178
 179static void show_config_origin(struct strbuf *buf)
 180{
 181        const char term = end_null ? '\0' : '\t';
 182
 183        strbuf_addstr(buf, current_config_origin_type());
 184        strbuf_addch(buf, ':');
 185        if (end_null)
 186                strbuf_addstr(buf, current_config_name());
 187        else
 188                quote_c_style(current_config_name(), buf, NULL, 0);
 189        strbuf_addch(buf, term);
 190}
 191
 192static int show_all_config(const char *key_, const char *value_, void *cb)
 193{
 194        if (show_origin) {
 195                struct strbuf buf = STRBUF_INIT;
 196                show_config_origin(&buf);
 197                /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
 198                fwrite(buf.buf, 1, buf.len, stdout);
 199                strbuf_release(&buf);
 200        }
 201        if (!omit_values && value_)
 202                printf("%s%c%s%c", key_, delim, value_, term);
 203        else
 204                printf("%s%c", key_, term);
 205        return 0;
 206}
 207
 208struct strbuf_list {
 209        struct strbuf *items;
 210        int nr;
 211        int alloc;
 212};
 213
 214static int format_config(struct strbuf *buf, const char *key_, const char *value_)
 215{
 216        if (show_origin)
 217                show_config_origin(buf);
 218        if (show_keys)
 219                strbuf_addstr(buf, key_);
 220        if (!omit_values) {
 221                if (show_keys)
 222                        strbuf_addch(buf, key_delim);
 223
 224                if (type == TYPE_INT)
 225                        strbuf_addf(buf, "%"PRId64,
 226                                    git_config_int64(key_, value_ ? value_ : ""));
 227                else if (type == TYPE_BOOL)
 228                        strbuf_addstr(buf, git_config_bool(key_, value_) ?
 229                                      "true" : "false");
 230                else if (type == TYPE_BOOL_OR_INT) {
 231                        int is_bool, v;
 232                        v = git_config_bool_or_int(key_, value_, &is_bool);
 233                        if (is_bool)
 234                                strbuf_addstr(buf, v ? "true" : "false");
 235                        else
 236                                strbuf_addf(buf, "%d", v);
 237                } else if (type == TYPE_PATH) {
 238                        const char *v;
 239                        if (git_config_pathname(&v, key_, value_) < 0)
 240                                return -1;
 241                        strbuf_addstr(buf, v);
 242                        free((char *)v);
 243                } else if (type == TYPE_EXPIRY_DATE) {
 244                        timestamp_t t;
 245                        if (git_config_expiry_date(&t, key_, value_) < 0)
 246                                return -1;
 247                        strbuf_addf(buf, "%"PRItime, t);
 248                } else if (type == TYPE_COLOR) {
 249                        char v[COLOR_MAXLEN];
 250                        if (git_config_color(v, key_, value_) < 0)
 251                                return -1;
 252                        strbuf_addstr(buf, v);
 253                } else if (value_) {
 254                        strbuf_addstr(buf, value_);
 255                } else {
 256                        /* Just show the key name; back out delimiter */
 257                        if (show_keys)
 258                                strbuf_setlen(buf, buf->len - 1);
 259                }
 260        }
 261        strbuf_addch(buf, term);
 262        return 0;
 263}
 264
 265static int collect_config(const char *key_, const char *value_, void *cb)
 266{
 267        struct strbuf_list *values = cb;
 268
 269        if (!use_key_regexp && strcmp(key_, key))
 270                return 0;
 271        if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
 272                return 0;
 273        if (regexp != NULL &&
 274            (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
 275                return 0;
 276
 277        ALLOC_GROW(values->items, values->nr + 1, values->alloc);
 278        strbuf_init(&values->items[values->nr], 0);
 279
 280        return format_config(&values->items[values->nr++], key_, value_);
 281}
 282
 283static int get_value(const char *key_, const char *regex_)
 284{
 285        int ret = CONFIG_GENERIC_ERROR;
 286        struct strbuf_list values = {NULL};
 287        int i;
 288
 289        if (use_key_regexp) {
 290                char *tl;
 291
 292                /*
 293                 * NEEDSWORK: this naive pattern lowercasing obviously does not
 294                 * work for more complex patterns like "^[^.]*Foo.*bar".
 295                 * Perhaps we should deprecate this altogether someday.
 296                 */
 297
 298                key = xstrdup(key_);
 299                for (tl = key + strlen(key) - 1;
 300                     tl >= key && *tl != '.';
 301                     tl--)
 302                        *tl = tolower(*tl);
 303                for (tl = key; *tl && *tl != '.'; tl++)
 304                        *tl = tolower(*tl);
 305
 306                key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 307                if (regcomp(key_regexp, key, REG_EXTENDED)) {
 308                        error(_("invalid key pattern: %s"), key_);
 309                        FREE_AND_NULL(key_regexp);
 310                        ret = CONFIG_INVALID_PATTERN;
 311                        goto free_strings;
 312                }
 313        } else {
 314                if (git_config_parse_key(key_, &key, NULL)) {
 315                        ret = CONFIG_INVALID_KEY;
 316                        goto free_strings;
 317                }
 318        }
 319
 320        if (regex_) {
 321                if (regex_[0] == '!') {
 322                        do_not_match = 1;
 323                        regex_++;
 324                }
 325
 326                regexp = (regex_t*)xmalloc(sizeof(regex_t));
 327                if (regcomp(regexp, regex_, REG_EXTENDED)) {
 328                        error(_("invalid pattern: %s"), regex_);
 329                        FREE_AND_NULL(regexp);
 330                        ret = CONFIG_INVALID_PATTERN;
 331                        goto free_strings;
 332                }
 333        }
 334
 335        config_with_options(collect_config, &values,
 336                            &given_config_source, &config_options);
 337
 338        if (!values.nr && default_value) {
 339                struct strbuf *item;
 340                ALLOC_GROW(values.items, values.nr + 1, values.alloc);
 341                item = &values.items[values.nr++];
 342                strbuf_init(item, 0);
 343                if (format_config(item, key_, default_value) < 0)
 344                        die(_("failed to format default config value: %s"),
 345                                default_value);
 346        }
 347
 348        ret = !values.nr;
 349
 350        for (i = 0; i < values.nr; i++) {
 351                struct strbuf *buf = values.items + i;
 352                if (do_all || i == values.nr - 1)
 353                        fwrite(buf->buf, 1, buf->len, stdout);
 354                strbuf_release(buf);
 355        }
 356        free(values.items);
 357
 358free_strings:
 359        free(key);
 360        if (key_regexp) {
 361                regfree(key_regexp);
 362                free(key_regexp);
 363        }
 364        if (regexp) {
 365                regfree(regexp);
 366                free(regexp);
 367        }
 368
 369        return ret;
 370}
 371
 372static char *normalize_value(const char *key, const char *value)
 373{
 374        if (!value)
 375                return NULL;
 376
 377        if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
 378                /*
 379                 * We don't do normalization for TYPE_PATH here: If
 380                 * the path is like ~/foobar/, we prefer to store
 381                 * "~/foobar/" in the config file, and to expand the ~
 382                 * when retrieving the value.
 383                 * Also don't do normalization for expiry dates.
 384                 */
 385                return xstrdup(value);
 386        if (type == TYPE_INT)
 387                return xstrfmt("%"PRId64, git_config_int64(key, value));
 388        if (type == TYPE_BOOL)
 389                return xstrdup(git_config_bool(key, value) ?  "true" : "false");
 390        if (type == TYPE_BOOL_OR_INT) {
 391                int is_bool, v;
 392                v = git_config_bool_or_int(key, value, &is_bool);
 393                if (!is_bool)
 394                        return xstrfmt("%d", v);
 395                else
 396                        return xstrdup(v ? "true" : "false");
 397        }
 398        if (type == TYPE_COLOR) {
 399                char v[COLOR_MAXLEN];
 400                if (git_config_color(v, key, value))
 401                        die(_("cannot parse color '%s'"), value);
 402
 403                /*
 404                 * The contents of `v` now contain an ANSI escape
 405                 * sequence, not suitable for including within a
 406                 * configuration file. Treat the above as a
 407                 * "sanity-check", and return the given value, which we
 408                 * know is representable as valid color code.
 409                 */
 410                return xstrdup(value);
 411        }
 412
 413        BUG("cannot normalize type %d", type);
 414}
 415
 416static int get_color_found;
 417static const char *get_color_slot;
 418static const char *get_colorbool_slot;
 419static char parsed_color[COLOR_MAXLEN];
 420
 421static int git_get_color_config(const char *var, const char *value, void *cb)
 422{
 423        if (!strcmp(var, get_color_slot)) {
 424                if (!value)
 425                        config_error_nonbool(var);
 426                if (color_parse(value, parsed_color) < 0)
 427                        return -1;
 428                get_color_found = 1;
 429        }
 430        return 0;
 431}
 432
 433static void get_color(const char *var, const char *def_color)
 434{
 435        get_color_slot = var;
 436        get_color_found = 0;
 437        parsed_color[0] = '\0';
 438        config_with_options(git_get_color_config, NULL,
 439                            &given_config_source, &config_options);
 440
 441        if (!get_color_found && def_color) {
 442                if (color_parse(def_color, parsed_color) < 0)
 443                        die(_("unable to parse default color value"));
 444        }
 445
 446        fputs(parsed_color, stdout);
 447}
 448
 449static int get_colorbool_found;
 450static int get_diff_color_found;
 451static int get_color_ui_found;
 452static int git_get_colorbool_config(const char *var, const char *value,
 453                void *cb)
 454{
 455        if (!strcmp(var, get_colorbool_slot))
 456                get_colorbool_found = git_config_colorbool(var, value);
 457        else if (!strcmp(var, "diff.color"))
 458                get_diff_color_found = git_config_colorbool(var, value);
 459        else if (!strcmp(var, "color.ui"))
 460                get_color_ui_found = git_config_colorbool(var, value);
 461        return 0;
 462}
 463
 464static int get_colorbool(const char *var, int print)
 465{
 466        get_colorbool_slot = var;
 467        get_colorbool_found = -1;
 468        get_diff_color_found = -1;
 469        get_color_ui_found = -1;
 470        config_with_options(git_get_colorbool_config, NULL,
 471                            &given_config_source, &config_options);
 472
 473        if (get_colorbool_found < 0) {
 474                if (!strcmp(get_colorbool_slot, "color.diff"))
 475                        get_colorbool_found = get_diff_color_found;
 476                if (get_colorbool_found < 0)
 477                        get_colorbool_found = get_color_ui_found;
 478        }
 479
 480        if (get_colorbool_found < 0)
 481                /* default value if none found in config */
 482                get_colorbool_found = GIT_COLOR_AUTO;
 483
 484        get_colorbool_found = want_color(get_colorbool_found);
 485
 486        if (print) {
 487                printf("%s\n", get_colorbool_found ? "true" : "false");
 488                return 0;
 489        } else
 490                return get_colorbool_found ? 0 : 1;
 491}
 492
 493static void check_write(void)
 494{
 495        if (!given_config_source.file && !startup_info->have_repository)
 496                die(_("not in a git directory"));
 497
 498        if (given_config_source.use_stdin)
 499                die(_("writing to stdin is not supported"));
 500
 501        if (given_config_source.blob)
 502                die(_("writing config blobs is not supported"));
 503}
 504
 505struct urlmatch_current_candidate_value {
 506        char value_is_null;
 507        struct strbuf value;
 508};
 509
 510static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
 511{
 512        struct string_list *values = cb;
 513        struct string_list_item *item = string_list_insert(values, var);
 514        struct urlmatch_current_candidate_value *matched = item->util;
 515
 516        if (!matched) {
 517                matched = xmalloc(sizeof(*matched));
 518                strbuf_init(&matched->value, 0);
 519                item->util = matched;
 520        } else {
 521                strbuf_reset(&matched->value);
 522        }
 523
 524        if (value) {
 525                strbuf_addstr(&matched->value, value);
 526                matched->value_is_null = 0;
 527        } else {
 528                matched->value_is_null = 1;
 529        }
 530        return 0;
 531}
 532
 533static int get_urlmatch(const char *var, const char *url)
 534{
 535        int ret;
 536        char *section_tail;
 537        struct string_list_item *item;
 538        struct urlmatch_config config = { STRING_LIST_INIT_DUP };
 539        struct string_list values = STRING_LIST_INIT_DUP;
 540
 541        config.collect_fn = urlmatch_collect_fn;
 542        config.cascade_fn = NULL;
 543        config.cb = &values;
 544
 545        if (!url_normalize(url, &config.url))
 546                die("%s", config.url.err);
 547
 548        config.section = xstrdup_tolower(var);
 549        section_tail = strchr(config.section, '.');
 550        if (section_tail) {
 551                *section_tail = '\0';
 552                config.key = section_tail + 1;
 553                show_keys = 0;
 554        } else {
 555                config.key = NULL;
 556                show_keys = 1;
 557        }
 558
 559        config_with_options(urlmatch_config_entry, &config,
 560                            &given_config_source, &config_options);
 561
 562        ret = !values.nr;
 563
 564        for_each_string_list_item(item, &values) {
 565                struct urlmatch_current_candidate_value *matched = item->util;
 566                struct strbuf buf = STRBUF_INIT;
 567
 568                format_config(&buf, item->string,
 569                              matched->value_is_null ? NULL : matched->value.buf);
 570                fwrite(buf.buf, 1, buf.len, stdout);
 571                strbuf_release(&buf);
 572
 573                strbuf_release(&matched->value);
 574        }
 575        string_list_clear(&config.vars, 1);
 576        string_list_clear(&values, 1);
 577        free(config.url.url);
 578
 579        free((void *)config.section);
 580        return ret;
 581}
 582
 583static char *default_user_config(void)
 584{
 585        struct strbuf buf = STRBUF_INIT;
 586        strbuf_addf(&buf,
 587                    _("# This is Git's per-user configuration file.\n"
 588                      "[user]\n"
 589                      "# Please adapt and uncomment the following lines:\n"
 590                      "#        name = %s\n"
 591                      "#        email = %s\n"),
 592                    ident_default_name(),
 593                    ident_default_email());
 594        return strbuf_detach(&buf, NULL);
 595}
 596
 597int cmd_config(int argc, const char **argv, const char *prefix)
 598{
 599        int nongit = !startup_info->have_repository;
 600        char *value;
 601
 602        given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
 603
 604        argc = parse_options(argc, argv, prefix, builtin_config_options,
 605                             builtin_config_usage,
 606                             PARSE_OPT_STOP_AT_NON_OPTION);
 607
 608        if (use_global_config + use_system_config + use_local_config +
 609            use_worktree_config +
 610            !!given_config_source.file + !!given_config_source.blob > 1) {
 611                error(_("only one config file at a time"));
 612                usage_builtin_config();
 613        }
 614
 615        if (use_local_config && nongit)
 616                die(_("--local can only be used inside a git repository"));
 617
 618        if (given_config_source.blob && nongit)
 619                die(_("--blob can only be used inside a git repository"));
 620
 621        if (given_config_source.file &&
 622                        !strcmp(given_config_source.file, "-")) {
 623                given_config_source.file = NULL;
 624                given_config_source.use_stdin = 1;
 625        }
 626
 627        if (use_global_config) {
 628                char *user_config = expand_user_path("~/.gitconfig", 0);
 629                char *xdg_config = xdg_config_home("config");
 630
 631                if (!user_config)
 632                        /*
 633                         * It is unknown if HOME/.gitconfig exists, so
 634                         * we do not know if we should write to XDG
 635                         * location; error out even if XDG_CONFIG_HOME
 636                         * is set and points at a sane location.
 637                         */
 638                        die(_("$HOME not set"));
 639
 640                if (access_or_warn(user_config, R_OK, 0) &&
 641                    xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
 642                        given_config_source.file = xdg_config;
 643                        free(user_config);
 644                } else {
 645                        given_config_source.file = user_config;
 646                        free(xdg_config);
 647                }
 648        }
 649        else if (use_system_config)
 650                given_config_source.file = git_etc_gitconfig();
 651        else if (use_local_config)
 652                given_config_source.file = git_pathdup("config");
 653        else if (use_worktree_config) {
 654                struct worktree **worktrees = get_worktrees(0);
 655                if (repository_format_worktree_config)
 656                        given_config_source.file = git_pathdup("config.worktree");
 657                else if (worktrees[0] && worktrees[1])
 658                        die(_("--worktree cannot be used with multiple "
 659                              "working trees unless the config\n"
 660                              "extension worktreeConfig is enabled. "
 661                              "Please read \"CONFIGURATION FILE\"\n"
 662                              "section in \"git help worktree\" for details"));
 663                else
 664                        given_config_source.file = git_pathdup("config");
 665                free_worktrees(worktrees);
 666        } else if (given_config_source.file) {
 667                if (!is_absolute_path(given_config_source.file) && prefix)
 668                        given_config_source.file =
 669                                prefix_filename(prefix, given_config_source.file);
 670        }
 671
 672        if (respect_includes_opt == -1)
 673                config_options.respect_includes = !given_config_source.file;
 674        else
 675                config_options.respect_includes = respect_includes_opt;
 676        if (!nongit) {
 677                config_options.commondir = get_git_common_dir();
 678                config_options.git_dir = get_git_dir();
 679        }
 680
 681        if (end_null) {
 682                term = '\0';
 683                delim = '\n';
 684                key_delim = '\n';
 685        }
 686
 687        if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
 688                error(_("--get-color and variable type are incoherent"));
 689                usage_builtin_config();
 690        }
 691
 692        if (HAS_MULTI_BITS(actions)) {
 693                error(_("only one action at a time"));
 694                usage_builtin_config();
 695        }
 696        if (actions == 0)
 697                switch (argc) {
 698                case 1: actions = ACTION_GET; break;
 699                case 2: actions = ACTION_SET; break;
 700                case 3: actions = ACTION_SET_ALL; break;
 701                default:
 702                        usage_builtin_config();
 703                }
 704        if (omit_values &&
 705            !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
 706                error(_("--name-only is only applicable to --list or --get-regexp"));
 707                usage_builtin_config();
 708        }
 709
 710        if (show_origin && !(actions &
 711                (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
 712                error(_("--show-origin is only applicable to --get, --get-all, "
 713                        "--get-regexp, and --list"));
 714                usage_builtin_config();
 715        }
 716
 717        if (default_value && !(actions & ACTION_GET)) {
 718                error(_("--default is only applicable to --get"));
 719                usage_builtin_config();
 720        }
 721
 722        if (actions & PAGING_ACTIONS)
 723                setup_auto_pager("config", 1);
 724
 725        if (actions == ACTION_LIST) {
 726                check_argc(argc, 0, 0);
 727                if (config_with_options(show_all_config, NULL,
 728                                        &given_config_source,
 729                                        &config_options) < 0) {
 730                        if (given_config_source.file)
 731                                die_errno(_("unable to read config file '%s'"),
 732                                          given_config_source.file);
 733                        else
 734                                die(_("error processing config file(s)"));
 735                }
 736        }
 737        else if (actions == ACTION_EDIT) {
 738                char *config_file;
 739
 740                check_argc(argc, 0, 0);
 741                if (!given_config_source.file && nongit)
 742                        die(_("not in a git directory"));
 743                if (given_config_source.use_stdin)
 744                        die(_("editing stdin is not supported"));
 745                if (given_config_source.blob)
 746                        die(_("editing blobs is not supported"));
 747                git_config(git_default_config, NULL);
 748                config_file = given_config_source.file ?
 749                                xstrdup(given_config_source.file) :
 750                                git_pathdup("config");
 751                if (use_global_config) {
 752                        int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
 753                        if (fd >= 0) {
 754                                char *content = default_user_config();
 755                                write_str_in_full(fd, content);
 756                                free(content);
 757                                close(fd);
 758                        }
 759                        else if (errno != EEXIST)
 760                                die_errno(_("cannot create configuration file %s"), config_file);
 761                }
 762                launch_editor(config_file, NULL, NULL);
 763                free(config_file);
 764        }
 765        else if (actions == ACTION_SET) {
 766                int ret;
 767                check_write();
 768                check_argc(argc, 2, 2);
 769                value = normalize_value(argv[0], argv[1]);
 770                UNLEAK(value);
 771                ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
 772                if (ret == CONFIG_NOTHING_SET)
 773                        error(_("cannot overwrite multiple values with a single value\n"
 774                        "       Use a regexp, --add or --replace-all to change %s."), argv[0]);
 775                return ret;
 776        }
 777        else if (actions == ACTION_SET_ALL) {
 778                check_write();
 779                check_argc(argc, 2, 3);
 780                value = normalize_value(argv[0], argv[1]);
 781                UNLEAK(value);
 782                return git_config_set_multivar_in_file_gently(given_config_source.file,
 783                                                              argv[0], value, argv[2], 0);
 784        }
 785        else if (actions == ACTION_ADD) {
 786                check_write();
 787                check_argc(argc, 2, 2);
 788                value = normalize_value(argv[0], argv[1]);
 789                UNLEAK(value);
 790                return git_config_set_multivar_in_file_gently(given_config_source.file,
 791                                                              argv[0], value,
 792                                                              CONFIG_REGEX_NONE, 0);
 793        }
 794        else if (actions == ACTION_REPLACE_ALL) {
 795                check_write();
 796                check_argc(argc, 2, 3);
 797                value = normalize_value(argv[0], argv[1]);
 798                UNLEAK(value);
 799                return git_config_set_multivar_in_file_gently(given_config_source.file,
 800                                                              argv[0], value, argv[2], 1);
 801        }
 802        else if (actions == ACTION_GET) {
 803                check_argc(argc, 1, 2);
 804                return get_value(argv[0], argv[1]);
 805        }
 806        else if (actions == ACTION_GET_ALL) {
 807                do_all = 1;
 808                check_argc(argc, 1, 2);
 809                return get_value(argv[0], argv[1]);
 810        }
 811        else if (actions == ACTION_GET_REGEXP) {
 812                show_keys = 1;
 813                use_key_regexp = 1;
 814                do_all = 1;
 815                check_argc(argc, 1, 2);
 816                return get_value(argv[0], argv[1]);
 817        }
 818        else if (actions == ACTION_GET_URLMATCH) {
 819                check_argc(argc, 2, 2);
 820                return get_urlmatch(argv[0], argv[1]);
 821        }
 822        else if (actions == ACTION_UNSET) {
 823                check_write();
 824                check_argc(argc, 1, 2);
 825                if (argc == 2)
 826                        return git_config_set_multivar_in_file_gently(given_config_source.file,
 827                                                                      argv[0], NULL, argv[1], 0);
 828                else
 829                        return git_config_set_in_file_gently(given_config_source.file,
 830                                                             argv[0], NULL);
 831        }
 832        else if (actions == ACTION_UNSET_ALL) {
 833                check_write();
 834                check_argc(argc, 1, 2);
 835                return git_config_set_multivar_in_file_gently(given_config_source.file,
 836                                                              argv[0], NULL, argv[1], 1);
 837        }
 838        else if (actions == ACTION_RENAME_SECTION) {
 839                int ret;
 840                check_write();
 841                check_argc(argc, 2, 2);
 842                ret = git_config_rename_section_in_file(given_config_source.file,
 843                                                        argv[0], argv[1]);
 844                if (ret < 0)
 845                        return ret;
 846                if (ret == 0)
 847                        die(_("no such section: %s"), argv[0]);
 848        }
 849        else if (actions == ACTION_REMOVE_SECTION) {
 850                int ret;
 851                check_write();
 852                check_argc(argc, 1, 1);
 853                ret = git_config_rename_section_in_file(given_config_source.file,
 854                                                        argv[0], NULL);
 855                if (ret < 0)
 856                        return ret;
 857                if (ret == 0)
 858                        die(_("no such section: %s"), argv[0]);
 859        }
 860        else if (actions == ACTION_GET_COLOR) {
 861                check_argc(argc, 1, 2);
 862                get_color(argv[0], argv[1]);
 863        }
 864        else if (actions == ACTION_GET_COLORBOOL) {
 865                check_argc(argc, 1, 2);
 866                if (argc == 2)
 867                        color_stdout_is_tty = git_config_bool("command line", argv[1]);
 868                return get_colorbool(argv[0], argc == 2);
 869        }
 870
 871        return 0;
 872}