builtin / config.con commit pack-refs: merge code from pack-refs.{c,h} into refs.{c,h} (32d462c)
   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        git_config_with_options(git_get_colorbool_config, NULL,
 333                                given_config_file, respect_includes);
 334
 335        if (get_colorbool_found < 0) {
 336                if (!strcmp(get_colorbool_slot, "color.diff"))
 337                        get_colorbool_found = get_diff_color_found;
 338                if (get_colorbool_found < 0)
 339                        get_colorbool_found = get_color_ui_found;
 340        }
 341
 342        get_colorbool_found = want_color(get_colorbool_found);
 343
 344        if (print) {
 345                printf("%s\n", get_colorbool_found ? "true" : "false");
 346                return 0;
 347        } else
 348                return get_colorbool_found ? 0 : 1;
 349}
 350
 351int cmd_config(int argc, const char **argv, const char *prefix)
 352{
 353        int nongit = !startup_info->have_repository;
 354        char *value;
 355
 356        given_config_file = getenv(CONFIG_ENVIRONMENT);
 357
 358        argc = parse_options(argc, argv, prefix, builtin_config_options,
 359                             builtin_config_usage,
 360                             PARSE_OPT_STOP_AT_NON_OPTION);
 361
 362        if (use_global_config + use_system_config + use_local_config + !!given_config_file > 1) {
 363                error("only one config file at a time.");
 364                usage_with_options(builtin_config_usage, builtin_config_options);
 365        }
 366
 367        if (use_global_config) {
 368                char *user_config = NULL;
 369                char *xdg_config = NULL;
 370
 371                home_config_paths(&user_config, &xdg_config, "config");
 372
 373                if (!user_config)
 374                        /*
 375                         * It is unknown if HOME/.gitconfig exists, so
 376                         * we do not know if we should write to XDG
 377                         * location; error out even if XDG_CONFIG_HOME
 378                         * is set and points at a sane location.
 379                         */
 380                        die("$HOME not set");
 381
 382                if (access_or_warn(user_config, R_OK) &&
 383                    xdg_config && !access_or_warn(xdg_config, R_OK))
 384                        given_config_file = xdg_config;
 385                else
 386                        given_config_file = user_config;
 387        }
 388        else if (use_system_config)
 389                given_config_file = git_etc_gitconfig();
 390        else if (use_local_config)
 391                given_config_file = git_pathdup("config");
 392        else if (given_config_file) {
 393                if (!is_absolute_path(given_config_file) && prefix)
 394                        given_config_file =
 395                                xstrdup(prefix_filename(prefix,
 396                                                        strlen(prefix),
 397                                                        given_config_file));
 398        }
 399
 400        if (respect_includes == -1)
 401                respect_includes = !given_config_file;
 402
 403        if (end_null) {
 404                term = '\0';
 405                delim = '\n';
 406                key_delim = '\n';
 407        }
 408
 409        if (HAS_MULTI_BITS(types)) {
 410                error("only one type at a time.");
 411                usage_with_options(builtin_config_usage, builtin_config_options);
 412        }
 413
 414        if (get_color_slot)
 415            actions |= ACTION_GET_COLOR;
 416        if (get_colorbool_slot)
 417            actions |= ACTION_GET_COLORBOOL;
 418
 419        if ((get_color_slot || get_colorbool_slot) && types) {
 420                error("--get-color and variable type are incoherent");
 421                usage_with_options(builtin_config_usage, builtin_config_options);
 422        }
 423
 424        if (HAS_MULTI_BITS(actions)) {
 425                error("only one action at a time.");
 426                usage_with_options(builtin_config_usage, builtin_config_options);
 427        }
 428        if (actions == 0)
 429                switch (argc) {
 430                case 1: actions = ACTION_GET; break;
 431                case 2: actions = ACTION_SET; break;
 432                case 3: actions = ACTION_SET_ALL; break;
 433                default:
 434                        usage_with_options(builtin_config_usage, builtin_config_options);
 435                }
 436
 437        if (actions == ACTION_LIST) {
 438                check_argc(argc, 0, 0);
 439                if (git_config_with_options(show_all_config, NULL,
 440                                            given_config_file,
 441                                            respect_includes) < 0) {
 442                        if (given_config_file)
 443                                die_errno("unable to read config file '%s'",
 444                                          given_config_file);
 445                        else
 446                                die("error processing config file(s)");
 447                }
 448        }
 449        else if (actions == ACTION_EDIT) {
 450                check_argc(argc, 0, 0);
 451                if (!given_config_file && nongit)
 452                        die("not in a git directory");
 453                git_config(git_default_config, NULL);
 454                launch_editor(given_config_file ?
 455                              given_config_file : git_path("config"),
 456                              NULL, NULL);
 457        }
 458        else if (actions == ACTION_SET) {
 459                int ret;
 460                check_argc(argc, 2, 2);
 461                value = normalize_value(argv[0], argv[1]);
 462                ret = git_config_set_in_file(given_config_file, argv[0], value);
 463                if (ret == CONFIG_NOTHING_SET)
 464                        error("cannot overwrite multiple values with a single value\n"
 465                        "       Use a regexp, --add or --replace-all to change %s.", argv[0]);
 466                return ret;
 467        }
 468        else if (actions == ACTION_SET_ALL) {
 469                check_argc(argc, 2, 3);
 470                value = normalize_value(argv[0], argv[1]);
 471                return git_config_set_multivar_in_file(given_config_file,
 472                                                       argv[0], value, argv[2], 0);
 473        }
 474        else if (actions == ACTION_ADD) {
 475                check_argc(argc, 2, 2);
 476                value = normalize_value(argv[0], argv[1]);
 477                return git_config_set_multivar_in_file(given_config_file,
 478                                                       argv[0], value, "^$", 0);
 479        }
 480        else if (actions == ACTION_REPLACE_ALL) {
 481                check_argc(argc, 2, 3);
 482                value = normalize_value(argv[0], argv[1]);
 483                return git_config_set_multivar_in_file(given_config_file,
 484                                                       argv[0], value, argv[2], 1);
 485        }
 486        else if (actions == ACTION_GET) {
 487                check_argc(argc, 1, 2);
 488                return get_value(argv[0], argv[1]);
 489        }
 490        else if (actions == ACTION_GET_ALL) {
 491                do_all = 1;
 492                check_argc(argc, 1, 2);
 493                return get_value(argv[0], argv[1]);
 494        }
 495        else if (actions == ACTION_GET_REGEXP) {
 496                show_keys = 1;
 497                use_key_regexp = 1;
 498                do_all = 1;
 499                check_argc(argc, 1, 2);
 500                return get_value(argv[0], argv[1]);
 501        }
 502        else if (actions == ACTION_UNSET) {
 503                check_argc(argc, 1, 2);
 504                if (argc == 2)
 505                        return git_config_set_multivar_in_file(given_config_file,
 506                                                               argv[0], NULL, argv[1], 0);
 507                else
 508                        return git_config_set_in_file(given_config_file,
 509                                                      argv[0], NULL);
 510        }
 511        else if (actions == ACTION_UNSET_ALL) {
 512                check_argc(argc, 1, 2);
 513                return git_config_set_multivar_in_file(given_config_file,
 514                                                       argv[0], NULL, argv[1], 1);
 515        }
 516        else if (actions == ACTION_RENAME_SECTION) {
 517                int ret;
 518                check_argc(argc, 2, 2);
 519                ret = git_config_rename_section_in_file(given_config_file,
 520                                                        argv[0], argv[1]);
 521                if (ret < 0)
 522                        return ret;
 523                if (ret == 0)
 524                        die("No such section!");
 525        }
 526        else if (actions == ACTION_REMOVE_SECTION) {
 527                int ret;
 528                check_argc(argc, 1, 1);
 529                ret = git_config_rename_section_in_file(given_config_file,
 530                                                        argv[0], NULL);
 531                if (ret < 0)
 532                        return ret;
 533                if (ret == 0)
 534                        die("No such section!");
 535        }
 536        else if (actions == ACTION_GET_COLOR) {
 537                get_color(argv[0]);
 538        }
 539        else if (actions == ACTION_GET_COLORBOOL) {
 540                if (argc == 1)
 541                        color_stdout_is_tty = git_config_bool("command line", argv[0]);
 542                return get_colorbool(argc != 0);
 543        }
 544
 545        return 0;
 546}
 547
 548int cmd_repo_config(int argc, const char **argv, const char *prefix)
 549{
 550        fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n");
 551        return cmd_config(argc, argv, prefix);
 552}