builtin-config.con commit Merge branch 'master' into db/fetch-pack (2e13e5d)
   1#include "builtin.h"
   2#include "cache.h"
   3
   4static const char git_config_set_usage[] =
   5"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";
   6
   7static char *key;
   8static regex_t *key_regexp;
   9static regex_t *regexp;
  10static int show_keys;
  11static int use_key_regexp;
  12static int do_all;
  13static int do_not_match;
  14static int seen;
  15static char delim = '=';
  16static char key_delim = ' ';
  17static char term = '\n';
  18static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
  19
  20static int show_all_config(const char *key_, const char *value_)
  21{
  22        if (value_)
  23                printf("%s%c%s%c", key_, delim, value_, term);
  24        else
  25                printf("%s%c", key_, term);
  26        return 0;
  27}
  28
  29static int show_config(const char* key_, const char* value_)
  30{
  31        char value[256];
  32        const char *vptr = value;
  33        int dup_error = 0;
  34
  35        if (!use_key_regexp && strcmp(key_, key))
  36                return 0;
  37        if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
  38                return 0;
  39        if (regexp != NULL &&
  40                         (do_not_match ^
  41                          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 (home)
  83                        global = xstrdup(mkpath("%s/.gitconfig", home));
  84                system_wide = ETC_GITCONFIG;
  85        }
  86
  87        key = xstrdup(key_);
  88        for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
  89                *tl = tolower(*tl);
  90        for (tl=key; *tl && *tl != '.'; ++tl)
  91                *tl = tolower(*tl);
  92
  93        if (use_key_regexp) {
  94                key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
  95                if (regcomp(key_regexp, key, REG_EXTENDED)) {
  96                        fprintf(stderr, "Invalid key pattern: %s\n", key_);
  97                        goto free_strings;
  98                }
  99        }
 100
 101        if (regex_) {
 102                if (regex_[0] == '!') {
 103                        do_not_match = 1;
 104                        regex_++;
 105                }
 106
 107                regexp = (regex_t*)xmalloc(sizeof(regex_t));
 108                if (regcomp(regexp, regex_, REG_EXTENDED)) {
 109                        fprintf(stderr, "Invalid pattern: %s\n", regex_);
 110                        goto free_strings;
 111                }
 112        }
 113
 114        if (do_all && system_wide)
 115                git_config_from_file(show_config, system_wide);
 116        if (do_all && global)
 117                git_config_from_file(show_config, global);
 118        git_config_from_file(show_config, local);
 119        if (!do_all && !seen && global)
 120                git_config_from_file(show_config, global);
 121        if (!do_all && !seen && system_wide)
 122                git_config_from_file(show_config, system_wide);
 123
 124        free(key);
 125        if (regexp) {
 126                regfree(regexp);
 127                free(regexp);
 128        }
 129
 130        if (do_all)
 131                ret = !seen;
 132        else
 133                ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
 134
 135free_strings:
 136        free(repo_config);
 137        free(global);
 138        return ret;
 139}
 140
 141char *normalize_value(const char *key, const char *value)
 142{
 143        char *normalized;
 144
 145        if (!value)
 146                return NULL;
 147
 148        if (type == T_RAW)
 149                normalized = xstrdup(value);
 150        else {
 151                normalized = xmalloc(64);
 152                if (type == T_INT) {
 153                        int v = git_config_int(key, value);
 154                        sprintf(normalized, "%d", v);
 155                }
 156                else if (type == T_BOOL)
 157                        sprintf(normalized, "%s",
 158                                git_config_bool(key, value) ? "true" : "false");
 159        }
 160
 161        return normalized;
 162}
 163
 164int cmd_config(int argc, const char **argv, const char *prefix)
 165{
 166        int nongit = 0;
 167        char* value;
 168        const char *file = setup_git_directory_gently(&nongit);
 169
 170        while (1 < argc) {
 171                if (!strcmp(argv[1], "--int"))
 172                        type = T_INT;
 173                else if (!strcmp(argv[1], "--bool"))
 174                        type = T_BOOL;
 175                else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
 176                        if (argc != 2)
 177                                usage(git_config_set_usage);
 178                        return git_config(show_all_config);
 179                }
 180                else if (!strcmp(argv[1], "--global")) {
 181                        char *home = getenv("HOME");
 182                        if (home) {
 183                                char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
 184                                setenv(CONFIG_ENVIRONMENT, user_config, 1);
 185                                free(user_config);
 186                        } else {
 187                                die("$HOME not set");
 188                        }
 189                }
 190                else if (!strcmp(argv[1], "--system"))
 191                        setenv(CONFIG_ENVIRONMENT, ETC_GITCONFIG, 1);
 192                else if (!strcmp(argv[1], "--file") || !strcmp(argv[1], "-f")) {
 193                        if (argc < 3)
 194                                usage(git_config_set_usage);
 195                        if (!is_absolute_path(argv[2]) && file)
 196                                file = prefix_filename(file, strlen(file),
 197                                                       argv[2]);
 198                        else
 199                                file = argv[2];
 200                        setenv(CONFIG_ENVIRONMENT, file, 1);
 201                        argc--;
 202                        argv++;
 203                }
 204                else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
 205                        term = '\0';
 206                        delim = '\n';
 207                        key_delim = '\n';
 208                }
 209                else if (!strcmp(argv[1], "--rename-section")) {
 210                        int ret;
 211                        if (argc != 4)
 212                                usage(git_config_set_usage);
 213                        ret = git_config_rename_section(argv[2], argv[3]);
 214                        if (ret < 0)
 215                                return ret;
 216                        if (ret == 0) {
 217                                fprintf(stderr, "No such section!\n");
 218                                return 1;
 219                        }
 220                        return 0;
 221                }
 222                else if (!strcmp(argv[1], "--remove-section")) {
 223                        int ret;
 224                        if (argc != 3)
 225                                usage(git_config_set_usage);
 226                        ret = git_config_rename_section(argv[2], NULL);
 227                        if (ret < 0)
 228                                return ret;
 229                        if (ret == 0) {
 230                                fprintf(stderr, "No such section!\n");
 231                                return 1;
 232                        }
 233                        return 0;
 234                }
 235                else
 236                        break;
 237                argc--;
 238                argv++;
 239        }
 240
 241        switch (argc) {
 242        case 2:
 243                return get_value(argv[1], NULL);
 244        case 3:
 245                if (!strcmp(argv[1], "--unset"))
 246                        return git_config_set(argv[2], NULL);
 247                else if (!strcmp(argv[1], "--unset-all"))
 248                        return git_config_set_multivar(argv[2], NULL, NULL, 1);
 249                else if (!strcmp(argv[1], "--get"))
 250                        return get_value(argv[2], NULL);
 251                else if (!strcmp(argv[1], "--get-all")) {
 252                        do_all = 1;
 253                        return get_value(argv[2], NULL);
 254                } else if (!strcmp(argv[1], "--get-regexp")) {
 255                        show_keys = 1;
 256                        use_key_regexp = 1;
 257                        do_all = 1;
 258                        return get_value(argv[2], NULL);
 259                } else {
 260                        value = normalize_value(argv[1], argv[2]);
 261                        return git_config_set(argv[1], value);
 262                }
 263        case 4:
 264                if (!strcmp(argv[1], "--unset"))
 265                        return git_config_set_multivar(argv[2], NULL, argv[3], 0);
 266                else if (!strcmp(argv[1], "--unset-all"))
 267                        return git_config_set_multivar(argv[2], NULL, argv[3], 1);
 268                else if (!strcmp(argv[1], "--get"))
 269                        return get_value(argv[2], argv[3]);
 270                else if (!strcmp(argv[1], "--get-all")) {
 271                        do_all = 1;
 272                        return get_value(argv[2], argv[3]);
 273                } else if (!strcmp(argv[1], "--get-regexp")) {
 274                        show_keys = 1;
 275                        use_key_regexp = 1;
 276                        do_all = 1;
 277                        return get_value(argv[2], argv[3]);
 278                } else if (!strcmp(argv[1], "--add")) {
 279                        value = normalize_value(argv[2], argv[3]);
 280                        return git_config_set_multivar(argv[2], value, "^$", 0);
 281                } else if (!strcmp(argv[1], "--replace-all")) {
 282                        value = normalize_value(argv[2], argv[3]);
 283                        return git_config_set_multivar(argv[2], value, NULL, 1);
 284                } else {
 285                        value = normalize_value(argv[1], argv[2]);
 286                        return git_config_set_multivar(argv[1], value, argv[3], 0);
 287                }
 288        case 5:
 289                if (!strcmp(argv[1], "--replace-all")) {
 290                        value = normalize_value(argv[2], argv[3]);
 291                        return git_config_set_multivar(argv[2], value, argv[4], 1);
 292                }
 293        case 1:
 294        default:
 295                usage(git_config_set_usage);
 296        }
 297        return 0;
 298}