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