repo-config.con commit Merge branch 'jc/cache-tree' into next (70b1fb2)
   1#include "cache.h"
   2#include <regex.h>
   3
   4static const char git_config_set_usage[] =
   5"git-repo-config [ --bool | --int ] [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
   6
   7static char* key = NULL;
   8static char* value = NULL;
   9static regex_t* regexp = NULL;
  10static int do_all = 0;
  11static int do_not_match = 0;
  12static int seen = 0;
  13static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
  14
  15static int show_all_config(const char *key_, const char *value_)
  16{
  17        if (value_)
  18                printf("%s=%s\n", key_, value_);
  19        else
  20                printf("%s\n", key_);
  21        return 0;
  22}
  23
  24static int show_config(const char* key_, const char* value_)
  25{
  26        if (value_ == NULL)
  27                value_ = "";
  28
  29        if (!strcmp(key_, key) &&
  30                        (regexp == NULL ||
  31                         (do_not_match ^
  32                          !regexec(regexp, value_, 0, NULL, 0)))) {
  33                if (do_all) {
  34                        printf("%s\n", value_);
  35                        return 0;
  36                }
  37                if (seen > 0) {
  38                        fprintf(stderr, "More than one value: %s\n", value);
  39                        free(value);
  40                }
  41
  42                if (type == T_INT) {
  43                        value = malloc(256);
  44                        sprintf(value, "%d", git_config_int(key_, value_));
  45                } else if (type == T_BOOL) {
  46                        value = malloc(256);
  47                        sprintf(value, "%s", git_config_bool(key_, value_)
  48                                             ? "true" : "false");
  49                } else {
  50                        value = strdup(value_);
  51                }
  52                seen++;
  53        }
  54        return 0;
  55}
  56
  57static int get_value(const char* key_, const char* regex_)
  58{
  59        int i;
  60
  61        key = malloc(strlen(key_)+1);
  62        for (i = 0; key_[i]; i++)
  63                key[i] = tolower(key_[i]);
  64        key[i] = 0;
  65
  66        if (regex_) {
  67                if (regex_[0] == '!') {
  68                        do_not_match = 1;
  69                        regex_++;
  70                }
  71
  72                regexp = (regex_t*)malloc(sizeof(regex_t));
  73                if (regcomp(regexp, regex_, REG_EXTENDED)) {
  74                        fprintf(stderr, "Invalid pattern: %s\n", regex_);
  75                        return -1;
  76                }
  77        }
  78
  79        git_config(show_config);
  80        if (value) {
  81                printf("%s\n", value);
  82                free(value);
  83        }
  84        free(key);
  85        if (regexp) {
  86                regfree(regexp);
  87                free(regexp);
  88        }
  89
  90        if (do_all)
  91                return 0;
  92
  93        return seen == 1 ? 0 : 1;
  94}
  95
  96int main(int argc, const char **argv)
  97{
  98        setup_git_directory();
  99
 100        while (1 < argc) {
 101                if (!strcmp(argv[1], "--int"))
 102                        type = T_INT;
 103                else if (!strcmp(argv[1], "--bool"))
 104                        type = T_BOOL;
 105                else
 106                        break;
 107                argc--;
 108                argv++;
 109        }
 110
 111        if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
 112                return git_config(show_all_config);
 113
 114        switch (argc) {
 115        case 2:
 116                return get_value(argv[1], NULL);
 117        case 3:
 118                if (!strcmp(argv[1], "--unset"))
 119                        return git_config_set(argv[2], NULL);
 120                else if (!strcmp(argv[1], "--unset-all"))
 121                        return git_config_set_multivar(argv[2], NULL, NULL, 1);
 122                else if (!strcmp(argv[1], "--get"))
 123                        return get_value(argv[2], NULL);
 124                else if (!strcmp(argv[1], "--get-all")) {
 125                        do_all = 1;
 126                        return get_value(argv[2], NULL);
 127                } else
 128
 129                        return git_config_set(argv[1], argv[2]);
 130        case 4:
 131                if (!strcmp(argv[1], "--unset"))
 132                        return git_config_set_multivar(argv[2], NULL, argv[3], 0);
 133                else if (!strcmp(argv[1], "--unset-all"))
 134                        return git_config_set_multivar(argv[2], NULL, argv[3], 1);
 135                else if (!strcmp(argv[1], "--get"))
 136                        return get_value(argv[2], argv[3]);
 137                else if (!strcmp(argv[1], "--get-all")) {
 138                        do_all = 1;
 139                        return get_value(argv[2], argv[3]);
 140                } else if (!strcmp(argv[1], "--replace-all"))
 141
 142                        return git_config_set_multivar(argv[2], argv[3], NULL, 1);
 143                else
 144
 145                        return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
 146        case 5:
 147                if (!strcmp(argv[1], "--replace-all"))
 148                        return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
 149        case 1:
 150        default:
 151                usage(git_config_set_usage);
 152        }
 153        return 0;
 154}