Merge branch 'ew/rebase'
[git/git.git] / repo-config.c
1 #include "cache.h"
2 #include <regex.h>
3
4 static const char git_config_set_usage[] =
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 regex_t* key_regexp = NULL;
9 static regex_t* regexp = NULL;
10 static int show_keys = 0;
11 static int use_key_regexp = 0;
12 static int do_all = 0;
13 static int do_not_match = 0;
14 static int seen = 0;
15 static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
16
17 static int show_all_config(const char *key_, const char *value_)
18 {
19 if (value_)
20 printf("%s=%s\n", key_, value_);
21 else
22 printf("%s\n", key_);
23 return 0;
24 }
25
26 static int show_config(const char* key_, const char* value_)
27 {
28 char value[256];
29 const char *vptr = value;
30 int dup_error = 0;
31
32 if (value_ == NULL)
33 value_ = "";
34
35 if (!use_key_regexp && strcmp(key_, key))
36 return 0;
37 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
38 return 0;
39 if (regexp != NULL &&
40 (do_not_match ^
41 regexec(regexp, value_, 0, NULL, 0)))
42 return 0;
43
44 if (show_keys)
45 printf("%s ", key_);
46 if (seen && !do_all)
47 dup_error = 1;
48 if (type == T_INT)
49 sprintf(value, "%d", git_config_int(key_, value_));
50 else if (type == T_BOOL)
51 vptr = git_config_bool(key_, value_) ? "true" : "false";
52 else
53 vptr = value_;
54 seen++;
55 if (dup_error) {
56 error("More than one value for the key %s: %s",
57 key_, vptr);
58 }
59 else
60 printf("%s\n", vptr);
61
62 return 0;
63 }
64
65 static int get_value(const char* key_, const char* regex_)
66 {
67 int ret = -1;
68 char *tl;
69 char *global = NULL, *repo_config = NULL;
70 const char *local;
71
72 local = getenv("GIT_CONFIG");
73 if (!local) {
74 const char *home = getenv("HOME");
75 local = getenv("GIT_CONFIG_LOCAL");
76 if (!local)
77 local = repo_config = strdup(git_path("config"));
78 if (home)
79 global = strdup(mkpath("%s/.gitconfig", home));
80 }
81
82 key = strdup(key_);
83 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
84 *tl = tolower(*tl);
85 for (tl=key; *tl && *tl != '.'; ++tl)
86 *tl = tolower(*tl);
87
88 if (use_key_regexp) {
89 key_regexp = (regex_t*)malloc(sizeof(regex_t));
90 if (regcomp(key_regexp, key, REG_EXTENDED)) {
91 fprintf(stderr, "Invalid key pattern: %s\n", key_);
92 goto free_strings;
93 }
94 }
95
96 if (regex_) {
97 if (regex_[0] == '!') {
98 do_not_match = 1;
99 regex_++;
100 }
101
102 regexp = (regex_t*)malloc(sizeof(regex_t));
103 if (regcomp(regexp, regex_, REG_EXTENDED)) {
104 fprintf(stderr, "Invalid pattern: %s\n", regex_);
105 goto free_strings;
106 }
107 }
108
109 if (do_all && global)
110 git_config_from_file(show_config, global);
111 git_config_from_file(show_config, local);
112 if (!do_all && !seen && global)
113 git_config_from_file(show_config, global);
114
115 free(key);
116 if (regexp) {
117 regfree(regexp);
118 free(regexp);
119 }
120
121 if (do_all)
122 ret = !seen;
123 else
124 ret = (seen == 1) ? 0 : 1;
125
126 free_strings:
127 if (repo_config)
128 free(repo_config);
129 if (global)
130 free(global);
131 return ret;
132 }
133
134 int main(int argc, const char **argv)
135 {
136 int nongit = 0;
137 setup_git_directory_gently(&nongit);
138
139 while (1 < argc) {
140 if (!strcmp(argv[1], "--int"))
141 type = T_INT;
142 else if (!strcmp(argv[1], "--bool"))
143 type = T_BOOL;
144 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
145 return git_config(show_all_config);
146 else
147 break;
148 argc--;
149 argv++;
150 }
151
152 switch (argc) {
153 case 2:
154 return get_value(argv[1], NULL);
155 case 3:
156 if (!strcmp(argv[1], "--unset"))
157 return git_config_set(argv[2], NULL);
158 else if (!strcmp(argv[1], "--unset-all"))
159 return git_config_set_multivar(argv[2], NULL, NULL, 1);
160 else if (!strcmp(argv[1], "--get"))
161 return get_value(argv[2], NULL);
162 else if (!strcmp(argv[1], "--get-all")) {
163 do_all = 1;
164 return get_value(argv[2], NULL);
165 } else if (!strcmp(argv[1], "--get-regexp")) {
166 show_keys = 1;
167 use_key_regexp = 1;
168 do_all = 1;
169 return get_value(argv[2], NULL);
170 } else
171
172 return git_config_set(argv[1], argv[2]);
173 case 4:
174 if (!strcmp(argv[1], "--unset"))
175 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
176 else if (!strcmp(argv[1], "--unset-all"))
177 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
178 else if (!strcmp(argv[1], "--get"))
179 return get_value(argv[2], argv[3]);
180 else if (!strcmp(argv[1], "--get-all")) {
181 do_all = 1;
182 return get_value(argv[2], argv[3]);
183 } else if (!strcmp(argv[1], "--get-regexp")) {
184 show_keys = 1;
185 use_key_regexp = 1;
186 do_all = 1;
187 return get_value(argv[2], argv[3]);
188 } else if (!strcmp(argv[1], "--replace-all"))
189
190 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
191 else
192
193 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
194 case 5:
195 if (!strcmp(argv[1], "--replace-all"))
196 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
197 case 1:
198 default:
199 usage(git_config_set_usage);
200 }
201 return 0;
202 }