config: add support for --bool and --int while setting values
Signed-off-by: Frank Lichtenheld <frank@lichtenheld.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Frank Lichtenheld committed
Jun 25, 2007 at 16:00 UTC
db1696b8ab1d1bde67e78c773c58d837fa8b5e70
4 files changed
+95
-21
Documentation/git-config.txt
+4
-5
@@ -9,9 +9,9 @@ git-config - Get and set repository or global options
9
SYNOPSIS
10
--------
11
[verse]
12
-'git-config' [--system | --global] [-z|--null] name [value [value_regex]]
13
-'git-config' [--system | --global] --add name value
14
-'git-config' [--system | --global] --replace-all name [value [value_regex]]
12
+'git-config' [--system | --global] [type] [-z|--null] name [value [value_regex]]
13
+'git-config' [--system | --global] [type] --add name value
14
+'git-config' [--system | --global] [type] --replace-all name [value [value_regex]]
15
'git-config' [--system | --global] [type] [-z|--null] --get name [value_regex]
16
'git-config' [--system | --global] [type] [-z|--null] --get-all name [value_regex]
17
'git-config' [--system | --global] [type] [-z|--null] --get-regexp name_regex [value_regex]
@@ -37,8 +37,7 @@ prepend a single exclamation mark in front (see also <<EXAMPLES>>).
37
The type specifier can be either '--int' or '--bool', which will make
38
'git-config' ensure that the variable(s) are of the given type and
39
convert the value to the canonical form (simple decimal number for int,
40
-a "true" or "false" string for bool). Type specifiers currently only
41
-take effect for reading operations. If no type specifier is passed,
40
+a "true" or "false" string for bool). If no type specifier is passed,
41
no checks or transformations are performed on the value.
42
43
This command will fail if:
builtin-config.c
+42
-13
@@ -138,9 +138,33 @@ free_strings:
138
return ret;
139
}
140
141
+char *normalize_value(const char *key, const char *value)
142
+{
143
+ char *normalized;
144
+
145
+ if (!value)
146
+ return NULL;
147
+
148
+ if (type == T_RAW)
149
+ normalized = xstrdup(value);
150
+ else {
151
+ normalized = xmalloc(64);
152
+ if (type == T_INT) {
153
+ int v = git_config_int(key, value);
154
+ sprintf(normalized, "%d", v);
155
+ }
156
+ else if (type == T_BOOL)
157
+ sprintf(normalized, "%s",
158
+ git_config_bool(key, value) ? "true" : "false");
159
+ }
160
+
161
+ return normalized;
162
+}
163
+
164
int cmd_config(int argc, const char **argv, const char *prefix)
165
{
166
int nongit = 0;
167
+ char* value;
168
setup_git_directory_gently(&nongit);
169
170
while (1 < argc) {
@@ -217,9 +241,10 @@ int cmd_config(int argc, const char **argv, const char *prefix)
241
use_key_regexp = 1;
242
do_all = 1;
243
return get_value(argv[2], NULL);
220
- } else
221
-
222
- return git_config_set(argv[1], argv[2]);
244
+ } else {
245
+ value = normalize_value(argv[1], argv[2]);
246
+ return git_config_set(argv[1], value);
247
+ }
248
case 4:
249
if (!strcmp(argv[1], "--unset"))
250
return git_config_set_multivar(argv[2], NULL, argv[3], 0);
@@ -235,17 +260,21 @@ int cmd_config(int argc, const char **argv, const char *prefix)
260
use_key_regexp = 1;
261
do_all = 1;
262
return get_value(argv[2], argv[3]);
238
- } else if (!strcmp(argv[1], "--add"))
239
- return git_config_set_multivar(argv[2], argv[3], "^$", 0);
240
- else if (!strcmp(argv[1], "--replace-all"))
241
-
242
- return git_config_set_multivar(argv[2], argv[3], NULL, 1);
243
- else
244
-
245
- return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
263
+ } else if (!strcmp(argv[1], "--add")) {
264
+ value = normalize_value(argv[2], argv[3]);
265
+ return git_config_set_multivar(argv[2], value, "^$", 0);
266
+ } else if (!strcmp(argv[1], "--replace-all")) {
267
+ value = normalize_value(argv[2], argv[3]);
268
+ return git_config_set_multivar(argv[2], value, NULL, 1);
269
+ } else {
270
+ value = normalize_value(argv[1], argv[2]);
271
+ return git_config_set_multivar(argv[1], value, argv[3], 0);
272
+ }
273
case 5:
247
- if (!strcmp(argv[1], "--replace-all"))
248
- return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
274
+ if (!strcmp(argv[1], "--replace-all")) {
275
+ value = normalize_value(argv[2], argv[3]);
276
+ return git_config_set_multivar(argv[2], value, argv[4], 1);
277
+ }
278
case 1:
279
default:
280
usage(git_config_set_usage);
t/t1300-repo-config.sh
+47
-1
@@ -471,11 +471,57 @@ test_expect_success bool '
471
done &&
472
cmp expect result'
473
474
-test_expect_failure 'invalid bool' '
474
+test_expect_failure 'invalid bool (--get)' '
475
476
git-config bool.nobool foobar &&
477
git-config --bool --get bool.nobool'
478
479
+test_expect_failure 'invalid bool (set)' '
480
+
481
+ git-config --bool bool.nobool foobar'
482
+
483
+rm .git/config
484
+
485
+cat > expect <<\EOF
486
+[bool]
487
+ true1 = true
488
+ true2 = true
489
+ true3 = true
490
+ true4 = true
491
+ false1 = false
492
+ false2 = false
493
+ false3 = false
494
+ false4 = false
495
+EOF
496
+
497
+test_expect_success 'set --bool' '
498
+
499
+ git-config --bool bool.true1 01 &&
500
+ git-config --bool bool.true2 -1 &&
501
+ git-config --bool bool.true3 YeS &&
502
+ git-config --bool bool.true4 true &&
503
+ git-config --bool bool.false1 000 &&
504
+ git-config --bool bool.false2 "" &&
505
+ git-config --bool bool.false3 nO &&
506
+ git-config --bool bool.false4 FALSE &&
507
+ cmp expect .git/config'
508
+
509
+rm .git/config
510
+
511
+cat > expect <<\EOF
512
+[int]
513
+ val1 = 1
514
+ val2 = -1
515
+ val3 = 5242880
516
+EOF
517
+
518
+test_expect_success 'set --int' '
519
+
520
+ git-config --int int.val1 01 &&
521
+ git-config --int int.val2 -1 &&
522
+ git-config --int int.val3 5m &&
523
+ cmp expect .git/config'
524
+
525
rm .git/config
526
527
git-config quote.leading " test"
t/t9400-git-cvsserver-server.sh
+2
-2
@@ -38,7 +38,7 @@ echo >empty &&
38
git commit -q -m "First Commit" &&
39
git clone -q --local --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 &&
40
GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
41
- GIT_DIR="$SERVERDIR" git config --bool gitcvs.logfile "$SERVERDIR/gitcvs.log" ||
41
+ GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log" ||
42
exit 1
43
44
# note that cvs doesn't accept absolute pathnames
@@ -255,7 +255,7 @@ rm -fr "$SERVERDIR"
255
cd "$WORKDIR" &&
256
git clone -q --local --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 &&
257
GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
258
-GIT_DIR="$SERVERDIR" git config --bool gitcvs.logfile "$SERVERDIR/gitcvs.log" ||
258
+GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log" ||
259
exit 1
260
261
test_expect_success 'cvs update (create new file)' \