repo-config: fix segfault with no argument.
[git/dscho.git] / repo-config.c
blobe35063034fc94ac14722a0bb8c544d3c5134a9ec
1 #include "cache.h"
2 #include <regex.h>
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";
7 static char* key = NULL;
8 static char* value = NULL;
9 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;
15 static int show_all_config(const char *key_, const char *value_)
17 if (value_)
18 printf("%s=%s\n", key_, value_);
19 else
20 printf("%s\n", key_);
21 return 0;
24 static int show_config(const char* key_, const char* value_)
26 if (value_ == NULL)
27 value_ = "";
29 if (!strcmp(key_, key) &&
30 (regexp == NULL ||
31 (do_not_match ^
32 !regexec(regexp, value_, 0, NULL, 0)))) {
33 if (do_all) {
34 printf("%s\n", value_);
35 return 0;
37 if (seen > 0) {
38 fprintf(stderr, "More than one value: %s\n", value);
39 free(value);
42 if (type == T_INT) {
43 value = malloc(256);
44 sprintf(value, "%d", git_config_int(key_, value_));
45 } else if (type == T_BOOL) {
46 value = malloc(256);
47 sprintf(value, "%s", git_config_bool(key_, value_)
48 ? "true" : "false");
49 } else {
50 value = strdup(value_);
52 seen++;
54 return 0;
57 static int get_value(const char* key_, const char* regex_)
59 int i;
61 key = malloc(strlen(key_)+1);
62 for (i = 0; key_[i]; i++)
63 key[i] = tolower(key_[i]);
64 key[i] = 0;
66 if (regex_) {
67 if (regex_[0] == '!') {
68 do_not_match = 1;
69 regex_++;
72 regexp = (regex_t*)malloc(sizeof(regex_t));
73 if (regcomp(regexp, regex_, REG_EXTENDED)) {
74 fprintf(stderr, "Invalid pattern: %s\n", regex_);
75 return -1;
79 git_config(show_config);
80 if (value) {
81 printf("%s\n", value);
82 free(value);
84 free(key);
85 if (regexp) {
86 regfree(regexp);
87 free(regexp);
90 if (do_all)
91 return 0;
93 return seen == 1 ? 0 : 1;
96 int main(int argc, const char **argv)
98 setup_git_directory();
100 while (1 < argc) {
101 if (!strcmp(argv[1], "--int"))
102 type = T_INT;
103 else if (!strcmp(argv[1], "--bool"))
104 type = T_BOOL;
105 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
106 return git_config(show_all_config);
107 else
108 break;
109 argc--;
110 argv++;
113 switch (argc) {
114 case 2:
115 return get_value(argv[1], NULL);
116 case 3:
117 if (!strcmp(argv[1], "--unset"))
118 return git_config_set(argv[2], NULL);
119 else if (!strcmp(argv[1], "--unset-all"))
120 return git_config_set_multivar(argv[2], NULL, NULL, 1);
121 else if (!strcmp(argv[1], "--get"))
122 return get_value(argv[2], NULL);
123 else if (!strcmp(argv[1], "--get-all")) {
124 do_all = 1;
125 return get_value(argv[2], NULL);
126 } else
128 return git_config_set(argv[1], argv[2]);
129 case 4:
130 if (!strcmp(argv[1], "--unset"))
131 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
132 else if (!strcmp(argv[1], "--unset-all"))
133 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
134 else if (!strcmp(argv[1], "--get"))
135 return get_value(argv[2], argv[3]);
136 else if (!strcmp(argv[1], "--get-all")) {
137 do_all = 1;
138 return get_value(argv[2], argv[3]);
139 } else if (!strcmp(argv[1], "--replace-all"))
141 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
142 else
144 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
145 case 5:
146 if (!strcmp(argv[1], "--replace-all"))
147 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
148 case 1:
149 default:
150 usage(git_config_set_usage);
152 return 0;