builtin-repo-config.con commit [PATCH] honor --author even with --amend, -C, and -c. (83e24dc)
   1#include "builtin.h"
   2#include "cache.h"
   3
   4static const char git_config_set_usage[] =
   5"git-repo-config [ --global ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_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 enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
  16
  17static int show_all_config(const char *key_, const char *value_)
  18{
  19        if (value_)
  20                printf("%s=%s\n", key_, value_);
  21        else
  22                printf("%s\n", key_);
  23        return 0;
  24}
  25
  26static int show_config(const char* key_, const char* value_)
  27{
  28        char value[256];
  29        const char *vptr = value;
  30        int dup_error = 0;
  31
  32        if (!use_key_regexp && strcmp(key_, key))
  33                return 0;
  34        if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
  35                return 0;
  36        if (regexp != NULL &&
  37                         (do_not_match ^
  38                          regexec(regexp, (value_?value_:""), 0, NULL, 0)))
  39                return 0;
  40
  41        if (show_keys)
  42                printf("%s ", key_);
  43        if (seen && !do_all)
  44                dup_error = 1;
  45        if (type == T_INT)
  46                sprintf(value, "%d", git_config_int(key_, value_?value_:""));
  47        else if (type == T_BOOL)
  48                vptr = git_config_bool(key_, value_) ? "true" : "false";
  49        else
  50                vptr = value_?value_:"";
  51        seen++;
  52        if (dup_error) {
  53                error("More than one value for the key %s: %s",
  54                                key_, vptr);
  55        }
  56        else
  57                printf("%s\n", vptr);
  58
  59        return 0;
  60}
  61
  62static int get_value(const char* key_, const char* regex_)
  63{
  64        int ret = -1;
  65        char *tl;
  66        char *global = NULL, *repo_config = NULL;
  67        const char *local;
  68
  69        local = getenv(CONFIG_ENVIRONMENT);
  70        if (!local) {
  71                const char *home = getenv("HOME");
  72                local = getenv(CONFIG_LOCAL_ENVIRONMENT);
  73                if (!local)
  74                        local = repo_config = xstrdup(git_path("config"));
  75                if (home)
  76                        global = xstrdup(mkpath("%s/.gitconfig", home));
  77        }
  78
  79        key = xstrdup(key_);
  80        for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
  81                *tl = tolower(*tl);
  82        for (tl=key; *tl && *tl != '.'; ++tl)
  83                *tl = tolower(*tl);
  84
  85        if (use_key_regexp) {
  86                key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
  87                if (regcomp(key_regexp, key, REG_EXTENDED)) {
  88                        fprintf(stderr, "Invalid key pattern: %s\n", key_);
  89                        goto free_strings;
  90                }
  91        }
  92
  93        if (regex_) {
  94                if (regex_[0] == '!') {
  95                        do_not_match = 1;
  96                        regex_++;
  97                }
  98
  99                regexp = (regex_t*)xmalloc(sizeof(regex_t));
 100                if (regcomp(regexp, regex_, REG_EXTENDED)) {
 101                        fprintf(stderr, "Invalid pattern: %s\n", regex_);
 102                        goto free_strings;
 103                }
 104        }
 105
 106        if (do_all && global)
 107                git_config_from_file(show_config, global);
 108        git_config_from_file(show_config, local);
 109        if (!do_all && !seen && global)
 110                git_config_from_file(show_config, global);
 111
 112        free(key);
 113        if (regexp) {
 114                regfree(regexp);
 115                free(regexp);
 116        }
 117
 118        if (do_all)
 119                ret = !seen;
 120        else
 121                ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
 122
 123free_strings:
 124        free(repo_config);
 125        free(global);
 126        return ret;
 127}
 128
 129int cmd_repo_config(int argc, const char **argv, const char *prefix)
 130{
 131        int nongit = 0;
 132        setup_git_directory_gently(&nongit);
 133
 134        while (1 < argc) {
 135                if (!strcmp(argv[1], "--int"))
 136                        type = T_INT;
 137                else if (!strcmp(argv[1], "--bool"))
 138                        type = T_BOOL;
 139                else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
 140                        return git_config(show_all_config);
 141                else if (!strcmp(argv[1], "--global")) {
 142                        char *home = getenv("HOME");
 143                        if (home) {
 144                                char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
 145                                setenv("GIT_CONFIG", user_config, 1);
 146                                free(user_config);
 147                        } else {
 148                                die("$HOME not set");
 149                        }
 150                } else if (!strcmp(argv[1], "--rename-section")) {
 151                        int ret;
 152                        if (argc != 4)
 153                                usage(git_config_set_usage);
 154                        ret = git_config_rename_section(argv[2], argv[3]);
 155                        if (ret < 0)
 156                                return ret;
 157                        if (ret == 0) {
 158                                fprintf(stderr, "No such section!\n");
 159                                return 1;
 160                        }
 161                        return 0;
 162                } else
 163                        break;
 164                argc--;
 165                argv++;
 166        }
 167
 168        switch (argc) {
 169        case 2:
 170                return get_value(argv[1], NULL);
 171        case 3:
 172                if (!strcmp(argv[1], "--unset"))
 173                        return git_config_set(argv[2], NULL);
 174                else if (!strcmp(argv[1], "--unset-all"))
 175                        return git_config_set_multivar(argv[2], NULL, NULL, 1);
 176                else if (!strcmp(argv[1], "--get"))
 177                        return get_value(argv[2], NULL);
 178                else if (!strcmp(argv[1], "--get-all")) {
 179                        do_all = 1;
 180                        return get_value(argv[2], NULL);
 181                } else if (!strcmp(argv[1], "--get-regexp")) {
 182                        show_keys = 1;
 183                        use_key_regexp = 1;
 184                        do_all = 1;
 185                        return get_value(argv[2], NULL);
 186                } else
 187
 188                        return git_config_set(argv[1], argv[2]);
 189        case 4:
 190                if (!strcmp(argv[1], "--unset"))
 191                        return git_config_set_multivar(argv[2], NULL, argv[3], 0);
 192                else if (!strcmp(argv[1], "--unset-all"))
 193                        return git_config_set_multivar(argv[2], NULL, argv[3], 1);
 194                else if (!strcmp(argv[1], "--get"))
 195                        return get_value(argv[2], argv[3]);
 196                else if (!strcmp(argv[1], "--get-all")) {
 197                        do_all = 1;
 198                        return get_value(argv[2], argv[3]);
 199                } else if (!strcmp(argv[1], "--get-regexp")) {
 200                        show_keys = 1;
 201                        use_key_regexp = 1;
 202                        do_all = 1;
 203                        return get_value(argv[2], argv[3]);
 204                } else if (!strcmp(argv[1], "--add"))
 205                        return git_config_set_multivar(argv[2], argv[3], "^$", 0);
 206                else if (!strcmp(argv[1], "--replace-all"))
 207
 208                        return git_config_set_multivar(argv[2], argv[3], NULL, 1);
 209                else
 210
 211                        return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
 212        case 5:
 213                if (!strcmp(argv[1], "--replace-all"))
 214                        return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
 215        case 1:
 216        default:
 217                usage(git_config_set_usage);
 218        }
 219        return 0;
 220}