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