The eleventh batch
[alt-git.git] / builtin / config.c
blob20a0b64090eab56d3b6a54073e338fdbf5db4448
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"
18 static const char *const builtin_config_usage[] = {
19 N_("git config list [<file-option>] [<display-option>] [--includes]"),
20 N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"),
21 N_("git config set [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
22 N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
23 N_("git config rename-section [<file-option>] <old-name> <new-name>"),
24 N_("git config remove-section [<file-option>] <name>"),
25 N_("git config edit [<file-option>]"),
26 N_("git config [<file-option>] --get-colorbool <name> [<stdout-is-tty>]"),
27 NULL
30 static const char *const builtin_config_list_usage[] = {
31 N_("git config list [<file-option>] [<display-option>] [--includes]"),
32 NULL
35 static const char *const builtin_config_get_usage[] = {
36 N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"),
37 NULL
40 static const char *const builtin_config_set_usage[] = {
41 N_("git config set [<file-option>] [--type=<type>] [--comment=<message>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
42 NULL
45 static const char *const builtin_config_unset_usage[] = {
46 N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
47 NULL
50 static const char *const builtin_config_rename_section_usage[] = {
51 N_("git config rename-section [<file-option>] <old-name> <new-name>"),
52 NULL
55 static const char *const builtin_config_remove_section_usage[] = {
56 N_("git config remove-section [<file-option>] <name>"),
57 NULL
60 static const char *const builtin_config_edit_usage[] = {
61 N_("git config edit [<file-option>]"),
62 NULL
65 #define CONFIG_LOCATION_OPTIONS(opts) \
66 OPT_GROUP(N_("Config file location")), \
67 OPT_BOOL(0, "global", &opts.use_global_config, N_("use global config file")), \
68 OPT_BOOL(0, "system", &opts.use_system_config, N_("use system config file")), \
69 OPT_BOOL(0, "local", &opts.use_local_config, N_("use repository config file")), \
70 OPT_BOOL(0, "worktree", &opts.use_worktree_config, N_("use per-worktree config file")), \
71 OPT_STRING('f', "file", &opts.source.file, N_("file"), N_("use given config file")), \
72 OPT_STRING(0, "blob", &opts.source.blob, N_("blob-id"), N_("read config from given blob object"))
74 struct config_location_options {
75 struct git_config_source source;
76 struct config_options options;
77 char *file_to_free;
78 int use_global_config;
79 int use_system_config;
80 int use_local_config;
81 int use_worktree_config;
82 int respect_includes_opt;
84 #define CONFIG_LOCATION_OPTIONS_INIT { \
85 .respect_includes_opt = -1, \
88 #define CONFIG_TYPE_OPTIONS(type) \
89 OPT_GROUP(N_("Type")), \
90 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), \
91 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), \
92 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), \
93 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), \
94 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), \
95 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), \
96 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE)
98 #define CONFIG_DISPLAY_OPTIONS(opts) \
99 OPT_GROUP(N_("Display options")), \
100 OPT_BOOL('z', "null", &opts.end_nul, N_("terminate values with NUL byte")), \
101 OPT_BOOL(0, "name-only", &opts.omit_values, N_("show variable names only")), \
102 OPT_BOOL(0, "show-origin", &opts.show_origin, N_("show origin of config (file, standard input, blob, command line)")), \
103 OPT_BOOL(0, "show-scope", &opts.show_scope, N_("show scope of config (worktree, local, global, system, command)")), \
104 OPT_BOOL(0, "show-names", &opts.show_keys, N_("show config keys in addition to their values")), \
105 CONFIG_TYPE_OPTIONS(opts.type)
107 struct config_display_options {
108 int end_nul;
109 int omit_values;
110 int show_origin;
111 int show_scope;
112 int show_keys;
113 int type;
114 char *default_value;
115 /* Populated via `display_options_init()`. */
116 int term;
117 int delim;
118 int key_delim;
120 #define CONFIG_DISPLAY_OPTIONS_INIT { \
121 .term = '\n', \
122 .delim = '=', \
123 .key_delim = ' ', \
126 #define TYPE_BOOL 1
127 #define TYPE_INT 2
128 #define TYPE_BOOL_OR_INT 3
129 #define TYPE_PATH 4
130 #define TYPE_EXPIRY_DATE 5
131 #define TYPE_COLOR 6
132 #define TYPE_BOOL_OR_STR 7
134 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
135 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
136 PARSE_OPT_NONEG, option_parse_type, (i) }
138 static int option_parse_type(const struct option *opt, const char *arg,
139 int unset)
141 int new_type, *to_type;
143 if (unset) {
144 *((int *) opt->value) = 0;
145 return 0;
149 * To support '--<type>' style flags, begin with new_type equal to
150 * opt->defval.
152 new_type = opt->defval;
153 if (!new_type) {
154 if (!strcmp(arg, "bool"))
155 new_type = TYPE_BOOL;
156 else if (!strcmp(arg, "int"))
157 new_type = TYPE_INT;
158 else if (!strcmp(arg, "bool-or-int"))
159 new_type = TYPE_BOOL_OR_INT;
160 else if (!strcmp(arg, "bool-or-str"))
161 new_type = TYPE_BOOL_OR_STR;
162 else if (!strcmp(arg, "path"))
163 new_type = TYPE_PATH;
164 else if (!strcmp(arg, "expiry-date"))
165 new_type = TYPE_EXPIRY_DATE;
166 else if (!strcmp(arg, "color"))
167 new_type = TYPE_COLOR;
168 else
169 die(_("unrecognized --type argument, %s"), arg);
172 to_type = opt->value;
173 if (*to_type && *to_type != new_type) {
175 * Complain when there is a new type not equal to the old type.
176 * This allows for combinations like '--int --type=int' and
177 * '--type=int --type=int', but disallows ones like '--type=bool
178 * --int' and '--type=bool
179 * --type=int'.
181 error(_("only one type at a time"));
182 exit(129);
184 *to_type = new_type;
186 return 0;
189 static void check_argc(int argc, int min, int max)
191 if (argc >= min && argc <= max)
192 return;
193 if (min == max)
194 error(_("wrong number of arguments, should be %d"), min);
195 else
196 error(_("wrong number of arguments, should be from %d to %d"),
197 min, max);
198 exit(129);
201 static void show_config_origin(const struct config_display_options *opts,
202 const struct key_value_info *kvi,
203 struct strbuf *buf)
205 const char term = opts->end_nul ? '\0' : '\t';
207 strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
208 strbuf_addch(buf, ':');
209 if (opts->end_nul)
210 strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
211 else
212 quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0);
213 strbuf_addch(buf, term);
216 static void show_config_scope(const struct config_display_options *opts,
217 const struct key_value_info *kvi,
218 struct strbuf *buf)
220 const char term = opts->end_nul ? '\0' : '\t';
221 const char *scope = config_scope_name(kvi->scope);
223 strbuf_addstr(buf, N_(scope));
224 strbuf_addch(buf, term);
227 static int show_all_config(const char *key_, const char *value_,
228 const struct config_context *ctx,
229 void *cb)
231 const struct config_display_options *opts = cb;
232 const struct key_value_info *kvi = ctx->kvi;
234 if (opts->show_origin || opts->show_scope) {
235 struct strbuf buf = STRBUF_INIT;
236 if (opts->show_scope)
237 show_config_scope(opts, kvi, &buf);
238 if (opts->show_origin)
239 show_config_origin(opts, kvi, &buf);
240 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
241 fwrite(buf.buf, 1, buf.len, stdout);
242 strbuf_release(&buf);
244 if (!opts->omit_values && value_)
245 printf("%s%c%s%c", key_, opts->delim, value_, opts->term);
246 else
247 printf("%s%c", key_, opts->term);
248 return 0;
251 struct strbuf_list {
252 struct strbuf *items;
253 int nr;
254 int alloc;
257 static int format_config(const struct config_display_options *opts,
258 struct strbuf *buf, const char *key_,
259 const char *value_, const struct key_value_info *kvi)
261 if (opts->show_scope)
262 show_config_scope(opts, kvi, buf);
263 if (opts->show_origin)
264 show_config_origin(opts, kvi, buf);
265 if (opts->show_keys)
266 strbuf_addstr(buf, key_);
267 if (!opts->omit_values) {
268 if (opts->show_keys)
269 strbuf_addch(buf, opts->key_delim);
271 if (opts->type == TYPE_INT)
272 strbuf_addf(buf, "%"PRId64,
273 git_config_int64(key_, value_ ? value_ : "", kvi));
274 else if (opts->type == TYPE_BOOL)
275 strbuf_addstr(buf, git_config_bool(key_, value_) ?
276 "true" : "false");
277 else if (opts->type == TYPE_BOOL_OR_INT) {
278 int is_bool, v;
279 v = git_config_bool_or_int(key_, value_, kvi,
280 &is_bool);
281 if (is_bool)
282 strbuf_addstr(buf, v ? "true" : "false");
283 else
284 strbuf_addf(buf, "%d", v);
285 } else if (opts->type == TYPE_BOOL_OR_STR) {
286 int v = git_parse_maybe_bool(value_);
287 if (v < 0)
288 strbuf_addstr(buf, value_);
289 else
290 strbuf_addstr(buf, v ? "true" : "false");
291 } else if (opts->type == TYPE_PATH) {
292 char *v;
293 if (git_config_pathname(&v, key_, value_) < 0)
294 return -1;
295 strbuf_addstr(buf, v);
296 free((char *)v);
297 } else if (opts->type == TYPE_EXPIRY_DATE) {
298 timestamp_t t;
299 if (git_config_expiry_date(&t, key_, value_) < 0)
300 return -1;
301 strbuf_addf(buf, "%"PRItime, t);
302 } else if (opts->type == TYPE_COLOR) {
303 char v[COLOR_MAXLEN];
304 if (git_config_color(v, key_, value_) < 0)
305 return -1;
306 strbuf_addstr(buf, v);
307 } else if (value_) {
308 strbuf_addstr(buf, value_);
309 } else {
310 /* Just show the key name; back out delimiter */
311 if (opts->show_keys)
312 strbuf_setlen(buf, buf->len - 1);
315 strbuf_addch(buf, opts->term);
316 return 0;
319 #define GET_VALUE_ALL (1 << 0)
320 #define GET_VALUE_KEY_REGEXP (1 << 1)
322 struct collect_config_data {
323 const struct config_display_options *display_opts;
324 struct strbuf_list *values;
325 const char *value_pattern;
326 const char *key;
327 regex_t *regexp;
328 regex_t *key_regexp;
329 int do_not_match;
330 unsigned get_value_flags;
331 unsigned flags;
334 static int collect_config(const char *key_, const char *value_,
335 const struct config_context *ctx, void *cb)
337 struct collect_config_data *data = cb;
338 struct strbuf_list *values = data->values;
339 const struct key_value_info *kvi = ctx->kvi;
341 if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
342 strcmp(key_, data->key))
343 return 0;
344 if ((data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
345 regexec(data->key_regexp, key_, 0, NULL, 0))
346 return 0;
347 if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) &&
348 strcmp(data->value_pattern, (value_?value_:"")))
349 return 0;
350 if (data->regexp &&
351 (data->do_not_match ^ !!regexec(data->regexp, (value_?value_:""), 0, NULL, 0)))
352 return 0;
354 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
355 strbuf_init(&values->items[values->nr], 0);
357 return format_config(data->display_opts, &values->items[values->nr++],
358 key_, value_, kvi);
361 static int get_value(const struct config_location_options *opts,
362 const struct config_display_options *display_opts,
363 const char *key_, const char *regex_,
364 unsigned get_value_flags, unsigned flags)
366 int ret = CONFIG_GENERIC_ERROR;
367 struct strbuf_list values = {NULL};
368 struct collect_config_data data = {
369 .display_opts = display_opts,
370 .values = &values,
371 .get_value_flags = get_value_flags,
372 .flags = flags,
374 char *key = NULL;
375 int i;
377 if (get_value_flags & GET_VALUE_KEY_REGEXP) {
378 char *tl;
381 * NEEDSWORK: this naive pattern lowercasing obviously does not
382 * work for more complex patterns like "^[^.]*Foo.*bar".
383 * Perhaps we should deprecate this altogether someday.
386 key = xstrdup(key_);
387 for (tl = key + strlen(key) - 1;
388 tl >= key && *tl != '.';
389 tl--)
390 *tl = tolower(*tl);
391 for (tl = key; *tl && *tl != '.'; tl++)
392 *tl = tolower(*tl);
394 data.key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
395 if (regcomp(data.key_regexp, key, REG_EXTENDED)) {
396 error(_("invalid key pattern: %s"), key_);
397 FREE_AND_NULL(data.key_regexp);
398 ret = CONFIG_INVALID_PATTERN;
399 goto free_strings;
401 } else {
402 if (git_config_parse_key(key_, &key, NULL)) {
403 ret = CONFIG_INVALID_KEY;
404 goto free_strings;
407 data.key = key;
410 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
411 data.value_pattern = regex_;
412 else if (regex_) {
413 if (regex_[0] == '!') {
414 data.do_not_match = 1;
415 regex_++;
418 data.regexp = (regex_t*)xmalloc(sizeof(regex_t));
419 if (regcomp(data.regexp, regex_, REG_EXTENDED)) {
420 error(_("invalid pattern: %s"), regex_);
421 FREE_AND_NULL(data.regexp);
422 ret = CONFIG_INVALID_PATTERN;
423 goto free_strings;
427 config_with_options(collect_config, &data,
428 &opts->source, the_repository,
429 &opts->options);
431 if (!values.nr && display_opts->default_value) {
432 struct key_value_info kvi = KVI_INIT;
433 struct strbuf *item;
435 kvi_from_param(&kvi);
436 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
437 item = &values.items[values.nr++];
438 strbuf_init(item, 0);
439 if (format_config(display_opts, item, key_,
440 display_opts->default_value, &kvi) < 0)
441 die(_("failed to format default config value: %s"),
442 display_opts->default_value);
445 ret = !values.nr;
447 for (i = 0; i < values.nr; i++) {
448 struct strbuf *buf = values.items + i;
449 if ((get_value_flags & GET_VALUE_ALL) || i == values.nr - 1)
450 fwrite(buf->buf, 1, buf->len, stdout);
451 strbuf_release(buf);
453 free(values.items);
455 free_strings:
456 free(key);
457 if (data.key_regexp) {
458 regfree(data.key_regexp);
459 free(data.key_regexp);
461 if (data.regexp) {
462 regfree(data.regexp);
463 free(data.regexp);
466 return ret;
469 static char *normalize_value(const char *key, const char *value,
470 int type, struct key_value_info *kvi)
472 if (!value)
473 return NULL;
475 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
477 * We don't do normalization for TYPE_PATH here: If
478 * the path is like ~/foobar/, we prefer to store
479 * "~/foobar/" in the config file, and to expand the ~
480 * when retrieving the value.
481 * Also don't do normalization for expiry dates.
483 return xstrdup(value);
484 if (type == TYPE_INT)
485 return xstrfmt("%"PRId64, git_config_int64(key, value, kvi));
486 if (type == TYPE_BOOL)
487 return xstrdup(git_config_bool(key, value) ? "true" : "false");
488 if (type == TYPE_BOOL_OR_INT) {
489 int is_bool, v;
490 v = git_config_bool_or_int(key, value, kvi, &is_bool);
491 if (!is_bool)
492 return xstrfmt("%d", v);
493 else
494 return xstrdup(v ? "true" : "false");
496 if (type == TYPE_BOOL_OR_STR) {
497 int v = git_parse_maybe_bool(value);
498 if (v < 0)
499 return xstrdup(value);
500 else
501 return xstrdup(v ? "true" : "false");
503 if (type == TYPE_COLOR) {
504 char v[COLOR_MAXLEN];
505 if (git_config_color(v, key, value))
506 die(_("cannot parse color '%s'"), value);
509 * The contents of `v` now contain an ANSI escape
510 * sequence, not suitable for including within a
511 * configuration file. Treat the above as a
512 * "sanity-check", and return the given value, which we
513 * know is representable as valid color code.
515 return xstrdup(value);
518 BUG("cannot normalize type %d", type);
521 struct get_color_config_data {
522 int get_color_found;
523 const char *get_color_slot;
524 char parsed_color[COLOR_MAXLEN];
527 static int git_get_color_config(const char *var, const char *value,
528 const struct config_context *ctx UNUSED,
529 void *cb)
531 struct get_color_config_data *data = cb;
533 if (!strcmp(var, data->get_color_slot)) {
534 if (!value)
535 config_error_nonbool(var);
536 if (color_parse(value, data->parsed_color) < 0)
537 return -1;
538 data->get_color_found = 1;
540 return 0;
543 static void get_color(const struct config_location_options *opts,
544 const char *var, const char *def_color)
546 struct get_color_config_data data = {
547 .get_color_slot = var,
548 .parsed_color[0] = '\0',
551 config_with_options(git_get_color_config, &data,
552 &opts->source, the_repository,
553 &opts->options);
555 if (!data.get_color_found && def_color) {
556 if (color_parse(def_color, data.parsed_color) < 0)
557 die(_("unable to parse default color value"));
560 fputs(data.parsed_color, stdout);
563 struct get_colorbool_config_data {
564 int get_colorbool_found;
565 int get_diff_color_found;
566 int get_color_ui_found;
567 const char *get_colorbool_slot;
570 static int git_get_colorbool_config(const char *var, const char *value,
571 const struct config_context *ctx UNUSED,
572 void *cb)
574 struct get_colorbool_config_data *data = cb;
576 if (!strcmp(var, data->get_colorbool_slot))
577 data->get_colorbool_found = git_config_colorbool(var, value);
578 else if (!strcmp(var, "diff.color"))
579 data->get_diff_color_found = git_config_colorbool(var, value);
580 else if (!strcmp(var, "color.ui"))
581 data->get_color_ui_found = git_config_colorbool(var, value);
582 return 0;
585 static int get_colorbool(const struct config_location_options *opts,
586 const char *var, int print)
588 struct get_colorbool_config_data data = {
589 .get_colorbool_slot = var,
590 .get_colorbool_found = -1,
591 .get_diff_color_found = -1,
592 .get_color_ui_found = -1,
595 config_with_options(git_get_colorbool_config, &data,
596 &opts->source, the_repository,
597 &opts->options);
599 if (data.get_colorbool_found < 0) {
600 if (!strcmp(data.get_colorbool_slot, "color.diff"))
601 data.get_colorbool_found = data.get_diff_color_found;
602 if (data.get_colorbool_found < 0)
603 data.get_colorbool_found = data.get_color_ui_found;
606 if (data.get_colorbool_found < 0)
607 /* default value if none found in config */
608 data.get_colorbool_found = GIT_COLOR_AUTO;
610 data.get_colorbool_found = want_color(data.get_colorbool_found);
612 if (print) {
613 printf("%s\n", data.get_colorbool_found ? "true" : "false");
614 return 0;
615 } else
616 return data.get_colorbool_found ? 0 : 1;
619 static void check_write(const struct git_config_source *source)
621 if (!source->file && !startup_info->have_repository)
622 die(_("not in a git directory"));
624 if (source->use_stdin)
625 die(_("writing to stdin is not supported"));
627 if (source->blob)
628 die(_("writing config blobs is not supported"));
631 struct urlmatch_current_candidate_value {
632 char value_is_null;
633 struct strbuf value;
634 struct key_value_info kvi;
637 static int urlmatch_collect_fn(const char *var, const char *value,
638 const struct config_context *ctx,
639 void *cb)
641 struct string_list *values = cb;
642 struct string_list_item *item = string_list_insert(values, var);
643 struct urlmatch_current_candidate_value *matched = item->util;
644 const struct key_value_info *kvi = ctx->kvi;
646 if (!matched) {
647 matched = xmalloc(sizeof(*matched));
648 strbuf_init(&matched->value, 0);
649 item->util = matched;
650 } else {
651 strbuf_reset(&matched->value);
653 matched->kvi = *kvi;
655 if (value) {
656 strbuf_addstr(&matched->value, value);
657 matched->value_is_null = 0;
658 } else {
659 matched->value_is_null = 1;
661 return 0;
664 static int get_urlmatch(const struct config_location_options *opts,
665 const struct config_display_options *_display_opts,
666 const char *var, const char *url)
668 int ret;
669 char *section_tail;
670 struct config_display_options display_opts = *_display_opts;
671 struct string_list_item *item;
672 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
673 struct string_list values = STRING_LIST_INIT_DUP;
675 config.collect_fn = urlmatch_collect_fn;
676 config.cascade_fn = NULL;
677 config.cb = &values;
679 if (!url_normalize(url, &config.url))
680 die("%s", config.url.err);
682 config.section = xstrdup_tolower(var);
683 section_tail = strchr(config.section, '.');
684 if (section_tail) {
685 *section_tail = '\0';
686 config.key = section_tail + 1;
687 display_opts.show_keys = 0;
688 } else {
689 config.key = NULL;
690 display_opts.show_keys = 1;
693 config_with_options(urlmatch_config_entry, &config,
694 &opts->source, the_repository,
695 &opts->options);
697 ret = !values.nr;
699 for_each_string_list_item(item, &values) {
700 struct urlmatch_current_candidate_value *matched = item->util;
701 struct strbuf buf = STRBUF_INIT;
703 format_config(&display_opts, &buf, item->string,
704 matched->value_is_null ? NULL : matched->value.buf,
705 &matched->kvi);
706 fwrite(buf.buf, 1, buf.len, stdout);
707 strbuf_release(&buf);
709 strbuf_release(&matched->value);
711 urlmatch_config_release(&config);
712 string_list_clear(&values, 1);
713 free(config.url.url);
715 free((void *)config.section);
716 return ret;
719 static char *default_user_config(void)
721 struct strbuf buf = STRBUF_INIT;
722 strbuf_addf(&buf,
723 _("# This is Git's per-user configuration file.\n"
724 "[user]\n"
725 "# Please adapt and uncomment the following lines:\n"
726 "# name = %s\n"
727 "# email = %s\n"),
728 ident_default_name(),
729 ident_default_email());
730 return strbuf_detach(&buf, NULL);
733 static void location_options_init(struct config_location_options *opts,
734 const char *prefix)
736 if (!opts->source.file)
737 opts->source.file = opts->file_to_free =
738 xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
740 if (opts->use_global_config + opts->use_system_config +
741 opts->use_local_config + opts->use_worktree_config +
742 !!opts->source.file + !!opts->source.blob > 1) {
743 error(_("only one config file at a time"));
744 exit(129);
747 if (!startup_info->have_repository) {
748 if (opts->use_local_config)
749 die(_("--local can only be used inside a git repository"));
750 if (opts->source.blob)
751 die(_("--blob can only be used inside a git repository"));
752 if (opts->use_worktree_config)
753 die(_("--worktree can only be used inside a git repository"));
756 if (opts->source.file &&
757 !strcmp(opts->source.file, "-")) {
758 opts->source.file = NULL;
759 opts->source.use_stdin = 1;
760 opts->source.scope = CONFIG_SCOPE_COMMAND;
763 if (opts->use_global_config) {
764 opts->source.file = opts->file_to_free = git_global_config();
765 if (!opts->source.file)
767 * It is unknown if HOME/.gitconfig exists, so
768 * we do not know if we should write to XDG
769 * location; error out even if XDG_CONFIG_HOME
770 * is set and points at a sane location.
772 die(_("$HOME not set"));
773 opts->source.scope = CONFIG_SCOPE_GLOBAL;
774 } else if (opts->use_system_config) {
775 opts->source.file = opts->file_to_free = git_system_config();
776 opts->source.scope = CONFIG_SCOPE_SYSTEM;
777 } else if (opts->use_local_config) {
778 opts->source.file = opts->file_to_free = git_pathdup("config");
779 opts->source.scope = CONFIG_SCOPE_LOCAL;
780 } else if (opts->use_worktree_config) {
781 struct worktree **worktrees = get_worktrees();
782 if (the_repository->repository_format_worktree_config)
783 opts->source.file = opts->file_to_free =
784 git_pathdup("config.worktree");
785 else if (worktrees[0] && worktrees[1])
786 die(_("--worktree cannot be used with multiple "
787 "working trees unless the config\n"
788 "extension worktreeConfig is enabled. "
789 "Please read \"CONFIGURATION FILE\"\n"
790 "section in \"git help worktree\" for details"));
791 else
792 opts->source.file = opts->file_to_free =
793 git_pathdup("config");
794 opts->source.scope = CONFIG_SCOPE_LOCAL;
795 free_worktrees(worktrees);
796 } else if (opts->source.file) {
797 if (!is_absolute_path(opts->source.file) && prefix)
798 opts->source.file = opts->file_to_free =
799 prefix_filename(prefix, opts->source.file);
800 opts->source.scope = CONFIG_SCOPE_COMMAND;
801 } else if (opts->source.blob) {
802 opts->source.scope = CONFIG_SCOPE_COMMAND;
805 if (opts->respect_includes_opt == -1)
806 opts->options.respect_includes = !opts->source.file;
807 else
808 opts->options.respect_includes = opts->respect_includes_opt;
809 if (startup_info->have_repository) {
810 opts->options.commondir = get_git_common_dir();
811 opts->options.git_dir = get_git_dir();
815 static void location_options_release(struct config_location_options *opts)
817 free(opts->file_to_free);
820 static void display_options_init(struct config_display_options *opts)
822 if (opts->end_nul) {
823 opts->term = '\0';
824 opts->delim = '\n';
825 opts->key_delim = '\n';
829 static int cmd_config_list(int argc, const char **argv, const char *prefix)
831 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
832 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
833 struct option opts[] = {
834 CONFIG_LOCATION_OPTIONS(location_opts),
835 CONFIG_DISPLAY_OPTIONS(display_opts),
836 OPT_GROUP(N_("Other")),
837 OPT_BOOL(0, "includes", &location_opts.respect_includes_opt,
838 N_("respect include directives on lookup")),
839 OPT_END(),
842 argc = parse_options(argc, argv, prefix, opts, builtin_config_list_usage, 0);
843 check_argc(argc, 0, 0);
845 location_options_init(&location_opts, prefix);
846 display_options_init(&display_opts);
848 setup_auto_pager("config", 1);
850 if (config_with_options(show_all_config, &display_opts,
851 &location_opts.source, the_repository,
852 &location_opts.options) < 0) {
853 if (location_opts.source.file)
854 die_errno(_("unable to read config file '%s'"),
855 location_opts.source.file);
856 else
857 die(_("error processing config file(s)"));
860 location_options_release(&location_opts);
861 return 0;
864 static int cmd_config_get(int argc, const char **argv, const char *prefix)
866 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
867 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
868 const char *value_pattern = NULL, *url = NULL;
869 int flags = 0;
870 unsigned get_value_flags = 0;
871 struct option opts[] = {
872 CONFIG_LOCATION_OPTIONS(location_opts),
873 OPT_GROUP(N_("Filter options")),
874 OPT_BIT(0, "all", &get_value_flags, N_("return all values for multi-valued config options"), GET_VALUE_ALL),
875 OPT_BIT(0, "regexp", &get_value_flags, N_("interpret the name as a regular expression"), GET_VALUE_KEY_REGEXP),
876 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
877 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
878 OPT_STRING(0, "url", &url, N_("URL"), N_("show config matching the given URL")),
879 CONFIG_DISPLAY_OPTIONS(display_opts),
880 OPT_GROUP(N_("Other")),
881 OPT_BOOL(0, "includes", &location_opts.respect_includes_opt,
882 N_("respect include directives on lookup")),
883 OPT_STRING(0, "default", &display_opts.default_value,
884 N_("value"), N_("use default value when missing entry")),
885 OPT_END(),
887 int ret;
889 argc = parse_options(argc, argv, prefix, opts, builtin_config_get_usage,
890 PARSE_OPT_STOP_AT_NON_OPTION);
891 check_argc(argc, 1, 1);
893 if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
894 die(_("--fixed-value only applies with 'value-pattern'"));
895 if (display_opts.default_value &&
896 ((get_value_flags & GET_VALUE_ALL) || url))
897 die(_("--default= cannot be used with --all or --url="));
898 if (url && ((get_value_flags & GET_VALUE_ALL) ||
899 (get_value_flags & GET_VALUE_KEY_REGEXP) ||
900 value_pattern))
901 die(_("--url= cannot be used with --all, --regexp or --value"));
903 location_options_init(&location_opts, prefix);
904 display_options_init(&display_opts);
906 setup_auto_pager("config", 1);
908 if (url)
909 ret = get_urlmatch(&location_opts, &display_opts, argv[0], url);
910 else
911 ret = get_value(&location_opts, &display_opts, argv[0], value_pattern,
912 get_value_flags, flags);
914 location_options_release(&location_opts);
915 return ret;
918 static int cmd_config_set(int argc, const char **argv, const char *prefix)
920 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
921 const char *value_pattern = NULL, *comment_arg = NULL;
922 char *comment = NULL;
923 int flags = 0, append = 0, type = 0;
924 struct option opts[] = {
925 CONFIG_LOCATION_OPTIONS(location_opts),
926 CONFIG_TYPE_OPTIONS(type),
927 OPT_GROUP(N_("Filter")),
928 OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE),
929 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
930 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
931 OPT_GROUP(N_("Other")),
932 OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
933 OPT_BOOL(0, "append", &append, N_("add a new line without altering any existing values")),
934 OPT_END(),
936 struct key_value_info default_kvi = KVI_INIT;
937 char *value;
938 int ret;
940 argc = parse_options(argc, argv, prefix, opts, builtin_config_set_usage,
941 PARSE_OPT_STOP_AT_NON_OPTION);
942 check_argc(argc, 2, 2);
944 if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
945 die(_("--fixed-value only applies with --value=<pattern>"));
946 if (append && value_pattern)
947 die(_("--append cannot be used with --value=<pattern>"));
948 if (append)
949 value_pattern = CONFIG_REGEX_NONE;
951 comment = git_config_prepare_comment_string(comment_arg);
953 location_options_init(&location_opts, prefix);
954 check_write(&location_opts.source);
956 value = normalize_value(argv[0], argv[1], type, &default_kvi);
958 if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) {
959 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
960 argv[0], value, value_pattern,
961 comment, flags);
962 } else {
963 ret = git_config_set_in_file_gently(location_opts.source.file,
964 argv[0], comment, value);
965 if (ret == CONFIG_NOTHING_SET)
966 error(_("cannot overwrite multiple values with a single value\n"
967 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
970 location_options_release(&location_opts);
971 free(comment);
972 free(value);
973 return ret;
976 static int cmd_config_unset(int argc, const char **argv, const char *prefix)
978 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
979 const char *value_pattern = NULL;
980 int flags = 0;
981 struct option opts[] = {
982 CONFIG_LOCATION_OPTIONS(location_opts),
983 OPT_GROUP(N_("Filter")),
984 OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE),
985 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
986 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
987 OPT_END(),
989 int ret;
991 argc = parse_options(argc, argv, prefix, opts, builtin_config_unset_usage,
992 PARSE_OPT_STOP_AT_NON_OPTION);
993 check_argc(argc, 1, 1);
995 if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
996 die(_("--fixed-value only applies with 'value-pattern'"));
998 location_options_init(&location_opts, prefix);
999 check_write(&location_opts.source);
1001 if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern)
1002 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
1003 argv[0], NULL, value_pattern,
1004 NULL, flags);
1005 else
1006 ret = git_config_set_in_file_gently(location_opts.source.file, argv[0],
1007 NULL, NULL);
1009 location_options_release(&location_opts);
1010 return ret;
1013 static int cmd_config_rename_section(int argc, const char **argv, const char *prefix)
1015 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
1016 struct option opts[] = {
1017 CONFIG_LOCATION_OPTIONS(location_opts),
1018 OPT_END(),
1020 int ret;
1022 argc = parse_options(argc, argv, prefix, opts, builtin_config_rename_section_usage,
1023 PARSE_OPT_STOP_AT_NON_OPTION);
1024 check_argc(argc, 2, 2);
1026 location_options_init(&location_opts, prefix);
1027 check_write(&location_opts.source);
1029 ret = git_config_rename_section_in_file(location_opts.source.file,
1030 argv[0], argv[1]);
1031 if (ret < 0)
1032 goto out;
1033 else if (!ret)
1034 die(_("no such section: %s"), argv[0]);
1035 ret = 0;
1037 out:
1038 location_options_release(&location_opts);
1039 return ret;
1042 static int cmd_config_remove_section(int argc, const char **argv, const char *prefix)
1044 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
1045 struct option opts[] = {
1046 CONFIG_LOCATION_OPTIONS(location_opts),
1047 OPT_END(),
1049 int ret;
1051 argc = parse_options(argc, argv, prefix, opts, builtin_config_remove_section_usage,
1052 PARSE_OPT_STOP_AT_NON_OPTION);
1053 check_argc(argc, 1, 1);
1055 location_options_init(&location_opts, prefix);
1056 check_write(&location_opts.source);
1058 ret = git_config_rename_section_in_file(location_opts.source.file,
1059 argv[0], NULL);
1060 if (ret < 0)
1061 goto out;
1062 else if (!ret)
1063 die(_("no such section: %s"), argv[0]);
1064 ret = 0;
1066 out:
1067 location_options_release(&location_opts);
1068 return ret;
1071 static int show_editor(struct config_location_options *opts)
1073 char *config_file;
1075 if (!opts->source.file && !startup_info->have_repository)
1076 die(_("not in a git directory"));
1077 if (opts->source.use_stdin)
1078 die(_("editing stdin is not supported"));
1079 if (opts->source.blob)
1080 die(_("editing blobs is not supported"));
1081 git_config(git_default_config, NULL);
1082 config_file = opts->source.file ?
1083 xstrdup(opts->source.file) :
1084 git_pathdup("config");
1085 if (opts->use_global_config) {
1086 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
1087 if (fd >= 0) {
1088 char *content = default_user_config();
1089 write_str_in_full(fd, content);
1090 free(content);
1091 close(fd);
1093 else if (errno != EEXIST)
1094 die_errno(_("cannot create configuration file %s"), config_file);
1096 launch_editor(config_file, NULL, NULL);
1097 free(config_file);
1099 return 0;
1102 static int cmd_config_edit(int argc, const char **argv, const char *prefix)
1104 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
1105 struct option opts[] = {
1106 CONFIG_LOCATION_OPTIONS(location_opts),
1107 OPT_END(),
1109 int ret;
1111 argc = parse_options(argc, argv, prefix, opts, builtin_config_edit_usage, 0);
1112 check_argc(argc, 0, 0);
1114 location_options_init(&location_opts, prefix);
1115 check_write(&location_opts.source);
1117 ret = show_editor(&location_opts);
1118 location_options_release(&location_opts);
1119 return ret;
1122 static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1124 enum {
1125 ACTION_GET = (1<<0),
1126 ACTION_GET_ALL = (1<<1),
1127 ACTION_GET_REGEXP = (1<<2),
1128 ACTION_REPLACE_ALL = (1<<3),
1129 ACTION_ADD = (1<<4),
1130 ACTION_UNSET = (1<<5),
1131 ACTION_UNSET_ALL = (1<<6),
1132 ACTION_RENAME_SECTION = (1<<7),
1133 ACTION_REMOVE_SECTION = (1<<8),
1134 ACTION_LIST = (1<<9),
1135 ACTION_EDIT = (1<<10),
1136 ACTION_SET = (1<<11),
1137 ACTION_SET_ALL = (1<<12),
1138 ACTION_GET_COLOR = (1<<13),
1139 ACTION_GET_COLORBOOL = (1<<14),
1140 ACTION_GET_URLMATCH = (1<<15),
1142 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
1143 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
1144 const char *comment_arg = NULL;
1145 int actions = 0;
1146 unsigned flags = 0;
1147 struct option opts[] = {
1148 CONFIG_LOCATION_OPTIONS(location_opts),
1149 OPT_GROUP(N_("Action")),
1150 OPT_CMDMODE(0, "get", &actions, N_("get value: name [<value-pattern>]"), ACTION_GET),
1151 OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL),
1152 OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP),
1153 OPT_CMDMODE(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
1154 OPT_CMDMODE(0, "replace-all", &actions, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL),
1155 OPT_CMDMODE(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
1156 OPT_CMDMODE(0, "unset", &actions, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET),
1157 OPT_CMDMODE(0, "unset-all", &actions, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL),
1158 OPT_CMDMODE(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
1159 OPT_CMDMODE(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
1160 OPT_CMDMODE('l', "list", &actions, N_("list all"), ACTION_LIST),
1161 OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
1162 OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
1163 OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
1164 CONFIG_DISPLAY_OPTIONS(display_opts),
1165 OPT_GROUP(N_("Other")),
1166 OPT_STRING(0, "default", &display_opts.default_value,
1167 N_("value"), N_("with --get, use default value when missing entry")),
1168 OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
1169 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
1170 OPT_BOOL(0, "includes", &location_opts.respect_includes_opt,
1171 N_("respect include directives on lookup")),
1172 OPT_END(),
1174 char *value = NULL, *comment = NULL;
1175 int ret = 0;
1176 struct key_value_info default_kvi = KVI_INIT;
1178 argc = parse_options(argc, argv, prefix, opts,
1179 builtin_config_usage,
1180 PARSE_OPT_STOP_AT_NON_OPTION);
1182 location_options_init(&location_opts, prefix);
1183 display_options_init(&display_opts);
1185 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && display_opts.type) {
1186 error(_("--get-color and variable type are incoherent"));
1187 exit(129);
1190 if (actions == 0)
1191 switch (argc) {
1192 case 1: actions = ACTION_GET; break;
1193 case 2: actions = ACTION_SET; break;
1194 case 3: actions = ACTION_SET_ALL; break;
1195 default:
1196 error(_("no action specified"));
1197 exit(129);
1199 if (display_opts.omit_values &&
1200 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
1201 error(_("--name-only is only applicable to --list or --get-regexp"));
1202 exit(129);
1205 if (display_opts.show_origin && !(actions &
1206 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
1207 error(_("--show-origin is only applicable to --get, --get-all, "
1208 "--get-regexp, and --list"));
1209 exit(129);
1212 if (display_opts.default_value && !(actions & ACTION_GET)) {
1213 error(_("--default is only applicable to --get"));
1214 exit(129);
1217 if (comment_arg &&
1218 !(actions & (ACTION_ADD|ACTION_SET|ACTION_SET_ALL|ACTION_REPLACE_ALL))) {
1219 error(_("--comment is only applicable to add/set/replace operations"));
1220 exit(129);
1223 /* check usage of --fixed-value */
1224 if (flags & CONFIG_FLAGS_FIXED_VALUE) {
1225 int allowed_usage = 0;
1227 switch (actions) {
1228 /* git config --get <name> <value-pattern> */
1229 case ACTION_GET:
1230 /* git config --get-all <name> <value-pattern> */
1231 case ACTION_GET_ALL:
1232 /* git config --get-regexp <name-pattern> <value-pattern> */
1233 case ACTION_GET_REGEXP:
1234 /* git config --unset <name> <value-pattern> */
1235 case ACTION_UNSET:
1236 /* git config --unset-all <name> <value-pattern> */
1237 case ACTION_UNSET_ALL:
1238 allowed_usage = argc > 1 && !!argv[1];
1239 break;
1241 /* git config <name> <value> <value-pattern> */
1242 case ACTION_SET_ALL:
1243 /* git config --replace-all <name> <value> <value-pattern> */
1244 case ACTION_REPLACE_ALL:
1245 allowed_usage = argc > 2 && !!argv[2];
1246 break;
1248 /* other options don't allow --fixed-value */
1251 if (!allowed_usage) {
1252 error(_("--fixed-value only applies with 'value-pattern'"));
1253 exit(129);
1257 comment = git_config_prepare_comment_string(comment_arg);
1260 * The following actions may produce more than one line of output and
1261 * should therefore be paged.
1263 if (actions & (ACTION_LIST | ACTION_GET_ALL | ACTION_GET_REGEXP | ACTION_GET_URLMATCH))
1264 setup_auto_pager("config", 1);
1266 if (actions == ACTION_LIST) {
1267 check_argc(argc, 0, 0);
1268 if (config_with_options(show_all_config, &display_opts,
1269 &location_opts.source, the_repository,
1270 &location_opts.options) < 0) {
1271 if (location_opts.source.file)
1272 die_errno(_("unable to read config file '%s'"),
1273 location_opts.source.file);
1274 else
1275 die(_("error processing config file(s)"));
1278 else if (actions == ACTION_EDIT) {
1279 ret = show_editor(&location_opts);
1281 else if (actions == ACTION_SET) {
1282 check_write(&location_opts.source);
1283 check_argc(argc, 2, 2);
1284 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1285 ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], comment, value);
1286 if (ret == CONFIG_NOTHING_SET)
1287 error(_("cannot overwrite multiple values with a single value\n"
1288 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
1290 else if (actions == ACTION_SET_ALL) {
1291 check_write(&location_opts.source);
1292 check_argc(argc, 2, 3);
1293 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1294 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
1295 argv[0], value, argv[2],
1296 comment, flags);
1298 else if (actions == ACTION_ADD) {
1299 check_write(&location_opts.source);
1300 check_argc(argc, 2, 2);
1301 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1302 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
1303 argv[0], value,
1304 CONFIG_REGEX_NONE,
1305 comment, flags);
1307 else if (actions == ACTION_REPLACE_ALL) {
1308 check_write(&location_opts.source);
1309 check_argc(argc, 2, 3);
1310 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1311 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
1312 argv[0], value, argv[2],
1313 comment, flags | CONFIG_FLAGS_MULTI_REPLACE);
1315 else if (actions == ACTION_GET) {
1316 check_argc(argc, 1, 2);
1317 ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1318 0, flags);
1320 else if (actions == ACTION_GET_ALL) {
1321 check_argc(argc, 1, 2);
1322 ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1323 GET_VALUE_ALL, flags);
1325 else if (actions == ACTION_GET_REGEXP) {
1326 display_opts.show_keys = 1;
1327 check_argc(argc, 1, 2);
1328 ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1329 GET_VALUE_ALL|GET_VALUE_KEY_REGEXP, flags);
1331 else if (actions == ACTION_GET_URLMATCH) {
1332 check_argc(argc, 2, 2);
1333 ret = get_urlmatch(&location_opts, &display_opts, argv[0], argv[1]);
1335 else if (actions == ACTION_UNSET) {
1336 check_write(&location_opts.source);
1337 check_argc(argc, 1, 2);
1338 if (argc == 2)
1339 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
1340 argv[0], NULL, argv[1],
1341 NULL, flags);
1342 else
1343 ret = git_config_set_in_file_gently(location_opts.source.file,
1344 argv[0], NULL, NULL);
1346 else if (actions == ACTION_UNSET_ALL) {
1347 check_write(&location_opts.source);
1348 check_argc(argc, 1, 2);
1349 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
1350 argv[0], NULL, argv[1],
1351 NULL, flags | CONFIG_FLAGS_MULTI_REPLACE);
1353 else if (actions == ACTION_RENAME_SECTION) {
1354 check_write(&location_opts.source);
1355 check_argc(argc, 2, 2);
1356 ret = git_config_rename_section_in_file(location_opts.source.file,
1357 argv[0], argv[1]);
1358 if (ret < 0)
1359 goto out;
1360 else if (!ret)
1361 die(_("no such section: %s"), argv[0]);
1362 else
1363 ret = 0;
1365 else if (actions == ACTION_REMOVE_SECTION) {
1366 check_write(&location_opts.source);
1367 check_argc(argc, 1, 1);
1368 ret = git_config_rename_section_in_file(location_opts.source.file,
1369 argv[0], NULL);
1370 if (ret < 0)
1371 goto out;
1372 else if (!ret)
1373 die(_("no such section: %s"), argv[0]);
1374 else
1375 ret = 0;
1377 else if (actions == ACTION_GET_COLOR) {
1378 check_argc(argc, 1, 2);
1379 get_color(&location_opts, argv[0], argv[1]);
1381 else if (actions == ACTION_GET_COLORBOOL) {
1382 check_argc(argc, 1, 2);
1383 if (argc == 2)
1384 color_stdout_is_tty = git_config_bool("command line", argv[1]);
1385 ret = get_colorbool(&location_opts, argv[0], argc == 2);
1388 out:
1389 location_options_release(&location_opts);
1390 free(comment);
1391 free(value);
1392 return ret;
1395 int cmd_config(int argc, const char **argv, const char *prefix)
1397 parse_opt_subcommand_fn *subcommand = NULL;
1398 struct option subcommand_opts[] = {
1399 OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
1400 OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
1401 OPT_SUBCOMMAND("set", &subcommand, cmd_config_set),
1402 OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset),
1403 OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section),
1404 OPT_SUBCOMMAND("remove-section", &subcommand, cmd_config_remove_section),
1405 OPT_SUBCOMMAND("edit", &subcommand, cmd_config_edit),
1406 OPT_END(),
1410 * This is somewhat hacky: we first parse the command line while
1411 * keeping all args intact in order to determine whether a subcommand
1412 * has been specified. If so, we re-parse it a second time, but this
1413 * time we drop KEEP_ARGV0. This is so that we don't munge the command
1414 * line in case no subcommand was given, which would otherwise confuse
1415 * us when parsing the legacy-style modes that don't use subcommands.
1417 argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage,
1418 PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT);
1419 if (subcommand) {
1420 argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage,
1421 PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT);
1422 return subcommand(argc, argv, prefix);
1425 return cmd_config_actions(argc, argv, prefix);