repo-config.con commit GIT 1.0.4 (6ab5889)
   1#include "cache.h"
   2#include <regex.h>
   3
   4static const char git_config_set_usage[] =
   5"git-repo-config [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
   6
   7static char* key = NULL;
   8static char* value = NULL;
   9static regex_t* regex = NULL;
  10static int do_all = 0;
  11static int do_not_match = 0;
  12static int seen = 0;
  13
  14static int show_config(const char* key_, const char* value_)
  15{
  16        if (!strcmp(key_, key) &&
  17                        (regex == NULL ||
  18                         (do_not_match ^
  19                          !regexec(regex, value_, 0, NULL, 0)))) {
  20                if (do_all) {
  21                        printf("%s\n", value_);
  22                        return 0;
  23                }
  24                if (seen > 0) {
  25                        fprintf(stderr, "More than one value: %s\n", value);
  26                        free(value);
  27                }
  28                value = strdup(value_);
  29                seen++;
  30        }
  31        return 0;
  32}
  33
  34static int get_value(const char* key_, const char* regex_)
  35{
  36        int i;
  37
  38        key = malloc(strlen(key_)+1);
  39        for (i = 0; key_[i]; i++)
  40                key[i] = tolower(key_[i]);
  41        key[i] = 0;
  42
  43        if (regex_) {
  44                if (regex_[0] == '!') {
  45                        do_not_match = 1;
  46                        regex_++;
  47                }
  48
  49                regex = (regex_t*)malloc(sizeof(regex_t));
  50                if (regcomp(regex, regex_, REG_EXTENDED)) {
  51                        fprintf(stderr, "Invalid pattern: %s\n", regex_);
  52                        return -1;
  53                }
  54        }
  55
  56        i = git_config(show_config);
  57        if (value) {
  58                printf("%s\n", value);
  59                free(value);
  60        }
  61        free(key);
  62        if (regex) {
  63                regfree(regex);
  64                free(regex);
  65        }
  66
  67        if (do_all)
  68                return 0;
  69
  70        return seen == 1 ? 0 : 1;
  71}
  72
  73int main(int argc, const char **argv)
  74{
  75        setup_git_directory();
  76        switch (argc) {
  77        case 2:
  78                return get_value(argv[1], NULL);
  79        case 3:
  80                if (!strcmp(argv[1], "--unset"))
  81                        return git_config_set(argv[2], NULL);
  82                else if (!strcmp(argv[1], "--unset-all"))
  83                        return git_config_set_multivar(argv[2], NULL, NULL, 1);
  84                else if (!strcmp(argv[1], "--get"))
  85                        return get_value(argv[2], NULL);
  86                else if (!strcmp(argv[1], "--get-all")) {
  87                        do_all = 1;
  88                        return get_value(argv[2], NULL);
  89                } else
  90
  91                        return git_config_set(argv[1], argv[2]);
  92        case 4:
  93                if (!strcmp(argv[1], "--unset"))
  94                        return git_config_set_multivar(argv[2], NULL, argv[3], 0);
  95                else if (!strcmp(argv[1], "--unset-all"))
  96                        return git_config_set_multivar(argv[2], NULL, argv[3], 1);
  97                else if (!strcmp(argv[1], "--get"))
  98                        return get_value(argv[2], argv[3]);
  99                else if (!strcmp(argv[1], "--get-all")) {
 100                        do_all = 1;
 101                        return get_value(argv[2], argv[3]);
 102                } else if (!strcmp(argv[1], "--replace-all"))
 103
 104                        return git_config_set_multivar(argv[2], argv[3], NULL, 1);
 105                else
 106
 107                        return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
 108        case 5:
 109                if (!strcmp(argv[1], "--replace-all"))
 110                        return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
 111        case 1:
 112        default:
 113                usage(git_config_set_usage);
 114        }
 115        return 0;
 116}