repo-config: readability fixups.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
May 2, 2006 at 21:06 UTC
e098c6f82a28efe1488c7eb1bed4e92f76dd2afd
1 file changed
+20
-26
repo-config.c
+20
-26
@@ -2,10 +2,9 @@
2
#include <regex.h>
3
4
static const char git_config_set_usage[] =
5
-"git-repo-config [ --bool | --int ] [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
5
+"git-repo-config [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
6
7
static char* key = NULL;
8
-static char* value = NULL;
8
static regex_t* key_regexp = NULL;
9
static regex_t* regexp = NULL;
10
static int show_keys = 0;
@@ -26,6 +25,9 @@ static int show_all_config(const char *key_, const char *value_)
25
26
static int show_config(const char* key_, const char* value_)
27
{
28
+ char value[256];
29
+ const char *vptr = value;
30
+
31
if (value_ == NULL)
32
value_ = "";
33
@@ -35,28 +37,25 @@ static int show_config(const char* key_, const char* value_)
37
(regexp == NULL ||
38
(do_not_match ^
39
!regexec(regexp, value_, 0, NULL, 0)))) {
40
+ int dup_error = 0;
41
if (show_keys)
42
printf("%s ", key_);
40
- if (seen > 0) {
41
- if (!do_all)
42
- fprintf(stderr, "More than one value: %s\n",
43
- value);
44
- free(value);
45
- }
46
-
47
- if (type == T_INT) {
48
- value = malloc(256);
43
+ if (seen && !do_all)
44
+ dup_error = 1;
45
+ if (type == T_INT)
46
sprintf(value, "%d", git_config_int(key_, value_));
50
- } else if (type == T_BOOL) {
51
- value = malloc(256);
47
+ else if (type == T_BOOL)
48
sprintf(value, "%s", git_config_bool(key_, value_)
49
? "true" : "false");
54
- } else {
55
- value = strdup(value_);
56
- }
50
+ else
51
+ vptr = value_;
52
seen++;
58
- if (do_all)
59
- printf("%s\n", value);
53
+ if (dup_error) {
54
+ error("More than one value for the key %s: %s",
55
+ key_, vptr);
56
+ }
57
+ else
58
+ printf("%s\n", vptr);
59
}
60
return 0;
61
}
@@ -73,7 +72,7 @@ static int get_value(const char* key_, const char* regex_)
72
if (use_key_regexp) {
73
key_regexp = (regex_t*)malloc(sizeof(regex_t));
74
if (regcomp(key_regexp, key, REG_EXTENDED)) {
76
- fprintf(stderr, "Invalid key pattern: %s\n", regex_);
75
+ fprintf(stderr, "Invalid key pattern: %s\n", key_);
76
return -1;
77
}
78
}
@@ -92,11 +91,6 @@ static int get_value(const char* key_, const char* regex_)
91
}
92
93
git_config(show_config);
95
- if (value) {
96
- if (!do_all)
97
- printf("%s\n", value);
98
- free(value);
99
- }
94
free(key);
95
if (regexp) {
96
regfree(regexp);
@@ -104,9 +98,9 @@ static int get_value(const char* key_, const char* regex_)
98
}
99
100
if (do_all)
107
- return 0;
101
+ return !seen;
102
109
- return seen == 1 ? 0 : 1;
103
+ return (seen == 1) ? 0 : 1;
104
}
105
106
int main(int argc, const char **argv)