builtin-config.con commit allow suppressing of global and system config (ab88c36)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "color.h"
   4
   5static const char git_config_set_usage[] =
   6"git-config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int ] [ -z | --null ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list | --get-color var [default] | --get-colorbool name [stdout-is-tty]";
   7
   8static char *key;
   9static regex_t *key_regexp;
  10static regex_t *regexp;
  11static int show_keys;
  12static int use_key_regexp;
  13static int do_all;
  14static int do_not_match;
  15static int seen;
  16static char delim = '=';
  17static char key_delim = ' ';
  18static char term = '\n';
  19static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
  20
  21static int show_all_config(const char *key_, const char *value_)
  22{
  23        if (value_)
  24                printf("%s%c%s%c", key_, delim, value_, term);
  25        else
  26                printf("%s%c", key_, term);
  27        return 0;
  28}
  29
  30static int show_config(const char* key_, const char* value_)
  31{
  32        char value[256];
  33        const char *vptr = value;
  34        int dup_error = 0;
  35
  36        if (!use_key_regexp && strcmp(key_, key))
  37                return 0;
  38        if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
  39                return 0;
  40        if (regexp != NULL &&
  41            (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
  42                return 0;
  43
  44        if (show_keys) {
  45                if (value_)
  46                        printf("%s%c", key_, key_delim);
  47                else
  48                        printf("%s", key_);
  49        }
  50        if (seen && !do_all)
  51                dup_error = 1;
  52        if (type == T_INT)
  53                sprintf(value, "%d", git_config_int(key_, value_?value_:""));
  54        else if (type == T_BOOL)
  55                vptr = git_config_bool(key_, value_) ? "true" : "false";
  56        else
  57                vptr = value_?value_:"";
  58        seen++;
  59        if (dup_error) {
  60                error("More than one value for the key %s: %s",
  61                                key_, vptr);
  62        }
  63        else
  64                printf("%s%c", vptr, term);
  65
  66        return 0;
  67}
  68
  69static int get_value(const char* key_, const char* regex_)
  70{
  71        int ret = -1;
  72        char *tl;
  73        char *global = NULL, *repo_config = NULL;
  74        const char *system_wide = NULL, *local;
  75
  76        local = getenv(CONFIG_ENVIRONMENT);
  77        if (!local) {
  78                const char *home = getenv("HOME");
  79                local = getenv(CONFIG_LOCAL_ENVIRONMENT);
  80                if (!local)
  81                        local = repo_config = xstrdup(git_path("config"));
  82                if (git_config_global() && home)
  83                        global = xstrdup(mkpath("%s/.gitconfig", home));
  84                if (git_config_system())
  85                        system_wide = git_etc_gitconfig();
  86        }
  87
  88        key = xstrdup(key_);
  89        for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
  90                *tl = tolower(*tl);
  91        for (tl=key; *tl && *tl != '.'; ++tl)
  92                *tl = tolower(*tl);
  93
  94        if (use_key_regexp) {
  95                key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
  96                if (regcomp(key_regexp, key, REG_EXTENDED)) {
  97                        fprintf(stderr, "Invalid key pattern: %s\n", key_);
  98                        goto free_strings;
  99                }
 100        }
 101
 102        if (regex_) {
 103                if (regex_[0] == '!') {
 104                        do_not_match = 1;
 105                        regex_++;
 106                }
 107
 108                regexp = (regex_t*)xmalloc(sizeof(regex_t));
 109                if (regcomp(regexp, regex_, REG_EXTENDED)) {
 110                        fprintf(stderr, "Invalid pattern: %s\n", regex_);
 111                        goto free_strings;
 112                }
 113        }
 114
 115        if (do_all && system_wide)
 116                git_config_from_file(show_config, system_wide);
 117        if (do_all && global)
 118                git_config_from_file(show_config, global);
 119        git_config_from_file(show_config, local);
 120        if (!do_all && !seen && global)
 121                git_config_from_file(show_config, global);
 122        if (!do_all && !seen && system_wide)
 123                git_config_from_file(show_config, system_wide);
 124
 125        free(key);
 126        if (regexp) {
 127                regfree(regexp);
 128                free(regexp);
 129        }
 130
 131        if (do_all)
 132                ret = !seen;
 133        else
 134                ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
 135
 136free_strings:
 137        free(repo_config);
 138        free(global);
 139        return ret;
 140}
 141
 142char *normalize_value(const char *key, const char *value)
 143{
 144        char *normalized;
 145
 146        if (!value)
 147                return NULL;
 148
 149        if (type == T_RAW)
 150                normalized = xstrdup(value);
 151        else {
 152                normalized = xmalloc(64);
 153                if (type == T_INT) {
 154                        int v = git_config_int(key, value);
 155                        sprintf(normalized, "%d", v);
 156                }
 157                else if (type == T_BOOL)
 158                        sprintf(normalized, "%s",
 159                                git_config_bool(key, value) ? "true" : "false");
 160        }
 161
 162        return normalized;
 163}
 164
 165static int get_color_found;
 166static const char *get_color_slot;
 167static char parsed_color[COLOR_MAXLEN];
 168
 169static int git_get_color_config(const char *var, const char *value)
 170{
 171        if (!strcmp(var, get_color_slot)) {
 172                color_parse(value, var, parsed_color);
 173                get_color_found = 1;
 174        }
 175        return 0;
 176}
 177
 178static int get_color(int argc, const char **argv)
 179{
 180        /*
 181         * grab the color setting for the given slot from the configuration,
 182         * or parse the default value if missing, and return ANSI color
 183         * escape sequence.
 184         *
 185         * e.g.
 186         * git config --get-color color.diff.whitespace "blue reverse"
 187         */
 188        const char *def_color = NULL;
 189
 190        switch (argc) {
 191        default:
 192                usage(git_config_set_usage);
 193        case 2:
 194                def_color = argv[1];
 195                /* fallthru */
 196        case 1:
 197                get_color_slot = argv[0];
 198                break;
 199        }
 200
 201        get_color_found = 0;
 202        parsed_color[0] = '\0';
 203        git_config(git_get_color_config);
 204
 205        if (!get_color_found && def_color)
 206                color_parse(def_color, "command line", parsed_color);
 207
 208        fputs(parsed_color, stdout);
 209        return 0;
 210}
 211
 212static int stdout_is_tty;
 213static int get_colorbool_found;
 214static int get_diff_color_found;
 215static int git_get_colorbool_config(const char *var, const char *value)
 216{
 217        if (!strcmp(var, get_color_slot)) {
 218                get_colorbool_found =
 219                        git_config_colorbool(var, value, stdout_is_tty);
 220        }
 221        if (!strcmp(var, "diff.color")) {
 222                get_diff_color_found =
 223                        git_config_colorbool(var, value, stdout_is_tty);
 224        }
 225        return 0;
 226}
 227
 228static int get_colorbool(int argc, const char **argv)
 229{
 230        /*
 231         * git config --get-colorbool <slot> [<stdout-is-tty>]
 232         *
 233         * returns "true" or "false" depending on how <slot>
 234         * is configured.
 235         */
 236
 237        if (argc == 2)
 238                stdout_is_tty = git_config_bool("command line", argv[1]);
 239        else if (argc == 1)
 240                stdout_is_tty = isatty(1);
 241        else
 242                usage(git_config_set_usage);
 243        get_colorbool_found = -1;
 244        get_diff_color_found = -1;
 245        get_color_slot = argv[0];
 246        git_config(git_get_colorbool_config);
 247
 248        if (get_colorbool_found < 0) {
 249                if (!strcmp(get_color_slot, "color.diff"))
 250                        get_colorbool_found = get_diff_color_found;
 251                if (get_colorbool_found < 0)
 252                        get_colorbool_found = 0;
 253        }
 254
 255        if (argc == 1) {
 256                return get_colorbool_found ? 0 : 1;
 257        } else {
 258                printf("%s\n", get_colorbool_found ? "true" : "false");
 259                return 0;
 260        }
 261}
 262
 263int cmd_config(int argc, const char **argv, const char *prefix)
 264{
 265        int nongit = 0;
 266        char* value;
 267        const char *file = setup_git_directory_gently(&nongit);
 268
 269        while (1 < argc) {
 270                if (!strcmp(argv[1], "--int"))
 271                        type = T_INT;
 272                else if (!strcmp(argv[1], "--bool"))
 273                        type = T_BOOL;
 274                else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
 275                        if (argc != 2)
 276                                usage(git_config_set_usage);
 277                        if (git_config(show_all_config) < 0 && file && errno)
 278                                die("unable to read config file %s: %s", file,
 279                                    strerror(errno));
 280                        return 0;
 281                }
 282                else if (!strcmp(argv[1], "--global")) {
 283                        char *home = getenv("HOME");
 284                        if (home) {
 285                                char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
 286                                setenv(CONFIG_ENVIRONMENT, user_config, 1);
 287                                free(user_config);
 288                        } else {
 289                                die("$HOME not set");
 290                        }
 291                }
 292                else if (!strcmp(argv[1], "--system"))
 293                        setenv(CONFIG_ENVIRONMENT, git_etc_gitconfig(), 1);
 294                else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
 295                        if (argc < 3)
 296                                usage(git_config_set_usage);
 297                        if (!is_absolute_path(argv[2]) && file)
 298                                file = prefix_filename(file, strlen(file),
 299                                                       argv[2]);
 300                        else
 301                                file = argv[2];
 302                        setenv(CONFIG_ENVIRONMENT, file, 1);
 303                        argc--;
 304                        argv++;
 305                }
 306                else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
 307                        term = '\0';
 308                        delim = '\n';
 309                        key_delim = '\n';
 310                }
 311                else if (!strcmp(argv[1], "--rename-section")) {
 312                        int ret;
 313                        if (argc != 4)
 314                                usage(git_config_set_usage);
 315                        ret = git_config_rename_section(argv[2], argv[3]);
 316                        if (ret < 0)
 317                                return ret;
 318                        if (ret == 0) {
 319                                fprintf(stderr, "No such section!\n");
 320                                return 1;
 321                        }
 322                        return 0;
 323                }
 324                else if (!strcmp(argv[1], "--remove-section")) {
 325                        int ret;
 326                        if (argc != 3)
 327                                usage(git_config_set_usage);
 328                        ret = git_config_rename_section(argv[2], NULL);
 329                        if (ret < 0)
 330                                return ret;
 331                        if (ret == 0) {
 332                                fprintf(stderr, "No such section!\n");
 333                                return 1;
 334                        }
 335                        return 0;
 336                } else if (!strcmp(argv[1], "--get-color")) {
 337                        return get_color(argc-2, argv+2);
 338                } else if (!strcmp(argv[1], "--get-colorbool")) {
 339                        return get_colorbool(argc-2, argv+2);
 340                } else
 341                        break;
 342                argc--;
 343                argv++;
 344        }
 345
 346        switch (argc) {
 347        case 2:
 348                return get_value(argv[1], NULL);
 349        case 3:
 350                if (!strcmp(argv[1], "--unset"))
 351                        return git_config_set(argv[2], NULL);
 352                else if (!strcmp(argv[1], "--unset-all"))
 353                        return git_config_set_multivar(argv[2], NULL, NULL, 1);
 354                else if (!strcmp(argv[1], "--get"))
 355                        return get_value(argv[2], NULL);
 356                else if (!strcmp(argv[1], "--get-all")) {
 357                        do_all = 1;
 358                        return get_value(argv[2], NULL);
 359                } else if (!strcmp(argv[1], "--get-regexp")) {
 360                        show_keys = 1;
 361                        use_key_regexp = 1;
 362                        do_all = 1;
 363                        return get_value(argv[2], NULL);
 364                } else {
 365                        value = normalize_value(argv[1], argv[2]);
 366                        return git_config_set(argv[1], value);
 367                }
 368        case 4:
 369                if (!strcmp(argv[1], "--unset"))
 370                        return git_config_set_multivar(argv[2], NULL, argv[3], 0);
 371                else if (!strcmp(argv[1], "--unset-all"))
 372                        return git_config_set_multivar(argv[2], NULL, argv[3], 1);
 373                else if (!strcmp(argv[1], "--get"))
 374                        return get_value(argv[2], argv[3]);
 375                else if (!strcmp(argv[1], "--get-all")) {
 376                        do_all = 1;
 377                        return get_value(argv[2], argv[3]);
 378                } else if (!strcmp(argv[1], "--get-regexp")) {
 379                        show_keys = 1;
 380                        use_key_regexp = 1;
 381                        do_all = 1;
 382                        return get_value(argv[2], argv[3]);
 383                } else if (!strcmp(argv[1], "--add")) {
 384                        value = normalize_value(argv[2], argv[3]);
 385                        return git_config_set_multivar(argv[2], value, "^$", 0);
 386                } else if (!strcmp(argv[1], "--replace-all")) {
 387                        value = normalize_value(argv[2], argv[3]);
 388                        return git_config_set_multivar(argv[2], value, NULL, 1);
 389                } else {
 390                        value = normalize_value(argv[1], argv[2]);
 391                        return git_config_set_multivar(argv[1], value, argv[3], 0);
 392                }
 393        case 5:
 394                if (!strcmp(argv[1], "--replace-all")) {
 395                        value = normalize_value(argv[2], argv[3]);
 396                        return git_config_set_multivar(argv[2], value, argv[4], 1);
 397                }
 398        case 1:
 399        default:
 400                usage(git_config_set_usage);
 401        }
 402        return 0;
 403}