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