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