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