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 const char *value_pattern
;
18 static regex_t
*regexp
;
20 static int omit_values
;
21 static int use_key_regexp
;
23 static int do_not_match
;
24 static char delim
= '=';
25 static char key_delim
= ' ';
26 static char term
= '\n';
28 static int use_global_config
, use_system_config
, use_local_config
;
29 static int use_worktree_config
;
30 static struct git_config_source given_config_source
;
31 static int actions
, type
;
32 static char *default_value
;
34 static int respect_includes_opt
= -1;
35 static struct config_options config_options
;
36 static int show_origin
;
37 static int show_scope
;
38 static int fixed_value
;
40 #define ACTION_GET (1<<0)
41 #define ACTION_GET_ALL (1<<1)
42 #define ACTION_GET_REGEXP (1<<2)
43 #define ACTION_REPLACE_ALL (1<<3)
44 #define ACTION_ADD (1<<4)
45 #define ACTION_UNSET (1<<5)
46 #define ACTION_UNSET_ALL (1<<6)
47 #define ACTION_RENAME_SECTION (1<<7)
48 #define ACTION_REMOVE_SECTION (1<<8)
49 #define ACTION_LIST (1<<9)
50 #define ACTION_EDIT (1<<10)
51 #define ACTION_SET (1<<11)
52 #define ACTION_SET_ALL (1<<12)
53 #define ACTION_GET_COLOR (1<<13)
54 #define ACTION_GET_COLORBOOL (1<<14)
55 #define ACTION_GET_URLMATCH (1<<15)
58 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
59 * one line of output and which should therefore be paged.
61 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
62 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
66 #define TYPE_BOOL_OR_INT 3
68 #define TYPE_EXPIRY_DATE 5
70 #define TYPE_BOOL_OR_STR 7
72 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
73 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
74 PARSE_OPT_NONEG, option_parse_type, (i) }
76 static NORETURN
void usage_builtin_config(void);
78 static int option_parse_type(const struct option
*opt
, const char *arg
,
81 int new_type
, *to_type
;
84 *((int *) opt
->value
) = 0;
89 * To support '--<type>' style flags, begin with new_type equal to
92 new_type
= opt
->defval
;
94 if (!strcmp(arg
, "bool"))
96 else if (!strcmp(arg
, "int"))
98 else if (!strcmp(arg
, "bool-or-int"))
99 new_type
= TYPE_BOOL_OR_INT
;
100 else if (!strcmp(arg
, "bool-or-str"))
101 new_type
= TYPE_BOOL_OR_STR
;
102 else if (!strcmp(arg
, "path"))
103 new_type
= TYPE_PATH
;
104 else if (!strcmp(arg
, "expiry-date"))
105 new_type
= TYPE_EXPIRY_DATE
;
106 else if (!strcmp(arg
, "color"))
107 new_type
= TYPE_COLOR
;
109 die(_("unrecognized --type argument, %s"), arg
);
112 to_type
= opt
->value
;
113 if (*to_type
&& *to_type
!= new_type
) {
115 * Complain when there is a new type not equal to the old type.
116 * This allows for combinations like '--int --type=int' and
117 * '--type=int --type=int', but disallows ones like '--type=bool
118 * --int' and '--type=bool
121 error(_("only one type at a time"));
122 usage_builtin_config();
129 static struct option builtin_config_options
[] = {
130 OPT_GROUP(N_("Config file location")),
131 OPT_BOOL(0, "global", &use_global_config
, N_("use global config file")),
132 OPT_BOOL(0, "system", &use_system_config
, N_("use system config file")),
133 OPT_BOOL(0, "local", &use_local_config
, N_("use repository config file")),
134 OPT_BOOL(0, "worktree", &use_worktree_config
, N_("use per-worktree config file")),
135 OPT_STRING('f', "file", &given_config_source
.file
, N_("file"), N_("use given config file")),
136 OPT_STRING(0, "blob", &given_config_source
.blob
, N_("blob-id"), N_("read config from given blob object")),
137 OPT_GROUP(N_("Action")),
138 OPT_BIT(0, "get", &actions
, N_("get value: name [value-pattern]"), ACTION_GET
),
139 OPT_BIT(0, "get-all", &actions
, N_("get all values: key [value-pattern]"), ACTION_GET_ALL
),
140 OPT_BIT(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP
),
141 OPT_BIT(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
142 OPT_BIT(0, "replace-all", &actions
, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL
),
143 OPT_BIT(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
144 OPT_BIT(0, "unset", &actions
, N_("remove a variable: name [value-pattern]"), ACTION_UNSET
),
145 OPT_BIT(0, "unset-all", &actions
, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL
),
146 OPT_BIT(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
147 OPT_BIT(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
148 OPT_BIT('l', "list", &actions
, N_("list all"), ACTION_LIST
),
149 OPT_BOOL(0, "fixed-value", &fixed_value
, N_("use string equality when comparing values to 'value-pattern'")),
150 OPT_BIT('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
151 OPT_BIT(0, "get-color", &actions
, N_("find the color configured: slot [default]"), ACTION_GET_COLOR
),
152 OPT_BIT(0, "get-colorbool", &actions
, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL
),
153 OPT_GROUP(N_("Type")),
154 OPT_CALLBACK('t', "type", &type
, "", N_("value is given this type"), option_parse_type
),
155 OPT_CALLBACK_VALUE(0, "bool", &type
, N_("value is \"true\" or \"false\""), TYPE_BOOL
),
156 OPT_CALLBACK_VALUE(0, "int", &type
, N_("value is decimal number"), TYPE_INT
),
157 OPT_CALLBACK_VALUE(0, "bool-or-int", &type
, N_("value is --bool or --int"), TYPE_BOOL_OR_INT
),
158 OPT_CALLBACK_VALUE(0, "bool-or-str", &type
, N_("value is --bool or string"), TYPE_BOOL_OR_STR
),
159 OPT_CALLBACK_VALUE(0, "path", &type
, N_("value is a path (file or directory name)"), TYPE_PATH
),
160 OPT_CALLBACK_VALUE(0, "expiry-date", &type
, N_("value is an expiry date"), TYPE_EXPIRY_DATE
),
161 OPT_GROUP(N_("Other")),
162 OPT_BOOL('z', "null", &end_nul
, N_("terminate values with NUL byte")),
163 OPT_BOOL(0, "name-only", &omit_values
, N_("show variable names only")),
164 OPT_BOOL(0, "includes", &respect_includes_opt
, N_("respect include directives on lookup")),
165 OPT_BOOL(0, "show-origin", &show_origin
, N_("show origin of config (file, standard input, blob, command line)")),
166 OPT_BOOL(0, "show-scope", &show_scope
, N_("show scope of config (worktree, local, global, system, command)")),
167 OPT_STRING(0, "default", &default_value
, N_("value"), N_("with --get, use default value when missing entry")),
171 static NORETURN
void usage_builtin_config(void)
173 usage_with_options(builtin_config_usage
, builtin_config_options
);
176 static void check_argc(int argc
, int min
, int max
)
178 if (argc
>= min
&& argc
<= max
)
181 error(_("wrong number of arguments, should be %d"), min
);
183 error(_("wrong number of arguments, should be from %d to %d"),
185 usage_builtin_config();
188 static void show_config_origin(struct strbuf
*buf
)
190 const char term
= end_nul
? '\0' : '\t';
192 strbuf_addstr(buf
, current_config_origin_type());
193 strbuf_addch(buf
, ':');
195 strbuf_addstr(buf
, current_config_name());
197 quote_c_style(current_config_name(), buf
, NULL
, 0);
198 strbuf_addch(buf
, term
);
201 static void show_config_scope(struct strbuf
*buf
)
203 const char term
= end_nul
? '\0' : '\t';
204 const char *scope
= config_scope_name(current_config_scope());
206 strbuf_addstr(buf
, N_(scope
));
207 strbuf_addch(buf
, term
);
210 static int show_all_config(const char *key_
, const char *value_
, void *cb
)
212 if (show_origin
|| show_scope
) {
213 struct strbuf buf
= STRBUF_INIT
;
215 show_config_scope(&buf
);
217 show_config_origin(&buf
);
218 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
219 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
220 strbuf_release(&buf
);
222 if (!omit_values
&& value_
)
223 printf("%s%c%s%c", key_
, delim
, value_
, term
);
225 printf("%s%c", key_
, term
);
230 struct strbuf
*items
;
235 static int format_config(struct strbuf
*buf
, const char *key_
, const char *value_
)
238 show_config_scope(buf
);
240 show_config_origin(buf
);
242 strbuf_addstr(buf
, key_
);
245 strbuf_addch(buf
, key_delim
);
247 if (type
== TYPE_INT
)
248 strbuf_addf(buf
, "%"PRId64
,
249 git_config_int64(key_
, value_
? value_
: ""));
250 else if (type
== TYPE_BOOL
)
251 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
253 else if (type
== TYPE_BOOL_OR_INT
) {
255 v
= git_config_bool_or_int(key_
, value_
, &is_bool
);
257 strbuf_addstr(buf
, v
? "true" : "false");
259 strbuf_addf(buf
, "%d", v
);
260 } else if (type
== TYPE_BOOL_OR_STR
) {
261 int v
= git_parse_maybe_bool(value_
);
263 strbuf_addstr(buf
, value_
);
265 strbuf_addstr(buf
, v
? "true" : "false");
266 } else if (type
== TYPE_PATH
) {
268 if (git_config_pathname(&v
, key_
, value_
) < 0)
270 strbuf_addstr(buf
, v
);
272 } else if (type
== TYPE_EXPIRY_DATE
) {
274 if (git_config_expiry_date(&t
, key_
, value_
) < 0)
276 strbuf_addf(buf
, "%"PRItime
, t
);
277 } else if (type
== TYPE_COLOR
) {
278 char v
[COLOR_MAXLEN
];
279 if (git_config_color(v
, key_
, value_
) < 0)
281 strbuf_addstr(buf
, v
);
283 strbuf_addstr(buf
, value_
);
285 /* Just show the key name; back out delimiter */
287 strbuf_setlen(buf
, buf
->len
- 1);
290 strbuf_addch(buf
, term
);
294 static int collect_config(const char *key_
, const char *value_
, void *cb
)
296 struct strbuf_list
*values
= cb
;
298 if (!use_key_regexp
&& strcmp(key_
, key
))
300 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
302 if (fixed_value
&& strcmp(value_pattern
, (value_
?value_
:"")))
304 if (regexp
!= NULL
&&
305 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
308 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
309 strbuf_init(&values
->items
[values
->nr
], 0);
311 return format_config(&values
->items
[values
->nr
++], key_
, value_
);
314 static int get_value(const char *key_
, const char *regex_
, unsigned flags
)
316 int ret
= CONFIG_GENERIC_ERROR
;
317 struct strbuf_list values
= {NULL
};
320 if (use_key_regexp
) {
324 * NEEDSWORK: this naive pattern lowercasing obviously does not
325 * work for more complex patterns like "^[^.]*Foo.*bar".
326 * Perhaps we should deprecate this altogether someday.
330 for (tl
= key
+ strlen(key
) - 1;
331 tl
>= key
&& *tl
!= '.';
334 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
337 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
338 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
339 error(_("invalid key pattern: %s"), key_
);
340 FREE_AND_NULL(key_regexp
);
341 ret
= CONFIG_INVALID_PATTERN
;
345 if (git_config_parse_key(key_
, &key
, NULL
)) {
346 ret
= CONFIG_INVALID_KEY
;
351 if (regex_
&& (flags
& CONFIG_FLAGS_FIXED_VALUE
))
352 value_pattern
= regex_
;
354 if (regex_
[0] == '!') {
359 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
360 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
361 error(_("invalid pattern: %s"), regex_
);
362 FREE_AND_NULL(regexp
);
363 ret
= CONFIG_INVALID_PATTERN
;
368 config_with_options(collect_config
, &values
,
369 &given_config_source
, &config_options
);
371 if (!values
.nr
&& default_value
) {
373 ALLOC_GROW(values
.items
, values
.nr
+ 1, values
.alloc
);
374 item
= &values
.items
[values
.nr
++];
375 strbuf_init(item
, 0);
376 if (format_config(item
, key_
, default_value
) < 0)
377 die(_("failed to format default config value: %s"),
383 for (i
= 0; i
< values
.nr
; i
++) {
384 struct strbuf
*buf
= values
.items
+ i
;
385 if (do_all
|| i
== values
.nr
- 1)
386 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
405 static char *normalize_value(const char *key
, const char *value
)
410 if (type
== 0 || type
== TYPE_PATH
|| type
== TYPE_EXPIRY_DATE
)
412 * We don't do normalization for TYPE_PATH here: If
413 * the path is like ~/foobar/, we prefer to store
414 * "~/foobar/" in the config file, and to expand the ~
415 * when retrieving the value.
416 * Also don't do normalization for expiry dates.
418 return xstrdup(value
);
419 if (type
== TYPE_INT
)
420 return xstrfmt("%"PRId64
, git_config_int64(key
, value
));
421 if (type
== TYPE_BOOL
)
422 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
423 if (type
== TYPE_BOOL_OR_INT
) {
425 v
= git_config_bool_or_int(key
, value
, &is_bool
);
427 return xstrfmt("%d", v
);
429 return xstrdup(v
? "true" : "false");
431 if (type
== TYPE_BOOL_OR_STR
) {
432 int v
= git_parse_maybe_bool(value
);
434 return xstrdup(value
);
436 return xstrdup(v
? "true" : "false");
438 if (type
== TYPE_COLOR
) {
439 char v
[COLOR_MAXLEN
];
440 if (git_config_color(v
, key
, value
))
441 die(_("cannot parse color '%s'"), value
);
444 * The contents of `v` now contain an ANSI escape
445 * sequence, not suitable for including within a
446 * configuration file. Treat the above as a
447 * "sanity-check", and return the given value, which we
448 * know is representable as valid color code.
450 return xstrdup(value
);
453 BUG("cannot normalize type %d", type
);
456 static int get_color_found
;
457 static const char *get_color_slot
;
458 static const char *get_colorbool_slot
;
459 static char parsed_color
[COLOR_MAXLEN
];
461 static int git_get_color_config(const char *var
, const char *value
, void *cb
)
463 if (!strcmp(var
, get_color_slot
)) {
465 config_error_nonbool(var
);
466 if (color_parse(value
, parsed_color
) < 0)
473 static void get_color(const char *var
, const char *def_color
)
475 get_color_slot
= var
;
477 parsed_color
[0] = '\0';
478 config_with_options(git_get_color_config
, NULL
,
479 &given_config_source
, &config_options
);
481 if (!get_color_found
&& def_color
) {
482 if (color_parse(def_color
, parsed_color
) < 0)
483 die(_("unable to parse default color value"));
486 fputs(parsed_color
, stdout
);
489 static int get_colorbool_found
;
490 static int get_diff_color_found
;
491 static int get_color_ui_found
;
492 static int git_get_colorbool_config(const char *var
, const char *value
,
495 if (!strcmp(var
, get_colorbool_slot
))
496 get_colorbool_found
= git_config_colorbool(var
, value
);
497 else if (!strcmp(var
, "diff.color"))
498 get_diff_color_found
= git_config_colorbool(var
, value
);
499 else if (!strcmp(var
, "color.ui"))
500 get_color_ui_found
= git_config_colorbool(var
, value
);
504 static int get_colorbool(const char *var
, int print
)
506 get_colorbool_slot
= var
;
507 get_colorbool_found
= -1;
508 get_diff_color_found
= -1;
509 get_color_ui_found
= -1;
510 config_with_options(git_get_colorbool_config
, NULL
,
511 &given_config_source
, &config_options
);
513 if (get_colorbool_found
< 0) {
514 if (!strcmp(get_colorbool_slot
, "color.diff"))
515 get_colorbool_found
= get_diff_color_found
;
516 if (get_colorbool_found
< 0)
517 get_colorbool_found
= get_color_ui_found
;
520 if (get_colorbool_found
< 0)
521 /* default value if none found in config */
522 get_colorbool_found
= GIT_COLOR_AUTO
;
524 get_colorbool_found
= want_color(get_colorbool_found
);
527 printf("%s\n", get_colorbool_found
? "true" : "false");
530 return get_colorbool_found
? 0 : 1;
533 static void check_write(void)
535 if (!given_config_source
.file
&& !startup_info
->have_repository
)
536 die(_("not in a git directory"));
538 if (given_config_source
.use_stdin
)
539 die(_("writing to stdin is not supported"));
541 if (given_config_source
.blob
)
542 die(_("writing config blobs is not supported"));
545 struct urlmatch_current_candidate_value
{
550 static int urlmatch_collect_fn(const char *var
, const char *value
, void *cb
)
552 struct string_list
*values
= cb
;
553 struct string_list_item
*item
= string_list_insert(values
, var
);
554 struct urlmatch_current_candidate_value
*matched
= item
->util
;
557 matched
= xmalloc(sizeof(*matched
));
558 strbuf_init(&matched
->value
, 0);
559 item
->util
= matched
;
561 strbuf_reset(&matched
->value
);
565 strbuf_addstr(&matched
->value
, value
);
566 matched
->value_is_null
= 0;
568 matched
->value_is_null
= 1;
573 static int get_urlmatch(const char *var
, const char *url
)
577 struct string_list_item
*item
;
578 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
579 struct string_list values
= STRING_LIST_INIT_DUP
;
581 config
.collect_fn
= urlmatch_collect_fn
;
582 config
.cascade_fn
= NULL
;
585 if (!url_normalize(url
, &config
.url
))
586 die("%s", config
.url
.err
);
588 config
.section
= xstrdup_tolower(var
);
589 section_tail
= strchr(config
.section
, '.');
591 *section_tail
= '\0';
592 config
.key
= section_tail
+ 1;
599 config_with_options(urlmatch_config_entry
, &config
,
600 &given_config_source
, &config_options
);
604 for_each_string_list_item(item
, &values
) {
605 struct urlmatch_current_candidate_value
*matched
= item
->util
;
606 struct strbuf buf
= STRBUF_INIT
;
608 format_config(&buf
, item
->string
,
609 matched
->value_is_null
? NULL
: matched
->value
.buf
);
610 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
611 strbuf_release(&buf
);
613 strbuf_release(&matched
->value
);
615 string_list_clear(&config
.vars
, 1);
616 string_list_clear(&values
, 1);
617 free(config
.url
.url
);
619 free((void *)config
.section
);
623 static char *default_user_config(void)
625 struct strbuf buf
= STRBUF_INIT
;
627 _("# This is Git's per-user configuration file.\n"
629 "# Please adapt and uncomment the following lines:\n"
632 ident_default_name(),
633 ident_default_email());
634 return strbuf_detach(&buf
, NULL
);
637 int cmd_config(int argc
, const char **argv
, const char *prefix
)
639 int nongit
= !startup_info
->have_repository
;
643 given_config_source
.file
= xstrdup_or_null(getenv(CONFIG_ENVIRONMENT
));
645 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
646 builtin_config_usage
,
647 PARSE_OPT_STOP_AT_NON_OPTION
);
649 if (use_global_config
+ use_system_config
+ use_local_config
+
650 use_worktree_config
+
651 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
652 error(_("only one config file at a time"));
653 usage_builtin_config();
657 if (use_local_config
)
658 die(_("--local can only be used inside a git repository"));
659 if (given_config_source
.blob
)
660 die(_("--blob can only be used inside a git repository"));
661 if (use_worktree_config
)
662 die(_("--worktree can only be used inside a git repository"));
666 if (given_config_source
.file
&&
667 !strcmp(given_config_source
.file
, "-")) {
668 given_config_source
.file
= NULL
;
669 given_config_source
.use_stdin
= 1;
670 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
673 if (use_global_config
) {
674 char *user_config
= expand_user_path("~/.gitconfig", 0);
675 char *xdg_config
= xdg_config_home("config");
679 * It is unknown if HOME/.gitconfig exists, so
680 * we do not know if we should write to XDG
681 * location; error out even if XDG_CONFIG_HOME
682 * is set and points at a sane location.
684 die(_("$HOME not set"));
686 given_config_source
.scope
= CONFIG_SCOPE_GLOBAL
;
688 if (access_or_warn(user_config
, R_OK
, 0) &&
689 xdg_config
&& !access_or_warn(xdg_config
, R_OK
, 0)) {
690 given_config_source
.file
= xdg_config
;
693 given_config_source
.file
= user_config
;
697 else if (use_system_config
) {
698 given_config_source
.file
= git_etc_gitconfig();
699 given_config_source
.scope
= CONFIG_SCOPE_SYSTEM
;
700 } else if (use_local_config
) {
701 given_config_source
.file
= git_pathdup("config");
702 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
703 } else if (use_worktree_config
) {
704 struct worktree
**worktrees
= get_worktrees();
705 if (repository_format_worktree_config
)
706 given_config_source
.file
= git_pathdup("config.worktree");
707 else if (worktrees
[0] && worktrees
[1])
708 die(_("--worktree cannot be used with multiple "
709 "working trees unless the config\n"
710 "extension worktreeConfig is enabled. "
711 "Please read \"CONFIGURATION FILE\"\n"
712 "section in \"git help worktree\" for details"));
714 given_config_source
.file
= git_pathdup("config");
715 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
716 free_worktrees(worktrees
);
717 } else if (given_config_source
.file
) {
718 if (!is_absolute_path(given_config_source
.file
) && prefix
)
719 given_config_source
.file
=
720 prefix_filename(prefix
, given_config_source
.file
);
721 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
722 } else if (given_config_source
.blob
) {
723 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
727 if (respect_includes_opt
== -1)
728 config_options
.respect_includes
= !given_config_source
.file
;
730 config_options
.respect_includes
= respect_includes_opt
;
732 config_options
.commondir
= get_git_common_dir();
733 config_options
.git_dir
= get_git_dir();
742 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && type
) {
743 error(_("--get-color and variable type are incoherent"));
744 usage_builtin_config();
747 if (HAS_MULTI_BITS(actions
)) {
748 error(_("only one action at a time"));
749 usage_builtin_config();
753 case 1: actions
= ACTION_GET
; break;
754 case 2: actions
= ACTION_SET
; break;
755 case 3: actions
= ACTION_SET_ALL
; break;
757 usage_builtin_config();
760 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
761 error(_("--name-only is only applicable to --list or --get-regexp"));
762 usage_builtin_config();
765 if (show_origin
&& !(actions
&
766 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
767 error(_("--show-origin is only applicable to --get, --get-all, "
768 "--get-regexp, and --list"));
769 usage_builtin_config();
772 if (default_value
&& !(actions
& ACTION_GET
)) {
773 error(_("--default is only applicable to --get"));
774 usage_builtin_config();
777 /* check usage of --fixed-value */
779 int allowed_usage
= 0;
782 /* git config --get <name> <value-pattern> */
784 /* git config --get-all <name> <value-pattern> */
786 /* git config --get-regexp <name-pattern> <value-pattern> */
787 case ACTION_GET_REGEXP
:
788 /* git config --unset <name> <value-pattern> */
790 /* git config --unset-all <name> <value-pattern> */
791 case ACTION_UNSET_ALL
:
792 allowed_usage
= argc
> 1 && !!argv
[1];
795 /* git config <name> <value> <value-pattern> */
797 /* git config --replace-all <name> <value> <value-pattern> */
798 case ACTION_REPLACE_ALL
:
799 allowed_usage
= argc
> 2 && !!argv
[2];
802 /* other options don't allow --fixed-value */
805 if (!allowed_usage
) {
806 error(_("--fixed-value only applies with 'value-pattern'"));
807 usage_builtin_config();
810 flags
|= CONFIG_FLAGS_FIXED_VALUE
;
813 if (actions
& PAGING_ACTIONS
)
814 setup_auto_pager("config", 1);
816 if (actions
== ACTION_LIST
) {
817 check_argc(argc
, 0, 0);
818 if (config_with_options(show_all_config
, NULL
,
819 &given_config_source
,
820 &config_options
) < 0) {
821 if (given_config_source
.file
)
822 die_errno(_("unable to read config file '%s'"),
823 given_config_source
.file
);
825 die(_("error processing config file(s)"));
828 else if (actions
== ACTION_EDIT
) {
831 check_argc(argc
, 0, 0);
832 if (!given_config_source
.file
&& nongit
)
833 die(_("not in a git directory"));
834 if (given_config_source
.use_stdin
)
835 die(_("editing stdin is not supported"));
836 if (given_config_source
.blob
)
837 die(_("editing blobs is not supported"));
838 git_config(git_default_config
, NULL
);
839 config_file
= given_config_source
.file
?
840 xstrdup(given_config_source
.file
) :
841 git_pathdup("config");
842 if (use_global_config
) {
843 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
845 char *content
= default_user_config();
846 write_str_in_full(fd
, content
);
850 else if (errno
!= EEXIST
)
851 die_errno(_("cannot create configuration file %s"), config_file
);
853 launch_editor(config_file
, NULL
, NULL
);
856 else if (actions
== ACTION_SET
) {
859 check_argc(argc
, 2, 2);
860 value
= normalize_value(argv
[0], argv
[1]);
862 ret
= git_config_set_in_file_gently(given_config_source
.file
, argv
[0], value
);
863 if (ret
== CONFIG_NOTHING_SET
)
864 error(_("cannot overwrite multiple values with a single value\n"
865 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
868 else if (actions
== ACTION_SET_ALL
) {
870 check_argc(argc
, 2, 3);
871 value
= normalize_value(argv
[0], argv
[1]);
873 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
874 argv
[0], value
, argv
[2],
877 else if (actions
== ACTION_ADD
) {
879 check_argc(argc
, 2, 2);
880 value
= normalize_value(argv
[0], argv
[1]);
882 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
887 else if (actions
== ACTION_REPLACE_ALL
) {
889 check_argc(argc
, 2, 3);
890 value
= normalize_value(argv
[0], argv
[1]);
892 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
893 argv
[0], value
, argv
[2],
894 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
896 else if (actions
== ACTION_GET
) {
897 check_argc(argc
, 1, 2);
898 return get_value(argv
[0], argv
[1], flags
);
900 else if (actions
== ACTION_GET_ALL
) {
902 check_argc(argc
, 1, 2);
903 return get_value(argv
[0], argv
[1], flags
);
905 else if (actions
== ACTION_GET_REGEXP
) {
909 check_argc(argc
, 1, 2);
910 return get_value(argv
[0], argv
[1], flags
);
912 else if (actions
== ACTION_GET_URLMATCH
) {
913 check_argc(argc
, 2, 2);
914 return get_urlmatch(argv
[0], argv
[1]);
916 else if (actions
== ACTION_UNSET
) {
918 check_argc(argc
, 1, 2);
920 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
921 argv
[0], NULL
, argv
[1],
924 return git_config_set_in_file_gently(given_config_source
.file
,
927 else if (actions
== ACTION_UNSET_ALL
) {
929 check_argc(argc
, 1, 2);
930 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
931 argv
[0], NULL
, argv
[1],
932 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
934 else if (actions
== ACTION_RENAME_SECTION
) {
937 check_argc(argc
, 2, 2);
938 ret
= git_config_rename_section_in_file(given_config_source
.file
,
943 die(_("no such section: %s"), argv
[0]);
945 else if (actions
== ACTION_REMOVE_SECTION
) {
948 check_argc(argc
, 1, 1);
949 ret
= git_config_rename_section_in_file(given_config_source
.file
,
954 die(_("no such section: %s"), argv
[0]);
956 else if (actions
== ACTION_GET_COLOR
) {
957 check_argc(argc
, 1, 2);
958 get_color(argv
[0], argv
[1]);
960 else if (actions
== ACTION_GET_COLORBOOL
) {
961 check_argc(argc
, 1, 2);
963 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
964 return get_colorbool(argv
[0], argc
== 2);