1 #define USE_THE_REPOSITORY_VARIABLE
7 #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] [--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>"),
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>"),
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
{
115 /* Populated via `display_options_init()`. */
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 .type = OPTION_CALLBACK, \
140 .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, \
141 .callback = option_parse_type, \
145 static int option_parse_type(const struct option
*opt
, const char *arg
,
148 int new_type
, *to_type
;
151 *((int *) opt
->value
) = 0;
156 * To support '--<type>' style flags, begin with new_type equal to
159 new_type
= opt
->defval
;
161 if (!strcmp(arg
, "bool"))
162 new_type
= TYPE_BOOL
;
163 else if (!strcmp(arg
, "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
;
176 die(_("unrecognized --type argument, %s"), arg
);
179 to_type
= opt
->value
;
180 if (*to_type
&& *to_type
!= new_type
) {
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
188 error(_("only one type at a time"));
196 static void check_argc(int argc
, int min
, int max
)
198 if (argc
>= min
&& argc
<= max
)
201 error(_("wrong number of arguments, should be %d"), min
);
203 error(_("wrong number of arguments, should be from %d to %d"),
208 static void show_config_origin(const struct config_display_options
*opts
,
209 const struct key_value_info
*kvi
,
212 const char term
= opts
->end_nul
? '\0' : '\t';
214 strbuf_addstr(buf
, config_origin_type_name(kvi
->origin_type
));
215 strbuf_addch(buf
, ':');
217 strbuf_addstr(buf
, kvi
->filename
? kvi
->filename
: "");
219 quote_c_style(kvi
->filename
? kvi
->filename
: "", buf
, NULL
, 0);
220 strbuf_addch(buf
, term
);
223 static void show_config_scope(const struct config_display_options
*opts
,
224 const struct key_value_info
*kvi
,
227 const char term
= opts
->end_nul
? '\0' : '\t';
228 const char *scope
= config_scope_name(kvi
->scope
);
230 strbuf_addstr(buf
, N_(scope
));
231 strbuf_addch(buf
, term
);
234 static int show_all_config(const char *key_
, const char *value_
,
235 const struct config_context
*ctx
,
238 const struct config_display_options
*opts
= cb
;
239 const struct key_value_info
*kvi
= ctx
->kvi
;
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
);
251 if (!opts
->omit_values
&& value_
)
252 printf("%s%c%s%c", key_
, opts
->delim
, value_
, opts
->term
);
254 printf("%s%c", key_
, opts
->term
);
259 struct strbuf
*items
;
264 static 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
)
268 if (opts
->show_scope
)
269 show_config_scope(opts
, kvi
, buf
);
270 if (opts
->show_origin
)
271 show_config_origin(opts
, kvi
, buf
);
273 strbuf_addstr(buf
, key_
);
274 if (!opts
->omit_values
) {
276 strbuf_addch(buf
, opts
->key_delim
);
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_
) ?
284 else if (opts
->type
== TYPE_BOOL_OR_INT
) {
286 v
= git_config_bool_or_int(key_
, value_
, kvi
,
289 strbuf_addstr(buf
, v
? "true" : "false");
291 strbuf_addf(buf
, "%d", v
);
292 } else if (opts
->type
== TYPE_BOOL_OR_STR
) {
293 int v
= git_parse_maybe_bool(value_
);
295 strbuf_addstr(buf
, value_
);
297 strbuf_addstr(buf
, v
? "true" : "false");
298 } else if (opts
->type
== TYPE_PATH
) {
300 if (git_config_pathname(&v
, key_
, value_
) < 0)
302 strbuf_addstr(buf
, v
);
304 } else if (opts
->type
== TYPE_EXPIRY_DATE
) {
306 if (git_config_expiry_date(&t
, key_
, value_
) < 0)
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)
313 strbuf_addstr(buf
, v
);
315 strbuf_addstr(buf
, value_
);
317 /* Just show the key name; back out delimiter */
319 strbuf_setlen(buf
, buf
->len
- 1);
322 strbuf_addch(buf
, opts
->term
);
326 #define GET_VALUE_ALL (1 << 0)
327 #define GET_VALUE_KEY_REGEXP (1 << 1)
329 struct collect_config_data
{
330 const struct config_display_options
*display_opts
;
331 struct strbuf_list
*values
;
332 const char *value_pattern
;
337 unsigned get_value_flags
;
341 static int collect_config(const char *key_
, const char *value_
,
342 const struct config_context
*ctx
, void *cb
)
344 struct collect_config_data
*data
= cb
;
345 struct strbuf_list
*values
= data
->values
;
346 const struct key_value_info
*kvi
= ctx
->kvi
;
348 if (!(data
->get_value_flags
& GET_VALUE_KEY_REGEXP
) &&
349 strcmp(key_
, data
->key
))
351 if ((data
->get_value_flags
& GET_VALUE_KEY_REGEXP
) &&
352 regexec(data
->key_regexp
, key_
, 0, NULL
, 0))
354 if ((data
->flags
& CONFIG_FLAGS_FIXED_VALUE
) &&
355 strcmp(data
->value_pattern
, (value_
?value_
:"")))
358 (data
->do_not_match
^ !!regexec(data
->regexp
, (value_
?value_
:""), 0, NULL
, 0)))
361 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
362 strbuf_init(&values
->items
[values
->nr
], 0);
364 return format_config(data
->display_opts
, &values
->items
[values
->nr
++],
368 static 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
)
373 int ret
= CONFIG_GENERIC_ERROR
;
374 struct strbuf_list values
= {NULL
};
375 struct collect_config_data data
= {
376 .display_opts
= display_opts
,
378 .get_value_flags
= get_value_flags
,
384 if (get_value_flags
& GET_VALUE_KEY_REGEXP
) {
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.
394 for (tl
= key
+ strlen(key
) - 1;
395 tl
>= key
&& *tl
!= '.';
398 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
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
;
409 if (git_config_parse_key(key_
, &key
, NULL
)) {
410 ret
= CONFIG_INVALID_KEY
;
417 if (regex_
&& (flags
& CONFIG_FLAGS_FIXED_VALUE
))
418 data
.value_pattern
= regex_
;
420 if (regex_
[0] == '!') {
421 data
.do_not_match
= 1;
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
;
434 config_with_options(collect_config
, &data
,
435 &opts
->source
, the_repository
,
438 if (!values
.nr
&& display_opts
->default_value
) {
439 struct key_value_info kvi
= KVI_INIT
;
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
);
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
);
464 if (data
.key_regexp
) {
465 regfree(data
.key_regexp
);
466 free(data
.key_regexp
);
469 regfree(data
.regexp
);
476 static char *normalize_value(const char *key
, const char *value
,
477 int type
, struct key_value_info
*kvi
)
482 if (type
== 0 || type
== TYPE_PATH
|| type
== TYPE_EXPIRY_DATE
)
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.
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
) {
497 v
= git_config_bool_or_int(key
, value
, kvi
, &is_bool
);
499 return xstrfmt("%d", v
);
501 return xstrdup(v
? "true" : "false");
503 if (type
== TYPE_BOOL_OR_STR
) {
504 int v
= git_parse_maybe_bool(value
);
506 return xstrdup(value
);
508 return xstrdup(v
? "true" : "false");
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
);
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.
522 return xstrdup(value
);
525 BUG("cannot normalize type %d", type
);
528 struct get_color_config_data
{
530 const char *get_color_slot
;
531 char parsed_color
[COLOR_MAXLEN
];
534 static int git_get_color_config(const char *var
, const char *value
,
535 const struct config_context
*ctx UNUSED
,
538 struct get_color_config_data
*data
= cb
;
540 if (!strcmp(var
, data
->get_color_slot
)) {
542 config_error_nonbool(var
);
543 if (color_parse(value
, data
->parsed_color
) < 0)
545 data
->get_color_found
= 1;
550 static void get_color(const struct config_location_options
*opts
,
551 const char *var
, const char *def_color
)
553 struct get_color_config_data data
= {
554 .get_color_slot
= var
,
555 .parsed_color
[0] = '\0',
558 config_with_options(git_get_color_config
, &data
,
559 &opts
->source
, the_repository
,
562 if (!data
.get_color_found
&& def_color
) {
563 if (color_parse(def_color
, data
.parsed_color
) < 0)
564 die(_("unable to parse default color value"));
567 fputs(data
.parsed_color
, stdout
);
570 struct get_colorbool_config_data
{
571 int get_colorbool_found
;
572 int get_diff_color_found
;
573 int get_color_ui_found
;
574 const char *get_colorbool_slot
;
577 static int git_get_colorbool_config(const char *var
, const char *value
,
578 const struct config_context
*ctx UNUSED
,
581 struct get_colorbool_config_data
*data
= cb
;
583 if (!strcmp(var
, data
->get_colorbool_slot
))
584 data
->get_colorbool_found
= git_config_colorbool(var
, value
);
585 else if (!strcmp(var
, "diff.color"))
586 data
->get_diff_color_found
= git_config_colorbool(var
, value
);
587 else if (!strcmp(var
, "color.ui"))
588 data
->get_color_ui_found
= git_config_colorbool(var
, value
);
592 static int get_colorbool(const struct config_location_options
*opts
,
593 const char *var
, int print
)
595 struct get_colorbool_config_data data
= {
596 .get_colorbool_slot
= var
,
597 .get_colorbool_found
= -1,
598 .get_diff_color_found
= -1,
599 .get_color_ui_found
= -1,
602 config_with_options(git_get_colorbool_config
, &data
,
603 &opts
->source
, the_repository
,
606 if (data
.get_colorbool_found
< 0) {
607 if (!strcmp(data
.get_colorbool_slot
, "color.diff"))
608 data
.get_colorbool_found
= data
.get_diff_color_found
;
609 if (data
.get_colorbool_found
< 0)
610 data
.get_colorbool_found
= data
.get_color_ui_found
;
613 if (data
.get_colorbool_found
< 0)
614 /* default value if none found in config */
615 data
.get_colorbool_found
= GIT_COLOR_AUTO
;
617 data
.get_colorbool_found
= want_color(data
.get_colorbool_found
);
620 printf("%s\n", data
.get_colorbool_found
? "true" : "false");
623 return data
.get_colorbool_found
? 0 : 1;
626 static void check_write(const struct git_config_source
*source
)
628 if (!source
->file
&& !startup_info
->have_repository
)
629 die(_("not in a git directory"));
631 if (source
->use_stdin
)
632 die(_("writing to stdin is not supported"));
635 die(_("writing config blobs is not supported"));
638 struct urlmatch_current_candidate_value
{
641 struct key_value_info kvi
;
644 static int urlmatch_collect_fn(const char *var
, const char *value
,
645 const struct config_context
*ctx
,
648 struct string_list
*values
= cb
;
649 struct string_list_item
*item
= string_list_insert(values
, var
);
650 struct urlmatch_current_candidate_value
*matched
= item
->util
;
651 const struct key_value_info
*kvi
= ctx
->kvi
;
654 matched
= xmalloc(sizeof(*matched
));
655 strbuf_init(&matched
->value
, 0);
656 item
->util
= matched
;
658 strbuf_reset(&matched
->value
);
663 strbuf_addstr(&matched
->value
, value
);
664 matched
->value_is_null
= 0;
666 matched
->value_is_null
= 1;
671 static int get_urlmatch(const struct config_location_options
*opts
,
672 const struct config_display_options
*_display_opts
,
673 const char *var
, const char *url
)
677 struct config_display_options display_opts
= *_display_opts
;
678 struct string_list_item
*item
;
679 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
680 struct string_list values
= STRING_LIST_INIT_DUP
;
682 config
.collect_fn
= urlmatch_collect_fn
;
683 config
.cascade_fn
= NULL
;
686 if (!url_normalize(url
, &config
.url
))
687 die("%s", config
.url
.err
);
689 config
.section
= xstrdup_tolower(var
);
690 section_tail
= strchr(config
.section
, '.');
692 *section_tail
= '\0';
693 config
.key
= section_tail
+ 1;
694 display_opts
.show_keys
= 0;
697 display_opts
.show_keys
= 1;
700 config_with_options(urlmatch_config_entry
, &config
,
701 &opts
->source
, the_repository
,
706 for_each_string_list_item(item
, &values
) {
707 struct urlmatch_current_candidate_value
*matched
= item
->util
;
708 struct strbuf buf
= STRBUF_INIT
;
710 format_config(&display_opts
, &buf
, item
->string
,
711 matched
->value_is_null
? NULL
: matched
->value
.buf
,
713 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
714 strbuf_release(&buf
);
716 strbuf_release(&matched
->value
);
718 urlmatch_config_release(&config
);
719 string_list_clear(&values
, 1);
720 free(config
.url
.url
);
722 free((void *)config
.section
);
726 static char *default_user_config(void)
728 struct strbuf buf
= STRBUF_INIT
;
730 _("# This is Git's per-user configuration file.\n"
732 "# Please adapt and uncomment the following lines:\n"
735 ident_default_name(),
736 ident_default_email());
737 return strbuf_detach(&buf
, NULL
);
740 static void location_options_init(struct config_location_options
*opts
,
743 if (!opts
->source
.file
)
744 opts
->source
.file
= opts
->file_to_free
=
745 xstrdup_or_null(getenv(CONFIG_ENVIRONMENT
));
747 if (opts
->use_global_config
+ opts
->use_system_config
+
748 opts
->use_local_config
+ opts
->use_worktree_config
+
749 !!opts
->source
.file
+ !!opts
->source
.blob
> 1) {
750 error(_("only one config file at a time"));
754 if (!startup_info
->have_repository
) {
755 if (opts
->use_local_config
)
756 die(_("--local can only be used inside a git repository"));
757 if (opts
->source
.blob
)
758 die(_("--blob can only be used inside a git repository"));
759 if (opts
->use_worktree_config
)
760 die(_("--worktree can only be used inside a git repository"));
763 if (opts
->source
.file
&&
764 !strcmp(opts
->source
.file
, "-")) {
765 opts
->source
.file
= NULL
;
766 opts
->source
.use_stdin
= 1;
767 opts
->source
.scope
= CONFIG_SCOPE_COMMAND
;
770 if (opts
->use_global_config
) {
771 opts
->source
.file
= opts
->file_to_free
= git_global_config();
772 if (!opts
->source
.file
)
774 * It is unknown if HOME/.gitconfig exists, so
775 * we do not know if we should write to XDG
776 * location; error out even if XDG_CONFIG_HOME
777 * is set and points at a sane location.
779 die(_("$HOME not set"));
780 opts
->source
.scope
= CONFIG_SCOPE_GLOBAL
;
781 } else if (opts
->use_system_config
) {
782 opts
->source
.file
= opts
->file_to_free
= git_system_config();
783 opts
->source
.scope
= CONFIG_SCOPE_SYSTEM
;
784 } else if (opts
->use_local_config
) {
785 opts
->source
.file
= opts
->file_to_free
= repo_git_path(the_repository
, "config");
786 opts
->source
.scope
= CONFIG_SCOPE_LOCAL
;
787 } else if (opts
->use_worktree_config
) {
788 struct worktree
**worktrees
= get_worktrees();
789 if (the_repository
->repository_format_worktree_config
)
790 opts
->source
.file
= opts
->file_to_free
=
791 repo_git_path(the_repository
, "config.worktree");
792 else if (worktrees
[0] && worktrees
[1])
793 die(_("--worktree cannot be used with multiple "
794 "working trees unless the config\n"
795 "extension worktreeConfig is enabled. "
796 "Please read \"CONFIGURATION FILE\"\n"
797 "section in \"git help worktree\" for details"));
799 opts
->source
.file
= opts
->file_to_free
=
800 repo_git_path(the_repository
, "config");
801 opts
->source
.scope
= CONFIG_SCOPE_LOCAL
;
802 free_worktrees(worktrees
);
803 } else if (opts
->source
.file
) {
804 if (!is_absolute_path(opts
->source
.file
) && prefix
)
805 opts
->source
.file
= opts
->file_to_free
=
806 prefix_filename(prefix
, opts
->source
.file
);
807 opts
->source
.scope
= CONFIG_SCOPE_COMMAND
;
808 } else if (opts
->source
.blob
) {
809 opts
->source
.scope
= CONFIG_SCOPE_COMMAND
;
812 if (opts
->respect_includes_opt
== -1)
813 opts
->options
.respect_includes
= !opts
->source
.file
;
815 opts
->options
.respect_includes
= opts
->respect_includes_opt
;
816 if (startup_info
->have_repository
) {
817 opts
->options
.commondir
= repo_get_common_dir(the_repository
);
818 opts
->options
.git_dir
= repo_get_git_dir(the_repository
);
822 static void location_options_release(struct config_location_options
*opts
)
824 free(opts
->file_to_free
);
827 static void display_options_init(struct config_display_options
*opts
)
832 opts
->key_delim
= '\n';
836 static int cmd_config_list(int argc
, const char **argv
, const char *prefix
,
837 struct repository
*repo UNUSED
)
839 struct config_location_options location_opts
= CONFIG_LOCATION_OPTIONS_INIT
;
840 struct config_display_options display_opts
= CONFIG_DISPLAY_OPTIONS_INIT
;
841 struct option opts
[] = {
842 CONFIG_LOCATION_OPTIONS(location_opts
),
843 CONFIG_DISPLAY_OPTIONS(display_opts
),
844 OPT_GROUP(N_("Other")),
845 OPT_BOOL(0, "includes", &location_opts
.respect_includes_opt
,
846 N_("respect include directives on lookup")),
850 argc
= parse_options(argc
, argv
, prefix
, opts
, builtin_config_list_usage
, 0);
851 check_argc(argc
, 0, 0);
853 location_options_init(&location_opts
, prefix
);
854 display_options_init(&display_opts
);
856 setup_auto_pager("config", 1);
858 if (config_with_options(show_all_config
, &display_opts
,
859 &location_opts
.source
, the_repository
,
860 &location_opts
.options
) < 0) {
861 if (location_opts
.source
.file
)
862 die_errno(_("unable to read config file '%s'"),
863 location_opts
.source
.file
);
865 die(_("error processing config file(s)"));
868 location_options_release(&location_opts
);
872 static int cmd_config_get(int argc
, const char **argv
, const char *prefix
,
873 struct repository
*repo UNUSED
)
875 struct config_location_options location_opts
= CONFIG_LOCATION_OPTIONS_INIT
;
876 struct config_display_options display_opts
= CONFIG_DISPLAY_OPTIONS_INIT
;
877 const char *value_pattern
= NULL
, *url
= NULL
;
879 unsigned get_value_flags
= 0;
880 struct option opts
[] = {
881 CONFIG_LOCATION_OPTIONS(location_opts
),
882 OPT_GROUP(N_("Filter options")),
883 OPT_BIT(0, "all", &get_value_flags
, N_("return all values for multi-valued config options"), GET_VALUE_ALL
),
884 OPT_BIT(0, "regexp", &get_value_flags
, N_("interpret the name as a regular expression"), GET_VALUE_KEY_REGEXP
),
885 OPT_STRING(0, "value", &value_pattern
, N_("pattern"), N_("show config with values matching the pattern")),
886 OPT_BIT(0, "fixed-value", &flags
, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE
),
887 OPT_STRING(0, "url", &url
, N_("URL"), N_("show config matching the given URL")),
888 CONFIG_DISPLAY_OPTIONS(display_opts
),
889 OPT_GROUP(N_("Other")),
890 OPT_BOOL(0, "includes", &location_opts
.respect_includes_opt
,
891 N_("respect include directives on lookup")),
892 OPT_STRING(0, "default", &display_opts
.default_value
,
893 N_("value"), N_("use default value when missing entry")),
898 argc
= parse_options(argc
, argv
, prefix
, opts
, builtin_config_get_usage
,
899 PARSE_OPT_STOP_AT_NON_OPTION
);
900 check_argc(argc
, 1, 1);
902 if ((flags
& CONFIG_FLAGS_FIXED_VALUE
) && !value_pattern
)
903 die(_("--fixed-value only applies with 'value-pattern'"));
904 if (display_opts
.default_value
&&
905 ((get_value_flags
& GET_VALUE_ALL
) || url
))
906 die(_("--default= cannot be used with --all or --url="));
907 if (url
&& ((get_value_flags
& GET_VALUE_ALL
) ||
908 (get_value_flags
& GET_VALUE_KEY_REGEXP
) ||
910 die(_("--url= cannot be used with --all, --regexp or --value"));
912 location_options_init(&location_opts
, prefix
);
913 display_options_init(&display_opts
);
915 setup_auto_pager("config", 1);
918 ret
= get_urlmatch(&location_opts
, &display_opts
, argv
[0], url
);
920 ret
= get_value(&location_opts
, &display_opts
, argv
[0], value_pattern
,
921 get_value_flags
, flags
);
923 location_options_release(&location_opts
);
927 static int cmd_config_set(int argc
, const char **argv
, const char *prefix
,
928 struct repository
*repo UNUSED
)
930 struct config_location_options location_opts
= CONFIG_LOCATION_OPTIONS_INIT
;
931 const char *value_pattern
= NULL
, *comment_arg
= NULL
;
932 char *comment
= NULL
;
933 int flags
= 0, append
= 0, type
= 0;
934 struct option opts
[] = {
935 CONFIG_LOCATION_OPTIONS(location_opts
),
936 CONFIG_TYPE_OPTIONS(type
),
937 OPT_GROUP(N_("Filter")),
938 OPT_BIT(0, "all", &flags
, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE
),
939 OPT_STRING(0, "value", &value_pattern
, N_("pattern"), N_("show config with values matching the pattern")),
940 OPT_BIT(0, "fixed-value", &flags
, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE
),
941 OPT_GROUP(N_("Other")),
942 OPT_STRING(0, "comment", &comment_arg
, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
943 OPT_BOOL(0, "append", &append
, N_("add a new line without altering any existing values")),
946 struct key_value_info default_kvi
= KVI_INIT
;
950 argc
= parse_options(argc
, argv
, prefix
, opts
, builtin_config_set_usage
,
951 PARSE_OPT_STOP_AT_NON_OPTION
);
952 check_argc(argc
, 2, 2);
954 if ((flags
& CONFIG_FLAGS_FIXED_VALUE
) && !value_pattern
)
955 die(_("--fixed-value only applies with --value=<pattern>"));
956 if (append
&& value_pattern
)
957 die(_("--append cannot be used with --value=<pattern>"));
959 value_pattern
= CONFIG_REGEX_NONE
;
961 comment
= git_config_prepare_comment_string(comment_arg
);
963 location_options_init(&location_opts
, prefix
);
964 check_write(&location_opts
.source
);
966 value
= normalize_value(argv
[0], argv
[1], type
, &default_kvi
);
968 if ((flags
& CONFIG_FLAGS_MULTI_REPLACE
) || value_pattern
) {
969 ret
= git_config_set_multivar_in_file_gently(location_opts
.source
.file
,
970 argv
[0], value
, value_pattern
,
973 ret
= git_config_set_in_file_gently(location_opts
.source
.file
,
974 argv
[0], comment
, value
);
975 if (ret
== CONFIG_NOTHING_SET
)
976 error(_("cannot overwrite multiple values with a single value\n"
977 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
980 location_options_release(&location_opts
);
986 static int cmd_config_unset(int argc
, const char **argv
, const char *prefix
,
987 struct repository
*repo UNUSED
)
989 struct config_location_options location_opts
= CONFIG_LOCATION_OPTIONS_INIT
;
990 const char *value_pattern
= NULL
;
992 struct option opts
[] = {
993 CONFIG_LOCATION_OPTIONS(location_opts
),
994 OPT_GROUP(N_("Filter")),
995 OPT_BIT(0, "all", &flags
, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE
),
996 OPT_STRING(0, "value", &value_pattern
, N_("pattern"), N_("show config with values matching the pattern")),
997 OPT_BIT(0, "fixed-value", &flags
, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE
),
1002 argc
= parse_options(argc
, argv
, prefix
, opts
, builtin_config_unset_usage
,
1003 PARSE_OPT_STOP_AT_NON_OPTION
);
1004 check_argc(argc
, 1, 1);
1006 if ((flags
& CONFIG_FLAGS_FIXED_VALUE
) && !value_pattern
)
1007 die(_("--fixed-value only applies with 'value-pattern'"));
1009 location_options_init(&location_opts
, prefix
);
1010 check_write(&location_opts
.source
);
1012 if ((flags
& CONFIG_FLAGS_MULTI_REPLACE
) || value_pattern
)
1013 ret
= git_config_set_multivar_in_file_gently(location_opts
.source
.file
,
1014 argv
[0], NULL
, value_pattern
,
1017 ret
= git_config_set_in_file_gently(location_opts
.source
.file
, argv
[0],
1020 location_options_release(&location_opts
);
1024 static int cmd_config_rename_section(int argc
, const char **argv
, const char *prefix
,
1025 struct repository
*repo UNUSED
)
1027 struct config_location_options location_opts
= CONFIG_LOCATION_OPTIONS_INIT
;
1028 struct option opts
[] = {
1029 CONFIG_LOCATION_OPTIONS(location_opts
),
1034 argc
= parse_options(argc
, argv
, prefix
, opts
, builtin_config_rename_section_usage
,
1035 PARSE_OPT_STOP_AT_NON_OPTION
);
1036 check_argc(argc
, 2, 2);
1038 location_options_init(&location_opts
, prefix
);
1039 check_write(&location_opts
.source
);
1041 ret
= repo_config_rename_section_in_file(the_repository
, location_opts
.source
.file
,
1046 die(_("no such section: %s"), argv
[0]);
1050 location_options_release(&location_opts
);
1054 static int cmd_config_remove_section(int argc
, const char **argv
, const char *prefix
,
1055 struct repository
*repo UNUSED
)
1057 struct config_location_options location_opts
= CONFIG_LOCATION_OPTIONS_INIT
;
1058 struct option opts
[] = {
1059 CONFIG_LOCATION_OPTIONS(location_opts
),
1064 argc
= parse_options(argc
, argv
, prefix
, opts
, builtin_config_remove_section_usage
,
1065 PARSE_OPT_STOP_AT_NON_OPTION
);
1066 check_argc(argc
, 1, 1);
1068 location_options_init(&location_opts
, prefix
);
1069 check_write(&location_opts
.source
);
1071 ret
= repo_config_rename_section_in_file(the_repository
, location_opts
.source
.file
,
1076 die(_("no such section: %s"), argv
[0]);
1080 location_options_release(&location_opts
);
1084 static int show_editor(struct config_location_options
*opts
)
1088 if (!opts
->source
.file
&& !startup_info
->have_repository
)
1089 die(_("not in a git directory"));
1090 if (opts
->source
.use_stdin
)
1091 die(_("editing stdin is not supported"));
1092 if (opts
->source
.blob
)
1093 die(_("editing blobs is not supported"));
1094 git_config(git_default_config
, NULL
);
1095 config_file
= opts
->source
.file
?
1096 xstrdup(opts
->source
.file
) :
1097 repo_git_path(the_repository
, "config");
1098 if (opts
->use_global_config
) {
1099 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
1101 char *content
= default_user_config();
1102 write_str_in_full(fd
, content
);
1106 else if (errno
!= EEXIST
)
1107 die_errno(_("cannot create configuration file %s"), config_file
);
1109 launch_editor(config_file
, NULL
, NULL
);
1115 static int cmd_config_edit(int argc
, const char **argv
, const char *prefix
,
1116 struct repository
*repo UNUSED
)
1118 struct config_location_options location_opts
= CONFIG_LOCATION_OPTIONS_INIT
;
1119 struct option opts
[] = {
1120 CONFIG_LOCATION_OPTIONS(location_opts
),
1125 argc
= parse_options(argc
, argv
, prefix
, opts
, builtin_config_edit_usage
, 0);
1126 check_argc(argc
, 0, 0);
1128 location_options_init(&location_opts
, prefix
);
1129 check_write(&location_opts
.source
);
1131 ret
= show_editor(&location_opts
);
1132 location_options_release(&location_opts
);
1136 static int cmd_config_actions(int argc
, const char **argv
, const char *prefix
)
1139 ACTION_GET
= (1<<0),
1140 ACTION_GET_ALL
= (1<<1),
1141 ACTION_GET_REGEXP
= (1<<2),
1142 ACTION_REPLACE_ALL
= (1<<3),
1143 ACTION_ADD
= (1<<4),
1144 ACTION_UNSET
= (1<<5),
1145 ACTION_UNSET_ALL
= (1<<6),
1146 ACTION_RENAME_SECTION
= (1<<7),
1147 ACTION_REMOVE_SECTION
= (1<<8),
1148 ACTION_LIST
= (1<<9),
1149 ACTION_EDIT
= (1<<10),
1150 ACTION_SET
= (1<<11),
1151 ACTION_SET_ALL
= (1<<12),
1152 ACTION_GET_COLOR
= (1<<13),
1153 ACTION_GET_COLORBOOL
= (1<<14),
1154 ACTION_GET_URLMATCH
= (1<<15),
1156 struct config_location_options location_opts
= CONFIG_LOCATION_OPTIONS_INIT
;
1157 struct config_display_options display_opts
= CONFIG_DISPLAY_OPTIONS_INIT
;
1158 const char *comment_arg
= NULL
;
1161 struct option opts
[] = {
1162 CONFIG_LOCATION_OPTIONS(location_opts
),
1163 OPT_GROUP(N_("Action")),
1164 OPT_CMDMODE(0, "get", &actions
, N_("get value: name [<value-pattern>]"), ACTION_GET
),
1165 OPT_CMDMODE(0, "get-all", &actions
, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL
),
1166 OPT_CMDMODE(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP
),
1167 OPT_CMDMODE(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
1168 OPT_CMDMODE(0, "replace-all", &actions
, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL
),
1169 OPT_CMDMODE(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
1170 OPT_CMDMODE(0, "unset", &actions
, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET
),
1171 OPT_CMDMODE(0, "unset-all", &actions
, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL
),
1172 OPT_CMDMODE(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
1173 OPT_CMDMODE(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
1174 OPT_CMDMODE('l', "list", &actions
, N_("list all"), ACTION_LIST
),
1175 OPT_CMDMODE('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
1176 OPT_CMDMODE(0, "get-color", &actions
, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR
),
1177 OPT_CMDMODE(0, "get-colorbool", &actions
, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL
),
1178 CONFIG_DISPLAY_OPTIONS(display_opts
),
1179 OPT_GROUP(N_("Other")),
1180 OPT_STRING(0, "default", &display_opts
.default_value
,
1181 N_("value"), N_("with --get, use default value when missing entry")),
1182 OPT_STRING(0, "comment", &comment_arg
, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
1183 OPT_BIT(0, "fixed-value", &flags
, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE
),
1184 OPT_BOOL(0, "includes", &location_opts
.respect_includes_opt
,
1185 N_("respect include directives on lookup")),
1188 char *value
= NULL
, *comment
= NULL
;
1190 struct key_value_info default_kvi
= KVI_INIT
;
1192 argc
= parse_options(argc
, argv
, prefix
, opts
,
1193 builtin_config_usage
,
1194 PARSE_OPT_STOP_AT_NON_OPTION
);
1196 location_options_init(&location_opts
, prefix
);
1197 display_options_init(&display_opts
);
1199 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && display_opts
.type
) {
1200 error(_("--get-color and variable type are incoherent"));
1206 case 1: actions
= ACTION_GET
; break;
1207 case 2: actions
= ACTION_SET
; break;
1208 case 3: actions
= ACTION_SET_ALL
; break;
1210 error(_("no action specified"));
1213 if (display_opts
.omit_values
&&
1214 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
1215 error(_("--name-only is only applicable to --list or --get-regexp"));
1219 if (display_opts
.show_origin
&& !(actions
&
1220 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
1221 error(_("--show-origin is only applicable to --get, --get-all, "
1222 "--get-regexp, and --list"));
1226 if (display_opts
.default_value
&& !(actions
& ACTION_GET
)) {
1227 error(_("--default is only applicable to --get"));
1232 !(actions
& (ACTION_ADD
|ACTION_SET
|ACTION_SET_ALL
|ACTION_REPLACE_ALL
))) {
1233 error(_("--comment is only applicable to add/set/replace operations"));
1237 /* check usage of --fixed-value */
1238 if (flags
& CONFIG_FLAGS_FIXED_VALUE
) {
1239 int allowed_usage
= 0;
1242 /* git config --get <name> <value-pattern> */
1244 /* git config --get-all <name> <value-pattern> */
1245 case ACTION_GET_ALL
:
1246 /* git config --get-regexp <name-pattern> <value-pattern> */
1247 case ACTION_GET_REGEXP
:
1248 /* git config --unset <name> <value-pattern> */
1250 /* git config --unset-all <name> <value-pattern> */
1251 case ACTION_UNSET_ALL
:
1252 allowed_usage
= argc
> 1 && !!argv
[1];
1255 /* git config <name> <value> <value-pattern> */
1256 case ACTION_SET_ALL
:
1257 /* git config --replace-all <name> <value> <value-pattern> */
1258 case ACTION_REPLACE_ALL
:
1259 allowed_usage
= argc
> 2 && !!argv
[2];
1262 /* other options don't allow --fixed-value */
1265 if (!allowed_usage
) {
1266 error(_("--fixed-value only applies with 'value-pattern'"));
1271 comment
= git_config_prepare_comment_string(comment_arg
);
1274 * The following actions may produce more than one line of output and
1275 * should therefore be paged.
1277 if (actions
& (ACTION_LIST
| ACTION_GET_ALL
| ACTION_GET_REGEXP
| ACTION_GET_URLMATCH
))
1278 setup_auto_pager("config", 1);
1280 if (actions
== ACTION_LIST
) {
1281 check_argc(argc
, 0, 0);
1282 if (config_with_options(show_all_config
, &display_opts
,
1283 &location_opts
.source
, the_repository
,
1284 &location_opts
.options
) < 0) {
1285 if (location_opts
.source
.file
)
1286 die_errno(_("unable to read config file '%s'"),
1287 location_opts
.source
.file
);
1289 die(_("error processing config file(s)"));
1292 else if (actions
== ACTION_EDIT
) {
1293 ret
= show_editor(&location_opts
);
1295 else if (actions
== ACTION_SET
) {
1296 check_write(&location_opts
.source
);
1297 check_argc(argc
, 2, 2);
1298 value
= normalize_value(argv
[0], argv
[1], display_opts
.type
, &default_kvi
);
1299 ret
= git_config_set_in_file_gently(location_opts
.source
.file
, argv
[0], comment
, value
);
1300 if (ret
== CONFIG_NOTHING_SET
)
1301 error(_("cannot overwrite multiple values with a single value\n"
1302 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
1304 else if (actions
== ACTION_SET_ALL
) {
1305 check_write(&location_opts
.source
);
1306 check_argc(argc
, 2, 3);
1307 value
= normalize_value(argv
[0], argv
[1], display_opts
.type
, &default_kvi
);
1308 ret
= git_config_set_multivar_in_file_gently(location_opts
.source
.file
,
1309 argv
[0], value
, argv
[2],
1312 else if (actions
== ACTION_ADD
) {
1313 check_write(&location_opts
.source
);
1314 check_argc(argc
, 2, 2);
1315 value
= normalize_value(argv
[0], argv
[1], display_opts
.type
, &default_kvi
);
1316 ret
= git_config_set_multivar_in_file_gently(location_opts
.source
.file
,
1321 else if (actions
== ACTION_REPLACE_ALL
) {
1322 check_write(&location_opts
.source
);
1323 check_argc(argc
, 2, 3);
1324 value
= normalize_value(argv
[0], argv
[1], display_opts
.type
, &default_kvi
);
1325 ret
= git_config_set_multivar_in_file_gently(location_opts
.source
.file
,
1326 argv
[0], value
, argv
[2],
1327 comment
, flags
| CONFIG_FLAGS_MULTI_REPLACE
);
1329 else if (actions
== ACTION_GET
) {
1330 check_argc(argc
, 1, 2);
1331 ret
= get_value(&location_opts
, &display_opts
, argv
[0], argv
[1],
1334 else if (actions
== ACTION_GET_ALL
) {
1335 check_argc(argc
, 1, 2);
1336 ret
= get_value(&location_opts
, &display_opts
, argv
[0], argv
[1],
1337 GET_VALUE_ALL
, flags
);
1339 else if (actions
== ACTION_GET_REGEXP
) {
1340 display_opts
.show_keys
= 1;
1341 check_argc(argc
, 1, 2);
1342 ret
= get_value(&location_opts
, &display_opts
, argv
[0], argv
[1],
1343 GET_VALUE_ALL
|GET_VALUE_KEY_REGEXP
, flags
);
1345 else if (actions
== ACTION_GET_URLMATCH
) {
1346 check_argc(argc
, 2, 2);
1347 ret
= get_urlmatch(&location_opts
, &display_opts
, argv
[0], argv
[1]);
1349 else if (actions
== ACTION_UNSET
) {
1350 check_write(&location_opts
.source
);
1351 check_argc(argc
, 1, 2);
1353 ret
= git_config_set_multivar_in_file_gently(location_opts
.source
.file
,
1354 argv
[0], NULL
, argv
[1],
1357 ret
= git_config_set_in_file_gently(location_opts
.source
.file
,
1358 argv
[0], NULL
, NULL
);
1360 else if (actions
== ACTION_UNSET_ALL
) {
1361 check_write(&location_opts
.source
);
1362 check_argc(argc
, 1, 2);
1363 ret
= git_config_set_multivar_in_file_gently(location_opts
.source
.file
,
1364 argv
[0], NULL
, argv
[1],
1365 NULL
, flags
| CONFIG_FLAGS_MULTI_REPLACE
);
1367 else if (actions
== ACTION_RENAME_SECTION
) {
1368 check_write(&location_opts
.source
);
1369 check_argc(argc
, 2, 2);
1370 ret
= repo_config_rename_section_in_file(the_repository
, location_opts
.source
.file
,
1375 die(_("no such section: %s"), argv
[0]);
1379 else if (actions
== ACTION_REMOVE_SECTION
) {
1380 check_write(&location_opts
.source
);
1381 check_argc(argc
, 1, 1);
1382 ret
= repo_config_rename_section_in_file(the_repository
, location_opts
.source
.file
,
1387 die(_("no such section: %s"), argv
[0]);
1391 else if (actions
== ACTION_GET_COLOR
) {
1392 check_argc(argc
, 1, 2);
1393 get_color(&location_opts
, argv
[0], argv
[1]);
1395 else if (actions
== ACTION_GET_COLORBOOL
) {
1396 check_argc(argc
, 1, 2);
1398 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
1399 ret
= get_colorbool(&location_opts
, argv
[0], argc
== 2);
1403 location_options_release(&location_opts
);
1409 int cmd_config(int argc
,
1412 struct repository
*repo
)
1414 parse_opt_subcommand_fn
*subcommand
= NULL
;
1415 struct option subcommand_opts
[] = {
1416 OPT_SUBCOMMAND("list", &subcommand
, cmd_config_list
),
1417 OPT_SUBCOMMAND("get", &subcommand
, cmd_config_get
),
1418 OPT_SUBCOMMAND("set", &subcommand
, cmd_config_set
),
1419 OPT_SUBCOMMAND("unset", &subcommand
, cmd_config_unset
),
1420 OPT_SUBCOMMAND("rename-section", &subcommand
, cmd_config_rename_section
),
1421 OPT_SUBCOMMAND("remove-section", &subcommand
, cmd_config_remove_section
),
1422 OPT_SUBCOMMAND("edit", &subcommand
, cmd_config_edit
),
1427 * This is somewhat hacky: we first parse the command line while
1428 * keeping all args intact in order to determine whether a subcommand
1429 * has been specified. If so, we re-parse it a second time, but this
1430 * time we drop KEEP_ARGV0. This is so that we don't munge the command
1431 * line in case no subcommand was given, which would otherwise confuse
1432 * us when parsing the legacy-style modes that don't use subcommands.
1434 argc
= parse_options(argc
, argv
, prefix
, subcommand_opts
, builtin_config_usage
,
1435 PARSE_OPT_SUBCOMMAND_OPTIONAL
|PARSE_OPT_KEEP_ARGV0
|PARSE_OPT_KEEP_UNKNOWN_OPT
);
1437 argc
= parse_options(argc
, argv
, prefix
, subcommand_opts
, builtin_config_usage
,
1438 PARSE_OPT_SUBCOMMAND_OPTIONAL
|PARSE_OPT_KEEP_UNKNOWN_OPT
);
1439 return subcommand(argc
, argv
, prefix
, repo
);
1442 return cmd_config_actions(argc
, argv
, prefix
);