repo-config: support --get-regexp
With --get-regexp, output all key/value pairs where the key matches a regexp. Example: git-repo-config --get-regexp remote.*.url will output something like remote.junio.url git://git.kernel.org/pub/scm/git/git.git remote.gitk.url git://git.kernel.org/pub/scm/gitk/gitk.git Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
May 2, 2006 at 14:22 UTC
2fa9a0fb31cbf01e8318a02c3e222d7fd3fd0a83
3 files changed
+60
-8
Documentation/git-repo-config.txt
+4
-1
@@ -49,7 +49,7 @@ OPTIONS
49
50
--replace-all::
51
Default behaviour is to replace at most one line. This replaces
52
- all lines matching the key (and optionally the value_regex)
52
+ all lines matching the key (and optionally the value_regex).
53
54
--get::
55
Get the value for a given key (optionally filtered by a regex
@@ -59,6 +59,9 @@ OPTIONS
59
Like get, but does not fail if the number of values for the key
60
is not exactly one.
61
62
+--get-regexp::
63
+ Like --get-all, but interprets the name as a regular expression.
64
+
65
--unset::
66
Remove the line matching the key from .git/config.
67
repo-config.c
+33
-7
@@ -6,7 +6,10 @@ static const char git_config_set_usage[] =
6
7
static char* key = NULL;
8
static char* value = NULL;
9
+static regex_t* key_regexp = NULL;
10
static regex_t* regexp = NULL;
11
+static int show_keys = 0;
12
+static int use_key_regexp = 0;
13
static int do_all = 0;
14
static int do_not_match = 0;
15
static int seen = 0;
@@ -26,16 +29,18 @@ static int show_config(const char* key_, const char* value_)
29
if (value_ == NULL)
30
value_ = "";
31
29
- if (!strcmp(key_, key) &&
32
+ if ((use_key_regexp || !strcmp(key_, key)) &&
33
+ (!use_key_regexp ||
34
+ !regexec(key_regexp, key_, 0, NULL, 0)) &&
35
(regexp == NULL ||
36
(do_not_match ^
37
!regexec(regexp, value_, 0, NULL, 0)))) {
33
- if (do_all) {
34
- printf("%s\n", value_);
35
- return 0;
36
- }
38
+ if (show_keys)
39
+ printf("%s ", key_);
40
if (seen > 0) {
38
- fprintf(stderr, "More than one value: %s\n", value);
41
+ if (!do_all)
42
+ fprintf(stderr, "More than one value: %s\n",
43
+ value);
44
free(value);
45
}
46
@@ -50,6 +55,8 @@ static int show_config(const char* key_, const char* value_)
55
value = strdup(value_);
56
}
57
seen++;
58
+ if (do_all)
59
+ printf("%s\n", value);
60
}
61
return 0;
62
}
@@ -63,6 +70,14 @@ static int get_value(const char* key_, const char* regex_)
70
key[i] = tolower(key_[i]);
71
key[i] = 0;
72
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
+
81
if (regex_) {
82
if (regex_[0] == '!') {
83
do_not_match = 1;
@@ -78,7 +93,8 @@ static int get_value(const char* key_, const char* regex_)
93
94
git_config(show_config);
95
if (value) {
81
- printf("%s\n", value);
96
+ if (!do_all)
97
+ printf("%s\n", value);
98
free(value);
99
}
100
free(key);
@@ -123,6 +139,11 @@ int main(int argc, const char **argv)
139
else if (!strcmp(argv[1], "--get-all")) {
140
do_all = 1;
141
return get_value(argv[2], NULL);
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);
147
} else
148
149
return git_config_set(argv[1], argv[2]);
@@ -136,6 +157,11 @@ int main(int argc, const char **argv)
157
else if (!strcmp(argv[1], "--get-all")) {
158
do_all = 1;
159
return get_value(argv[2], argv[3]);
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]);
165
} else if (!strcmp(argv[1], "--replace-all"))
166
167
return git_config_set_multivar(argv[2], argv[3], NULL, 1);
t/t1300-repo-config.sh
+23
@@ -247,6 +247,24 @@ EOF
247
248
test_expect_success 'hierarchical section value' 'cmp .git/config expect'
249
250
+cat > expect << EOF
251
+beta.noindent=sillyValue
252
+nextsection.nonewline=wow2 for me
253
+123456.a123=987
254
+1.2.3.alpha=beta
255
+EOF
256
+
257
+test_expect_success 'working --list' \
258
+ 'git-repo-config --list > output && cmp output expect'
259
+
260
+cat > expect << EOF
261
+beta.noindent sillyValue
262
+nextsection.nonewline wow2 for me
263
+EOF
264
+
265
+test_expect_success '--get-regexp' \
266
+ 'git-repo-config --get-regexp in > output && cmp output expect'
267
+
268
cat > .git/config << EOF
269
[novalue]
270
variable
@@ -255,5 +273,10 @@ EOF
273
test_expect_success 'get variable with no value' \
274
'git-repo-config --get novalue.variable ^$'
275
276
+git-repo-config > output 2>&1
277
+
278
+test_expect_success 'no arguments, but no crash' \
279
+ "test $? = 129 && grep usage output"
280
+
281
test_done
282