]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/config.c
config: add --comment option to add a comment
[thirdparty/git.git] / builtin / config.c
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "config.h"
4 #include "color.h"
5 #include "editor.h"
6 #include "environment.h"
7 #include "repository.h"
8 #include "gettext.h"
9 #include "ident.h"
10 #include "parse-options.h"
11 #include "urlmatch.h"
12 #include "path.h"
13 #include "quote.h"
14 #include "setup.h"
15 #include "strbuf.h"
16 #include "worktree.h"
17
18 static const char *const builtin_config_usage[] = {
19 N_("git config [<options>]"),
20 NULL
21 };
22
23 static char *key;
24 static regex_t *key_regexp;
25 static const char *value_pattern;
26 static regex_t *regexp;
27 static int show_keys;
28 static int omit_values;
29 static int use_key_regexp;
30 static int do_all;
31 static int do_not_match;
32 static char delim = '=';
33 static char key_delim = ' ';
34 static char term = '\n';
35
36 static int use_global_config, use_system_config, use_local_config;
37 static int use_worktree_config;
38 static struct git_config_source given_config_source;
39 static int actions, type;
40 static char *default_value;
41 static int end_nul;
42 static int respect_includes_opt = -1;
43 static struct config_options config_options;
44 static int show_origin;
45 static int show_scope;
46 static int fixed_value;
47 static const char *comment;
48
49 #define ACTION_GET (1<<0)
50 #define ACTION_GET_ALL (1<<1)
51 #define ACTION_GET_REGEXP (1<<2)
52 #define ACTION_REPLACE_ALL (1<<3)
53 #define ACTION_ADD (1<<4)
54 #define ACTION_UNSET (1<<5)
55 #define ACTION_UNSET_ALL (1<<6)
56 #define ACTION_RENAME_SECTION (1<<7)
57 #define ACTION_REMOVE_SECTION (1<<8)
58 #define ACTION_LIST (1<<9)
59 #define ACTION_EDIT (1<<10)
60 #define ACTION_SET (1<<11)
61 #define ACTION_SET_ALL (1<<12)
62 #define ACTION_GET_COLOR (1<<13)
63 #define ACTION_GET_COLORBOOL (1<<14)
64 #define ACTION_GET_URLMATCH (1<<15)
65
66 /*
67 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
68 * one line of output and which should therefore be paged.
69 */
70 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
71 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
72
73 #define TYPE_BOOL 1
74 #define TYPE_INT 2
75 #define TYPE_BOOL_OR_INT 3
76 #define TYPE_PATH 4
77 #define TYPE_EXPIRY_DATE 5
78 #define TYPE_COLOR 6
79 #define TYPE_BOOL_OR_STR 7
80
81 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
82 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
83 PARSE_OPT_NONEG, option_parse_type, (i) }
84
85 static NORETURN void usage_builtin_config(void);
86
87 static int option_parse_type(const struct option *opt, const char *arg,
88 int unset)
89 {
90 int new_type, *to_type;
91
92 if (unset) {
93 *((int *) opt->value) = 0;
94 return 0;
95 }
96
97 /*
98 * To support '--<type>' style flags, begin with new_type equal to
99 * opt->defval.
100 */
101 new_type = opt->defval;
102 if (!new_type) {
103 if (!strcmp(arg, "bool"))
104 new_type = TYPE_BOOL;
105 else if (!strcmp(arg, "int"))
106 new_type = TYPE_INT;
107 else if (!strcmp(arg, "bool-or-int"))
108 new_type = TYPE_BOOL_OR_INT;
109 else if (!strcmp(arg, "bool-or-str"))
110 new_type = TYPE_BOOL_OR_STR;
111 else if (!strcmp(arg, "path"))
112 new_type = TYPE_PATH;
113 else if (!strcmp(arg, "expiry-date"))
114 new_type = TYPE_EXPIRY_DATE;
115 else if (!strcmp(arg, "color"))
116 new_type = TYPE_COLOR;
117 else
118 die(_("unrecognized --type argument, %s"), arg);
119 }
120
121 to_type = opt->value;
122 if (*to_type && *to_type != new_type) {
123 /*
124 * Complain when there is a new type not equal to the old type.
125 * This allows for combinations like '--int --type=int' and
126 * '--type=int --type=int', but disallows ones like '--type=bool
127 * --int' and '--type=bool
128 * --type=int'.
129 */
130 error(_("only one type at a time"));
131 usage_builtin_config();
132 }
133 *to_type = new_type;
134
135 return 0;
136 }
137
138 static struct option builtin_config_options[] = {
139 OPT_GROUP(N_("Config file location")),
140 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
141 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
142 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
143 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
144 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
145 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
146 OPT_GROUP(N_("Action")),
147 OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
148 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
149 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
150 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
151 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
152 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
153 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
154 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
155 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
156 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
157 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
158 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
159 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
160 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
161 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
162 OPT_GROUP(N_("Type")),
163 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
164 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
165 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
166 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
167 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
168 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
169 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
170 OPT_GROUP(N_("Other")),
171 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
172 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
173 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
174 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
175 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
176 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
177 OPT_STRING(0, "comment", &comment, N_("value"), N_("human-readable comment string (# will be prepended automatically)")),
178 OPT_END(),
179 };
180
181 static NORETURN void usage_builtin_config(void)
182 {
183 usage_with_options(builtin_config_usage, builtin_config_options);
184 }
185
186 static void check_argc(int argc, int min, int max)
187 {
188 if (argc >= min && argc <= max)
189 return;
190 if (min == max)
191 error(_("wrong number of arguments, should be %d"), min);
192 else
193 error(_("wrong number of arguments, should be from %d to %d"),
194 min, max);
195 usage_builtin_config();
196 }
197
198 static void show_config_origin(const struct key_value_info *kvi,
199 struct strbuf *buf)
200 {
201 const char term = end_nul ? '\0' : '\t';
202
203 strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
204 strbuf_addch(buf, ':');
205 if (end_nul)
206 strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
207 else
208 quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0);
209 strbuf_addch(buf, term);
210 }
211
212 static void show_config_scope(const struct key_value_info *kvi,
213 struct strbuf *buf)
214 {
215 const char term = end_nul ? '\0' : '\t';
216 const char *scope = config_scope_name(kvi->scope);
217
218 strbuf_addstr(buf, N_(scope));
219 strbuf_addch(buf, term);
220 }
221
222 static int show_all_config(const char *key_, const char *value_,
223 const struct config_context *ctx,
224 void *cb UNUSED)
225 {
226 const struct key_value_info *kvi = ctx->kvi;
227
228 if (show_origin || show_scope) {
229 struct strbuf buf = STRBUF_INIT;
230 if (show_scope)
231 show_config_scope(kvi, &buf);
232 if (show_origin)
233 show_config_origin(kvi, &buf);
234 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
235 fwrite(buf.buf, 1, buf.len, stdout);
236 strbuf_release(&buf);
237 }
238 if (!omit_values && value_)
239 printf("%s%c%s%c", key_, delim, value_, term);
240 else
241 printf("%s%c", key_, term);
242 return 0;
243 }
244
245 struct strbuf_list {
246 struct strbuf *items;
247 int nr;
248 int alloc;
249 };
250
251 static int format_config(struct strbuf *buf, const char *key_,
252 const char *value_, const struct key_value_info *kvi)
253 {
254 if (show_scope)
255 show_config_scope(kvi, buf);
256 if (show_origin)
257 show_config_origin(kvi, buf);
258 if (show_keys)
259 strbuf_addstr(buf, key_);
260 if (!omit_values) {
261 if (show_keys)
262 strbuf_addch(buf, key_delim);
263
264 if (type == TYPE_INT)
265 strbuf_addf(buf, "%"PRId64,
266 git_config_int64(key_, value_ ? value_ : "", kvi));
267 else if (type == TYPE_BOOL)
268 strbuf_addstr(buf, git_config_bool(key_, value_) ?
269 "true" : "false");
270 else if (type == TYPE_BOOL_OR_INT) {
271 int is_bool, v;
272 v = git_config_bool_or_int(key_, value_, kvi,
273 &is_bool);
274 if (is_bool)
275 strbuf_addstr(buf, v ? "true" : "false");
276 else
277 strbuf_addf(buf, "%d", v);
278 } else if (type == TYPE_BOOL_OR_STR) {
279 int v = git_parse_maybe_bool(value_);
280 if (v < 0)
281 strbuf_addstr(buf, value_);
282 else
283 strbuf_addstr(buf, v ? "true" : "false");
284 } else if (type == TYPE_PATH) {
285 const char *v;
286 if (git_config_pathname(&v, key_, value_) < 0)
287 return -1;
288 strbuf_addstr(buf, v);
289 free((char *)v);
290 } else if (type == TYPE_EXPIRY_DATE) {
291 timestamp_t t;
292 if (git_config_expiry_date(&t, key_, value_) < 0)
293 return -1;
294 strbuf_addf(buf, "%"PRItime, t);
295 } else if (type == TYPE_COLOR) {
296 char v[COLOR_MAXLEN];
297 if (git_config_color(v, key_, value_) < 0)
298 return -1;
299 strbuf_addstr(buf, v);
300 } else if (value_) {
301 strbuf_addstr(buf, value_);
302 } else {
303 /* Just show the key name; back out delimiter */
304 if (show_keys)
305 strbuf_setlen(buf, buf->len - 1);
306 }
307 }
308 strbuf_addch(buf, term);
309 return 0;
310 }
311
312 static int collect_config(const char *key_, const char *value_,
313 const struct config_context *ctx, void *cb)
314 {
315 struct strbuf_list *values = cb;
316 const struct key_value_info *kvi = ctx->kvi;
317
318 if (!use_key_regexp && strcmp(key_, key))
319 return 0;
320 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
321 return 0;
322 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
323 return 0;
324 if (regexp != NULL &&
325 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
326 return 0;
327
328 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
329 strbuf_init(&values->items[values->nr], 0);
330
331 return format_config(&values->items[values->nr++], key_, value_, kvi);
332 }
333
334 static int get_value(const char *key_, const char *regex_, unsigned flags)
335 {
336 int ret = CONFIG_GENERIC_ERROR;
337 struct strbuf_list values = {NULL};
338 int i;
339
340 if (use_key_regexp) {
341 char *tl;
342
343 /*
344 * NEEDSWORK: this naive pattern lowercasing obviously does not
345 * work for more complex patterns like "^[^.]*Foo.*bar".
346 * Perhaps we should deprecate this altogether someday.
347 */
348
349 key = xstrdup(key_);
350 for (tl = key + strlen(key) - 1;
351 tl >= key && *tl != '.';
352 tl--)
353 *tl = tolower(*tl);
354 for (tl = key; *tl && *tl != '.'; tl++)
355 *tl = tolower(*tl);
356
357 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
358 if (regcomp(key_regexp, key, REG_EXTENDED)) {
359 error(_("invalid key pattern: %s"), key_);
360 FREE_AND_NULL(key_regexp);
361 ret = CONFIG_INVALID_PATTERN;
362 goto free_strings;
363 }
364 } else {
365 if (git_config_parse_key(key_, &key, NULL)) {
366 ret = CONFIG_INVALID_KEY;
367 goto free_strings;
368 }
369 }
370
371 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
372 value_pattern = regex_;
373 else if (regex_) {
374 if (regex_[0] == '!') {
375 do_not_match = 1;
376 regex_++;
377 }
378
379 regexp = (regex_t*)xmalloc(sizeof(regex_t));
380 if (regcomp(regexp, regex_, REG_EXTENDED)) {
381 error(_("invalid pattern: %s"), regex_);
382 FREE_AND_NULL(regexp);
383 ret = CONFIG_INVALID_PATTERN;
384 goto free_strings;
385 }
386 }
387
388 config_with_options(collect_config, &values,
389 &given_config_source, the_repository,
390 &config_options);
391
392 if (!values.nr && default_value) {
393 struct key_value_info kvi = KVI_INIT;
394 struct strbuf *item;
395
396 kvi_from_param(&kvi);
397 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
398 item = &values.items[values.nr++];
399 strbuf_init(item, 0);
400 if (format_config(item, key_, default_value, &kvi) < 0)
401 die(_("failed to format default config value: %s"),
402 default_value);
403 }
404
405 ret = !values.nr;
406
407 for (i = 0; i < values.nr; i++) {
408 struct strbuf *buf = values.items + i;
409 if (do_all || i == values.nr - 1)
410 fwrite(buf->buf, 1, buf->len, stdout);
411 strbuf_release(buf);
412 }
413 free(values.items);
414
415 free_strings:
416 free(key);
417 if (key_regexp) {
418 regfree(key_regexp);
419 free(key_regexp);
420 }
421 if (regexp) {
422 regfree(regexp);
423 free(regexp);
424 }
425
426 return ret;
427 }
428
429 static char *normalize_value(const char *key, const char *value,
430 struct key_value_info *kvi)
431 {
432 if (!value)
433 return NULL;
434
435 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
436 /*
437 * We don't do normalization for TYPE_PATH here: If
438 * the path is like ~/foobar/, we prefer to store
439 * "~/foobar/" in the config file, and to expand the ~
440 * when retrieving the value.
441 * Also don't do normalization for expiry dates.
442 */
443 return xstrdup(value);
444 if (type == TYPE_INT)
445 return xstrfmt("%"PRId64, git_config_int64(key, value, kvi));
446 if (type == TYPE_BOOL)
447 return xstrdup(git_config_bool(key, value) ? "true" : "false");
448 if (type == TYPE_BOOL_OR_INT) {
449 int is_bool, v;
450 v = git_config_bool_or_int(key, value, kvi, &is_bool);
451 if (!is_bool)
452 return xstrfmt("%d", v);
453 else
454 return xstrdup(v ? "true" : "false");
455 }
456 if (type == TYPE_BOOL_OR_STR) {
457 int v = git_parse_maybe_bool(value);
458 if (v < 0)
459 return xstrdup(value);
460 else
461 return xstrdup(v ? "true" : "false");
462 }
463 if (type == TYPE_COLOR) {
464 char v[COLOR_MAXLEN];
465 if (git_config_color(v, key, value))
466 die(_("cannot parse color '%s'"), value);
467
468 /*
469 * The contents of `v` now contain an ANSI escape
470 * sequence, not suitable for including within a
471 * configuration file. Treat the above as a
472 * "sanity-check", and return the given value, which we
473 * know is representable as valid color code.
474 */
475 return xstrdup(value);
476 }
477
478 BUG("cannot normalize type %d", type);
479 }
480
481 static int get_color_found;
482 static const char *get_color_slot;
483 static const char *get_colorbool_slot;
484 static char parsed_color[COLOR_MAXLEN];
485
486 static int git_get_color_config(const char *var, const char *value,
487 const struct config_context *ctx UNUSED,
488 void *cb UNUSED)
489 {
490 if (!strcmp(var, get_color_slot)) {
491 if (!value)
492 config_error_nonbool(var);
493 if (color_parse(value, parsed_color) < 0)
494 return -1;
495 get_color_found = 1;
496 }
497 return 0;
498 }
499
500 static void get_color(const char *var, const char *def_color)
501 {
502 get_color_slot = var;
503 get_color_found = 0;
504 parsed_color[0] = '\0';
505 config_with_options(git_get_color_config, NULL,
506 &given_config_source, the_repository,
507 &config_options);
508
509 if (!get_color_found && def_color) {
510 if (color_parse(def_color, parsed_color) < 0)
511 die(_("unable to parse default color value"));
512 }
513
514 fputs(parsed_color, stdout);
515 }
516
517 static int get_colorbool_found;
518 static int get_diff_color_found;
519 static int get_color_ui_found;
520 static int git_get_colorbool_config(const char *var, const char *value,
521 const struct config_context *ctx UNUSED,
522 void *data UNUSED)
523 {
524 if (!strcmp(var, get_colorbool_slot))
525 get_colorbool_found = git_config_colorbool(var, value);
526 else if (!strcmp(var, "diff.color"))
527 get_diff_color_found = git_config_colorbool(var, value);
528 else if (!strcmp(var, "color.ui"))
529 get_color_ui_found = git_config_colorbool(var, value);
530 return 0;
531 }
532
533 static int get_colorbool(const char *var, int print)
534 {
535 get_colorbool_slot = var;
536 get_colorbool_found = -1;
537 get_diff_color_found = -1;
538 get_color_ui_found = -1;
539 config_with_options(git_get_colorbool_config, NULL,
540 &given_config_source, the_repository,
541 &config_options);
542
543 if (get_colorbool_found < 0) {
544 if (!strcmp(get_colorbool_slot, "color.diff"))
545 get_colorbool_found = get_diff_color_found;
546 if (get_colorbool_found < 0)
547 get_colorbool_found = get_color_ui_found;
548 }
549
550 if (get_colorbool_found < 0)
551 /* default value if none found in config */
552 get_colorbool_found = GIT_COLOR_AUTO;
553
554 get_colorbool_found = want_color(get_colorbool_found);
555
556 if (print) {
557 printf("%s\n", get_colorbool_found ? "true" : "false");
558 return 0;
559 } else
560 return get_colorbool_found ? 0 : 1;
561 }
562
563 static void check_write(void)
564 {
565 if (!given_config_source.file && !startup_info->have_repository)
566 die(_("not in a git directory"));
567
568 if (given_config_source.use_stdin)
569 die(_("writing to stdin is not supported"));
570
571 if (given_config_source.blob)
572 die(_("writing config blobs is not supported"));
573 }
574
575 struct urlmatch_current_candidate_value {
576 char value_is_null;
577 struct strbuf value;
578 struct key_value_info kvi;
579 };
580
581 static int urlmatch_collect_fn(const char *var, const char *value,
582 const struct config_context *ctx,
583 void *cb)
584 {
585 struct string_list *values = cb;
586 struct string_list_item *item = string_list_insert(values, var);
587 struct urlmatch_current_candidate_value *matched = item->util;
588 const struct key_value_info *kvi = ctx->kvi;
589
590 if (!matched) {
591 matched = xmalloc(sizeof(*matched));
592 strbuf_init(&matched->value, 0);
593 item->util = matched;
594 } else {
595 strbuf_reset(&matched->value);
596 }
597 matched->kvi = *kvi;
598
599 if (value) {
600 strbuf_addstr(&matched->value, value);
601 matched->value_is_null = 0;
602 } else {
603 matched->value_is_null = 1;
604 }
605 return 0;
606 }
607
608 static int get_urlmatch(const char *var, const char *url)
609 {
610 int ret;
611 char *section_tail;
612 struct string_list_item *item;
613 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
614 struct string_list values = STRING_LIST_INIT_DUP;
615
616 config.collect_fn = urlmatch_collect_fn;
617 config.cascade_fn = NULL;
618 config.cb = &values;
619
620 if (!url_normalize(url, &config.url))
621 die("%s", config.url.err);
622
623 config.section = xstrdup_tolower(var);
624 section_tail = strchr(config.section, '.');
625 if (section_tail) {
626 *section_tail = '\0';
627 config.key = section_tail + 1;
628 show_keys = 0;
629 } else {
630 config.key = NULL;
631 show_keys = 1;
632 }
633
634 config_with_options(urlmatch_config_entry, &config,
635 &given_config_source, the_repository,
636 &config_options);
637
638 ret = !values.nr;
639
640 for_each_string_list_item(item, &values) {
641 struct urlmatch_current_candidate_value *matched = item->util;
642 struct strbuf buf = STRBUF_INIT;
643
644 format_config(&buf, item->string,
645 matched->value_is_null ? NULL : matched->value.buf,
646 &matched->kvi);
647 fwrite(buf.buf, 1, buf.len, stdout);
648 strbuf_release(&buf);
649
650 strbuf_release(&matched->value);
651 }
652 urlmatch_config_release(&config);
653 string_list_clear(&values, 1);
654 free(config.url.url);
655
656 free((void *)config.section);
657 return ret;
658 }
659
660 static char *default_user_config(void)
661 {
662 struct strbuf buf = STRBUF_INIT;
663 strbuf_addf(&buf,
664 _("# This is Git's per-user configuration file.\n"
665 "[user]\n"
666 "# Please adapt and uncomment the following lines:\n"
667 "# name = %s\n"
668 "# email = %s\n"),
669 ident_default_name(),
670 ident_default_email());
671 return strbuf_detach(&buf, NULL);
672 }
673
674 int cmd_config(int argc, const char **argv, const char *prefix)
675 {
676 int nongit = !startup_info->have_repository;
677 char *value = NULL;
678 int flags = 0;
679 int ret = 0;
680 struct key_value_info default_kvi = KVI_INIT;
681
682 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
683
684 argc = parse_options(argc, argv, prefix, builtin_config_options,
685 builtin_config_usage,
686 PARSE_OPT_STOP_AT_NON_OPTION);
687
688 if (use_global_config + use_system_config + use_local_config +
689 use_worktree_config +
690 !!given_config_source.file + !!given_config_source.blob > 1) {
691 error(_("only one config file at a time"));
692 usage_builtin_config();
693 }
694
695 if (nongit) {
696 if (use_local_config)
697 die(_("--local can only be used inside a git repository"));
698 if (given_config_source.blob)
699 die(_("--blob can only be used inside a git repository"));
700 if (use_worktree_config)
701 die(_("--worktree can only be used inside a git repository"));
702
703 }
704
705 if (given_config_source.file &&
706 !strcmp(given_config_source.file, "-")) {
707 given_config_source.file = NULL;
708 given_config_source.use_stdin = 1;
709 given_config_source.scope = CONFIG_SCOPE_COMMAND;
710 }
711
712 if (use_global_config) {
713 given_config_source.file = git_global_config();
714 if (!given_config_source.file)
715 /*
716 * It is unknown if HOME/.gitconfig exists, so
717 * we do not know if we should write to XDG
718 * location; error out even if XDG_CONFIG_HOME
719 * is set and points at a sane location.
720 */
721 die(_("$HOME not set"));
722 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
723 } else if (use_system_config) {
724 given_config_source.file = git_system_config();
725 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
726 } else if (use_local_config) {
727 given_config_source.file = git_pathdup("config");
728 given_config_source.scope = CONFIG_SCOPE_LOCAL;
729 } else if (use_worktree_config) {
730 struct worktree **worktrees = get_worktrees();
731 if (the_repository->repository_format_worktree_config)
732 given_config_source.file = git_pathdup("config.worktree");
733 else if (worktrees[0] && worktrees[1])
734 die(_("--worktree cannot be used with multiple "
735 "working trees unless the config\n"
736 "extension worktreeConfig is enabled. "
737 "Please read \"CONFIGURATION FILE\"\n"
738 "section in \"git help worktree\" for details"));
739 else
740 given_config_source.file = git_pathdup("config");
741 given_config_source.scope = CONFIG_SCOPE_LOCAL;
742 free_worktrees(worktrees);
743 } else if (given_config_source.file) {
744 if (!is_absolute_path(given_config_source.file) && prefix)
745 given_config_source.file =
746 prefix_filename(prefix, given_config_source.file);
747 given_config_source.scope = CONFIG_SCOPE_COMMAND;
748 } else if (given_config_source.blob) {
749 given_config_source.scope = CONFIG_SCOPE_COMMAND;
750 }
751
752 if (respect_includes_opt == -1)
753 config_options.respect_includes = !given_config_source.file;
754 else
755 config_options.respect_includes = respect_includes_opt;
756 if (!nongit) {
757 config_options.commondir = get_git_common_dir();
758 config_options.git_dir = get_git_dir();
759 }
760
761 if (end_nul) {
762 term = '\0';
763 delim = '\n';
764 key_delim = '\n';
765 }
766
767 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
768 error(_("--get-color and variable type are incoherent"));
769 usage_builtin_config();
770 }
771
772 if (HAS_MULTI_BITS(actions)) {
773 error(_("only one action at a time"));
774 usage_builtin_config();
775 }
776 if (actions == 0)
777 switch (argc) {
778 case 1: actions = ACTION_GET; break;
779 case 2: actions = ACTION_SET; break;
780 case 3: actions = ACTION_SET_ALL; break;
781 default:
782 usage_builtin_config();
783 }
784 if (omit_values &&
785 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
786 error(_("--name-only is only applicable to --list or --get-regexp"));
787 usage_builtin_config();
788 }
789
790 if (show_origin && !(actions &
791 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
792 error(_("--show-origin is only applicable to --get, --get-all, "
793 "--get-regexp, and --list"));
794 usage_builtin_config();
795 }
796
797 if (default_value && !(actions & ACTION_GET)) {
798 error(_("--default is only applicable to --get"));
799 usage_builtin_config();
800 }
801
802 if (comment &&
803 !(actions & (ACTION_ADD|ACTION_SET|ACTION_SET_ALL|ACTION_REPLACE_ALL))) {
804 error(_("--comment is only applicable to add/set/replace operations"));
805 usage_builtin_config();
806 }
807
808 /* check usage of --fixed-value */
809 if (fixed_value) {
810 int allowed_usage = 0;
811
812 switch (actions) {
813 /* git config --get <name> <value-pattern> */
814 case ACTION_GET:
815 /* git config --get-all <name> <value-pattern> */
816 case ACTION_GET_ALL:
817 /* git config --get-regexp <name-pattern> <value-pattern> */
818 case ACTION_GET_REGEXP:
819 /* git config --unset <name> <value-pattern> */
820 case ACTION_UNSET:
821 /* git config --unset-all <name> <value-pattern> */
822 case ACTION_UNSET_ALL:
823 allowed_usage = argc > 1 && !!argv[1];
824 break;
825
826 /* git config <name> <value> <value-pattern> */
827 case ACTION_SET_ALL:
828 /* git config --replace-all <name> <value> <value-pattern> */
829 case ACTION_REPLACE_ALL:
830 allowed_usage = argc > 2 && !!argv[2];
831 break;
832
833 /* other options don't allow --fixed-value */
834 }
835
836 if (!allowed_usage) {
837 error(_("--fixed-value only applies with 'value-pattern'"));
838 usage_builtin_config();
839 }
840
841 flags |= CONFIG_FLAGS_FIXED_VALUE;
842 }
843
844 if (actions & PAGING_ACTIONS)
845 setup_auto_pager("config", 1);
846
847 if (actions == ACTION_LIST) {
848 check_argc(argc, 0, 0);
849 if (config_with_options(show_all_config, NULL,
850 &given_config_source, the_repository,
851 &config_options) < 0) {
852 if (given_config_source.file)
853 die_errno(_("unable to read config file '%s'"),
854 given_config_source.file);
855 else
856 die(_("error processing config file(s)"));
857 }
858 }
859 else if (actions == ACTION_EDIT) {
860 char *config_file;
861
862 check_argc(argc, 0, 0);
863 if (!given_config_source.file && nongit)
864 die(_("not in a git directory"));
865 if (given_config_source.use_stdin)
866 die(_("editing stdin is not supported"));
867 if (given_config_source.blob)
868 die(_("editing blobs is not supported"));
869 git_config(git_default_config, NULL);
870 config_file = given_config_source.file ?
871 xstrdup(given_config_source.file) :
872 git_pathdup("config");
873 if (use_global_config) {
874 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
875 if (fd >= 0) {
876 char *content = default_user_config();
877 write_str_in_full(fd, content);
878 free(content);
879 close(fd);
880 }
881 else if (errno != EEXIST)
882 die_errno(_("cannot create configuration file %s"), config_file);
883 }
884 launch_editor(config_file, NULL, NULL);
885 free(config_file);
886 }
887 else if (actions == ACTION_SET) {
888 check_write();
889 check_argc(argc, 2, 2);
890 value = normalize_value(argv[0], argv[1], &default_kvi);
891 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], comment, value);
892 if (ret == CONFIG_NOTHING_SET)
893 error(_("cannot overwrite multiple values with a single value\n"
894 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
895 }
896 else if (actions == ACTION_SET_ALL) {
897 check_write();
898 check_argc(argc, 2, 3);
899 value = normalize_value(argv[0], argv[1], &default_kvi);
900 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
901 argv[0], value, argv[2],
902 comment, flags);
903 }
904 else if (actions == ACTION_ADD) {
905 check_write();
906 check_argc(argc, 2, 2);
907 value = normalize_value(argv[0], argv[1], &default_kvi);
908 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
909 argv[0], value,
910 CONFIG_REGEX_NONE,
911 comment, flags);
912 }
913 else if (actions == ACTION_REPLACE_ALL) {
914 check_write();
915 check_argc(argc, 2, 3);
916 value = normalize_value(argv[0], argv[1], &default_kvi);
917 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
918 argv[0], value, argv[2],
919 comment, flags | CONFIG_FLAGS_MULTI_REPLACE);
920 }
921 else if (actions == ACTION_GET) {
922 check_argc(argc, 1, 2);
923 return get_value(argv[0], argv[1], flags);
924 }
925 else if (actions == ACTION_GET_ALL) {
926 do_all = 1;
927 check_argc(argc, 1, 2);
928 return get_value(argv[0], argv[1], flags);
929 }
930 else if (actions == ACTION_GET_REGEXP) {
931 show_keys = 1;
932 use_key_regexp = 1;
933 do_all = 1;
934 check_argc(argc, 1, 2);
935 return get_value(argv[0], argv[1], flags);
936 }
937 else if (actions == ACTION_GET_URLMATCH) {
938 check_argc(argc, 2, 2);
939 return get_urlmatch(argv[0], argv[1]);
940 }
941 else if (actions == ACTION_UNSET) {
942 check_write();
943 check_argc(argc, 1, 2);
944 if (argc == 2)
945 return git_config_set_multivar_in_file_gently(given_config_source.file,
946 argv[0], NULL, argv[1],
947 NULL, flags);
948 else
949 return git_config_set_in_file_gently(given_config_source.file,
950 argv[0], NULL, NULL);
951 }
952 else if (actions == ACTION_UNSET_ALL) {
953 check_write();
954 check_argc(argc, 1, 2);
955 return git_config_set_multivar_in_file_gently(given_config_source.file,
956 argv[0], NULL, argv[1],
957 NULL, flags | CONFIG_FLAGS_MULTI_REPLACE);
958 }
959 else if (actions == ACTION_RENAME_SECTION) {
960 check_write();
961 check_argc(argc, 2, 2);
962 ret = git_config_rename_section_in_file(given_config_source.file,
963 argv[0], argv[1]);
964 if (ret < 0)
965 return ret;
966 else if (!ret)
967 die(_("no such section: %s"), argv[0]);
968 else
969 ret = 0;
970 }
971 else if (actions == ACTION_REMOVE_SECTION) {
972 check_write();
973 check_argc(argc, 1, 1);
974 ret = git_config_rename_section_in_file(given_config_source.file,
975 argv[0], NULL);
976 if (ret < 0)
977 return ret;
978 else if (!ret)
979 die(_("no such section: %s"), argv[0]);
980 else
981 ret = 0;
982 }
983 else if (actions == ACTION_GET_COLOR) {
984 check_argc(argc, 1, 2);
985 get_color(argv[0], argv[1]);
986 }
987 else if (actions == ACTION_GET_COLORBOOL) {
988 check_argc(argc, 1, 2);
989 if (argc == 2)
990 color_stdout_is_tty = git_config_bool("command line", argv[1]);
991 return get_colorbool(argv[0], argc == 2);
992 }
993
994 free(value);
995 return ret;
996 }