@matheusbsbs / matheus-bagliano / commits / 7162dff3dd
Fork 0

Add support for explicit type specifiers when calling git-repo-config

Currently, git-repo-config will just return the raw value of option as specified in the config file; this makes things difficult for scripts calling it, especially if the value is supposed to be boolean. This patch makes it possible to ask git-repo-config to check if the option is of the given type (int or bool) and write out the value in its canonical form. If you do not pass --int or --bool, the behaviour stays unchanged and the raw value is emitted. This also incidentally fixes the segfault when option with no value is encountered. [jc: tweaked the option parsing a bit to make it easier to see that the patch does not change anything but the type stuff in the diff output. Also changed to avoid "foo ? : bar" construct. ] Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>

Petr Baudis committed Feb 12, 2006 at 04:14 UTC 7162dff3dde6fb0a2829fbc8e641fc6d1e7e76ec
2 files changed +37 -8
Documentation/git-repo-config.txt
+12 -6
@@ -8,12 +8,12 @@ git-repo-config - Get and set options in .git/config.
8
9 SYNOPSIS
10 --------
11 -'git-repo-config' name [value [value_regex]]
12 -'git-repo-config' --replace-all name [value [value_regex]]
13 -'git-repo-config' --get name [value_regex]
14 -'git-repo-config' --get-all name [value_regex]
15 -'git-repo-config' --unset name [value_regex]
16 -'git-repo-config' --unset-all name [value_regex]
11 +'git-repo-config' [type] name [value [value_regex]]
12 +'git-repo-config' [type] --replace-all name [value [value_regex]]
13 +'git-repo-config' [type] --get name [value_regex]
14 +'git-repo-config' [type] --get-all name [value_regex]
15 +'git-repo-config' [type] --unset name [value_regex]
16 +'git-repo-config' [type] --unset-all name [value_regex]
17
18 DESCRIPTION
19 -----------
@@ -26,6 +26,12 @@ should provide a POSIX regex for the value. If you want to handle the lines
26 *not* matching the regex, just prepend a single exclamation mark in front
27 (see EXAMPLES).
28
29 +The type specifier can be either '--int' or '--bool', which will make
30 +'git-repo-config' ensure that the variable(s) are of the given type and
31 +convert the value to the canonical form (simple decimal number for int,
32 +a "true" or "false" string for bool). If no type specifier is passed,
33 +no checks or transformations are performed on the value.
34 +
35 This command will fail if
36
37 . .git/config is invalid,
repo-config.c
+25 -2
@@ -2,7 +2,7 @@
2 #include <regex.h>
3
4 static const char git_config_set_usage[] =
5 -"git-repo-config [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
5 +"git-repo-config [ --bool | --int ] [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
6
7 static char* key = NULL;
8 static char* value = NULL;
@@ -10,6 +10,7 @@ static regex_t* regexp = NULL;
10 static int do_all = 0;
11 static int do_not_match = 0;
12 static int seen = 0;
13 +static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
14
15 static int show_config(const char* key_, const char* value_)
16 {
@@ -25,7 +26,17 @@ static int show_config(const char* key_, const char* value_)
26 fprintf(stderr, "More than one value: %s\n", value);
27 free(value);
28 }
28 - value = strdup(value_);
29 +
30 + if (type == T_INT) {
31 + value = malloc(256);
32 + sprintf(value, "%d", git_config_int(key_, value_));
33 + } else if (type == T_BOOL) {
34 + value = malloc(256);
35 + sprintf(value, "%s", git_config_bool(key_, value_)
36 + ? "true" : "false");
37 + } else {
38 + value = strdup(value_ ? value_ : "");
39 + }
40 seen++;
41 }
42 return 0;
@@ -73,6 +84,18 @@ static int get_value(const char* key_, const char* regex_)
84 int main(int argc, const char **argv)
85 {
86 setup_git_directory();
87 +
88 + while (1 < argc) {
89 + if (!strcmp(argv[1], "--int"))
90 + type = T_INT;
91 + else if (!strcmp(argv[1], "--bool"))
92 + type = T_BOOL;
93 + else
94 + break;
95 + argc--;
96 + argv++;
97 + }
98 +
99 switch (argc) {
100 case 2:
101 return get_value(argv[1], NULL);