git-config-set: support selecting values by non-matching regex
Extend the regex syntax of value_regex so that prepending an exclamation mark means non-match: [core] quetzal = "Dodo" for Brainf*ck quetzal = "T. Rex" for Malbolge quetzal = "cat" You can match the third line with git-config-set --get quetzal '! for ' Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Nov 20, 2005 at 13:24 UTC
f98d863d2122e1b8781dfb9889df98876a26f315
4 files changed
+70
-8
Documentation/git-config-set.txt
+17
-1
@@ -22,7 +22,9 @@ actually the section and the key separated by a dot, and the value will be
22
escaped.
23
24
If you want to set/unset an option which can occor on multiple lines, you
25
-should provide a POSIX regex for the value.
25
+should provide a POSIX regex for the value. If you want to handle the lines
26
+*not* matching the regex, just prepend a single exlamation mark in front
27
+(see EXAMPLES).
28
29
This command will fail if
30
@@ -82,6 +84,7 @@ Given a .git/config like this:
84
command="ssh" for "ssh://kernel.org/"
85
command="proxy-command" for kernel.org
86
command="myprotocol-command" for "my://"
87
+ command=default-proxy ; for all the rest
88
89
you can set the filemode to true with
90
@@ -139,6 +142,19 @@ new one with
142
% git config-set --replace-all proxy.command ssh
143
------------
144
145
+However, if you really only want to replace the line for the default proxy,
146
+i.e. the one without a "for ..." postfix, do something like this:
147
+
148
+------------
149
+% git config-set proxy.command ssh '! for '
150
+------------
151
+
152
+To actually match only values with an exclamation mark, you have to
153
+
154
+------------
155
+% git config-set section.key value '[!]'
156
+------------
157
+
158
159
Author
160
------
config-set.c
+8
-1
@@ -8,13 +8,15 @@ static char* key = NULL;
8
static char* value = NULL;
9
static regex_t* regex = NULL;
10
static int do_all = 0;
11
+static int do_not_match = 0;
12
static int seen = 0;
13
14
static int show_config(const char* key_, const char* value_)
15
{
16
if (!strcmp(key_, key) &&
17
(regex == NULL ||
17
- !regexec(regex, value_, 0, NULL, 0))) {
18
+ (do_not_match ^
19
+ !regexec(regex, value_, 0, NULL, 0)))) {
20
if (do_all) {
21
printf("%s\n", value_);
22
return 0;
@@ -38,6 +40,11 @@ static int get_value(const char* key_, const char* regex_)
40
key[i] = tolower(key_[i]);
41
42
if (regex_) {
43
+ if (regex_[0] == '!') {
44
+ do_not_match = 1;
45
+ regex_++;
46
+ }
47
+
48
regex = (regex_t*)malloc(sizeof(regex_t));
49
if (regcomp(regex, regex_, REG_EXTENDED)) {
50
fprintf(stderr, "Invalid pattern: %s\n", regex_);
config.c
+17
-6
@@ -269,6 +269,7 @@ int git_config(config_fn_t fn)
269
static struct {
270
int baselen;
271
char* key;
272
+ int do_not_match;
273
regex_t* value_regex;
274
int multi_replace;
275
off_t offset[MAX_MATCHES];
@@ -276,13 +277,19 @@ static struct {
277
int seen;
278
} store;
279
280
+static int matches(const char* key, const char* value)
281
+{
282
+ return !strcmp(key, store.key) &&
283
+ (store.value_regex == NULL ||
284
+ (store.do_not_match ^
285
+ !regexec(store.value_regex, value, 0, NULL, 0)));
286
+}
287
+
288
static int store_aux(const char* key, const char* value)
289
{
290
switch (store.state) {
291
case KEY_SEEN:
283
- if (!strcmp(key, store.key) &&
284
- (store.value_regex == NULL ||
285
- !regexec(store.value_regex, value, 0, NULL, 0))) {
292
+ if (matches(key, value)) {
293
if (store.seen == 1 && store.multi_replace == 0) {
294
fprintf(stderr,
295
"Warning: %s has multiple values\n",
@@ -306,9 +313,7 @@ static int store_aux(const char* key, const char* value)
313
/* fallthru */
314
case SECTION_END_SEEN:
315
case START:
309
- if (!strcmp(key, store.key) &&
310
- (store.value_regex == NULL ||
311
- !regexec(store.value_regex, value, 0, NULL, 0))) {
316
+ if (matches(key, value)) {
317
store.offset[store.seen] = ftell(config_file);
318
store.state = KEY_SEEN;
319
store.seen++;
@@ -471,6 +476,12 @@ int git_config_set_multivar(const char* key, const char* value,
476
if (value_regex == NULL)
477
store.value_regex = NULL;
478
else {
479
+ if (value_regex[0] == '!') {
480
+ store.do_not_match = 1;
481
+ value_regex++;
482
+ } else
483
+ store.do_not_match = 0;
484
+
485
store.value_regex = (regex_t*)malloc(sizeof(regex_t));
486
if (regcomp(store.value_regex, value_regex,
487
REG_EXTENDED)) {
t/t1300-config-set.sh
+28
@@ -69,6 +69,28 @@ EOF
69
70
test_expect_success 'similar section' 'cmp .git/config expect'
71
72
+test_expect_success 'replace with non-match' \
73
+ 'git-config-set core.penguin kingpin !blue'
74
+
75
+test_expect_success 'replace with non-match (actually matching)' \
76
+ 'git-config-set core.penguin "very blue" !kingpin'
77
+
78
+cat > expect << EOF
79
+#
80
+# This is the config file
81
+#
82
+
83
+[core]
84
+ penguin = very blue
85
+ Movie = BadPhysics
86
+ UPPERCASE = true
87
+ penguin = kingpin
88
+[Cores]
89
+ WhatEver = Second
90
+EOF
91
+
92
+test_expect_success 'non-match result' 'cmp .git/config expect'
93
+
94
cat > .git/config << EOF
95
[beta] ; silly comment # another comment
96
noIndent= sillyValue ; 'nother silly comment
@@ -173,6 +195,12 @@ EOF
195
196
test_expect_success 'multivar' 'cmp .git/config expect'
197
198
+test_expect_success 'non-match' \
199
+ 'git-config-set --get nextsection.nonewline !for'
200
+
201
+test_expect_success 'non-match value' \
202
+ 'test wow = $(git-config-set --get nextsection.nonewline !for)'
203
+
204
test_expect_failure 'ambiguous get' \
205
'git-config-set --get nextsection.nonewline'
206