5 #include "parse-options.h"
10 static const char *const builtin_config_usage
[] = {
11 N_("git config [<options>]"),
16 static regex_t
*key_regexp
;
17 static regex_t
*regexp
;
19 static int omit_values
;
20 static int use_key_regexp
;
22 static int do_not_match
;
23 static char delim
= '=';
24 static char key_delim
= ' ';
25 static char term
= '\n';
27 static int use_global_config
, use_system_config
, use_local_config
;
28 static int use_worktree_config
;
29 static struct git_config_source given_config_source
;
30 static int actions
, type
;
31 static char *default_value
;
33 static int respect_includes_opt
= -1;
34 static struct config_options config_options
;
35 static int show_origin
;
36 static int show_scope
;
38 #define ACTION_GET (1<<0)
39 #define ACTION_GET_ALL (1<<1)
40 #define ACTION_GET_REGEXP (1<<2)
41 #define ACTION_REPLACE_ALL (1<<3)
42 #define ACTION_ADD (1<<4)
43 #define ACTION_UNSET (1<<5)
44 #define ACTION_UNSET_ALL (1<<6)
45 #define ACTION_RENAME_SECTION (1<<7)
46 #define ACTION_REMOVE_SECTION (1<<8)
47 #define ACTION_LIST (1<<9)
48 #define ACTION_EDIT (1<<10)
49 #define ACTION_SET (1<<11)
50 #define ACTION_SET_ALL (1<<12)
51 #define ACTION_GET_COLOR (1<<13)
52 #define ACTION_GET_COLORBOOL (1<<14)
53 #define ACTION_GET_URLMATCH (1<<15)
56 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
57 * one line of output and which should therefore be paged.
59 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
60 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
64 #define TYPE_BOOL_OR_INT 3
66 #define TYPE_EXPIRY_DATE 5
68 #define TYPE_BOOL_OR_STR 7
70 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
71 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
72 PARSE_OPT_NONEG, option_parse_type, (i) }
74 static NORETURN
void usage_builtin_config(void);
76 static int option_parse_type(const struct option
*opt
, const char *arg
,
79 int new_type
, *to_type
;
82 *((int *) opt
->value
) = 0;
87 * To support '--<type>' style flags, begin with new_type equal to
90 new_type
= opt
->defval
;
92 if (!strcmp(arg
, "bool"))
94 else if (!strcmp(arg
, "int"))
96 else if (!strcmp(arg
, "bool-or-int"))
97 new_type
= TYPE_BOOL_OR_INT
;
98 else if (!strcmp(arg
, "bool-or-str"))
99 new_type
= TYPE_BOOL_OR_STR
;
100 else if (!strcmp(arg
, "path"))
101 new_type
= TYPE_PATH
;
102 else if (!strcmp(arg
, "expiry-date"))
103 new_type
= TYPE_EXPIRY_DATE
;
104 else if (!strcmp(arg
, "color"))
105 new_type
= TYPE_COLOR
;
107 die(_("unrecognized --type argument, %s"), arg
);
110 to_type
= opt
->value
;
111 if (*to_type
&& *to_type
!= new_type
) {
113 * Complain when there is a new type not equal to the old type.
114 * This allows for combinations like '--int --type=int' and
115 * '--type=int --type=int', but disallows ones like '--type=bool
116 * --int' and '--type=bool
119 error(_("only one type at a time"));
120 usage_builtin_config();
127 static struct option builtin_config_options
[] = {
128 OPT_GROUP(N_("Config file location")),
129 OPT_BOOL(0, "global", &use_global_config
, N_("use global config file")),
130 OPT_BOOL(0, "system", &use_system_config
, N_("use system config file")),
131 OPT_BOOL(0, "local", &use_local_config
, N_("use repository config file")),
132 OPT_BOOL(0, "worktree", &use_worktree_config
, N_("use per-worktree config file")),
133 OPT_STRING('f', "file", &given_config_source
.file
, N_("file"), N_("use given config file")),
134 OPT_STRING(0, "blob", &given_config_source
.blob
, N_("blob-id"), N_("read config from given blob object")),
135 OPT_GROUP(N_("Action")),
136 OPT_BIT(0, "get", &actions
, N_("get value: name [value-regex]"), ACTION_GET
),
137 OPT_BIT(0, "get-all", &actions
, N_("get all values: key [value-regex]"), ACTION_GET_ALL
),
138 OPT_BIT(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP
),
139 OPT_BIT(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
140 OPT_BIT(0, "replace-all", &actions
, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL
),
141 OPT_BIT(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
142 OPT_BIT(0, "unset", &actions
, N_("remove a variable: name [value-regex]"), ACTION_UNSET
),
143 OPT_BIT(0, "unset-all", &actions
, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL
),
144 OPT_BIT(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
145 OPT_BIT(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
146 OPT_BIT('l', "list", &actions
, N_("list all"), ACTION_LIST
),
147 OPT_BIT('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
148 OPT_BIT(0, "get-color", &actions
, N_("find the color configured: slot [default]"), ACTION_GET_COLOR
),
149 OPT_BIT(0, "get-colorbool", &actions
, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL
),
150 OPT_GROUP(N_("Type")),
151 OPT_CALLBACK('t', "type", &type
, "", N_("value is given this type"), option_parse_type
),
152 OPT_CALLBACK_VALUE(0, "bool", &type
, N_("value is \"true\" or \"false\""), TYPE_BOOL
),
153 OPT_CALLBACK_VALUE(0, "int", &type
, N_("value is decimal number"), TYPE_INT
),
154 OPT_CALLBACK_VALUE(0, "bool-or-int", &type
, N_("value is --bool or --int"), TYPE_BOOL_OR_INT
),
155 OPT_CALLBACK_VALUE(0, "bool-or-str", &type
, N_("value is --bool or string"), TYPE_BOOL_OR_STR
),
156 OPT_CALLBACK_VALUE(0, "path", &type
, N_("value is a path (file or directory name)"), TYPE_PATH
),
157 OPT_CALLBACK_VALUE(0, "expiry-date", &type
, N_("value is an expiry date"), TYPE_EXPIRY_DATE
),
158 OPT_GROUP(N_("Other")),
159 OPT_BOOL('z', "null", &end_nul
, N_("terminate values with NUL byte")),
160 OPT_BOOL(0, "name-only", &omit_values
, N_("show variable names only")),
161 OPT_BOOL(0, "includes", &respect_includes_opt
, N_("respect include directives on lookup")),
162 OPT_BOOL(0, "show-origin", &show_origin
, N_("show origin of config (file, standard input, blob, command line)")),
163 OPT_BOOL(0, "show-scope", &show_scope
, N_("show scope of config (worktree, local, global, system, command)")),
164 OPT_STRING(0, "default", &default_value
, N_("value"), N_("with --get, use default value when missing entry")),
168 static NORETURN
void usage_builtin_config(void)
170 usage_with_options(builtin_config_usage
, builtin_config_options
);
173 static void check_argc(int argc
, int min
, int max
)
175 if (argc
>= min
&& argc
<= max
)
178 error(_("wrong number of arguments, should be %d"), min
);
180 error(_("wrong number of arguments, should be from %d to %d"),
182 usage_builtin_config();
185 static void show_config_origin(struct strbuf
*buf
)
187 const char term
= end_nul
? '\0' : '\t';
189 strbuf_addstr(buf
, current_config_origin_type());
190 strbuf_addch(buf
, ':');
192 strbuf_addstr(buf
, current_config_name());
194 quote_c_style(current_config_name(), buf
, NULL
, 0);
195 strbuf_addch(buf
, term
);
198 static void show_config_scope(struct strbuf
*buf
)
200 const char term
= end_nul
? '\0' : '\t';
201 const char *scope
= config_scope_name(current_config_scope());
203 strbuf_addstr(buf
, N_(scope
));
204 strbuf_addch(buf
, term
);
207 static int show_all_config(const char *key_
, const char *value_
, void *cb
)
209 if (show_origin
|| show_scope
) {
210 struct strbuf buf
= STRBUF_INIT
;
212 show_config_scope(&buf
);
214 show_config_origin(&buf
);
215 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
216 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
217 strbuf_release(&buf
);
219 if (!omit_values
&& value_
)
220 printf("%s%c%s%c", key_
, delim
, value_
, term
);
222 printf("%s%c", key_
, term
);
227 struct strbuf
*items
;
232 static int format_config(struct strbuf
*buf
, const char *key_
, const char *value_
)
235 show_config_scope(buf
);
237 show_config_origin(buf
);
239 strbuf_addstr(buf
, key_
);
242 strbuf_addch(buf
, key_delim
);
244 if (type
== TYPE_INT
)
245 strbuf_addf(buf
, "%"PRId64
,
246 git_config_int64(key_
, value_
? value_
: ""));
247 else if (type
== TYPE_BOOL
)
248 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
250 else if (type
== TYPE_BOOL_OR_INT
) {
252 v
= git_config_bool_or_int(key_
, value_
, &is_bool
);
254 strbuf_addstr(buf
, v
? "true" : "false");
256 strbuf_addf(buf
, "%d", v
);
257 } else if (type
== TYPE_BOOL_OR_STR
) {
258 int v
= git_parse_maybe_bool(value_
);
260 strbuf_addstr(buf
, value_
);
262 strbuf_addstr(buf
, v
? "true" : "false");
263 } else if (type
== TYPE_PATH
) {
265 if (git_config_pathname(&v
, key_
, value_
) < 0)
267 strbuf_addstr(buf
, v
);
269 } else if (type
== TYPE_EXPIRY_DATE
) {
271 if (git_config_expiry_date(&t
, key_
, value_
) < 0)
273 strbuf_addf(buf
, "%"PRItime
, t
);
274 } else if (type
== TYPE_COLOR
) {
275 char v
[COLOR_MAXLEN
];
276 if (git_config_color(v
, key_
, value_
) < 0)
278 strbuf_addstr(buf
, v
);
280 strbuf_addstr(buf
, value_
);
282 /* Just show the key name; back out delimiter */
284 strbuf_setlen(buf
, buf
->len
- 1);
287 strbuf_addch(buf
, term
);
291 static int collect_config(const char *key_
, const char *value_
, void *cb
)
293 struct strbuf_list
*values
= cb
;
295 if (!use_key_regexp
&& strcmp(key_
, key
))
297 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
299 if (regexp
!= NULL
&&
300 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
303 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
304 strbuf_init(&values
->items
[values
->nr
], 0);
306 return format_config(&values
->items
[values
->nr
++], key_
, value_
);
309 static int get_value(const char *key_
, const char *regex_
)
311 int ret
= CONFIG_GENERIC_ERROR
;
312 struct strbuf_list values
= {NULL
};
315 if (use_key_regexp
) {
319 * NEEDSWORK: this naive pattern lowercasing obviously does not
320 * work for more complex patterns like "^[^.]*Foo.*bar".
321 * Perhaps we should deprecate this altogether someday.
325 for (tl
= key
+ strlen(key
) - 1;
326 tl
>= key
&& *tl
!= '.';
329 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
332 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
333 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
334 error(_("invalid key pattern: %s"), key_
);
335 FREE_AND_NULL(key_regexp
);
336 ret
= CONFIG_INVALID_PATTERN
;
340 if (git_config_parse_key(key_
, &key
, NULL
)) {
341 ret
= CONFIG_INVALID_KEY
;
347 if (regex_
[0] == '!') {
352 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
353 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
354 error(_("invalid pattern: %s"), regex_
);
355 FREE_AND_NULL(regexp
);
356 ret
= CONFIG_INVALID_PATTERN
;
361 config_with_options(collect_config
, &values
,
362 &given_config_source
, &config_options
);
364 if (!values
.nr
&& default_value
) {
366 ALLOC_GROW(values
.items
, values
.nr
+ 1, values
.alloc
);
367 item
= &values
.items
[values
.nr
++];
368 strbuf_init(item
, 0);
369 if (format_config(item
, key_
, default_value
) < 0)
370 die(_("failed to format default config value: %s"),
376 for (i
= 0; i
< values
.nr
; i
++) {
377 struct strbuf
*buf
= values
.items
+ i
;
378 if (do_all
|| i
== values
.nr
- 1)
379 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
398 static char *normalize_value(const char *key
, const char *value
)
403 if (type
== 0 || type
== TYPE_PATH
|| type
== TYPE_EXPIRY_DATE
)
405 * We don't do normalization for TYPE_PATH here: If
406 * the path is like ~/foobar/, we prefer to store
407 * "~/foobar/" in the config file, and to expand the ~
408 * when retrieving the value.
409 * Also don't do normalization for expiry dates.
411 return xstrdup(value
);
412 if (type
== TYPE_INT
)
413 return xstrfmt("%"PRId64
, git_config_int64(key
, value
));
414 if (type
== TYPE_BOOL
)
415 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
416 if (type
== TYPE_BOOL_OR_INT
) {
418 v
= git_config_bool_or_int(key
, value
, &is_bool
);
420 return xstrfmt("%d", v
);
422 return xstrdup(v
? "true" : "false");
424 if (type
== TYPE_BOOL_OR_STR
) {
425 int v
= git_parse_maybe_bool(value
);
427 return xstrdup(value
);
429 return xstrdup(v
? "true" : "false");
431 if (type
== TYPE_COLOR
) {
432 char v
[COLOR_MAXLEN
];
433 if (git_config_color(v
, key
, value
))
434 die(_("cannot parse color '%s'"), value
);
437 * The contents of `v` now contain an ANSI escape
438 * sequence, not suitable for including within a
439 * configuration file. Treat the above as a
440 * "sanity-check", and return the given value, which we
441 * know is representable as valid color code.
443 return xstrdup(value
);
446 BUG("cannot normalize type %d", type
);
449 static int get_color_found
;
450 static const char *get_color_slot
;
451 static const char *get_colorbool_slot
;
452 static char parsed_color
[COLOR_MAXLEN
];
454 static int git_get_color_config(const char *var
, const char *value
, void *cb
)
456 if (!strcmp(var
, get_color_slot
)) {
458 config_error_nonbool(var
);
459 if (color_parse(value
, parsed_color
) < 0)
466 static void get_color(const char *var
, const char *def_color
)
468 get_color_slot
= var
;
470 parsed_color
[0] = '\0';
471 config_with_options(git_get_color_config
, NULL
,
472 &given_config_source
, &config_options
);
474 if (!get_color_found
&& def_color
) {
475 if (color_parse(def_color
, parsed_color
) < 0)
476 die(_("unable to parse default color value"));
479 fputs(parsed_color
, stdout
);
482 static int get_colorbool_found
;
483 static int get_diff_color_found
;
484 static int get_color_ui_found
;
485 static int git_get_colorbool_config(const char *var
, const char *value
,
488 if (!strcmp(var
, get_colorbool_slot
))
489 get_colorbool_found
= git_config_colorbool(var
, value
);
490 else if (!strcmp(var
, "diff.color"))
491 get_diff_color_found
= git_config_colorbool(var
, value
);
492 else if (!strcmp(var
, "color.ui"))
493 get_color_ui_found
= git_config_colorbool(var
, value
);
497 static int get_colorbool(const char *var
, int print
)
499 get_colorbool_slot
= var
;
500 get_colorbool_found
= -1;
501 get_diff_color_found
= -1;
502 get_color_ui_found
= -1;
503 config_with_options(git_get_colorbool_config
, NULL
,
504 &given_config_source
, &config_options
);
506 if (get_colorbool_found
< 0) {
507 if (!strcmp(get_colorbool_slot
, "color.diff"))
508 get_colorbool_found
= get_diff_color_found
;
509 if (get_colorbool_found
< 0)
510 get_colorbool_found
= get_color_ui_found
;
513 if (get_colorbool_found
< 0)
514 /* default value if none found in config */
515 get_colorbool_found
= GIT_COLOR_AUTO
;
517 get_colorbool_found
= want_color(get_colorbool_found
);
520 printf("%s\n", get_colorbool_found
? "true" : "false");
523 return get_colorbool_found
? 0 : 1;
526 static void check_write(void)
528 if (!given_config_source
.file
&& !startup_info
->have_repository
)
529 die(_("not in a git directory"));
531 if (given_config_source
.use_stdin
)
532 die(_("writing to stdin is not supported"));
534 if (given_config_source
.blob
)
535 die(_("writing config blobs is not supported"));
538 struct urlmatch_current_candidate_value
{
543 static int urlmatch_collect_fn(const char *var
, const char *value
, void *cb
)
545 struct string_list
*values
= cb
;
546 struct string_list_item
*item
= string_list_insert(values
, var
);
547 struct urlmatch_current_candidate_value
*matched
= item
->util
;
550 matched
= xmalloc(sizeof(*matched
));
551 strbuf_init(&matched
->value
, 0);
552 item
->util
= matched
;
554 strbuf_reset(&matched
->value
);
558 strbuf_addstr(&matched
->value
, value
);
559 matched
->value_is_null
= 0;
561 matched
->value_is_null
= 1;
566 static int get_urlmatch(const char *var
, const char *url
)
570 struct string_list_item
*item
;
571 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
572 struct string_list values
= STRING_LIST_INIT_DUP
;
574 config
.collect_fn
= urlmatch_collect_fn
;
575 config
.cascade_fn
= NULL
;
578 if (!url_normalize(url
, &config
.url
))
579 die("%s", config
.url
.err
);
581 config
.section
= xstrdup_tolower(var
);
582 section_tail
= strchr(config
.section
, '.');
584 *section_tail
= '\0';
585 config
.key
= section_tail
+ 1;
592 config_with_options(urlmatch_config_entry
, &config
,
593 &given_config_source
, &config_options
);
597 for_each_string_list_item(item
, &values
) {
598 struct urlmatch_current_candidate_value
*matched
= item
->util
;
599 struct strbuf buf
= STRBUF_INIT
;
601 format_config(&buf
, item
->string
,
602 matched
->value_is_null
? NULL
: matched
->value
.buf
);
603 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
604 strbuf_release(&buf
);
606 strbuf_release(&matched
->value
);
608 string_list_clear(&config
.vars
, 1);
609 string_list_clear(&values
, 1);
610 free(config
.url
.url
);
612 free((void *)config
.section
);
616 static char *default_user_config(void)
618 struct strbuf buf
= STRBUF_INIT
;
620 _("# This is Git's per-user configuration file.\n"
622 "# Please adapt and uncomment the following lines:\n"
625 ident_default_name(),
626 ident_default_email());
627 return strbuf_detach(&buf
, NULL
);
630 int cmd_config(int argc
, const char **argv
, const char *prefix
)
632 int nongit
= !startup_info
->have_repository
;
635 given_config_source
.file
= xstrdup_or_null(getenv(CONFIG_ENVIRONMENT
));
637 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
638 builtin_config_usage
,
639 PARSE_OPT_STOP_AT_NON_OPTION
);
641 if (use_global_config
+ use_system_config
+ use_local_config
+
642 use_worktree_config
+
643 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
644 error(_("only one config file at a time"));
645 usage_builtin_config();
649 if (use_local_config
)
650 die(_("--local can only be used inside a git repository"));
651 if (given_config_source
.blob
)
652 die(_("--blob can only be used inside a git repository"));
653 if (use_worktree_config
)
654 die(_("--worktree can only be used inside a git repository"));
658 if (given_config_source
.file
&&
659 !strcmp(given_config_source
.file
, "-")) {
660 given_config_source
.file
= NULL
;
661 given_config_source
.use_stdin
= 1;
662 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
665 if (use_global_config
) {
666 char *user_config
= expand_user_path("~/.gitconfig", 0);
667 char *xdg_config
= xdg_config_home("config");
671 * It is unknown if HOME/.gitconfig exists, so
672 * we do not know if we should write to XDG
673 * location; error out even if XDG_CONFIG_HOME
674 * is set and points at a sane location.
676 die(_("$HOME not set"));
678 given_config_source
.scope
= CONFIG_SCOPE_GLOBAL
;
680 if (access_or_warn(user_config
, R_OK
, 0) &&
681 xdg_config
&& !access_or_warn(xdg_config
, R_OK
, 0)) {
682 given_config_source
.file
= xdg_config
;
685 given_config_source
.file
= user_config
;
689 else if (use_system_config
) {
690 given_config_source
.file
= git_etc_gitconfig();
691 given_config_source
.scope
= CONFIG_SCOPE_SYSTEM
;
692 } else if (use_local_config
) {
693 given_config_source
.file
= git_pathdup("config");
694 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
695 } else if (use_worktree_config
) {
696 struct worktree
**worktrees
= get_worktrees();
697 if (repository_format_worktree_config
)
698 given_config_source
.file
= git_pathdup("config.worktree");
699 else if (worktrees
[0] && worktrees
[1])
700 die(_("--worktree cannot be used with multiple "
701 "working trees unless the config\n"
702 "extension worktreeConfig is enabled. "
703 "Please read \"CONFIGURATION FILE\"\n"
704 "section in \"git help worktree\" for details"));
706 given_config_source
.file
= git_pathdup("config");
707 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
708 free_worktrees(worktrees
);
709 } else if (given_config_source
.file
) {
710 if (!is_absolute_path(given_config_source
.file
) && prefix
)
711 given_config_source
.file
=
712 prefix_filename(prefix
, given_config_source
.file
);
713 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
714 } else if (given_config_source
.blob
) {
715 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
719 if (respect_includes_opt
== -1)
720 config_options
.respect_includes
= !given_config_source
.file
;
722 config_options
.respect_includes
= respect_includes_opt
;
724 config_options
.commondir
= get_git_common_dir();
725 config_options
.git_dir
= get_git_dir();
734 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && type
) {
735 error(_("--get-color and variable type are incoherent"));
736 usage_builtin_config();
739 if (HAS_MULTI_BITS(actions
)) {
740 error(_("only one action at a time"));
741 usage_builtin_config();
745 case 1: actions
= ACTION_GET
; break;
746 case 2: actions
= ACTION_SET
; break;
747 case 3: actions
= ACTION_SET_ALL
; break;
749 usage_builtin_config();
752 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
753 error(_("--name-only is only applicable to --list or --get-regexp"));
754 usage_builtin_config();
757 if (show_origin
&& !(actions
&
758 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
759 error(_("--show-origin is only applicable to --get, --get-all, "
760 "--get-regexp, and --list"));
761 usage_builtin_config();
764 if (default_value
&& !(actions
& ACTION_GET
)) {
765 error(_("--default is only applicable to --get"));
766 usage_builtin_config();
769 if (actions
& PAGING_ACTIONS
)
770 setup_auto_pager("config", 1);
772 if (actions
== ACTION_LIST
) {
773 check_argc(argc
, 0, 0);
774 if (config_with_options(show_all_config
, NULL
,
775 &given_config_source
,
776 &config_options
) < 0) {
777 if (given_config_source
.file
)
778 die_errno(_("unable to read config file '%s'"),
779 given_config_source
.file
);
781 die(_("error processing config file(s)"));
784 else if (actions
== ACTION_EDIT
) {
787 check_argc(argc
, 0, 0);
788 if (!given_config_source
.file
&& nongit
)
789 die(_("not in a git directory"));
790 if (given_config_source
.use_stdin
)
791 die(_("editing stdin is not supported"));
792 if (given_config_source
.blob
)
793 die(_("editing blobs is not supported"));
794 git_config(git_default_config
, NULL
);
795 config_file
= given_config_source
.file
?
796 xstrdup(given_config_source
.file
) :
797 git_pathdup("config");
798 if (use_global_config
) {
799 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
801 char *content
= default_user_config();
802 write_str_in_full(fd
, content
);
806 else if (errno
!= EEXIST
)
807 die_errno(_("cannot create configuration file %s"), config_file
);
809 launch_editor(config_file
, NULL
, NULL
);
812 else if (actions
== ACTION_SET
) {
815 check_argc(argc
, 2, 2);
816 value
= normalize_value(argv
[0], argv
[1]);
818 ret
= git_config_set_in_file_gently(given_config_source
.file
, argv
[0], value
);
819 if (ret
== CONFIG_NOTHING_SET
)
820 error(_("cannot overwrite multiple values with a single value\n"
821 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
824 else if (actions
== ACTION_SET_ALL
) {
826 check_argc(argc
, 2, 3);
827 value
= normalize_value(argv
[0], argv
[1]);
829 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
830 argv
[0], value
, argv
[2], 0);
832 else if (actions
== ACTION_ADD
) {
834 check_argc(argc
, 2, 2);
835 value
= normalize_value(argv
[0], argv
[1]);
837 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
839 CONFIG_REGEX_NONE
, 0);
841 else if (actions
== ACTION_REPLACE_ALL
) {
843 check_argc(argc
, 2, 3);
844 value
= normalize_value(argv
[0], argv
[1]);
846 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
847 argv
[0], value
, argv
[2], 1);
849 else if (actions
== ACTION_GET
) {
850 check_argc(argc
, 1, 2);
851 return get_value(argv
[0], argv
[1]);
853 else if (actions
== ACTION_GET_ALL
) {
855 check_argc(argc
, 1, 2);
856 return get_value(argv
[0], argv
[1]);
858 else if (actions
== ACTION_GET_REGEXP
) {
862 check_argc(argc
, 1, 2);
863 return get_value(argv
[0], argv
[1]);
865 else if (actions
== ACTION_GET_URLMATCH
) {
866 check_argc(argc
, 2, 2);
867 return get_urlmatch(argv
[0], argv
[1]);
869 else if (actions
== ACTION_UNSET
) {
871 check_argc(argc
, 1, 2);
873 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
874 argv
[0], NULL
, argv
[1], 0);
876 return git_config_set_in_file_gently(given_config_source
.file
,
879 else if (actions
== ACTION_UNSET_ALL
) {
881 check_argc(argc
, 1, 2);
882 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
883 argv
[0], NULL
, argv
[1], 1);
885 else if (actions
== ACTION_RENAME_SECTION
) {
888 check_argc(argc
, 2, 2);
889 ret
= git_config_rename_section_in_file(given_config_source
.file
,
894 die(_("no such section: %s"), argv
[0]);
896 else if (actions
== ACTION_REMOVE_SECTION
) {
899 check_argc(argc
, 1, 1);
900 ret
= git_config_rename_section_in_file(given_config_source
.file
,
905 die(_("no such section: %s"), argv
[0]);
907 else if (actions
== ACTION_GET_COLOR
) {
908 check_argc(argc
, 1, 2);
909 get_color(argv
[0], argv
[1]);
911 else if (actions
== ACTION_GET_COLORBOOL
) {
912 check_argc(argc
, 1, 2);
914 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
915 return get_colorbool(argv
[0], argc
== 2);