git-config-set: add more options
... namely --replace-all, to replace any amount of matching lines, not just 0 or 1, --get, to get the value of one key, --get-all, the multivar version of --get, and --unset-all, which deletes all matching lines from .git/config 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 06:52 UTC
4ddba79db76bd6425f00e99ceb1d82d179319aec
5 files changed
+258
-61
Documentation/git-config-set.txt
+54
-8
@@ -8,12 +8,18 @@ git-config-set - Set options in .git/config.
8
9
SYNOPSIS
10
--------
11
-'git-config-set' ( name [value [value_regex]] | --unset name [value_regex] )
11
+'git-config-set' name [value [value_regex]]
12
+'git-config-set' --replace-all name [value [value_regex]]
13
+'git-config-set' --get name [value_regex]
14
+'git-config-set' --get-all name [value_regex]
15
+'git-config-set' --unset name [value_regex]
16
+'git-config-set' --unset-all name [value_regex]
17
18
DESCRIPTION
19
-----------
15
-You can set/replace/unset options with this command. The name is actually
16
-the section and the key separated by a dot, and the value will be escaped.
20
+You can query/set/replace/unset options with this command. The name is
21
+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.
@@ -31,8 +37,23 @@ This command will fail if
37
OPTIONS
38
-------
39
40
+--replace-all::
41
+ Default behaviour is to replace at most one line. This replaces
42
+ all lines matching the key (and optionally the value_regex)
43
+
44
+--get::
45
+ Get the value for a given key (optionally filtered by a regex
46
+ matching the value).
47
+
48
+--get-all::
49
+ Like get, but does not fail if the number of values for the key
50
+ is not exactly one.
51
+
52
--unset::
35
- Remove the given option from .git/config
53
+ Remove the line matching the key from .git/config.
54
+
55
+--unset-all::
56
+ Remove all matching lines from .git/config.
57
58
59
EXAMPLE
@@ -84,14 +105,39 @@ To delete the entry for renames, do
105
% git config-set --unset diff.renames
106
------------
107
87
-or just
108
+If you want to delete an entry for a multivar (like proxy.command above),
109
+you have to provide a regex matching the value of exactly one line.
110
+
111
+To query the value for a given key, do
112
113
------------
90
-% git config-set diff.renames
114
+% git config-set --get core.filemode
115
------------
116
93
-If you want to delete an entry for a multivar (like proxy.command above),
94
-you have to provide a regex matching the value of exactly one line.
117
+or
118
+
119
+------------
120
+% git config-set core.filemode
121
+------------
122
+
123
+or, to query a multivar:
124
+
125
+------------
126
+% git config-set --get proxy.command "for kernel.org$"
127
+------------
128
+
129
+If you want to know all the values for a multivar, do:
130
+
131
+------------
132
+% git config-set --get-all proxy.command
133
+------------
134
+
135
+If you like to live dangerous, you can replace *all* proxy.commands by a
136
+new one with
137
+
138
+------------
139
+% git config-set --replace-all proxy.command ssh
140
+------------
141
142
143
Author
cache.h
+1
-2
@@ -192,7 +192,6 @@ extern int diff_rename_limit_default;
192
193
/* Return a statically allocated filename matching the sha1 signature */
194
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
195
-extern char *enter_repo(char *path, int strict);
195
extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
196
extern char *sha1_file_name(const unsigned char *sha1);
197
extern char *sha1_pack_name(const unsigned char *sha1);
@@ -388,7 +387,7 @@ extern int git_config(config_fn_t fn);
387
extern int git_config_int(const char *, const char *);
388
extern int git_config_bool(const char *, const char *);
389
extern int git_config_set(const char *, const char *);
391
-extern int git_config_set_multivar(const char *, const char *, const char *);
390
+extern int git_config_set_multivar(const char *, const char *, const char *, int);
391
392
#define MAX_GITNAME (1000)
393
extern char git_default_email[MAX_GITNAME];
config-set.c
+87
-5
@@ -1,24 +1,106 @@
1
#include "cache.h"
2
+#include <regex.h>
3
4
static const char git_config_set_usage[] =
4
-"git-config-set name [value [value_regex]] | --unset name [value_regex]";
5
+"git-config-set [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
6
+
7
+static char* key = NULL;
8
+static char* value = NULL;
9
+static regex_t* regex = NULL;
10
+static int do_all = 0;
11
+static int seen = 0;
12
+
13
+static int show_config(const char* key_, const char* value_)
14
+{
15
+ if (!strcmp(key_, key) &&
16
+ (regex == NULL ||
17
+ !regexec(regex, value_, 0, NULL, 0))) {
18
+ if (do_all) {
19
+ printf("%s\n", value_);
20
+ return 0;
21
+ }
22
+ if (seen > 0) {
23
+ fprintf(stderr, "More than one value: %s\n", value);
24
+ free(value);
25
+ }
26
+ value = strdup(value_);
27
+ seen++;
28
+ }
29
+ return 0;
30
+}
31
+
32
+static int get_value(const char* key_, const char* regex_)
33
+{
34
+ int i;
35
+
36
+ key = malloc(strlen(key_)+1);
37
+ for (i = 0; key_[i]; i++)
38
+ key[i] = tolower(key_[i]);
39
+
40
+ if (regex_) {
41
+ regex = (regex_t*)malloc(sizeof(regex_t));
42
+ if (regcomp(regex, regex_, REG_EXTENDED)) {
43
+ fprintf(stderr, "Invalid pattern: %s\n", regex_);
44
+ return -1;
45
+ }
46
+ }
47
+
48
+ i = git_config(show_config);
49
+ if (value) {
50
+ printf("%s\n", value);
51
+ free(value);
52
+ }
53
+ free(key);
54
+ if (regex) {
55
+ regfree(regex);
56
+ free(regex);
57
+ }
58
+
59
+ if (do_all)
60
+ return 0;
61
+
62
+ return seen == 1 ? 0 : 1;
63
+}
64
65
int main(int argc, const char **argv)
66
{
67
setup_git_directory();
68
switch (argc) {
69
case 2:
11
- return git_config_set(argv[1], NULL);
70
+ return get_value(argv[1], NULL);
71
case 3:
72
if (!strcmp(argv[1], "--unset"))
73
return git_config_set(argv[2], NULL);
15
- else
74
+ else if (!strcmp(argv[1], "--unset-all"))
75
+ return git_config_set_multivar(argv[2], NULL, NULL, 1);
76
+ else if (!strcmp(argv[1], "--get"))
77
+ return get_value(argv[2], NULL);
78
+ else if (!strcmp(argv[1], "--get-all")) {
79
+ do_all = 1;
80
+ return get_value(argv[2], NULL);
81
+ } else
82
+
83
return git_config_set(argv[1], argv[2]);
84
case 4:
85
if (!strcmp(argv[1], "--unset"))
19
- return git_config_set_multivar(argv[2], NULL, argv[3]);
86
+ return git_config_set_multivar(argv[2], NULL, argv[3], 0);
87
+ else if (!strcmp(argv[1], "--unset-all"))
88
+ return git_config_set_multivar(argv[2], NULL, argv[3], 1);
89
+ else if (!strcmp(argv[1], "--get"))
90
+ return get_value(argv[2], argv[3]);
91
+ else if (!strcmp(argv[1], "--get-all")) {
92
+ do_all = 1;
93
+ return get_value(argv[2], argv[3]);
94
+ } else if (!strcmp(argv[1], "--replace-all"))
95
+
96
+ return git_config_set_multivar(argv[2], argv[3], NULL, 1);
97
else
21
- return git_config_set_multivar(argv[1], argv[2], argv[3]);
98
+
99
+ return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
100
+ case 5:
101
+ if (!strcmp(argv[1], "--replace-all"))
102
+ return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
103
+ case 1:
104
default:
105
usage(git_config_set_usage);
106
}
config.c
+71
-45
@@ -263,11 +263,15 @@ int git_config(config_fn_t fn)
263
/*
264
* Find all the stuff for git_config_set() below.
265
*/
266
+
267
+#define MAX_MATCHES 512
268
+
269
static struct {
270
int baselen;
271
char* key;
272
regex_t* value_regex;
270
- off_t offset;
273
+ int multi_replace;
274
+ off_t offset[MAX_MATCHES];
275
enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
276
int seen;
277
} store;
@@ -279,12 +283,16 @@ static int store_aux(const char* key, const char* value)
283
if (!strcmp(key, store.key) &&
284
(store.value_regex == NULL ||
285
!regexec(store.value_regex, value, 0, NULL, 0))) {
282
- if (store.seen == 1) {
286
+ if (store.seen == 1 && store.multi_replace == 0) {
287
fprintf(stderr,
288
"Warning: %s has multiple values\n",
289
key);
290
+ } else if (store.seen >= MAX_MATCHES) {
291
+ fprintf(stderr, "Too many matches\n");
292
+ return 1;
293
}
287
- store.offset = ftell(config_file);
294
+
295
+ store.offset[store.seen] = ftell(config_file);
296
store.seen++;
297
}
298
break;
@@ -293,14 +301,15 @@ static int store_aux(const char* key, const char* value)
301
store.state = SECTION_END_SEEN;
302
break;
303
} else
296
- store.offset = ftell(config_file);
304
+ /* do not increment matches: this is no match */
305
+ store.offset[store.seen] = ftell(config_file);
306
/* fallthru */
307
case SECTION_END_SEEN:
308
case START:
309
if (!strcmp(key, store.key) &&
310
(store.value_regex == NULL ||
311
!regexec(store.value_regex, value, 0, NULL, 0))) {
303
- store.offset = ftell(config_file);
312
+ store.offset[store.seen] = ftell(config_file);
313
store.state = KEY_SEEN;
314
store.seen++;
315
} else if(!strncmp(key, store.key, store.baselen))
@@ -334,14 +343,38 @@ static void store_write_pair(int fd, const char* key, const char* value)
343
write(fd, "\n", 1);
344
}
345
346
+static int find_beginning_of_line(const char* contents, int size,
347
+ int offset_, int* found_bracket)
348
+{
349
+ int equal_offset = size, bracket_offset = size;
350
+ int offset;
351
+
352
+ for (offset = offset_-2; offset > 0
353
+ && contents[offset] != '\n'; offset--)
354
+ switch (contents[offset]) {
355
+ case '=': equal_offset = offset; break;
356
+ case ']': bracket_offset = offset; break;
357
+ }
358
+ if (bracket_offset < equal_offset) {
359
+ *found_bracket = 1;
360
+ offset = bracket_offset+1;
361
+ } else
362
+ offset++;
363
+
364
+ return offset;
365
+}
366
+
367
int git_config_set(const char* key, const char* value)
368
{
339
- return git_config_set_multivar(key, value, NULL);
369
+ return git_config_set_multivar(key, value, NULL, 0);
370
}
371
372
/*
373
* If value==NULL, unset in (remove from) config,
374
* if value_regex!=NULL, disregard key/value pairs where value does not match.
375
+ * if multi_replace==0, nothing, or only one matching key/value is replaced,
376
+ * else all matching key/values (regardless how many) are removed,
377
+ * before the new pair is written.
378
*
379
* Returns 0 on success.
380
*
@@ -360,7 +393,7 @@ int git_config_set(const char* key, const char* value)
393
*
394
*/
395
int git_config_set_multivar(const char* key, const char* value,
363
- const char* value_regex)
396
+ const char* value_regex, int multi_replace)
397
{
398
int i;
399
struct stat st;
@@ -368,6 +401,8 @@ int git_config_set_multivar(const char* key, const char* value,
401
char* config_file = strdup(git_path("config"));
402
char* lock_file = strdup(git_path("config.lock"));
403
404
+ store.multi_replace = multi_replace;
405
+
406
/*
407
* Since "key" actually contains the section name and the real
408
* key name separated by a dot, we have to know where the dot is.
@@ -431,7 +466,7 @@ int git_config_set_multivar(const char* key, const char* value,
466
} else{
467
int in_fd;
468
char* contents;
434
- int offset, new_line = 0;
469
+ int i, copy_begin, copy_end, new_line = 0;
470
471
if (value_regex == NULL)
472
store.value_regex = NULL;
@@ -446,7 +481,7 @@ int git_config_set_multivar(const char* key, const char* value,
481
}
482
}
483
449
- store.offset = 0;
484
+ store.offset[0] = 0;
485
store.state = START;
486
store.seen = 0;
487
@@ -472,52 +507,42 @@ int git_config_set_multivar(const char* key, const char* value,
507
free(store.value_regex);
508
}
509
475
- /* if nothing to unset, error out */
476
- if (store.seen == 0 && value == NULL) {
510
+ /* if nothing to unset, or too many matches, error out */
511
+ if ((store.seen == 0 && value == NULL) ||
512
+ (store.seen > 1 && multi_replace == 0)) {
513
close(fd);
514
unlink(lock_file);
515
return 5;
516
}
517
482
- store.key = (char*)key;
483
-
518
in_fd = open(config_file, O_RDONLY, 0666);
519
contents = mmap(NULL, st.st_size, PROT_READ,
520
MAP_PRIVATE, in_fd, 0);
521
close(in_fd);
522
489
- if (store.offset == 0) {
490
- store.offset = offset = st.st_size;
491
- } else if (store.state != KEY_SEEN) {
492
- offset = store.offset;
493
- } else {
494
- int equal_offset = st.st_size,
495
- bracket_offset = st.st_size;
496
-
497
- if (value == NULL && store.seen > 1) {
498
- fprintf(stderr, "Cannot remove multivar (%s has %d values\n", key, store.seen);
499
- close(fd);
500
- unlink(lock_file);
501
- return 7;
502
- }
503
- for (offset = store.offset-2; offset > 0
504
- && contents[offset] != '\n'; offset--)
505
- switch (contents[offset]) {
506
- case '=': equal_offset = offset; break;
507
- case ']': bracket_offset = offset; break;
508
- }
509
- if (bracket_offset < equal_offset) {
510
- new_line = 1;
511
- offset = bracket_offset+1;
523
+ if (store.seen == 0)
524
+ store.seen = 1;
525
+
526
+ for (i = 0, copy_begin = 0; i < store.seen; i++) {
527
+ if (store.offset[i] == 0) {
528
+ store.offset[i] = copy_end = st.st_size;
529
+ } else if (store.state != KEY_SEEN) {
530
+ copy_end = store.offset[i];
531
} else
513
- offset++;
532
+ copy_end = find_beginning_of_line(
533
+ contents, st.st_size,
534
+ store.offset[i]-2, &new_line);
535
+
536
+ /* write the first part of the config */
537
+ if (copy_end > copy_begin) {
538
+ write(fd, contents + copy_begin,
539
+ copy_end - copy_begin);
540
+ if (new_line)
541
+ write(fd, "\n", 1);
542
+ }
543
+ copy_begin = store.offset[i];
544
}
545
516
- /* write the first part of the config */
517
- write(fd, contents, offset);
518
- if (new_line)
519
- write(fd, "\n", 1);
520
-
546
/* write the pair (value == NULL means unset) */
547
if (value != NULL) {
548
if (store.state == START)
@@ -526,9 +551,9 @@ int git_config_set_multivar(const char* key, const char* value,
551
}
552
553
/* write the rest of the config */
529
- if (store.offset < st.st_size)
530
- write(fd, contents + store.offset,
531
- st.st_size - store.offset);
554
+ if (copy_begin < st.st_size)
555
+ write(fd, contents + copy_begin,
556
+ st.st_size - copy_begin);
557
558
munmap(contents, st.st_size);
559
unlink(config_file);
@@ -544,3 +569,4 @@ int git_config_set_multivar(const char* key, const char* value,
569
return 0;
570
}
571
572
+
t/t1300-config-set.sh
renamed
+45
-1
@@ -76,9 +76,44 @@ noIndent= sillyValue ; 'nother silly comment
76
# empty line
77
; comment
78
haha ="beta" # last silly comment
79
+haha = hello
80
+ haha = bello
81
[nextSection] noNewline = ouch
82
EOF
83
84
+cp .git/config .git/config2
85
+
86
+test_expect_success 'multiple unset' \
87
+ 'git-config-set --unset-all beta.haha'
88
+
89
+cat > expect << EOF
90
+[beta] ; silly comment # another comment
91
+noIndent= sillyValue ; 'nother silly comment
92
+
93
+# empty line
94
+ ; comment
95
+[nextSection] noNewline = ouch
96
+EOF
97
+
98
+test_expect_success 'multiple unset is correct' 'cmp .git/config expect'
99
+
100
+mv .git/config2 .git/config
101
+
102
+test_expect_success '--replace-all' \
103
+ 'git-config-set --replace-all beta.haha gamma'
104
+
105
+cat > expect << EOF
106
+[beta] ; silly comment # another comment
107
+noIndent= sillyValue ; 'nother silly comment
108
+
109
+# empty line
110
+ ; comment
111
+ haha = gamma
112
+[nextSection] noNewline = ouch
113
+EOF
114
+
115
+test_expect_success 'all replaced' 'cmp .git/config expect'
116
+
117
git-config-set beta.haha alpha
118
119
cat > expect << EOF
@@ -108,7 +143,8 @@ EOF
143
144
test_expect_success 'really really mean test' 'cmp .git/config expect'
145
111
-git-config-set beta.haha
146
+test_expect_success 'get value' 'test alpha = $(git-config-set beta.haha)'
147
+git-config-set --unset beta.haha
148
149
cat > expect << EOF
150
[beta] ; silly comment # another comment
@@ -137,6 +173,12 @@ EOF
173
174
test_expect_success 'multivar' 'cmp .git/config expect'
175
176
+test_expect_failure 'ambiguous get' \
177
+ 'git-config-set --get nextsection.nonewline'
178
+
179
+test_expect_success 'get multivar' \
180
+ 'git-config-set --get-all nextsection.nonewline'
181
+
182
git-config-set nextsection.nonewline "wow3" "wow$"
183
184
cat > expect << EOF
@@ -152,6 +194,8 @@ EOF
194
195
test_expect_success 'multivar replace' 'cmp .git/config expect'
196
197
+test_expect_failure 'ambiguous value' 'git-config-set nextsection.nonewline'
198
+
199
test_expect_failure 'ambiguous unset' \
200
'git-config-set --unset nextsection.nonewline'
201