repo-config.con commit git config syntax updates (d14f776)
   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        char *tl;
  52
  53        key = strdup(key_);
  54        for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
  55                *tl = tolower(*tl);
  56        for (tl=key; *tl && *tl != '.'; ++tl)
  57                *tl = tolower(*tl);
  58
  59        if (regex_) {
  60                if (regex_[0] == '!') {
  61                        do_not_match = 1;
  62                        regex_++;
  63                }
  64
  65                regexp = (regex_t*)malloc(sizeof(regex_t));
  66                if (regcomp(regexp, regex_, REG_EXTENDED)) {
  67                        fprintf(stderr, "Invalid pattern: %s\n", regex_);
  68                        return -1;
  69                }
  70        }
  71
  72        i = git_config(show_config);
  73        if (value) {
  74                printf("%s\n", value);
  75                free(value);
  76        }
  77        free(key);
  78        if (regexp) {
  79                regfree(regexp);
  80                free(regexp);
  81        }
  82
  83        if (do_all)
  84                return 0;
  85
  86        return seen == 1 ? 0 : 1;
  87}
  88
  89int main(int argc, const char **argv)
  90{
  91        setup_git_directory();
  92
  93        while (1 < argc) {
  94                if (!strcmp(argv[1], "--int"))
  95                        type = T_INT;
  96                else if (!strcmp(argv[1], "--bool"))
  97                        type = T_BOOL;
  98                else
  99                        break;
 100                argc--;
 101                argv++;
 102        }
 103
 104        switch (argc) {
 105        case 2:
 106                return get_value(argv[1], NULL);
 107        case 3:
 108                if (!strcmp(argv[1], "--unset"))
 109                        return git_config_set(argv[2], NULL);
 110                else if (!strcmp(argv[1], "--unset-all"))
 111                        return git_config_set_multivar(argv[2], NULL, NULL, 1);
 112                else if (!strcmp(argv[1], "--get"))
 113                        return get_value(argv[2], NULL);
 114                else if (!strcmp(argv[1], "--get-all")) {
 115                        do_all = 1;
 116                        return get_value(argv[2], NULL);
 117                } else
 118
 119                        return git_config_set(argv[1], argv[2]);
 120        case 4:
 121                if (!strcmp(argv[1], "--unset"))
 122                        return git_config_set_multivar(argv[2], NULL, argv[3], 0);
 123                else if (!strcmp(argv[1], "--unset-all"))
 124                        return git_config_set_multivar(argv[2], NULL, argv[3], 1);
 125                else if (!strcmp(argv[1], "--get"))
 126                        return get_value(argv[2], argv[3]);
 127                else if (!strcmp(argv[1], "--get-all")) {
 128                        do_all = 1;
 129                        return get_value(argv[2], argv[3]);
 130                } else if (!strcmp(argv[1], "--replace-all"))
 131
 132                        return git_config_set_multivar(argv[2], argv[3], NULL, 1);
 133                else
 134
 135                        return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
 136        case 5:
 137                if (!strcmp(argv[1], "--replace-all"))
 138                        return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
 139        case 1:
 140        default:
 141                usage(git_config_set_usage);
 142        }
 143        return 0;
 144}