builtin / config.con commit Merge branch 'mm/color-auto-default' (6a5b9ce)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "color.h"
   4#include "parse-options.h"
   5
   6static const char *const builtin_config_usage[] = {
   7        N_("git config [options]"),
   8        NULL
   9};
  10
  11static char *key;
  12static regex_t *key_regexp;
  13static regex_t *regexp;
  14static int show_keys;
  15static int use_key_regexp;
  16static int do_all;
  17static int do_not_match;
  18static char delim = '=';
  19static char key_delim = ' ';
  20static char term = '\n';
  21
  22static int use_global_config, use_system_config, use_local_config;
  23static const char *given_config_file;
  24static int actions, types;
  25static const char *get_color_slot, *get_colorbool_slot;
  26static int end_null;
  27static int respect_includes = -1;
  28
  29#define ACTION_GET (1<<0)
  30#define ACTION_GET_ALL (1<<1)
  31#define ACTION_GET_REGEXP (1<<2)
  32#define ACTION_REPLACE_ALL (1<<3)
  33#define ACTION_ADD (1<<4)
  34#define ACTION_UNSET (1<<5)
  35#define ACTION_UNSET_ALL (1<<6)
  36#define ACTION_RENAME_SECTION (1<<7)
  37#define ACTION_REMOVE_SECTION (1<<8)
  38#define ACTION_LIST (1<<9)
  39#define ACTION_EDIT (1<<10)
  40#define ACTION_SET (1<<11)
  41#define ACTION_SET_ALL (1<<12)
  42#define ACTION_GET_COLOR (1<<13)
  43#define ACTION_GET_COLORBOOL (1<<14)
  44
  45#define TYPE_BOOL (1<<0)
  46#define TYPE_INT (1<<1)
  47#define TYPE_BOOL_OR_INT (1<<2)
  48#define TYPE_PATH (1<<3)
  49
  50static struct option builtin_config_options[] = {
  51        OPT_GROUP(N_("Config file location")),
  52        OPT_BOOLEAN(0, "global", &use_global_config, N_("use global config file")),
  53        OPT_BOOLEAN(0, "system", &use_system_config, N_("use system config file")),
  54        OPT_BOOLEAN(0, "local", &use_local_config, N_("use repository config file")),
  55        OPT_STRING('f', "file", &given_config_file, N_("file"), N_("use given config file")),
  56        OPT_GROUP(N_("Action")),
  57        OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
  58        OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
  59        OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
  60        OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
  61        OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
  62        OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
  63        OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
  64        OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
  65        OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
  66        OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
  67        OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
  68        OPT_STRING(0, "get-color", &get_color_slot, N_("slot"), N_("find the color configured: [default]")),
  69        OPT_STRING(0, "get-colorbool", &get_colorbool_slot, N_("slot"), N_("find the color setting: [stdout-is-tty]")),
  70        OPT_GROUP(N_("Type")),
  71        OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
  72        OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
  73        OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
  74        OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
  75        OPT_GROUP(N_("Other")),
  76        OPT_BOOLEAN('z', "null", &end_null, N_("terminate values with NUL byte")),
  77        OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
  78        OPT_END(),
  79};
  80
  81static void check_argc(int argc, int min, int max) {
  82        if (argc >= min && argc <= max)
  83                return;
  84        error("wrong number of arguments");
  85        usage_with_options(builtin_config_usage, builtin_config_options);
  86}
  87
  88static int show_all_config(const char *key_, const char *value_, void *cb)
  89{
  90        if (value_)
  91                printf("%s%c%s%c", key_, delim, value_, term);
  92        else
  93                printf("%s%c", key_, term);
  94        return 0;
  95}
  96
  97struct strbuf_list {
  98        struct strbuf *items;
  99        int nr;
 100        int alloc;
 101};
 102
 103static int collect_config(const char *key_, const char *value_, void *cb)
 104{
 105        struct strbuf_list *values = cb;
 106        struct strbuf *buf;
 107        char value[256];
 108        const char *vptr = value;
 109        int must_free_vptr = 0;
 110        int must_print_delim = 0;
 111
 112        if (!use_key_regexp && strcmp(key_, key))
 113                return 0;
 114        if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
 115                return 0;
 116        if (regexp != NULL &&
 117            (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
 118                return 0;
 119
 120        ALLOC_GROW(values->items, values->nr + 1, values->alloc);
 121        buf = &values->items[values->nr++];
 122        strbuf_init(buf, 0);
 123
 124        if (show_keys) {
 125                strbuf_addstr(buf, key_);
 126                must_print_delim = 1;
 127        }
 128        if (types == TYPE_INT)
 129                sprintf(value, "%d", git_config_int(key_, value_?value_:""));
 130        else if (types == TYPE_BOOL)
 131                vptr = git_config_bool(key_, value_) ? "true" : "false";
 132        else if (types == TYPE_BOOL_OR_INT) {
 133                int is_bool, v;
 134                v = git_config_bool_or_int(key_, value_, &is_bool);
 135                if (is_bool)
 136                        vptr = v ? "true" : "false";
 137                else
 138                        sprintf(value, "%d", v);
 139        } else if (types == TYPE_PATH) {
 140                if (git_config_pathname(&vptr, key_, value_) < 0)
 141                        return -1;
 142                must_free_vptr = 1;
 143        } else if (value_) {
 144                vptr = value_;
 145        } else {
 146                /* Just show the key name */
 147                vptr = "";
 148                must_print_delim = 0;
 149        }
 150
 151        if (must_print_delim)
 152                strbuf_addch(buf, key_delim);
 153        strbuf_addstr(buf, vptr);
 154        strbuf_addch(buf, term);
 155
 156        if (must_free_vptr)
 157                /* If vptr must be freed, it's a pointer to a
 158                 * dynamically allocated buffer, it's safe to cast to
 159                 * const.
 160                */
 161                free((char *)vptr);
 162
 163        return 0;
 164}
 165
 166static int get_value(const char *key_, const char *regex_)
 167{
 168        int ret = CONFIG_GENERIC_ERROR;
 169        struct strbuf_list values = {NULL};
 170        int i;
 171
 172        if (use_key_regexp) {
 173                char *tl;
 174
 175                /*
 176                 * NEEDSWORK: this naive pattern lowercasing obviously does not
 177                 * work for more complex patterns like "^[^.]*Foo.*bar".
 178                 * Perhaps we should deprecate this altogether someday.
 179                 */
 180
 181                key = xstrdup(key_);
 182                for (tl = key + strlen(key) - 1;
 183                     tl >= key && *tl != '.';
 184                     tl--)
 185                        *tl = tolower(*tl);
 186                for (tl = key; *tl && *tl != '.'; tl++)
 187                        *tl = tolower(*tl);
 188
 189                key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 190                if (regcomp(key_regexp, key, REG_EXTENDED)) {
 191                        fprintf(stderr, "Invalid key pattern: %s\n", key_);
 192                        free(key_regexp);
 193                        key_regexp = NULL;
 194                        ret = CONFIG_INVALID_PATTERN;
 195                        goto free_strings;
 196                }
 197        } else {
 198                if (git_config_parse_key(key_, &key, NULL)) {
 199                        ret = CONFIG_INVALID_KEY;
 200                        goto free_strings;
 201                }
 202        }
 203
 204        if (regex_) {
 205                if (regex_[0] == '!') {
 206                        do_not_match = 1;
 207                        regex_++;
 208                }
 209
 210                regexp = (regex_t*)xmalloc(sizeof(regex_t));
 211                if (regcomp(regexp, regex_, REG_EXTENDED)) {
 212                        fprintf(stderr, "Invalid pattern: %s\n", regex_);
 213                        free(regexp);
 214                        regexp = NULL;
 215                        ret = CONFIG_INVALID_PATTERN;
 216                        goto free_strings;
 217                }
 218        }
 219
 220        git_config_with_options(collect_config, &values,
 221                                given_config_file, respect_includes);
 222
 223        ret = !values.nr;
 224
 225        for (i = 0; i < values.nr; i++) {
 226                struct strbuf *buf = values.items + i;
 227                if (do_all || i == values.nr - 1)
 228                        fwrite(buf->buf, 1, buf->len, stdout);
 229                strbuf_release(buf);
 230        }
 231        free(values.items);
 232
 233free_strings:
 234        free(key);
 235        if (key_regexp) {
 236                regfree(key_regexp);
 237                free(key_regexp);
 238        }
 239        if (regexp) {
 240                regfree(regexp);
 241                free(regexp);
 242        }
 243
 244        return ret;
 245}
 246
 247static char *normalize_value(const char *key, const char *value)
 248{
 249        char *normalized;
 250
 251        if (!value)
 252                return NULL;
 253
 254        if (types == 0 || types == TYPE_PATH)
 255                /*
 256                 * We don't do normalization for TYPE_PATH here: If
 257                 * the path is like ~/foobar/, we prefer to store
 258                 * "~/foobar/" in the config file, and to expand the ~
 259                 * when retrieving the value.
 260                 */
 261                normalized = xstrdup(value);
 262        else {
 263                normalized = xmalloc(64);
 264                if (types == TYPE_INT) {
 265                        int v = git_config_int(key, value);
 266                        sprintf(normalized, "%d", v);
 267                }
 268                else if (types == TYPE_BOOL)
 269                        sprintf(normalized, "%s",
 270                                git_config_bool(key, value) ? "true" : "false");
 271                else if (types == TYPE_BOOL_OR_INT) {
 272                        int is_bool, v;
 273                        v = git_config_bool_or_int(key, value, &is_bool);
 274                        if (!is_bool)
 275                                sprintf(normalized, "%d", v);
 276                        else
 277                                sprintf(normalized, "%s", v ? "true" : "false");
 278                }
 279        }
 280
 281        return normalized;
 282}
 283
 284static int get_color_found;
 285static const char *get_color_slot;
 286static const char *get_colorbool_slot;
 287static char parsed_color[COLOR_MAXLEN];
 288
 289static int git_get_color_config(const char *var, const char *value, void *cb)
 290{
 291        if (!strcmp(var, get_color_slot)) {
 292                if (!value)
 293                        config_error_nonbool(var);
 294                color_parse(value, var, parsed_color);
 295                get_color_found = 1;
 296        }
 297        return 0;
 298}
 299
 300static void get_color(const char *def_color)
 301{
 302        get_color_found = 0;
 303        parsed_color[0] = '\0';
 304        git_config_with_options(git_get_color_config, NULL,
 305                                given_config_file, respect_includes);
 306
 307        if (!get_color_found && def_color)
 308                color_parse(def_color, "command line", parsed_color);
 309
 310        fputs(parsed_color, stdout);
 311}
 312
 313static int get_colorbool_found;
 314static int get_diff_color_found;
 315static int get_color_ui_found;
 316static int git_get_colorbool_config(const char *var, const char *value,
 317                void *cb)
 318{
 319        if (!strcmp(var, get_colorbool_slot))
 320                get_colorbool_found = git_config_colorbool(var, value);
 321        else if (!strcmp(var, "diff.color"))
 322                get_diff_color_found = git_config_colorbool(var, value);
 323        else if (!strcmp(var, "color.ui"))
 324                get_color_ui_found = git_config_colorbool(var, value);
 325        return 0;
 326}
 327
 328static int get_colorbool(int print)
 329{
 330        get_colorbool_found = -1;
 331        get_diff_color_found = -1;
 332        get_color_ui_found = -1;
 333        git_config_with_options(git_get_colorbool_config, NULL,
 334                                given_config_file, respect_includes);
 335
 336        if (get_colorbool_found < 0) {
 337                if (!strcmp(get_colorbool_slot, "color.diff"))
 338                        get_colorbool_found = get_diff_color_found;
 339                if (get_colorbool_found < 0)
 340                        get_colorbool_found = get_color_ui_found;
 341        }
 342
 343        if (get_colorbool_found < 0)
 344                /* default value if none found in config */
 345                get_colorbool_found = GIT_COLOR_AUTO;
 346
 347        get_colorbool_found = want_color(get_colorbool_found);
 348
 349        if (print) {
 350                printf("%s\n", get_colorbool_found ? "true" : "false");
 351                return 0;
 352        } else
 353                return get_colorbool_found ? 0 : 1;
 354}
 355
 356int cmd_config(int argc, const char **argv, const char *prefix)
 357{
 358        int nongit = !startup_info->have_repository;
 359        char *value;
 360
 361        given_config_file = getenv(CONFIG_ENVIRONMENT);
 362
 363        argc = parse_options(argc, argv, prefix, builtin_config_options,
 364                             builtin_config_usage,
 365                             PARSE_OPT_STOP_AT_NON_OPTION);
 366
 367        if (use_global_config + use_system_config + use_local_config + !!given_config_file > 1) {
 368                error("only one config file at a time.");
 369                usage_with_options(builtin_config_usage, builtin_config_options);
 370        }
 371
 372        if (use_global_config) {
 373                char *user_config = NULL;
 374                char *xdg_config = NULL;
 375
 376                home_config_paths(&user_config, &xdg_config, "config");
 377
 378                if (!user_config)
 379                        /*
 380                         * It is unknown if HOME/.gitconfig exists, so
 381                         * we do not know if we should write to XDG
 382                         * location; error out even if XDG_CONFIG_HOME
 383                         * is set and points at a sane location.
 384                         */
 385                        die("$HOME not set");
 386
 387                if (access_or_warn(user_config, R_OK, 0) &&
 388                    xdg_config && !access_or_warn(xdg_config, R_OK, 0))
 389                        given_config_file = xdg_config;
 390                else
 391                        given_config_file = user_config;
 392        }
 393        else if (use_system_config)
 394                given_config_file = git_etc_gitconfig();
 395        else if (use_local_config)
 396                given_config_file = git_pathdup("config");
 397        else if (given_config_file) {
 398                if (!is_absolute_path(given_config_file) && prefix)
 399                        given_config_file =
 400                                xstrdup(prefix_filename(prefix,
 401                                                        strlen(prefix),
 402                                                        given_config_file));
 403        }
 404
 405        if (respect_includes == -1)
 406                respect_includes = !given_config_file;
 407
 408        if (end_null) {
 409                term = '\0';
 410                delim = '\n';
 411                key_delim = '\n';
 412        }
 413
 414        if (HAS_MULTI_BITS(types)) {
 415                error("only one type at a time.");
 416                usage_with_options(builtin_config_usage, builtin_config_options);
 417        }
 418
 419        if (get_color_slot)
 420            actions |= ACTION_GET_COLOR;
 421        if (get_colorbool_slot)
 422            actions |= ACTION_GET_COLORBOOL;
 423
 424        if ((get_color_slot || get_colorbool_slot) && types) {
 425                error("--get-color and variable type are incoherent");
 426                usage_with_options(builtin_config_usage, builtin_config_options);
 427        }
 428
 429        if (HAS_MULTI_BITS(actions)) {
 430                error("only one action at a time.");
 431                usage_with_options(builtin_config_usage, builtin_config_options);
 432        }
 433        if (actions == 0)
 434                switch (argc) {
 435                case 1: actions = ACTION_GET; break;
 436                case 2: actions = ACTION_SET; break;
 437                case 3: actions = ACTION_SET_ALL; break;
 438                default:
 439                        usage_with_options(builtin_config_usage, builtin_config_options);
 440                }
 441
 442        if (actions == ACTION_LIST) {
 443                check_argc(argc, 0, 0);
 444                if (git_config_with_options(show_all_config, NULL,
 445                                            given_config_file,
 446                                            respect_includes) < 0) {
 447                        if (given_config_file)
 448                                die_errno("unable to read config file '%s'",
 449                                          given_config_file);
 450                        else
 451                                die("error processing config file(s)");
 452                }
 453        }
 454        else if (actions == ACTION_EDIT) {
 455                check_argc(argc, 0, 0);
 456                if (!given_config_file && nongit)
 457                        die("not in a git directory");
 458                git_config(git_default_config, NULL);
 459                launch_editor(given_config_file ?
 460                              given_config_file : git_path("config"),
 461                              NULL, NULL);
 462        }
 463        else if (actions == ACTION_SET) {
 464                int ret;
 465                check_argc(argc, 2, 2);
 466                value = normalize_value(argv[0], argv[1]);
 467                ret = git_config_set_in_file(given_config_file, argv[0], value);
 468                if (ret == CONFIG_NOTHING_SET)
 469                        error("cannot overwrite multiple values with a single value\n"
 470                        "       Use a regexp, --add or --replace-all to change %s.", argv[0]);
 471                return ret;
 472        }
 473        else if (actions == ACTION_SET_ALL) {
 474                check_argc(argc, 2, 3);
 475                value = normalize_value(argv[0], argv[1]);
 476                return git_config_set_multivar_in_file(given_config_file,
 477                                                       argv[0], value, argv[2], 0);
 478        }
 479        else if (actions == ACTION_ADD) {
 480                check_argc(argc, 2, 2);
 481                value = normalize_value(argv[0], argv[1]);
 482                return git_config_set_multivar_in_file(given_config_file,
 483                                                       argv[0], value, "^$", 0);
 484        }
 485        else if (actions == ACTION_REPLACE_ALL) {
 486                check_argc(argc, 2, 3);
 487                value = normalize_value(argv[0], argv[1]);
 488                return git_config_set_multivar_in_file(given_config_file,
 489                                                       argv[0], value, argv[2], 1);
 490        }
 491        else if (actions == ACTION_GET) {
 492                check_argc(argc, 1, 2);
 493                return get_value(argv[0], argv[1]);
 494        }
 495        else if (actions == ACTION_GET_ALL) {
 496                do_all = 1;
 497                check_argc(argc, 1, 2);
 498                return get_value(argv[0], argv[1]);
 499        }
 500        else if (actions == ACTION_GET_REGEXP) {
 501                show_keys = 1;
 502                use_key_regexp = 1;
 503                do_all = 1;
 504                check_argc(argc, 1, 2);
 505                return get_value(argv[0], argv[1]);
 506        }
 507        else if (actions == ACTION_UNSET) {
 508                check_argc(argc, 1, 2);
 509                if (argc == 2)
 510                        return git_config_set_multivar_in_file(given_config_file,
 511                                                               argv[0], NULL, argv[1], 0);
 512                else
 513                        return git_config_set_in_file(given_config_file,
 514                                                      argv[0], NULL);
 515        }
 516        else if (actions == ACTION_UNSET_ALL) {
 517                check_argc(argc, 1, 2);
 518                return git_config_set_multivar_in_file(given_config_file,
 519                                                       argv[0], NULL, argv[1], 1);
 520        }
 521        else if (actions == ACTION_RENAME_SECTION) {
 522                int ret;
 523                check_argc(argc, 2, 2);
 524                ret = git_config_rename_section_in_file(given_config_file,
 525                                                        argv[0], argv[1]);
 526                if (ret < 0)
 527                        return ret;
 528                if (ret == 0)
 529                        die("No such section!");
 530        }
 531        else if (actions == ACTION_REMOVE_SECTION) {
 532                int ret;
 533                check_argc(argc, 1, 1);
 534                ret = git_config_rename_section_in_file(given_config_file,
 535                                                        argv[0], NULL);
 536                if (ret < 0)
 537                        return ret;
 538                if (ret == 0)
 539                        die("No such section!");
 540        }
 541        else if (actions == ACTION_GET_COLOR) {
 542                get_color(argv[0]);
 543        }
 544        else if (actions == ACTION_GET_COLORBOOL) {
 545                if (argc == 1)
 546                        color_stdout_is_tty = git_config_bool("command line", argv[0]);
 547                return get_colorbool(argc != 0);
 548        }
 549
 550        return 0;
 551}
 552
 553int cmd_repo_config(int argc, const char **argv, const char *prefix)
 554{
 555        fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n");
 556        return cmd_config(argc, argv, prefix);
 557}