Fix git_config_bool_or_int
The earlier one botched the return value logic between config_bool and config_bool_and_int. The former should normalize between 0 and 1 while the latter should give back full range of integer values. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Apr 13, 2008 at 12:11 UTC
c35b0b5884a12685aa794e00f51bc7dfaa0dd91c
3 files changed
+80
-4
builtin-config.c
+20
-2
@@ -3,7 +3,7 @@
3
#include "color.h"
4
5
static const char git_config_set_usage[] =
6
-"git-config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int ] [ -z | --null ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list | --get-color var [default] | --get-colorbool name [stdout-is-tty]";
6
+"git-config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int | --bool-or-int ] [ -z | --null ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list | --get-color var [default] | --get-colorbool name [stdout-is-tty]";
7
8
static char *key;
9
static regex_t *key_regexp;
@@ -16,7 +16,7 @@ static int seen;
16
static char delim = '=';
17
static char key_delim = ' ';
18
static char term = '\n';
19
-static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
19
+static enum { T_RAW, T_INT, T_BOOL, T_BOOL_OR_INT } type = T_RAW;
20
21
static int show_all_config(const char *key_, const char *value_)
22
{
@@ -53,6 +53,14 @@ static int show_config(const char* key_, const char* value_)
53
sprintf(value, "%d", git_config_int(key_, value_?value_:""));
54
else if (type == T_BOOL)
55
vptr = git_config_bool(key_, value_) ? "true" : "false";
56
+ else if (type == T_BOOL_OR_INT) {
57
+ int is_bool, v;
58
+ v = git_config_bool_or_int(key_, value_, &is_bool);
59
+ if (is_bool)
60
+ vptr = v ? "true" : "false";
61
+ else
62
+ sprintf(value, "%d", v);
63
+ }
64
else
65
vptr = value_?value_:"";
66
seen++;
@@ -157,6 +165,14 @@ char *normalize_value(const char *key, const char *value)
165
else if (type == T_BOOL)
166
sprintf(normalized, "%s",
167
git_config_bool(key, value) ? "true" : "false");
168
+ else if (type == T_BOOL_OR_INT) {
169
+ int is_bool, v;
170
+ v = git_config_bool_or_int(key, value, &is_bool);
171
+ if (!is_bool)
172
+ sprintf(normalized, "%d", v);
173
+ else
174
+ sprintf(normalized, "%s", v ? "true" : "false");
175
+ }
176
}
177
178
return normalized;
@@ -273,6 +289,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
289
type = T_INT;
290
else if (!strcmp(argv[1], "--bool"))
291
type = T_BOOL;
292
+ else if (!strcmp(argv[1], "--bool-or-int"))
293
+ type = T_BOOL_OR_INT;
294
else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) {
295
if (argc != 2)
296
usage(git_config_set_usage);
config.c
+2
-2
@@ -315,13 +315,13 @@ int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
315
if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
316
return 0;
317
*is_bool = 0;
318
- return git_config_int(name, value) != 0;
318
+ return git_config_int(name, value);
319
}
320
321
int git_config_bool(const char *name, const char *value)
322
{
323
int discard;
324
- return git_config_bool_or_int(name, value, &discard);
324
+ return !!git_config_bool_or_int(name, value, &discard);
325
}
326
327
int git_config_string(const char **dest, const char *var, const char *value)
t/t1300-repo-config.sh
+58
@@ -595,6 +595,64 @@ test_expect_success 'set --int' '
595
596
rm .git/config
597
598
+cat >expect <<\EOF
599
+[bool]
600
+ true1 = true
601
+ true2 = true
602
+ false1 = false
603
+ false2 = false
604
+[int]
605
+ int1 = 0
606
+ int2 = 1
607
+ int3 = -1
608
+EOF
609
+
610
+test_expect_success 'get --bool-or-int' '
611
+ (
612
+ echo "[bool]"
613
+ echo true1
614
+ echo true2 = true
615
+ echo false = false
616
+ echo "[int]"
617
+ echo int1 = 0
618
+ echo int2 = 1
619
+ echo int3 = -1
620
+ ) >>.git/config &&
621
+ test $(git config --bool-or-int bool.true1) = true &&
622
+ test $(git config --bool-or-int bool.true2) = true &&
623
+ test $(git config --bool-or-int bool.false) = false &&
624
+ test $(git config --bool-or-int int.int1) = 0 &&
625
+ test $(git config --bool-or-int int.int2) = 1 &&
626
+ test $(git config --bool-or-int int.int3) = -1
627
+
628
+'
629
+
630
+rm .git/config
631
+cat >expect <<\EOF
632
+[bool]
633
+ true1 = true
634
+ false1 = false
635
+ true2 = true
636
+ false2 = false
637
+[int]
638
+ int1 = 0
639
+ int2 = 1
640
+ int3 = -1
641
+EOF
642
+
643
+test_expect_success 'set --bool-or-int' '
644
+ git config --bool-or-int bool.true1 true &&
645
+ git config --bool-or-int bool.false1 false &&
646
+ git config --bool-or-int bool.true2 yes &&
647
+ git config --bool-or-int bool.false2 no &&
648
+ git config --bool-or-int int.int1 0 &&
649
+ git config --bool-or-int int.int2 1 &&
650
+ git config --bool-or-int int.int3 -1 &&
651
+ test_cmp expect .git/config
652
+'
653
+
654
+rm .git/config
655
+
656
git config quote.leading " test"
657
git config quote.ending "test "
658
git config quote.semicolon "test;test"