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