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