repo-config: readability fixups.
[git/git.git] / repo-config.c
CommitLineData
1b1e59c5 1#include "cache.h"
4ddba79d 2#include <regex.h>
1b1e59c5
JS
3
4static const char git_config_set_usage[] =
e098c6f8 5"git-repo-config [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
4ddba79d
JS
6
7static char* key = NULL;
2fa9a0fb 8static regex_t* key_regexp = NULL;
0a152171 9static regex_t* regexp = NULL;
2fa9a0fb
JS
10static int show_keys = 0;
11static int use_key_regexp = 0;
4ddba79d 12static int do_all = 0;
f98d863d 13static int do_not_match = 0;
4ddba79d 14static int seen = 0;
7162dff3 15static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
4ddba79d 16
de791f15
PB
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
4ddba79d
JS
26static int show_config(const char* key_, const char* value_)
27{
e098c6f8
JH
28 char value[256];
29 const char *vptr = value;
30
f067a137
JF
31 if (value_ == NULL)
32 value_ = "";
33
2fa9a0fb
JS
34 if ((use_key_regexp || !strcmp(key_, key)) &&
35 (!use_key_regexp ||
36 !regexec(key_regexp, key_, 0, NULL, 0)) &&
0a152171 37 (regexp == NULL ||
f98d863d 38 (do_not_match ^
0a152171 39 !regexec(regexp, value_, 0, NULL, 0)))) {
e098c6f8 40 int dup_error = 0;
2fa9a0fb
JS
41 if (show_keys)
42 printf("%s ", key_);
e098c6f8
JH
43 if (seen && !do_all)
44 dup_error = 1;
45 if (type == T_INT)
7162dff3 46 sprintf(value, "%d", git_config_int(key_, value_));
e098c6f8 47 else if (type == T_BOOL)
7162dff3
PB
48 sprintf(value, "%s", git_config_bool(key_, value_)
49 ? "true" : "false");
e098c6f8
JH
50 else
51 vptr = value_;
4ddba79d 52 seen++;
e098c6f8
JH
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);
4ddba79d
JS
59 }
60 return 0;
61}
62
63static 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]);
3dd94e3b 70 key[i] = 0;
4ddba79d 71
2fa9a0fb
JS
72 if (use_key_regexp) {
73 key_regexp = (regex_t*)malloc(sizeof(regex_t));
74 if (regcomp(key_regexp, key, REG_EXTENDED)) {
e098c6f8 75 fprintf(stderr, "Invalid key pattern: %s\n", key_);
2fa9a0fb
JS
76 return -1;
77 }
78 }
79
4ddba79d 80 if (regex_) {
f98d863d
JS
81 if (regex_[0] == '!') {
82 do_not_match = 1;
83 regex_++;
84 }
85
0a152171
AW
86 regexp = (regex_t*)malloc(sizeof(regex_t));
87 if (regcomp(regexp, regex_, REG_EXTENDED)) {
4ddba79d
JS
88 fprintf(stderr, "Invalid pattern: %s\n", regex_);
89 return -1;
90 }
91 }
92
de791f15 93 git_config(show_config);
4ddba79d 94 free(key);
0a152171
AW
95 if (regexp) {
96 regfree(regexp);
97 free(regexp);
4ddba79d
JS
98 }
99
100 if (do_all)
e098c6f8 101 return !seen;
4ddba79d 102
e098c6f8 103 return (seen == 1) ? 0 : 1;
4ddba79d 104}
1b1e59c5
JS
105
106int main(int argc, const char **argv)
107{
108 setup_git_directory();
7162dff3
PB
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;
cfa24e18
JS
115 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
116 return git_config(show_all_config);
7162dff3
PB
117 else
118 break;
119 argc--;
120 argv++;
121 }
122
1b1e59c5
JS
123 switch (argc) {
124 case 2:
4ddba79d 125 return get_value(argv[1], NULL);
1b1e59c5
JS
126 case 3:
127 if (!strcmp(argv[1], "--unset"))
128 return git_config_set(argv[2], NULL);
4ddba79d
JS
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);
2fa9a0fb
JS
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);
4ddba79d
JS
141 } else
142
1b1e59c5
JS
143 return git_config_set(argv[1], argv[2]);
144 case 4:
145 if (!strcmp(argv[1], "--unset"))
4ddba79d
JS
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]);
2fa9a0fb
JS
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]);
4ddba79d
JS
159 } else if (!strcmp(argv[1], "--replace-all"))
160
161 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
1b1e59c5 162 else
4ddba79d
JS
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:
1b1e59c5
JS
169 default:
170 usage(git_config_set_usage);
171 }
172 return 0;
173}