]>
Commit | Line | Data |
---|---|---|
e12c095a | 1 | #include "builtin.h" |
1b1e59c5 | 2 | #include "cache.h" |
9ce03522 | 3 | #include "color.h" |
d64ec16c | 4 | #include "parse-options.h" |
d4770964 | 5 | #include "urlmatch.h" |
1b1e59c5 | 6 | |
d64ec16c | 7 | static const char *const builtin_config_usage[] = { |
1bd31ce6 | 8 | N_("git config [options]"), |
d64ec16c FC |
9 | NULL |
10 | }; | |
4ddba79d | 11 | |
96f1e58f DR |
12 | static char *key; |
13 | static regex_t *key_regexp; | |
14 | static regex_t *regexp; | |
15 | static int show_keys; | |
16 | static int use_key_regexp; | |
17 | static int do_all; | |
18 | static int do_not_match; | |
2275d502 FL |
19 | static char delim = '='; |
20 | static char key_delim = ' '; | |
21 | static char term = '\n'; | |
4ddba79d | 22 | |
57210a67 | 23 | static int use_global_config, use_system_config, use_local_config; |
c8985ce0 | 24 | static struct git_config_source given_config_source; |
16c1e939 | 25 | static int actions, types; |
d64ec16c FC |
26 | static const char *get_color_slot, *get_colorbool_slot; |
27 | static int end_null; | |
9b25a0b5 | 28 | static int respect_includes = -1; |
d64ec16c FC |
29 | |
30 | #define ACTION_GET (1<<0) | |
31 | #define ACTION_GET_ALL (1<<1) | |
32 | #define ACTION_GET_REGEXP (1<<2) | |
33 | #define ACTION_REPLACE_ALL (1<<3) | |
34 | #define ACTION_ADD (1<<4) | |
35 | #define ACTION_UNSET (1<<5) | |
36 | #define ACTION_UNSET_ALL (1<<6) | |
37 | #define ACTION_RENAME_SECTION (1<<7) | |
38 | #define ACTION_REMOVE_SECTION (1<<8) | |
39 | #define ACTION_LIST (1<<9) | |
40 | #define ACTION_EDIT (1<<10) | |
41 | #define ACTION_SET (1<<11) | |
42 | #define ACTION_SET_ALL (1<<12) | |
43 | #define ACTION_GET_COLOR (1<<13) | |
44 | #define ACTION_GET_COLORBOOL (1<<14) | |
d4770964 | 45 | #define ACTION_GET_URLMATCH (1<<15) |
d64ec16c | 46 | |
16c1e939 FC |
47 | #define TYPE_BOOL (1<<0) |
48 | #define TYPE_INT (1<<1) | |
49 | #define TYPE_BOOL_OR_INT (1<<2) | |
1349484e | 50 | #define TYPE_PATH (1<<3) |
16c1e939 | 51 | |
d64ec16c | 52 | static struct option builtin_config_options[] = { |
1bd31ce6 | 53 | OPT_GROUP(N_("Config file location")), |
21e047dc SB |
54 | OPT_BOOL(0, "global", &use_global_config, N_("use global config file")), |
55 | OPT_BOOL(0, "system", &use_system_config, N_("use system config file")), | |
56 | OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")), | |
c8985ce0 KS |
57 | OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")), |
58 | OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")), | |
1bd31ce6 NTND |
59 | OPT_GROUP(N_("Action")), |
60 | OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET), | |
61 | OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL), | |
62 | OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP), | |
d4770964 | 63 | OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH), |
1bd31ce6 | 64 | OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL), |
f63cf8c9 NTND |
65 | OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD), |
66 | OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET), | |
67 | OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL), | |
1bd31ce6 NTND |
68 | OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), |
69 | OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), | |
70 | OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST), | |
f63cf8c9 | 71 | OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), |
1bd31ce6 NTND |
72 | OPT_STRING(0, "get-color", &get_color_slot, N_("slot"), N_("find the color configured: [default]")), |
73 | OPT_STRING(0, "get-colorbool", &get_colorbool_slot, N_("slot"), N_("find the color setting: [stdout-is-tty]")), | |
74 | OPT_GROUP(N_("Type")), | |
75 | OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL), | |
76 | OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT), | |
77 | OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), | |
78 | OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH), | |
79 | OPT_GROUP(N_("Other")), | |
d5d09d47 | 80 | OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")), |
1bd31ce6 | 81 | OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")), |
d64ec16c FC |
82 | OPT_END(), |
83 | }; | |
84 | ||
85 | static void check_argc(int argc, int min, int max) { | |
86 | if (argc >= min && argc <= max) | |
87 | return; | |
88 | error("wrong number of arguments"); | |
89 | usage_with_options(builtin_config_usage, builtin_config_options); | |
90 | } | |
91 | ||
ef90d6d4 | 92 | static int show_all_config(const char *key_, const char *value_, void *cb) |
de791f15 PB |
93 | { |
94 | if (value_) | |
2275d502 | 95 | printf("%s%c%s%c", key_, delim, value_, term); |
de791f15 | 96 | else |
2275d502 | 97 | printf("%s%c", key_, term); |
de791f15 PB |
98 | return 0; |
99 | } | |
100 | ||
7acdd6f0 JK |
101 | struct strbuf_list { |
102 | struct strbuf *items; | |
103 | int nr; | |
104 | int alloc; | |
105 | }; | |
106 | ||
d9b9169b | 107 | static int format_config(struct strbuf *buf, const char *key_, const char *value_) |
4ddba79d | 108 | { |
1349484e | 109 | int must_free_vptr = 0; |
008e3cc5 | 110 | int must_print_delim = 0; |
d9b9169b JH |
111 | char value[256]; |
112 | const char *vptr = value; | |
e098c6f8 | 113 | |
7acdd6f0 JK |
114 | strbuf_init(buf, 0); |
115 | ||
b69ba460 | 116 | if (show_keys) { |
7acdd6f0 | 117 | strbuf_addstr(buf, key_); |
008e3cc5 | 118 | must_print_delim = 1; |
b69ba460 | 119 | } |
16c1e939 | 120 | if (types == TYPE_INT) |
00160242 JK |
121 | sprintf(value, "%"PRId64, |
122 | git_config_int64(key_, value_ ? value_ : "")); | |
16c1e939 | 123 | else if (types == TYPE_BOOL) |
8f5ff31f | 124 | vptr = git_config_bool(key_, value_) ? "true" : "false"; |
16c1e939 | 125 | else if (types == TYPE_BOOL_OR_INT) { |
c35b0b58 JH |
126 | int is_bool, v; |
127 | v = git_config_bool_or_int(key_, value_, &is_bool); | |
128 | if (is_bool) | |
129 | vptr = v ? "true" : "false"; | |
130 | else | |
131 | sprintf(value, "%d", v); | |
1349484e | 132 | } else if (types == TYPE_PATH) { |
962c38ee CMN |
133 | if (git_config_pathname(&vptr, key_, value_) < 0) |
134 | return -1; | |
1349484e | 135 | must_free_vptr = 1; |
008e3cc5 MM |
136 | } else if (value_) { |
137 | vptr = value_; | |
138 | } else { | |
139 | /* Just show the key name */ | |
140 | vptr = ""; | |
141 | must_print_delim = 0; | |
c35b0b58 | 142 | } |
00b347d3 JK |
143 | |
144 | if (must_print_delim) | |
145 | strbuf_addch(buf, key_delim); | |
146 | strbuf_addstr(buf, vptr); | |
147 | strbuf_addch(buf, term); | |
148 | ||
1349484e | 149 | if (must_free_vptr) |
1349484e | 150 | free((char *)vptr); |
4ddba79d JS |
151 | return 0; |
152 | } | |
153 | ||
d9b9169b JH |
154 | static int collect_config(const char *key_, const char *value_, void *cb) |
155 | { | |
156 | struct strbuf_list *values = cb; | |
157 | ||
158 | if (!use_key_regexp && strcmp(key_, key)) | |
159 | return 0; | |
160 | if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0)) | |
161 | return 0; | |
162 | if (regexp != NULL && | |
163 | (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0))) | |
164 | return 0; | |
165 | ||
166 | ALLOC_GROW(values->items, values->nr + 1, values->alloc); | |
167 | ||
168 | return format_config(&values->items[values->nr++], key_, value_); | |
169 | } | |
170 | ||
4b951b7e | 171 | static int get_value(const char *key_, const char *regex_) |
4ddba79d | 172 | { |
9409c7a5 | 173 | int ret = CONFIG_GENERIC_ERROR; |
5ba1a8a7 | 174 | struct strbuf_list values = {NULL}; |
7acdd6f0 | 175 | int i; |
4ddba79d | 176 | |
2fa9a0fb | 177 | if (use_key_regexp) { |
b09c53a3 LP |
178 | char *tl; |
179 | ||
180 | /* | |
181 | * NEEDSWORK: this naive pattern lowercasing obviously does not | |
182 | * work for more complex patterns like "^[^.]*Foo.*bar". | |
183 | * Perhaps we should deprecate this altogether someday. | |
184 | */ | |
185 | ||
186 | key = xstrdup(key_); | |
187 | for (tl = key + strlen(key) - 1; | |
188 | tl >= key && *tl != '.'; | |
189 | tl--) | |
190 | *tl = tolower(*tl); | |
191 | for (tl = key; *tl && *tl != '.'; tl++) | |
192 | *tl = tolower(*tl); | |
193 | ||
2d7320d0 | 194 | key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
2fa9a0fb | 195 | if (regcomp(key_regexp, key, REG_EXTENDED)) { |
e098c6f8 | 196 | fprintf(stderr, "Invalid key pattern: %s\n", key_); |
97ed50f9 JK |
197 | free(key_regexp); |
198 | key_regexp = NULL; | |
9409c7a5 | 199 | ret = CONFIG_INVALID_PATTERN; |
5f1a63e0 | 200 | goto free_strings; |
2fa9a0fb | 201 | } |
b09c53a3 | 202 | } else { |
9409c7a5 JH |
203 | if (git_config_parse_key(key_, &key, NULL)) { |
204 | ret = CONFIG_INVALID_KEY; | |
b09c53a3 | 205 | goto free_strings; |
9409c7a5 | 206 | } |
2fa9a0fb JS |
207 | } |
208 | ||
4ddba79d | 209 | if (regex_) { |
f98d863d JS |
210 | if (regex_[0] == '!') { |
211 | do_not_match = 1; | |
212 | regex_++; | |
213 | } | |
214 | ||
2d7320d0 | 215 | regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
0a152171 | 216 | if (regcomp(regexp, regex_, REG_EXTENDED)) { |
4ddba79d | 217 | fprintf(stderr, "Invalid pattern: %s\n", regex_); |
97ed50f9 JK |
218 | free(regexp); |
219 | regexp = NULL; | |
9409c7a5 | 220 | ret = CONFIG_INVALID_PATTERN; |
5f1a63e0 | 221 | goto free_strings; |
4ddba79d JS |
222 | } |
223 | } | |
224 | ||
e8955898 | 225 | git_config_with_options(collect_config, &values, |
c8985ce0 | 226 | &given_config_source, respect_includes); |
5f1a63e0 | 227 | |
00b347d3 | 228 | ret = !values.nr; |
9b25a0b5 | 229 | |
7acdd6f0 JK |
230 | for (i = 0; i < values.nr; i++) { |
231 | struct strbuf *buf = values.items + i; | |
00b347d3 JK |
232 | if (do_all || i == values.nr - 1) |
233 | fwrite(buf->buf, 1, buf->len, stdout); | |
7acdd6f0 JK |
234 | strbuf_release(buf); |
235 | } | |
236 | free(values.items); | |
5f1a63e0 | 237 | |
97ed50f9 | 238 | free_strings: |
4ddba79d | 239 | free(key); |
35998c89 JK |
240 | if (key_regexp) { |
241 | regfree(key_regexp); | |
242 | free(key_regexp); | |
243 | } | |
0a152171 AW |
244 | if (regexp) { |
245 | regfree(regexp); | |
246 | free(regexp); | |
4ddba79d JS |
247 | } |
248 | ||
5f1a63e0 | 249 | return ret; |
4ddba79d | 250 | } |
1b1e59c5 | 251 | |
186458b1 | 252 | static char *normalize_value(const char *key, const char *value) |
db1696b8 FL |
253 | { |
254 | char *normalized; | |
255 | ||
256 | if (!value) | |
257 | return NULL; | |
258 | ||
1349484e MM |
259 | if (types == 0 || types == TYPE_PATH) |
260 | /* | |
261 | * We don't do normalization for TYPE_PATH here: If | |
262 | * the path is like ~/foobar/, we prefer to store | |
263 | * "~/foobar/" in the config file, and to expand the ~ | |
264 | * when retrieving the value. | |
265 | */ | |
db1696b8 FL |
266 | normalized = xstrdup(value); |
267 | else { | |
268 | normalized = xmalloc(64); | |
16c1e939 | 269 | if (types == TYPE_INT) { |
00160242 JK |
270 | int64_t v = git_config_int64(key, value); |
271 | sprintf(normalized, "%"PRId64, v); | |
db1696b8 | 272 | } |
16c1e939 | 273 | else if (types == TYPE_BOOL) |
db1696b8 FL |
274 | sprintf(normalized, "%s", |
275 | git_config_bool(key, value) ? "true" : "false"); | |
16c1e939 | 276 | else if (types == TYPE_BOOL_OR_INT) { |
c35b0b58 JH |
277 | int is_bool, v; |
278 | v = git_config_bool_or_int(key, value, &is_bool); | |
279 | if (!is_bool) | |
280 | sprintf(normalized, "%d", v); | |
281 | else | |
282 | sprintf(normalized, "%s", v ? "true" : "false"); | |
283 | } | |
db1696b8 FL |
284 | } |
285 | ||
286 | return normalized; | |
287 | } | |
288 | ||
9ce03522 JH |
289 | static int get_color_found; |
290 | static const char *get_color_slot; | |
b408457f | 291 | static const char *get_colorbool_slot; |
9ce03522 JH |
292 | static char parsed_color[COLOR_MAXLEN]; |
293 | ||
ef90d6d4 | 294 | static int git_get_color_config(const char *var, const char *value, void *cb) |
9ce03522 JH |
295 | { |
296 | if (!strcmp(var, get_color_slot)) { | |
f769982d JH |
297 | if (!value) |
298 | config_error_nonbool(var); | |
9ce03522 JH |
299 | color_parse(value, var, parsed_color); |
300 | get_color_found = 1; | |
301 | } | |
302 | return 0; | |
303 | } | |
304 | ||
0e854a28 | 305 | static void get_color(const char *def_color) |
9ce03522 | 306 | { |
9ce03522 JH |
307 | get_color_found = 0; |
308 | parsed_color[0] = '\0'; | |
270a3443 | 309 | git_config_with_options(git_get_color_config, NULL, |
c8985ce0 | 310 | &given_config_source, respect_includes); |
9ce03522 JH |
311 | |
312 | if (!get_color_found && def_color) | |
313 | color_parse(def_color, "command line", parsed_color); | |
314 | ||
315 | fputs(parsed_color, stdout); | |
9ce03522 JH |
316 | } |
317 | ||
0f6f5a40 | 318 | static int get_colorbool_found; |
69243c2b | 319 | static int get_diff_color_found; |
c659f55b | 320 | static int get_color_ui_found; |
ef90d6d4 JS |
321 | static int git_get_colorbool_config(const char *var, const char *value, |
322 | void *cb) | |
0f6f5a40 | 323 | { |
e269eb79 JK |
324 | if (!strcmp(var, get_colorbool_slot)) |
325 | get_colorbool_found = git_config_colorbool(var, value); | |
326 | else if (!strcmp(var, "diff.color")) | |
327 | get_diff_color_found = git_config_colorbool(var, value); | |
328 | else if (!strcmp(var, "color.ui")) | |
c659f55b | 329 | get_color_ui_found = git_config_colorbool(var, value); |
0f6f5a40 JH |
330 | return 0; |
331 | } | |
332 | ||
0e854a28 | 333 | static int get_colorbool(int print) |
0f6f5a40 | 334 | { |
69243c2b JH |
335 | get_colorbool_found = -1; |
336 | get_diff_color_found = -1; | |
b8612b4d | 337 | get_color_ui_found = -1; |
270a3443 | 338 | git_config_with_options(git_get_colorbool_config, NULL, |
c8985ce0 | 339 | &given_config_source, respect_includes); |
0f6f5a40 | 340 | |
69243c2b | 341 | if (get_colorbool_found < 0) { |
b408457f | 342 | if (!strcmp(get_colorbool_slot, "color.diff")) |
69243c2b JH |
343 | get_colorbool_found = get_diff_color_found; |
344 | if (get_colorbool_found < 0) | |
c659f55b | 345 | get_colorbool_found = get_color_ui_found; |
69243c2b JH |
346 | } |
347 | ||
b8612b4d MM |
348 | if (get_colorbool_found < 0) |
349 | /* default value if none found in config */ | |
4c7f1819 | 350 | get_colorbool_found = GIT_COLOR_AUTO; |
b8612b4d | 351 | |
daa0c3d9 JK |
352 | get_colorbool_found = want_color(get_colorbool_found); |
353 | ||
0e854a28 | 354 | if (print) { |
0f6f5a40 JH |
355 | printf("%s\n", get_colorbool_found ? "true" : "false"); |
356 | return 0; | |
0e854a28 FC |
357 | } else |
358 | return get_colorbool_found ? 0 : 1; | |
0f6f5a40 JH |
359 | } |
360 | ||
6aea9f0f | 361 | static void check_write(void) |
1bc88819 | 362 | { |
3caec73b KS |
363 | if (given_config_source.use_stdin) |
364 | die("writing to stdin is not supported"); | |
365 | ||
c8985ce0 | 366 | if (given_config_source.blob) |
1bc88819 HV |
367 | die("writing config blobs is not supported"); |
368 | } | |
369 | ||
d4770964 JH |
370 | struct urlmatch_current_candidate_value { |
371 | char value_is_null; | |
372 | struct strbuf value; | |
373 | }; | |
374 | ||
375 | static int urlmatch_collect_fn(const char *var, const char *value, void *cb) | |
376 | { | |
377 | struct string_list *values = cb; | |
378 | struct string_list_item *item = string_list_insert(values, var); | |
379 | struct urlmatch_current_candidate_value *matched = item->util; | |
380 | ||
381 | if (!matched) { | |
382 | matched = xmalloc(sizeof(*matched)); | |
383 | strbuf_init(&matched->value, 0); | |
384 | item->util = matched; | |
385 | } else { | |
386 | strbuf_reset(&matched->value); | |
387 | } | |
388 | ||
389 | if (value) { | |
390 | strbuf_addstr(&matched->value, value); | |
391 | matched->value_is_null = 0; | |
392 | } else { | |
393 | matched->value_is_null = 1; | |
394 | } | |
395 | return 0; | |
396 | } | |
397 | ||
398 | static char *dup_downcase(const char *string) | |
399 | { | |
400 | char *result; | |
401 | size_t len, i; | |
402 | ||
403 | len = strlen(string); | |
404 | result = xmalloc(len + 1); | |
405 | for (i = 0; i < len; i++) | |
406 | result[i] = tolower(string[i]); | |
407 | result[i] = '\0'; | |
408 | return result; | |
409 | } | |
410 | ||
411 | static int get_urlmatch(const char *var, const char *url) | |
412 | { | |
413 | char *section_tail; | |
414 | struct string_list_item *item; | |
415 | struct urlmatch_config config = { STRING_LIST_INIT_DUP }; | |
416 | struct string_list values = STRING_LIST_INIT_DUP; | |
417 | ||
418 | config.collect_fn = urlmatch_collect_fn; | |
419 | config.cascade_fn = NULL; | |
420 | config.cb = &values; | |
421 | ||
422 | if (!url_normalize(url, &config.url)) | |
6667a6ac | 423 | die("%s", config.url.err); |
d4770964 JH |
424 | |
425 | config.section = dup_downcase(var); | |
426 | section_tail = strchr(config.section, '.'); | |
427 | if (section_tail) { | |
428 | *section_tail = '\0'; | |
429 | config.key = section_tail + 1; | |
430 | show_keys = 0; | |
431 | } else { | |
432 | config.key = NULL; | |
433 | show_keys = 1; | |
434 | } | |
435 | ||
436 | git_config_with_options(urlmatch_config_entry, &config, | |
c8985ce0 | 437 | &given_config_source, respect_includes); |
d4770964 JH |
438 | |
439 | for_each_string_list_item(item, &values) { | |
440 | struct urlmatch_current_candidate_value *matched = item->util; | |
441 | struct strbuf key = STRBUF_INIT; | |
442 | struct strbuf buf = STRBUF_INIT; | |
443 | ||
444 | strbuf_addstr(&key, item->string); | |
445 | format_config(&buf, key.buf, | |
446 | matched->value_is_null ? NULL : matched->value.buf); | |
447 | fwrite(buf.buf, 1, buf.len, stdout); | |
448 | strbuf_release(&key); | |
449 | strbuf_release(&buf); | |
450 | ||
451 | strbuf_release(&matched->value); | |
452 | } | |
453 | string_list_clear(&config.vars, 1); | |
454 | string_list_clear(&values, 1); | |
455 | free(config.url.url); | |
456 | ||
457 | free((void *)config.section); | |
458 | return 0; | |
459 | } | |
460 | ||
9830534e MM |
461 | static char *default_user_config(void) |
462 | { | |
463 | struct strbuf buf = STRBUF_INIT; | |
464 | strbuf_addf(&buf, | |
465 | _("# This is Git's per-user configuration file.\n" | |
466 | "[core]\n" | |
467 | "# Please adapt and uncomment the following lines:\n" | |
468 | "# user = %s\n" | |
469 | "# email = %s\n"), | |
470 | ident_default_name(), | |
471 | ident_default_email()); | |
472 | return strbuf_detach(&buf, NULL); | |
473 | } | |
474 | ||
3ba7e6e2 | 475 | int cmd_config(int argc, const char **argv, const char *prefix) |
1b1e59c5 | 476 | { |
3ba7e6e2 | 477 | int nongit = !startup_info->have_repository; |
4b951b7e | 478 | char *value; |
7162dff3 | 479 | |
c8985ce0 | 480 | given_config_source.file = getenv(CONFIG_ENVIRONMENT); |
dc871831 | 481 | |
37782920 SB |
482 | argc = parse_options(argc, argv, prefix, builtin_config_options, |
483 | builtin_config_usage, | |
d64ec16c FC |
484 | PARSE_OPT_STOP_AT_NON_OPTION); |
485 | ||
1bc88819 | 486 | if (use_global_config + use_system_config + use_local_config + |
c8985ce0 | 487 | !!given_config_source.file + !!given_config_source.blob > 1) { |
67052c9d FC |
488 | error("only one config file at a time."); |
489 | usage_with_options(builtin_config_usage, builtin_config_options); | |
490 | } | |
491 | ||
3caec73b KS |
492 | if (given_config_source.file && |
493 | !strcmp(given_config_source.file, "-")) { | |
494 | given_config_source.file = NULL; | |
495 | given_config_source.use_stdin = 1; | |
496 | } | |
497 | ||
d64ec16c | 498 | if (use_global_config) { |
21cf3227 HKNN |
499 | char *user_config = NULL; |
500 | char *xdg_config = NULL; | |
501 | ||
502 | home_config_paths(&user_config, &xdg_config, "config"); | |
503 | ||
e3ebc35b MM |
504 | if (!user_config) |
505 | /* | |
506 | * It is unknown if HOME/.gitconfig exists, so | |
507 | * we do not know if we should write to XDG | |
508 | * location; error out even if XDG_CONFIG_HOME | |
509 | * is set and points at a sane location. | |
510 | */ | |
511 | die("$HOME not set"); | |
512 | ||
4698c8fe JN |
513 | if (access_or_warn(user_config, R_OK, 0) && |
514 | xdg_config && !access_or_warn(xdg_config, R_OK, 0)) | |
c8985ce0 | 515 | given_config_source.file = xdg_config; |
21cf3227 | 516 | else |
c8985ce0 | 517 | given_config_source.file = user_config; |
d64ec16c FC |
518 | } |
519 | else if (use_system_config) | |
c8985ce0 | 520 | given_config_source.file = git_etc_gitconfig(); |
57210a67 | 521 | else if (use_local_config) |
c8985ce0 KS |
522 | given_config_source.file = git_pathdup("config"); |
523 | else if (given_config_source.file) { | |
524 | if (!is_absolute_path(given_config_source.file) && prefix) | |
525 | given_config_source.file = | |
839de252 JK |
526 | xstrdup(prefix_filename(prefix, |
527 | strlen(prefix), | |
c8985ce0 | 528 | given_config_source.file)); |
d64ec16c FC |
529 | } |
530 | ||
9b25a0b5 | 531 | if (respect_includes == -1) |
c8985ce0 | 532 | respect_includes = !given_config_source.file; |
9b25a0b5 | 533 | |
d64ec16c FC |
534 | if (end_null) { |
535 | term = '\0'; | |
536 | delim = '\n'; | |
537 | key_delim = '\n'; | |
538 | } | |
539 | ||
16c1e939 FC |
540 | if (HAS_MULTI_BITS(types)) { |
541 | error("only one type at a time."); | |
542 | usage_with_options(builtin_config_usage, builtin_config_options); | |
543 | } | |
544 | ||
d64ec16c FC |
545 | if (get_color_slot) |
546 | actions |= ACTION_GET_COLOR; | |
547 | if (get_colorbool_slot) | |
548 | actions |= ACTION_GET_COLORBOOL; | |
549 | ||
c2387358 FC |
550 | if ((get_color_slot || get_colorbool_slot) && types) { |
551 | error("--get-color and variable type are incoherent"); | |
552 | usage_with_options(builtin_config_usage, builtin_config_options); | |
553 | } | |
554 | ||
d64ec16c FC |
555 | if (HAS_MULTI_BITS(actions)) { |
556 | error("only one action at a time."); | |
557 | usage_with_options(builtin_config_usage, builtin_config_options); | |
558 | } | |
559 | if (actions == 0) | |
560 | switch (argc) { | |
561 | case 1: actions = ACTION_GET; break; | |
562 | case 2: actions = ACTION_SET; break; | |
563 | case 3: actions = ACTION_SET_ALL; break; | |
564 | default: | |
565 | usage_with_options(builtin_config_usage, builtin_config_options); | |
db1696b8 | 566 | } |
d64ec16c FC |
567 | |
568 | if (actions == ACTION_LIST) { | |
225a9caf | 569 | check_argc(argc, 0, 0); |
270a3443 | 570 | if (git_config_with_options(show_all_config, NULL, |
c8985ce0 | 571 | &given_config_source, |
9b25a0b5 | 572 | respect_includes) < 0) { |
c8985ce0 | 573 | if (given_config_source.file) |
d824cbba | 574 | die_errno("unable to read config file '%s'", |
c8985ce0 | 575 | given_config_source.file); |
d64ec16c FC |
576 | else |
577 | die("error processing config file(s)"); | |
db1696b8 | 578 | } |
1b1e59c5 | 579 | } |
d64ec16c | 580 | else if (actions == ACTION_EDIT) { |
9830534e MM |
581 | const char *config_file = given_config_source.file ? |
582 | given_config_source.file : git_path("config"); | |
225a9caf | 583 | check_argc(argc, 0, 0); |
c8985ce0 | 584 | if (!given_config_source.file && nongit) |
d212ca17 | 585 | die("not in a git directory"); |
3caec73b KS |
586 | if (given_config_source.use_stdin) |
587 | die("editing stdin is not supported"); | |
c8985ce0 | 588 | if (given_config_source.blob) |
1bc88819 | 589 | die("editing blobs is not supported"); |
d64ec16c | 590 | git_config(git_default_config, NULL); |
9830534e MM |
591 | if (use_global_config) { |
592 | int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
593 | if (fd) { | |
594 | char *content = default_user_config(); | |
595 | write_str_in_full(fd, content); | |
596 | free(content); | |
597 | close(fd); | |
598 | } | |
599 | else if (errno != EEXIST) | |
600 | die_errno(_("cannot create configuration file %s"), config_file); | |
601 | } | |
602 | launch_editor(config_file, NULL, NULL); | |
d64ec16c FC |
603 | } |
604 | else if (actions == ACTION_SET) { | |
5a2df368 | 605 | int ret; |
6aea9f0f | 606 | check_write(); |
d64ec16c FC |
607 | check_argc(argc, 2, 2); |
608 | value = normalize_value(argv[0], argv[1]); | |
c8985ce0 | 609 | ret = git_config_set_in_file(given_config_source.file, argv[0], value); |
5a2df368 MG |
610 | if (ret == CONFIG_NOTHING_SET) |
611 | error("cannot overwrite multiple values with a single value\n" | |
67e223ed | 612 | " Use a regexp, --add or --replace-all to change %s.", argv[0]); |
5a2df368 | 613 | return ret; |
d64ec16c FC |
614 | } |
615 | else if (actions == ACTION_SET_ALL) { | |
6aea9f0f | 616 | check_write(); |
d64ec16c FC |
617 | check_argc(argc, 2, 3); |
618 | value = normalize_value(argv[0], argv[1]); | |
c8985ce0 | 619 | return git_config_set_multivar_in_file(given_config_source.file, |
270a3443 | 620 | argv[0], value, argv[2], 0); |
d64ec16c FC |
621 | } |
622 | else if (actions == ACTION_ADD) { | |
6aea9f0f | 623 | check_write(); |
d64ec16c FC |
624 | check_argc(argc, 2, 2); |
625 | value = normalize_value(argv[0], argv[1]); | |
c8985ce0 | 626 | return git_config_set_multivar_in_file(given_config_source.file, |
270a3443 | 627 | argv[0], value, "^$", 0); |
d64ec16c FC |
628 | } |
629 | else if (actions == ACTION_REPLACE_ALL) { | |
6aea9f0f | 630 | check_write(); |
d64ec16c FC |
631 | check_argc(argc, 2, 3); |
632 | value = normalize_value(argv[0], argv[1]); | |
c8985ce0 | 633 | return git_config_set_multivar_in_file(given_config_source.file, |
270a3443 | 634 | argv[0], value, argv[2], 1); |
d64ec16c FC |
635 | } |
636 | else if (actions == ACTION_GET) { | |
637 | check_argc(argc, 1, 2); | |
638 | return get_value(argv[0], argv[1]); | |
639 | } | |
640 | else if (actions == ACTION_GET_ALL) { | |
641 | do_all = 1; | |
642 | check_argc(argc, 1, 2); | |
643 | return get_value(argv[0], argv[1]); | |
644 | } | |
645 | else if (actions == ACTION_GET_REGEXP) { | |
646 | show_keys = 1; | |
647 | use_key_regexp = 1; | |
648 | do_all = 1; | |
649 | check_argc(argc, 1, 2); | |
650 | return get_value(argv[0], argv[1]); | |
651 | } | |
d4770964 JH |
652 | else if (actions == ACTION_GET_URLMATCH) { |
653 | check_argc(argc, 2, 2); | |
654 | return get_urlmatch(argv[0], argv[1]); | |
655 | } | |
d64ec16c | 656 | else if (actions == ACTION_UNSET) { |
6aea9f0f | 657 | check_write(); |
d64ec16c FC |
658 | check_argc(argc, 1, 2); |
659 | if (argc == 2) | |
c8985ce0 | 660 | return git_config_set_multivar_in_file(given_config_source.file, |
270a3443 | 661 | argv[0], NULL, argv[1], 0); |
d64ec16c | 662 | else |
c8985ce0 | 663 | return git_config_set_in_file(given_config_source.file, |
270a3443 | 664 | argv[0], NULL); |
d64ec16c FC |
665 | } |
666 | else if (actions == ACTION_UNSET_ALL) { | |
6aea9f0f | 667 | check_write(); |
d64ec16c | 668 | check_argc(argc, 1, 2); |
c8985ce0 | 669 | return git_config_set_multivar_in_file(given_config_source.file, |
270a3443 | 670 | argv[0], NULL, argv[1], 1); |
d64ec16c FC |
671 | } |
672 | else if (actions == ACTION_RENAME_SECTION) { | |
673 | int ret; | |
6aea9f0f | 674 | check_write(); |
d64ec16c | 675 | check_argc(argc, 2, 2); |
c8985ce0 | 676 | ret = git_config_rename_section_in_file(given_config_source.file, |
270a3443 | 677 | argv[0], argv[1]); |
d64ec16c FC |
678 | if (ret < 0) |
679 | return ret; | |
680 | if (ret == 0) | |
681 | die("No such section!"); | |
682 | } | |
683 | else if (actions == ACTION_REMOVE_SECTION) { | |
684 | int ret; | |
6aea9f0f | 685 | check_write(); |
d64ec16c | 686 | check_argc(argc, 1, 1); |
c8985ce0 | 687 | ret = git_config_rename_section_in_file(given_config_source.file, |
270a3443 | 688 | argv[0], NULL); |
d64ec16c FC |
689 | if (ret < 0) |
690 | return ret; | |
691 | if (ret == 0) | |
692 | die("No such section!"); | |
693 | } | |
694 | else if (actions == ACTION_GET_COLOR) { | |
695 | get_color(argv[0]); | |
696 | } | |
697 | else if (actions == ACTION_GET_COLORBOOL) { | |
698 | if (argc == 1) | |
e269eb79 | 699 | color_stdout_is_tty = git_config_bool("command line", argv[0]); |
d64ec16c FC |
700 | return get_colorbool(argc != 0); |
701 | } | |
702 | ||
1b1e59c5 JS |
703 | return 0; |
704 | } |