Git fork
1#define USE_THE_REPOSITORY_VARIABLE
2#include "builtin.h"
3#include "abspath.h"
4#include "config.h"
5#include "color.h"
6#include "editor.h"
7#include "environment.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
18static 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] [--value=<pattern>] [--fixed-value] [--default=<default>] [--url=<url>] <name>"),
21 N_("git config set [<file-option>] [--type=<type>] [--all] [--value=<pattern>] [--fixed-value] <name> <value>"),
22 N_("git config unset [<file-option>] [--all] [--value=<pattern>] [--fixed-value] <name>"),
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
28};
29
30static const char *const builtin_config_list_usage[] = {
31 N_("git config list [<file-option>] [<display-option>] [--includes]"),
32 NULL
33};
34
35static const char *const builtin_config_get_usage[] = {
36 N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<pattern>] [--fixed-value] [--default=<default>] <name>"),
37 NULL
38};
39
40static const char *const builtin_config_set_usage[] = {
41 N_("git config set [<file-option>] [--type=<type>] [--comment=<message>] [--all] [--value=<pattern>] [--fixed-value] <name> <value>"),
42 NULL
43};
44
45static const char *const builtin_config_unset_usage[] = {
46 N_("git config unset [<file-option>] [--all] [--value=<pattern>] [--fixed-value] <name>"),
47 NULL
48};
49
50static const char *const builtin_config_rename_section_usage[] = {
51 N_("git config rename-section [<file-option>] <old-name> <new-name>"),
52 NULL
53};
54
55static const char *const builtin_config_remove_section_usage[] = {
56 N_("git config remove-section [<file-option>] <name>"),
57 NULL
58};
59
60static const char *const builtin_config_edit_usage[] = {
61 N_("git config edit [<file-option>]"),
62 NULL
63};
64
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"))
73
74struct 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;
83};
84#define CONFIG_LOCATION_OPTIONS_INIT { \
85 .respect_includes_opt = -1, \
86}
87
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)
97
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)
106
107struct 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;
119};
120#define CONFIG_DISPLAY_OPTIONS_INIT { \
121 .term = '\n', \
122 .delim = '=', \
123 .key_delim = ' ', \
124}
125
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
133
134#define OPT_CALLBACK_VALUE(s, l, v, h, i) { \
135 .type = OPTION_CALLBACK, \
136 .short_name = (s), \
137 .long_name = (l), \
138 .value = (v), \
139 .help = (h), \
140 .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, \
141 .callback = option_parse_type, \
142 .defval = (i), \
143}
144
145static int option_parse_type(const struct option *opt, const char *arg,
146 int unset)
147{
148 int new_type, *to_type;
149
150 if (unset) {
151 *((int *) opt->value) = 0;
152 return 0;
153 }
154
155 /*
156 * To support '--<type>' style flags, begin with new_type equal to
157 * opt->defval.
158 */
159 new_type = opt->defval;
160 if (!new_type) {
161 if (!strcmp(arg, "bool"))
162 new_type = TYPE_BOOL;
163 else if (!strcmp(arg, "int"))
164 new_type = TYPE_INT;
165 else if (!strcmp(arg, "bool-or-int"))
166 new_type = TYPE_BOOL_OR_INT;
167 else if (!strcmp(arg, "bool-or-str"))
168 new_type = TYPE_BOOL_OR_STR;
169 else if (!strcmp(arg, "path"))
170 new_type = TYPE_PATH;
171 else if (!strcmp(arg, "expiry-date"))
172 new_type = TYPE_EXPIRY_DATE;
173 else if (!strcmp(arg, "color"))
174 new_type = TYPE_COLOR;
175 else
176 die(_("unrecognized --type argument, %s"), arg);
177 }
178
179 to_type = opt->value;
180 if (*to_type && *to_type != new_type) {
181 /*
182 * Complain when there is a new type not equal to the old type.
183 * This allows for combinations like '--int --type=int' and
184 * '--type=int --type=int', but disallows ones like '--type=bool
185 * --int' and '--type=bool
186 * --type=int'.
187 */
188 error(_("only one type at a time"));
189 exit(129);
190 }
191 *to_type = new_type;
192
193 return 0;
194}
195
196static void check_argc(int argc, int min, int max)
197{
198 if (argc >= min && argc <= max)
199 return;
200 if (min == max)
201 error(_("wrong number of arguments, should be %d"), min);
202 else
203 error(_("wrong number of arguments, should be from %d to %d"),
204 min, max);
205 exit(129);
206}
207
208static void show_config_origin(const struct config_display_options *opts,
209 const struct key_value_info *kvi,
210 struct strbuf *buf)
211{
212 const char term = opts->end_nul ? '\0' : '\t';
213
214 strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
215 strbuf_addch(buf, ':');
216 if (opts->end_nul)
217 strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
218 else
219 quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0);
220 strbuf_addch(buf, term);
221}
222
223static void show_config_scope(const struct config_display_options *opts,
224 const struct key_value_info *kvi,
225 struct strbuf *buf)
226{
227 const char term = opts->end_nul ? '\0' : '\t';
228 const char *scope = config_scope_name(kvi->scope);
229
230 strbuf_addstr(buf, N_(scope));
231 strbuf_addch(buf, term);
232}
233
234static int show_all_config(const char *key_, const char *value_,
235 const struct config_context *ctx,
236 void *cb)
237{
238 const struct config_display_options *opts = cb;
239 const struct key_value_info *kvi = ctx->kvi;
240
241 if (opts->show_origin || opts->show_scope) {
242 struct strbuf buf = STRBUF_INIT;
243 if (opts->show_scope)
244 show_config_scope(opts, kvi, &buf);
245 if (opts->show_origin)
246 show_config_origin(opts, kvi, &buf);
247 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
248 fwrite(buf.buf, 1, buf.len, stdout);
249 strbuf_release(&buf);
250 }
251 if (!opts->omit_values && value_)
252 printf("%s%c%s%c", key_, opts->delim, value_, opts->term);
253 else
254 printf("%s%c", key_, opts->term);
255 return 0;
256}
257
258struct strbuf_list {
259 struct strbuf *items;
260 int nr;
261 int alloc;
262};
263
264static int format_config(const struct config_display_options *opts,
265 struct strbuf *buf, const char *key_,
266 const char *value_, const struct key_value_info *kvi)
267{
268 if (opts->show_scope)
269 show_config_scope(opts, kvi, buf);
270 if (opts->show_origin)
271 show_config_origin(opts, kvi, buf);
272 if (opts->show_keys)
273 strbuf_addstr(buf, key_);
274 if (!opts->omit_values) {
275 if (opts->show_keys)
276 strbuf_addch(buf, opts->key_delim);
277
278 if (opts->type == TYPE_INT)
279 strbuf_addf(buf, "%"PRId64,
280 git_config_int64(key_, value_ ? value_ : "", kvi));
281 else if (opts->type == TYPE_BOOL)
282 strbuf_addstr(buf, git_config_bool(key_, value_) ?
283 "true" : "false");
284 else if (opts->type == TYPE_BOOL_OR_INT) {
285 int is_bool, v;
286 v = git_config_bool_or_int(key_, value_, kvi,
287 &is_bool);
288 if (is_bool)
289 strbuf_addstr(buf, v ? "true" : "false");
290 else
291 strbuf_addf(buf, "%d", v);
292 } else if (opts->type == TYPE_BOOL_OR_STR) {
293 int v = git_parse_maybe_bool(value_);
294 if (v < 0)
295 strbuf_addstr(buf, value_);
296 else
297 strbuf_addstr(buf, v ? "true" : "false");
298 } else if (opts->type == TYPE_PATH) {
299 char *v;
300 if (git_config_pathname(&v, key_, value_) < 0)
301 return -1;
302 strbuf_addstr(buf, v);
303 free((char *)v);
304 } else if (opts->type == TYPE_EXPIRY_DATE) {
305 timestamp_t t;
306 if (git_config_expiry_date(&t, key_, value_) < 0)
307 return -1;
308 strbuf_addf(buf, "%"PRItime, t);
309 } else if (opts->type == TYPE_COLOR) {
310 char v[COLOR_MAXLEN];
311 if (git_config_color(v, key_, value_) < 0)
312 return -1;
313 strbuf_addstr(buf, v);
314 } else if (value_) {
315 strbuf_addstr(buf, value_);
316 } else {
317 /* Just show the key name; back out delimiter */
318 if (opts->show_keys)
319 strbuf_setlen(buf, buf->len - 1);
320 }
321 }
322 strbuf_addch(buf, opts->term);
323 return 0;
324}
325
326#define GET_VALUE_ALL (1 << 0)
327#define GET_VALUE_KEY_REGEXP (1 << 1)
328
329struct collect_config_data {
330 const struct config_display_options *display_opts;
331 struct strbuf_list *values;
332 const char *value_pattern;
333 const char *key;
334 regex_t *regexp;
335 regex_t *key_regexp;
336 int do_not_match;
337 unsigned get_value_flags;
338 unsigned flags;
339};
340
341static int collect_config(const char *key_, const char *value_,
342 const struct config_context *ctx, void *cb)
343{
344 struct collect_config_data *data = cb;
345 struct strbuf_list *values = data->values;
346 const struct key_value_info *kvi = ctx->kvi;
347
348 if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
349 strcmp(key_, data->key))
350 return 0;
351 if ((data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
352 regexec(data->key_regexp, key_, 0, NULL, 0))
353 return 0;
354 if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) &&
355 strcmp(data->value_pattern, (value_?value_:"")))
356 return 0;
357 if (data->regexp &&
358 (data->do_not_match ^ !!regexec(data->regexp, (value_?value_:""), 0, NULL, 0)))
359 return 0;
360
361 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
362 strbuf_init(&values->items[values->nr], 0);
363
364 return format_config(data->display_opts, &values->items[values->nr++],
365 key_, value_, kvi);
366}
367
368static int get_value(const struct config_location_options *opts,
369 const struct config_display_options *display_opts,
370 const char *key_, const char *regex_,
371 unsigned get_value_flags, unsigned flags)
372{
373 int ret = CONFIG_GENERIC_ERROR;
374 struct strbuf_list values = {NULL};
375 struct collect_config_data data = {
376 .display_opts = display_opts,
377 .values = &values,
378 .get_value_flags = get_value_flags,
379 .flags = flags,
380 };
381 char *key = NULL;
382 int i;
383
384 if (get_value_flags & GET_VALUE_KEY_REGEXP) {
385 char *tl;
386
387 /*
388 * NEEDSWORK: this naive pattern lowercasing obviously does not
389 * work for more complex patterns like "^[^.]*Foo.*bar".
390 * Perhaps we should deprecate this altogether someday.
391 */
392
393 key = xstrdup(key_);
394 for (tl = key + strlen(key) - 1;
395 tl >= key && *tl != '.';
396 tl--)
397 *tl = tolower(*tl);
398 for (tl = key; *tl && *tl != '.'; tl++)
399 *tl = tolower(*tl);
400
401 data.key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
402 if (regcomp(data.key_regexp, key, REG_EXTENDED)) {
403 error(_("invalid key pattern: %s"), key_);
404 FREE_AND_NULL(data.key_regexp);
405 ret = CONFIG_INVALID_PATTERN;
406 goto free_strings;
407 }
408 } else {
409 if (git_config_parse_key(key_, &key, NULL)) {
410 ret = CONFIG_INVALID_KEY;
411 goto free_strings;
412 }
413
414 data.key = key;
415 }
416
417 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
418 data.value_pattern = regex_;
419 else if (regex_) {
420 if (regex_[0] == '!') {
421 data.do_not_match = 1;
422 regex_++;
423 }
424
425 data.regexp = (regex_t*)xmalloc(sizeof(regex_t));
426 if (regcomp(data.regexp, regex_, REG_EXTENDED)) {
427 error(_("invalid pattern: %s"), regex_);
428 FREE_AND_NULL(data.regexp);
429 ret = CONFIG_INVALID_PATTERN;
430 goto free_strings;
431 }
432 }
433
434 config_with_options(collect_config, &data,
435 &opts->source, the_repository,
436 &opts->options);
437
438 if (!values.nr && display_opts->default_value) {
439 struct key_value_info kvi = KVI_INIT;
440 struct strbuf *item;
441
442 kvi_from_param(&kvi);
443 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
444 item = &values.items[values.nr++];
445 strbuf_init(item, 0);
446 if (format_config(display_opts, item, key_,
447 display_opts->default_value, &kvi) < 0)
448 die(_("failed to format default config value: %s"),
449 display_opts->default_value);
450 }
451
452 ret = !values.nr;
453
454 for (i = 0; i < values.nr; i++) {
455 struct strbuf *buf = values.items + i;
456 if ((get_value_flags & GET_VALUE_ALL) || i == values.nr - 1)
457 fwrite(buf->buf, 1, buf->len, stdout);
458 strbuf_release(buf);
459 }
460 free(values.items);
461
462free_strings:
463 free(key);
464 if (data.key_regexp) {
465 regfree(data.key_regexp);
466 free(data.key_regexp);
467 }
468 if (data.regexp) {
469 regfree(data.regexp);
470 free(data.regexp);
471 }
472
473 return ret;
474}
475
476static char *normalize_value(const char *key, const char *value,
477 int type, struct key_value_info *kvi)
478{
479 if (!value)
480 return NULL;
481
482 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
483 /*
484 * We don't do normalization for TYPE_PATH here: If
485 * the path is like ~/foobar/, we prefer to store
486 * "~/foobar/" in the config file, and to expand the ~
487 * when retrieving the value.
488 * Also don't do normalization for expiry dates.
489 */
490 return xstrdup(value);
491 if (type == TYPE_INT)
492 return xstrfmt("%"PRId64, git_config_int64(key, value, kvi));
493 if (type == TYPE_BOOL)
494 return xstrdup(git_config_bool(key, value) ? "true" : "false");
495 if (type == TYPE_BOOL_OR_INT) {
496 int is_bool, v;
497 v = git_config_bool_or_int(key, value, kvi, &is_bool);
498 if (!is_bool)
499 return xstrfmt("%d", v);
500 else
501 return xstrdup(v ? "true" : "false");
502 }
503 if (type == TYPE_BOOL_OR_STR) {
504 int v = git_parse_maybe_bool(value);
505 if (v < 0)
506 return xstrdup(value);
507 else
508 return xstrdup(v ? "true" : "false");
509 }
510 if (type == TYPE_COLOR) {
511 char v[COLOR_MAXLEN];
512 if (git_config_color(v, key, value))
513 die(_("cannot parse color '%s'"), value);
514
515 /*
516 * The contents of `v` now contain an ANSI escape
517 * sequence, not suitable for including within a
518 * configuration file. Treat the above as a
519 * "sanity-check", and return the given value, which we
520 * know is representable as valid color code.
521 */
522 return xstrdup(value);
523 }
524
525 BUG("cannot normalize type %d", type);
526}
527
528struct get_color_config_data {
529 int get_color_found;
530 const char *get_color_slot;
531 char parsed_color[COLOR_MAXLEN];
532};
533
534static int git_get_color_config(const char *var, const char *value,
535 const struct config_context *ctx UNUSED,
536 void *cb)
537{
538 struct get_color_config_data *data = cb;
539
540 if (!strcmp(var, data->get_color_slot)) {
541 if (!value)
542 config_error_nonbool(var);
543 if (color_parse(value, data->parsed_color) < 0)
544 return -1;
545 data->get_color_found = 1;
546 }
547 return 0;
548}
549
550static int get_color(const struct config_location_options *opts,
551 const char *var, const char *def_color)
552{
553 struct get_color_config_data data = {
554 .get_color_slot = var,
555 .parsed_color[0] = '\0',
556 };
557 int ret;
558
559 config_with_options(git_get_color_config, &data,
560 &opts->source, the_repository,
561 &opts->options);
562
563 if (!data.get_color_found && def_color) {
564 if (color_parse(def_color, data.parsed_color) < 0) {
565 ret = error(_("unable to parse default color value"));
566 goto out;
567 }
568 }
569
570 ret = 0;
571
572out:
573 fputs(data.parsed_color, stdout);
574 return ret;
575}
576
577struct get_colorbool_config_data {
578 enum git_colorbool get_colorbool_found;
579 enum git_colorbool get_diff_color_found;
580 enum git_colorbool get_color_ui_found;
581 const char *get_colorbool_slot;
582};
583
584static int git_get_colorbool_config(const char *var, const char *value,
585 const struct config_context *ctx UNUSED,
586 void *cb)
587{
588 struct get_colorbool_config_data *data = cb;
589
590 if (!strcmp(var, data->get_colorbool_slot))
591 data->get_colorbool_found = git_config_colorbool(var, value);
592 else if (!strcmp(var, "diff.color"))
593 data->get_diff_color_found = git_config_colorbool(var, value);
594 else if (!strcmp(var, "color.ui"))
595 data->get_color_ui_found = git_config_colorbool(var, value);
596 return 0;
597}
598
599static int get_colorbool(const struct config_location_options *opts,
600 const char *var, int print)
601{
602 struct get_colorbool_config_data data = {
603 .get_colorbool_slot = var,
604 .get_colorbool_found = GIT_COLOR_UNKNOWN,
605 .get_diff_color_found = GIT_COLOR_UNKNOWN,
606 .get_color_ui_found = GIT_COLOR_UNKNOWN,
607 };
608 bool result;
609
610 config_with_options(git_get_colorbool_config, &data,
611 &opts->source, the_repository,
612 &opts->options);
613
614 if (data.get_colorbool_found == GIT_COLOR_UNKNOWN) {
615 if (!strcmp(data.get_colorbool_slot, "color.diff"))
616 data.get_colorbool_found = data.get_diff_color_found;
617 if (data.get_colorbool_found == GIT_COLOR_UNKNOWN)
618 data.get_colorbool_found = data.get_color_ui_found;
619 }
620
621 if (data.get_colorbool_found == GIT_COLOR_UNKNOWN)
622 /* default value if none found in config */
623 data.get_colorbool_found = GIT_COLOR_AUTO;
624
625 result = want_color(data.get_colorbool_found);
626
627 if (print) {
628 printf("%s\n", result ? "true" : "false");
629 return 0;
630 } else
631 return result ? 0 : 1;
632}
633
634static void check_write(const struct git_config_source *source)
635{
636 if (!source->file && !startup_info->have_repository)
637 die(_("not in a git directory"));
638
639 if (source->use_stdin)
640 die(_("writing to stdin is not supported"));
641
642 if (source->blob)
643 die(_("writing config blobs is not supported"));
644}
645
646struct urlmatch_current_candidate_value {
647 char value_is_null;
648 struct strbuf value;
649 struct key_value_info kvi;
650};
651
652static int urlmatch_collect_fn(const char *var, const char *value,
653 const struct config_context *ctx,
654 void *cb)
655{
656 struct string_list *values = cb;
657 struct string_list_item *item = string_list_insert(values, var);
658 struct urlmatch_current_candidate_value *matched = item->util;
659 const struct key_value_info *kvi = ctx->kvi;
660
661 if (!matched) {
662 matched = xmalloc(sizeof(*matched));
663 strbuf_init(&matched->value, 0);
664 item->util = matched;
665 } else {
666 strbuf_reset(&matched->value);
667 }
668 matched->kvi = *kvi;
669
670 if (value) {
671 strbuf_addstr(&matched->value, value);
672 matched->value_is_null = 0;
673 } else {
674 matched->value_is_null = 1;
675 }
676 return 0;
677}
678
679static int get_urlmatch(const struct config_location_options *opts,
680 const struct config_display_options *_display_opts,
681 const char *var, const char *url)
682{
683 int ret;
684 char *section_tail;
685 struct config_display_options display_opts = *_display_opts;
686 struct string_list_item *item;
687 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
688 struct string_list values = STRING_LIST_INIT_DUP;
689
690 config.collect_fn = urlmatch_collect_fn;
691 config.cascade_fn = NULL;
692 config.cb = &values;
693
694 if (!url_normalize(url, &config.url))
695 die("%s", config.url.err);
696
697 config.section = xstrdup_tolower(var);
698 section_tail = strchr(config.section, '.');
699 if (section_tail) {
700 *section_tail = '\0';
701 config.key = section_tail + 1;
702 display_opts.show_keys = 0;
703 } else {
704 config.key = NULL;
705 display_opts.show_keys = 1;
706 }
707
708 config_with_options(urlmatch_config_entry, &config,
709 &opts->source, the_repository,
710 &opts->options);
711
712 ret = !values.nr;
713
714 for_each_string_list_item(item, &values) {
715 struct urlmatch_current_candidate_value *matched = item->util;
716 struct strbuf buf = STRBUF_INIT;
717
718 format_config(&display_opts, &buf, item->string,
719 matched->value_is_null ? NULL : matched->value.buf,
720 &matched->kvi);
721 fwrite(buf.buf, 1, buf.len, stdout);
722 strbuf_release(&buf);
723
724 strbuf_release(&matched->value);
725 }
726 urlmatch_config_release(&config);
727 string_list_clear(&values, 1);
728 free(config.url.url);
729
730 free((void *)config.section);
731 return ret;
732}
733
734static char *default_user_config(void)
735{
736 struct strbuf buf = STRBUF_INIT;
737 strbuf_addf(&buf,
738 _("# This is Git's per-user configuration file.\n"
739 "[user]\n"
740 "# Please adapt and uncomment the following lines:\n"
741 "# name = %s\n"
742 "# email = %s\n"),
743 ident_default_name(),
744 ident_default_email());
745 return strbuf_detach(&buf, NULL);
746}
747
748static void location_options_init(struct config_location_options *opts,
749 const char *prefix)
750{
751 if (!opts->source.file)
752 opts->source.file = opts->file_to_free =
753 xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
754
755 if (opts->use_global_config + opts->use_system_config +
756 opts->use_local_config + opts->use_worktree_config +
757 !!opts->source.file + !!opts->source.blob > 1) {
758 error(_("only one config file at a time"));
759 exit(129);
760 }
761
762 if (!startup_info->have_repository) {
763 if (opts->use_local_config)
764 die(_("--local can only be used inside a git repository"));
765 if (opts->source.blob)
766 die(_("--blob can only be used inside a git repository"));
767 if (opts->use_worktree_config)
768 die(_("--worktree can only be used inside a git repository"));
769 }
770
771 if (opts->source.file &&
772 !strcmp(opts->source.file, "-")) {
773 opts->source.file = NULL;
774 opts->source.use_stdin = 1;
775 opts->source.scope = CONFIG_SCOPE_COMMAND;
776 }
777
778 if (opts->use_global_config) {
779 opts->source.file = opts->file_to_free = git_global_config();
780 if (!opts->source.file)
781 /*
782 * It is unknown if HOME/.gitconfig exists, so
783 * we do not know if we should write to XDG
784 * location; error out even if XDG_CONFIG_HOME
785 * is set and points at a sane location.
786 */
787 die(_("$HOME not set"));
788 opts->source.scope = CONFIG_SCOPE_GLOBAL;
789 } else if (opts->use_system_config) {
790 opts->source.file = opts->file_to_free = git_system_config();
791 opts->source.scope = CONFIG_SCOPE_SYSTEM;
792 } else if (opts->use_local_config) {
793 opts->source.file = opts->file_to_free = repo_git_path(the_repository, "config");
794 opts->source.scope = CONFIG_SCOPE_LOCAL;
795 } else if (opts->use_worktree_config) {
796 struct worktree **worktrees = get_worktrees();
797 if (the_repository->repository_format_worktree_config)
798 opts->source.file = opts->file_to_free =
799 repo_git_path(the_repository, "config.worktree");
800 else if (worktrees[0] && worktrees[1])
801 die(_("--worktree cannot be used with multiple "
802 "working trees unless the config\n"
803 "extension worktreeConfig is enabled. "
804 "Please read \"CONFIGURATION FILE\"\n"
805 "section in \"git help worktree\" for details"));
806 else
807 opts->source.file = opts->file_to_free =
808 repo_git_path(the_repository, "config");
809 opts->source.scope = CONFIG_SCOPE_LOCAL;
810 free_worktrees(worktrees);
811 } else if (opts->source.file) {
812 if (!is_absolute_path(opts->source.file) && prefix)
813 opts->source.file = opts->file_to_free =
814 prefix_filename(prefix, opts->source.file);
815 opts->source.scope = CONFIG_SCOPE_COMMAND;
816 } else if (opts->source.blob) {
817 opts->source.scope = CONFIG_SCOPE_COMMAND;
818 }
819
820 if (opts->respect_includes_opt == -1)
821 opts->options.respect_includes = !opts->source.file;
822 else
823 opts->options.respect_includes = opts->respect_includes_opt;
824 if (startup_info->have_repository) {
825 opts->options.commondir = repo_get_common_dir(the_repository);
826 opts->options.git_dir = repo_get_git_dir(the_repository);
827 }
828}
829
830static void location_options_release(struct config_location_options *opts)
831{
832 free(opts->file_to_free);
833}
834
835static void display_options_init(struct config_display_options *opts)
836{
837 if (opts->end_nul) {
838 opts->term = '\0';
839 opts->delim = '\n';
840 opts->key_delim = '\n';
841 }
842}
843
844static int cmd_config_list(int argc, const char **argv, const char *prefix,
845 struct repository *repo UNUSED)
846{
847 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
848 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
849 struct option opts[] = {
850 CONFIG_LOCATION_OPTIONS(location_opts),
851 CONFIG_DISPLAY_OPTIONS(display_opts),
852 OPT_GROUP(N_("Other")),
853 OPT_BOOL(0, "includes", &location_opts.respect_includes_opt,
854 N_("respect include directives on lookup")),
855 OPT_END(),
856 };
857
858 argc = parse_options(argc, argv, prefix, opts, builtin_config_list_usage, 0);
859 check_argc(argc, 0, 0);
860
861 location_options_init(&location_opts, prefix);
862 display_options_init(&display_opts);
863
864 setup_auto_pager("config", 1);
865
866 if (config_with_options(show_all_config, &display_opts,
867 &location_opts.source, the_repository,
868 &location_opts.options) < 0) {
869 if (location_opts.source.file)
870 die_errno(_("unable to read config file '%s'"),
871 location_opts.source.file);
872 else
873 die(_("error processing config file(s)"));
874 }
875
876 location_options_release(&location_opts);
877 return 0;
878}
879
880static int cmd_config_get(int argc, const char **argv, const char *prefix,
881 struct repository *repo UNUSED)
882{
883 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
884 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
885 const char *value_pattern = NULL, *url = NULL;
886 int flags = 0;
887 unsigned get_value_flags = 0;
888 struct option opts[] = {
889 CONFIG_LOCATION_OPTIONS(location_opts),
890 OPT_GROUP(N_("Filter options")),
891 OPT_BIT(0, "all", &get_value_flags, N_("return all values for multi-valued config options"), GET_VALUE_ALL),
892 OPT_BIT(0, "regexp", &get_value_flags, N_("interpret the name as a regular expression"), GET_VALUE_KEY_REGEXP),
893 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
894 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
895 OPT_STRING(0, "url", &url, N_("URL"), N_("show config matching the given URL")),
896 CONFIG_DISPLAY_OPTIONS(display_opts),
897 OPT_GROUP(N_("Other")),
898 OPT_BOOL(0, "includes", &location_opts.respect_includes_opt,
899 N_("respect include directives on lookup")),
900 OPT_STRING(0, "default", &display_opts.default_value,
901 N_("value"), N_("use default value when missing entry")),
902 OPT_END(),
903 };
904 int ret;
905
906 argc = parse_options(argc, argv, prefix, opts, builtin_config_get_usage,
907 PARSE_OPT_STOP_AT_NON_OPTION);
908 check_argc(argc, 1, 1);
909
910 if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
911 die(_("--fixed-value only applies with 'value-pattern'"));
912 if (display_opts.default_value &&
913 ((get_value_flags & GET_VALUE_ALL) || url))
914 die(_("--default= cannot be used with --all or --url="));
915 if (url && ((get_value_flags & GET_VALUE_ALL) ||
916 (get_value_flags & GET_VALUE_KEY_REGEXP) ||
917 value_pattern))
918 die(_("--url= cannot be used with --all, --regexp or --value"));
919
920 location_options_init(&location_opts, prefix);
921 display_options_init(&display_opts);
922
923 if (display_opts.type != TYPE_COLOR)
924 setup_auto_pager("config", 1);
925
926 if (url)
927 ret = get_urlmatch(&location_opts, &display_opts, argv[0], url);
928 else if (display_opts.type == TYPE_COLOR && !strlen(argv[0]) && display_opts.default_value)
929 ret = get_color(&location_opts, "", display_opts.default_value);
930 else
931 ret = get_value(&location_opts, &display_opts, argv[0], value_pattern,
932 get_value_flags, flags);
933
934 location_options_release(&location_opts);
935 return ret;
936}
937
938static int cmd_config_set(int argc, const char **argv, const char *prefix,
939 struct repository *repo UNUSED)
940{
941 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
942 const char *value_pattern = NULL, *comment_arg = NULL;
943 char *comment = NULL;
944 int flags = 0, append = 0, type = 0;
945 struct option opts[] = {
946 CONFIG_LOCATION_OPTIONS(location_opts),
947 CONFIG_TYPE_OPTIONS(type),
948 OPT_GROUP(N_("Filter")),
949 OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE),
950 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
951 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
952 OPT_GROUP(N_("Other")),
953 OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
954 OPT_BOOL(0, "append", &append, N_("add a new line without altering any existing values")),
955 OPT_END(),
956 };
957 struct key_value_info default_kvi = KVI_INIT;
958 char *value;
959 int ret;
960
961 argc = parse_options(argc, argv, prefix, opts, builtin_config_set_usage,
962 PARSE_OPT_STOP_AT_NON_OPTION);
963 check_argc(argc, 2, 2);
964
965 if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
966 die(_("--fixed-value only applies with --value=<pattern>"));
967 if (append && value_pattern)
968 die(_("--append cannot be used with --value=<pattern>"));
969 if (append)
970 value_pattern = CONFIG_REGEX_NONE;
971
972 comment = git_config_prepare_comment_string(comment_arg);
973
974 location_options_init(&location_opts, prefix);
975 check_write(&location_opts.source);
976
977 value = normalize_value(argv[0], argv[1], type, &default_kvi);
978
979 if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) {
980 ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file,
981 argv[0], value, value_pattern,
982 comment, flags);
983 } else {
984 ret = repo_config_set_in_file_gently(the_repository, location_opts.source.file,
985 argv[0], comment, value);
986 if (ret == CONFIG_NOTHING_SET)
987 error(_("cannot overwrite multiple values with a single value\n"
988 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
989 }
990
991 location_options_release(&location_opts);
992 free(comment);
993 free(value);
994 return ret;
995}
996
997static int cmd_config_unset(int argc, const char **argv, const char *prefix,
998 struct repository *repo UNUSED)
999{
1000 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
1001 const char *value_pattern = NULL;
1002 int flags = 0;
1003 struct option opts[] = {
1004 CONFIG_LOCATION_OPTIONS(location_opts),
1005 OPT_GROUP(N_("Filter")),
1006 OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE),
1007 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
1008 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
1009 OPT_END(),
1010 };
1011 int ret;
1012
1013 argc = parse_options(argc, argv, prefix, opts, builtin_config_unset_usage,
1014 PARSE_OPT_STOP_AT_NON_OPTION);
1015 check_argc(argc, 1, 1);
1016
1017 if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
1018 die(_("--fixed-value only applies with 'value-pattern'"));
1019
1020 location_options_init(&location_opts, prefix);
1021 check_write(&location_opts.source);
1022
1023 if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern)
1024 ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file,
1025 argv[0], NULL, value_pattern,
1026 NULL, flags);
1027 else
1028 ret = repo_config_set_in_file_gently(the_repository, location_opts.source.file, argv[0],
1029 NULL, NULL);
1030
1031 location_options_release(&location_opts);
1032 return ret;
1033}
1034
1035static int cmd_config_rename_section(int argc, const char **argv, const char *prefix,
1036 struct repository *repo UNUSED)
1037{
1038 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
1039 struct option opts[] = {
1040 CONFIG_LOCATION_OPTIONS(location_opts),
1041 OPT_END(),
1042 };
1043 int ret;
1044
1045 argc = parse_options(argc, argv, prefix, opts, builtin_config_rename_section_usage,
1046 PARSE_OPT_STOP_AT_NON_OPTION);
1047 check_argc(argc, 2, 2);
1048
1049 location_options_init(&location_opts, prefix);
1050 check_write(&location_opts.source);
1051
1052 ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1053 argv[0], argv[1]);
1054 if (ret < 0)
1055 goto out;
1056 else if (!ret)
1057 die(_("no such section: %s"), argv[0]);
1058 ret = 0;
1059
1060out:
1061 location_options_release(&location_opts);
1062 return ret;
1063}
1064
1065static int cmd_config_remove_section(int argc, const char **argv, const char *prefix,
1066 struct repository *repo UNUSED)
1067{
1068 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
1069 struct option opts[] = {
1070 CONFIG_LOCATION_OPTIONS(location_opts),
1071 OPT_END(),
1072 };
1073 int ret;
1074
1075 argc = parse_options(argc, argv, prefix, opts, builtin_config_remove_section_usage,
1076 PARSE_OPT_STOP_AT_NON_OPTION);
1077 check_argc(argc, 1, 1);
1078
1079 location_options_init(&location_opts, prefix);
1080 check_write(&location_opts.source);
1081
1082 ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1083 argv[0], NULL);
1084 if (ret < 0)
1085 goto out;
1086 else if (!ret)
1087 die(_("no such section: %s"), argv[0]);
1088 ret = 0;
1089
1090out:
1091 location_options_release(&location_opts);
1092 return ret;
1093}
1094
1095static int show_editor(struct config_location_options *opts)
1096{
1097 char *config_file;
1098
1099 if (!opts->source.file && !startup_info->have_repository)
1100 die(_("not in a git directory"));
1101 if (opts->source.use_stdin)
1102 die(_("editing stdin is not supported"));
1103 if (opts->source.blob)
1104 die(_("editing blobs is not supported"));
1105 repo_config(the_repository, git_default_config, NULL);
1106 config_file = opts->source.file ?
1107 xstrdup(opts->source.file) :
1108 repo_git_path(the_repository, "config");
1109 if (opts->use_global_config) {
1110 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
1111 if (fd >= 0) {
1112 char *content = default_user_config();
1113 write_str_in_full(fd, content);
1114 free(content);
1115 close(fd);
1116 }
1117 else if (errno != EEXIST)
1118 die_errno(_("cannot create configuration file %s"), config_file);
1119 }
1120 launch_editor(config_file, NULL, NULL);
1121 free(config_file);
1122
1123 return 0;
1124}
1125
1126static int cmd_config_edit(int argc, const char **argv, const char *prefix,
1127 struct repository *repo UNUSED)
1128{
1129 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
1130 struct option opts[] = {
1131 CONFIG_LOCATION_OPTIONS(location_opts),
1132 OPT_END(),
1133 };
1134 int ret;
1135
1136 argc = parse_options(argc, argv, prefix, opts, builtin_config_edit_usage, 0);
1137 check_argc(argc, 0, 0);
1138
1139 location_options_init(&location_opts, prefix);
1140 check_write(&location_opts.source);
1141
1142 ret = show_editor(&location_opts);
1143 location_options_release(&location_opts);
1144 return ret;
1145}
1146
1147static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1148{
1149 enum {
1150 ACTION_GET = (1<<0),
1151 ACTION_GET_ALL = (1<<1),
1152 ACTION_GET_REGEXP = (1<<2),
1153 ACTION_REPLACE_ALL = (1<<3),
1154 ACTION_ADD = (1<<4),
1155 ACTION_UNSET = (1<<5),
1156 ACTION_UNSET_ALL = (1<<6),
1157 ACTION_RENAME_SECTION = (1<<7),
1158 ACTION_REMOVE_SECTION = (1<<8),
1159 ACTION_LIST = (1<<9),
1160 ACTION_EDIT = (1<<10),
1161 ACTION_SET = (1<<11),
1162 ACTION_SET_ALL = (1<<12),
1163 ACTION_GET_COLOR = (1<<13),
1164 ACTION_GET_COLORBOOL = (1<<14),
1165 ACTION_GET_URLMATCH = (1<<15),
1166 };
1167 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
1168 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
1169 const char *comment_arg = NULL;
1170 int actions = 0;
1171 unsigned flags = 0;
1172 struct option opts[] = {
1173 CONFIG_LOCATION_OPTIONS(location_opts),
1174 OPT_GROUP(N_("Action")),
1175 OPT_CMDMODE(0, "get", &actions, N_("get value: name [<value-pattern>]"), ACTION_GET),
1176 OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL),
1177 OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP),
1178 OPT_CMDMODE(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
1179 OPT_CMDMODE(0, "replace-all", &actions, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL),
1180 OPT_CMDMODE(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
1181 OPT_CMDMODE(0, "unset", &actions, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET),
1182 OPT_CMDMODE(0, "unset-all", &actions, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL),
1183 OPT_CMDMODE(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
1184 OPT_CMDMODE(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
1185 OPT_CMDMODE('l', "list", &actions, N_("list all"), ACTION_LIST),
1186 OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
1187 OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
1188 OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
1189 CONFIG_DISPLAY_OPTIONS(display_opts),
1190 OPT_GROUP(N_("Other")),
1191 OPT_STRING(0, "default", &display_opts.default_value,
1192 N_("value"), N_("with --get, use default value when missing entry")),
1193 OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
1194 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
1195 OPT_BOOL(0, "includes", &location_opts.respect_includes_opt,
1196 N_("respect include directives on lookup")),
1197 OPT_END(),
1198 };
1199 char *value = NULL, *comment = NULL;
1200 int ret = 0;
1201 struct key_value_info default_kvi = KVI_INIT;
1202
1203 argc = parse_options(argc, argv, prefix, opts,
1204 builtin_config_usage,
1205 PARSE_OPT_STOP_AT_NON_OPTION);
1206
1207 location_options_init(&location_opts, prefix);
1208 display_options_init(&display_opts);
1209
1210 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && display_opts.type) {
1211 error(_("--get-color and variable type are incoherent"));
1212 exit(129);
1213 }
1214
1215 if (actions == 0)
1216 switch (argc) {
1217 case 1: actions = ACTION_GET; break;
1218 case 2: actions = ACTION_SET; break;
1219 case 3: actions = ACTION_SET_ALL; break;
1220 default:
1221 error(_("no action specified"));
1222 exit(129);
1223 }
1224 if (display_opts.omit_values &&
1225 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
1226 error(_("--name-only is only applicable to --list or --get-regexp"));
1227 exit(129);
1228 }
1229
1230 if (display_opts.show_origin && !(actions &
1231 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
1232 error(_("--show-origin is only applicable to --get, --get-all, "
1233 "--get-regexp, and --list"));
1234 exit(129);
1235 }
1236
1237 if (display_opts.default_value && !(actions & ACTION_GET)) {
1238 error(_("--default is only applicable to --get"));
1239 exit(129);
1240 }
1241
1242 if (comment_arg &&
1243 !(actions & (ACTION_ADD|ACTION_SET|ACTION_SET_ALL|ACTION_REPLACE_ALL))) {
1244 error(_("--comment is only applicable to add/set/replace operations"));
1245 exit(129);
1246 }
1247
1248 /* check usage of --fixed-value */
1249 if (flags & CONFIG_FLAGS_FIXED_VALUE) {
1250 int allowed_usage = 0;
1251
1252 switch (actions) {
1253 /* git config --get <name> <value-pattern> */
1254 case ACTION_GET:
1255 /* git config --get-all <name> <value-pattern> */
1256 case ACTION_GET_ALL:
1257 /* git config --get-regexp <name-pattern> <value-pattern> */
1258 case ACTION_GET_REGEXP:
1259 /* git config --unset <name> <value-pattern> */
1260 case ACTION_UNSET:
1261 /* git config --unset-all <name> <value-pattern> */
1262 case ACTION_UNSET_ALL:
1263 allowed_usage = argc > 1 && !!argv[1];
1264 break;
1265
1266 /* git config <name> <value> <value-pattern> */
1267 case ACTION_SET_ALL:
1268 /* git config --replace-all <name> <value> <value-pattern> */
1269 case ACTION_REPLACE_ALL:
1270 allowed_usage = argc > 2 && !!argv[2];
1271 break;
1272
1273 /* other options don't allow --fixed-value */
1274 }
1275
1276 if (!allowed_usage) {
1277 error(_("--fixed-value only applies with 'value-pattern'"));
1278 exit(129);
1279 }
1280 }
1281
1282 comment = git_config_prepare_comment_string(comment_arg);
1283
1284 /*
1285 * The following actions may produce more than one line of output and
1286 * should therefore be paged.
1287 */
1288 if (actions & (ACTION_LIST | ACTION_GET_ALL | ACTION_GET_REGEXP | ACTION_GET_URLMATCH))
1289 setup_auto_pager("config", 1);
1290
1291 if (actions == ACTION_LIST) {
1292 check_argc(argc, 0, 0);
1293 if (config_with_options(show_all_config, &display_opts,
1294 &location_opts.source, the_repository,
1295 &location_opts.options) < 0) {
1296 if (location_opts.source.file)
1297 die_errno(_("unable to read config file '%s'"),
1298 location_opts.source.file);
1299 else
1300 die(_("error processing config file(s)"));
1301 }
1302 }
1303 else if (actions == ACTION_EDIT) {
1304 ret = show_editor(&location_opts);
1305 }
1306 else if (actions == ACTION_SET) {
1307 check_write(&location_opts.source);
1308 check_argc(argc, 2, 2);
1309 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1310 ret = repo_config_set_in_file_gently(the_repository, location_opts.source.file, argv[0], comment, value);
1311 if (ret == CONFIG_NOTHING_SET)
1312 error(_("cannot overwrite multiple values with a single value\n"
1313 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
1314 }
1315 else if (actions == ACTION_SET_ALL) {
1316 check_write(&location_opts.source);
1317 check_argc(argc, 2, 3);
1318 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1319 ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file,
1320 argv[0], value, argv[2],
1321 comment, flags);
1322 }
1323 else if (actions == ACTION_ADD) {
1324 check_write(&location_opts.source);
1325 check_argc(argc, 2, 2);
1326 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1327 ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file,
1328 argv[0], value,
1329 CONFIG_REGEX_NONE,
1330 comment, flags);
1331 }
1332 else if (actions == ACTION_REPLACE_ALL) {
1333 check_write(&location_opts.source);
1334 check_argc(argc, 2, 3);
1335 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1336 ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file,
1337 argv[0], value, argv[2],
1338 comment, flags | CONFIG_FLAGS_MULTI_REPLACE);
1339 }
1340 else if (actions == ACTION_GET) {
1341 check_argc(argc, 1, 2);
1342 ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1343 0, flags);
1344 }
1345 else if (actions == ACTION_GET_ALL) {
1346 check_argc(argc, 1, 2);
1347 ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1348 GET_VALUE_ALL, flags);
1349 }
1350 else if (actions == ACTION_GET_REGEXP) {
1351 display_opts.show_keys = 1;
1352 check_argc(argc, 1, 2);
1353 ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1354 GET_VALUE_ALL|GET_VALUE_KEY_REGEXP, flags);
1355 }
1356 else if (actions == ACTION_GET_URLMATCH) {
1357 check_argc(argc, 2, 2);
1358 ret = get_urlmatch(&location_opts, &display_opts, argv[0], argv[1]);
1359 }
1360 else if (actions == ACTION_UNSET) {
1361 check_write(&location_opts.source);
1362 check_argc(argc, 1, 2);
1363 if (argc == 2)
1364 ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file,
1365 argv[0], NULL, argv[1],
1366 NULL, flags);
1367 else
1368 ret = repo_config_set_in_file_gently(the_repository, location_opts.source.file,
1369 argv[0], NULL, NULL);
1370 }
1371 else if (actions == ACTION_UNSET_ALL) {
1372 check_write(&location_opts.source);
1373 check_argc(argc, 1, 2);
1374 ret = repo_config_set_multivar_in_file_gently(the_repository, location_opts.source.file,
1375 argv[0], NULL, argv[1],
1376 NULL, flags | CONFIG_FLAGS_MULTI_REPLACE);
1377 }
1378 else if (actions == ACTION_RENAME_SECTION) {
1379 check_write(&location_opts.source);
1380 check_argc(argc, 2, 2);
1381 ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1382 argv[0], argv[1]);
1383 if (ret < 0)
1384 goto out;
1385 else if (!ret)
1386 die(_("no such section: %s"), argv[0]);
1387 else
1388 ret = 0;
1389 }
1390 else if (actions == ACTION_REMOVE_SECTION) {
1391 check_write(&location_opts.source);
1392 check_argc(argc, 1, 1);
1393 ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file,
1394 argv[0], NULL);
1395 if (ret < 0)
1396 goto out;
1397 else if (!ret)
1398 die(_("no such section: %s"), argv[0]);
1399 else
1400 ret = 0;
1401 }
1402 else if (actions == ACTION_GET_COLOR) {
1403 check_argc(argc, 1, 2);
1404 ret = get_color(&location_opts, argv[0], argv[1]);
1405 }
1406 else if (actions == ACTION_GET_COLORBOOL) {
1407 check_argc(argc, 1, 2);
1408 if (argc == 2)
1409 color_stdout_is_tty = git_config_bool("command line", argv[1]);
1410 ret = get_colorbool(&location_opts, argv[0], argc == 2);
1411 }
1412
1413out:
1414 location_options_release(&location_opts);
1415 free(comment);
1416 free(value);
1417 return ret;
1418}
1419
1420int cmd_config(int argc,
1421 const char **argv,
1422 const char *prefix,
1423 struct repository *repo)
1424{
1425 parse_opt_subcommand_fn *subcommand = NULL;
1426 struct option subcommand_opts[] = {
1427 OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
1428 OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
1429 OPT_SUBCOMMAND("set", &subcommand, cmd_config_set),
1430 OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset),
1431 OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section),
1432 OPT_SUBCOMMAND("remove-section", &subcommand, cmd_config_remove_section),
1433 OPT_SUBCOMMAND("edit", &subcommand, cmd_config_edit),
1434 OPT_END(),
1435 };
1436
1437 /*
1438 * This is somewhat hacky: we first parse the command line while
1439 * keeping all args intact in order to determine whether a subcommand
1440 * has been specified. If so, we re-parse it a second time, but this
1441 * time we drop KEEP_ARGV0. This is so that we don't munge the command
1442 * line in case no subcommand was given, which would otherwise confuse
1443 * us when parsing the legacy-style modes that don't use subcommands.
1444 */
1445 argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage,
1446 PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT);
1447 if (subcommand) {
1448 argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage,
1449 PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT);
1450 return subcommand(argc, argv, prefix, repo);
1451 }
1452
1453 return cmd_config_actions(argc, argv, prefix);
1454}