6
#include "environment.h"
10
#include "parse-options.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>]"),
30
static const char *const builtin_config_list_usage[] = {
31
N_("git config list [<file-option>] [<display-option>] [--includes]"),
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>"),
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>"),
45
static const char *const builtin_config_unset_usage[] = {
46
N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
50
static const char *const builtin_config_rename_section_usage[] = {
51
N_("git config rename-section [<file-option>] <old-name> <new-name>"),
55
static const char *const builtin_config_remove_section_usage[] = {
56
N_("git config remove-section [<file-option>] <name>"),
60
static const char *const builtin_config_edit_usage[] = {
61
N_("git config edit [<file-option>]"),
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;
78
int use_global_config;
79
int use_system_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 {
120
#define CONFIG_DISPLAY_OPTIONS_INIT { \
128
#define TYPE_BOOL_OR_INT 3
130
#define TYPE_EXPIRY_DATE 5
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,
141
int new_type, *to_type;
144
*((int *) opt->value) = 0;
152
new_type = opt->defval;
154
if (!strcmp(arg, "bool"))
155
new_type = TYPE_BOOL;
156
else if (!strcmp(arg, "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;
169
die(_("unrecognized --type argument, %s"), arg);
172
to_type = opt->value;
173
if (*to_type && *to_type != new_type) {
181
error(_("only one type at a time"));
189
static void check_argc(int argc, int min, int max)
191
if (argc >= min && argc <= max)
194
error(_("wrong number of arguments, should be %d"), min);
196
error(_("wrong number of arguments, should be from %d to %d"),
201
static void show_config_origin(const struct config_display_options *opts,
202
const struct key_value_info *kvi,
205
const char term = opts->end_nul ? '\0' : '\t';
207
strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
208
strbuf_addch(buf, ':');
210
strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
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,
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,
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);
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);
247
printf("%s%c", key_, opts->term);
252
struct strbuf *items;
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);
266
strbuf_addstr(buf, key_);
267
if (!opts->omit_values) {
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_) ?
277
else if (opts->type == TYPE_BOOL_OR_INT) {
279
v = git_config_bool_or_int(key_, value_, kvi,
282
strbuf_addstr(buf, v ? "true" : "false");
284
strbuf_addf(buf, "%d", v);
285
} else if (opts->type == TYPE_BOOL_OR_STR) {
286
int v = git_parse_maybe_bool(value_);
288
strbuf_addstr(buf, value_);
290
strbuf_addstr(buf, v ? "true" : "false");
291
} else if (opts->type == TYPE_PATH) {
293
if (git_config_pathname(&v, key_, value_) < 0)
295
strbuf_addstr(buf, v);
297
} else if (opts->type == TYPE_EXPIRY_DATE) {
299
if (git_config_expiry_date(&t, key_, value_) < 0)
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)
306
strbuf_addstr(buf, v);
308
strbuf_addstr(buf, value_);
312
strbuf_setlen(buf, buf->len - 1);
315
strbuf_addch(buf, opts->term);
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;
330
unsigned get_value_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))
344
if ((data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
345
regexec(data->key_regexp, key_, 0, NULL, 0))
347
if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) &&
348
strcmp(data->value_pattern, (value_?value_:"")))
351
(data->do_not_match ^ !!regexec(data->regexp, (value_?value_:""), 0, NULL, 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++],
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,
371
.get_value_flags = get_value_flags,
377
if (get_value_flags & GET_VALUE_KEY_REGEXP) {
387
for (tl = key + strlen(key) - 1;
388
tl >= key && *tl != '.';
391
for (tl = key; *tl && *tl != '.'; 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;
402
if (git_config_parse_key(key_, &key, NULL)) {
403
ret = CONFIG_INVALID_KEY;
410
if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
411
data.value_pattern = regex_;
413
if (regex_[0] == '!') {
414
data.do_not_match = 1;
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;
427
config_with_options(collect_config, &data,
428
&opts->source, the_repository,
431
if (!values.nr && display_opts->default_value) {
432
struct key_value_info kvi = KVI_INIT;
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);
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);
457
if (data.key_regexp) {
458
regfree(data.key_regexp);
459
free(data.key_regexp);
462
regfree(data.regexp);
469
static char *normalize_value(const char *key, const char *value,
470
int type, struct key_value_info *kvi)
475
if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
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) {
490
v = git_config_bool_or_int(key, value, kvi, &is_bool);
492
return xstrfmt("%d", v);
494
return xstrdup(v ? "true" : "false");
496
if (type == TYPE_BOOL_OR_STR) {
497
int v = git_parse_maybe_bool(value);
499
return xstrdup(value);
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);
515
return xstrdup(value);
518
BUG("cannot normalize type %d", type);
521
struct get_color_config_data {
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,
531
struct get_color_config_data *data = cb;
533
if (!strcmp(var, data->get_color_slot)) {
535
config_error_nonbool(var);
536
if (color_parse(value, data->parsed_color) < 0)
538
data->get_color_found = 1;
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,
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,
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);
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,
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)
608
data.get_colorbool_found = GIT_COLOR_AUTO;
610
data.get_colorbool_found = want_color(data.get_colorbool_found);
613
printf("%s\n", data.get_colorbool_found ? "true" : "false");
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"));
628
die(_("writing config blobs is not supported"));
631
struct urlmatch_current_candidate_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,
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;
647
matched = xmalloc(sizeof(*matched));
648
strbuf_init(&matched->value, 0);
649
item->util = matched;
651
strbuf_reset(&matched->value);
656
strbuf_addstr(&matched->value, value);
657
matched->value_is_null = 0;
659
matched->value_is_null = 1;
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)
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;
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, '.');
685
*section_tail = '\0';
686
config.key = section_tail + 1;
687
display_opts.show_keys = 0;
690
display_opts.show_keys = 1;
693
config_with_options(urlmatch_config_entry, &config,
694
&opts->source, the_repository,
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,
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);
719
static char *default_user_config(void)
721
struct strbuf buf = STRBUF_INIT;
723
_("# This is Git's per-user configuration file.\n"
725
"# Please adapt and uncomment the following lines:\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,
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"));
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)
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"));
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;
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)
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")),
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);
857
die(_("error processing config file(s)"));
860
location_options_release(&location_opts);
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;
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")),
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) ||
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);
909
ret = get_urlmatch(&location_opts, &display_opts, argv[0], url);
911
ret = get_value(&location_opts, &display_opts, argv[0], value_pattern,
912
get_value_flags, flags);
914
location_options_release(&location_opts);
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")),
936
struct key_value_info default_kvi = KVI_INIT;
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>"));
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,
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);
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;
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),
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,
1006
ret = git_config_set_in_file_gently(location_opts.source.file, argv[0],
1009
location_options_release(&location_opts);
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),
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 = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1034
die(_("no such section: %s"), argv[0]);
1038
location_options_release(&location_opts);
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),
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 = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1063
die(_("no such section: %s"), argv[0]);
1067
location_options_release(&location_opts);
1071
static int show_editor(struct config_location_options *opts)
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);
1088
char *content = default_user_config();
1089
write_str_in_full(fd, content);
1093
else if (errno != EEXIST)
1094
die_errno(_("cannot create configuration file %s"), config_file);
1096
launch_editor(config_file, NULL, NULL);
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),
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);
1122
static int cmd_config_actions(int argc, const char **argv, const char *prefix)
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;
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")),
1174
char *value = NULL, *comment = NULL;
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"));
1192
case 1: actions = ACTION_GET; break;
1193
case 2: actions = ACTION_SET; break;
1194
case 3: actions = ACTION_SET_ALL; break;
1196
error(_("no action specified"));
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"));
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"));
1212
if (display_opts.default_value && !(actions & ACTION_GET)) {
1213
error(_("--default is only applicable to --get"));
1218
!(actions & (ACTION_ADD|ACTION_SET|ACTION_SET_ALL|ACTION_REPLACE_ALL))) {
1219
error(_("--comment is only applicable to add/set/replace operations"));
1224
if (flags & CONFIG_FLAGS_FIXED_VALUE) {
1225
int allowed_usage = 0;
1231
case ACTION_GET_ALL:
1233
case ACTION_GET_REGEXP:
1237
case ACTION_UNSET_ALL:
1238
allowed_usage = argc > 1 && !!argv[1];
1242
case ACTION_SET_ALL:
1244
case ACTION_REPLACE_ALL:
1245
allowed_usage = argc > 2 && !!argv[2];
1251
if (!allowed_usage) {
1252
error(_("--fixed-value only applies with 'value-pattern'"));
1257
comment = git_config_prepare_comment_string(comment_arg);
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);
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],
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,
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],
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);
1339
ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
1340
argv[0], NULL, argv[1],
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 = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1361
die(_("no such section: %s"), argv[0]);
1365
else if (actions == ACTION_REMOVE_SECTION) {
1366
check_write(&location_opts.source);
1367
check_argc(argc, 1, 1);
1368
ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1373
die(_("no such section: %s"), argv[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);
1384
color_stdout_is_tty = git_config_bool("command line", argv[1]);
1385
ret = get_colorbool(&location_opts, argv[0], argc == 2);
1389
location_options_release(&location_opts);
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),
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);
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);