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