6 #include "environment.h"
9 #include "parse-options.h"
15 static const char *const builtin_config_usage
[] = {
16 N_("git config [<options>]"),
21 static regex_t
*key_regexp
;
22 static const char *value_pattern
;
23 static regex_t
*regexp
;
25 static int omit_values
;
26 static int use_key_regexp
;
28 static int do_not_match
;
29 static char delim
= '=';
30 static char key_delim
= ' ';
31 static char term
= '\n';
33 static int use_global_config
, use_system_config
, use_local_config
;
34 static int use_worktree_config
;
35 static struct git_config_source given_config_source
;
36 static int actions
, type
;
37 static char *default_value
;
39 static int respect_includes_opt
= -1;
40 static struct config_options config_options
;
41 static int show_origin
;
42 static int show_scope
;
43 static int fixed_value
;
45 #define ACTION_GET (1<<0)
46 #define ACTION_GET_ALL (1<<1)
47 #define ACTION_GET_REGEXP (1<<2)
48 #define ACTION_REPLACE_ALL (1<<3)
49 #define ACTION_ADD (1<<4)
50 #define ACTION_UNSET (1<<5)
51 #define ACTION_UNSET_ALL (1<<6)
52 #define ACTION_RENAME_SECTION (1<<7)
53 #define ACTION_REMOVE_SECTION (1<<8)
54 #define ACTION_LIST (1<<9)
55 #define ACTION_EDIT (1<<10)
56 #define ACTION_SET (1<<11)
57 #define ACTION_SET_ALL (1<<12)
58 #define ACTION_GET_COLOR (1<<13)
59 #define ACTION_GET_COLORBOOL (1<<14)
60 #define ACTION_GET_URLMATCH (1<<15)
63 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
64 * one line of output and which should therefore be paged.
66 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
67 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
71 #define TYPE_BOOL_OR_INT 3
73 #define TYPE_EXPIRY_DATE 5
75 #define TYPE_BOOL_OR_STR 7
77 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
78 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
79 PARSE_OPT_NONEG, option_parse_type, (i) }
81 static NORETURN
void usage_builtin_config(void);
83 static int option_parse_type(const struct option
*opt
, const char *arg
,
86 int new_type
, *to_type
;
89 *((int *) opt
->value
) = 0;
94 * To support '--<type>' style flags, begin with new_type equal to
97 new_type
= opt
->defval
;
99 if (!strcmp(arg
, "bool"))
100 new_type
= TYPE_BOOL
;
101 else if (!strcmp(arg
, "int"))
103 else if (!strcmp(arg
, "bool-or-int"))
104 new_type
= TYPE_BOOL_OR_INT
;
105 else if (!strcmp(arg
, "bool-or-str"))
106 new_type
= TYPE_BOOL_OR_STR
;
107 else if (!strcmp(arg
, "path"))
108 new_type
= TYPE_PATH
;
109 else if (!strcmp(arg
, "expiry-date"))
110 new_type
= TYPE_EXPIRY_DATE
;
111 else if (!strcmp(arg
, "color"))
112 new_type
= TYPE_COLOR
;
114 die(_("unrecognized --type argument, %s"), arg
);
117 to_type
= opt
->value
;
118 if (*to_type
&& *to_type
!= new_type
) {
120 * Complain when there is a new type not equal to the old type.
121 * This allows for combinations like '--int --type=int' and
122 * '--type=int --type=int', but disallows ones like '--type=bool
123 * --int' and '--type=bool
126 error(_("only one type at a time"));
127 usage_builtin_config();
134 static struct option builtin_config_options
[] = {
135 OPT_GROUP(N_("Config file location")),
136 OPT_BOOL(0, "global", &use_global_config
, N_("use global config file")),
137 OPT_BOOL(0, "system", &use_system_config
, N_("use system config file")),
138 OPT_BOOL(0, "local", &use_local_config
, N_("use repository config file")),
139 OPT_BOOL(0, "worktree", &use_worktree_config
, N_("use per-worktree config file")),
140 OPT_STRING('f', "file", &given_config_source
.file
, N_("file"), N_("use given config file")),
141 OPT_STRING(0, "blob", &given_config_source
.blob
, N_("blob-id"), N_("read config from given blob object")),
142 OPT_GROUP(N_("Action")),
143 OPT_BIT(0, "get", &actions
, N_("get value: name [value-pattern]"), ACTION_GET
),
144 OPT_BIT(0, "get-all", &actions
, N_("get all values: key [value-pattern]"), ACTION_GET_ALL
),
145 OPT_BIT(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP
),
146 OPT_BIT(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
147 OPT_BIT(0, "replace-all", &actions
, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL
),
148 OPT_BIT(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
149 OPT_BIT(0, "unset", &actions
, N_("remove a variable: name [value-pattern]"), ACTION_UNSET
),
150 OPT_BIT(0, "unset-all", &actions
, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL
),
151 OPT_BIT(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
152 OPT_BIT(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
153 OPT_BIT('l', "list", &actions
, N_("list all"), ACTION_LIST
),
154 OPT_BOOL(0, "fixed-value", &fixed_value
, N_("use string equality when comparing values to 'value-pattern'")),
155 OPT_BIT('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
156 OPT_BIT(0, "get-color", &actions
, N_("find the color configured: slot [default]"), ACTION_GET_COLOR
),
157 OPT_BIT(0, "get-colorbool", &actions
, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL
),
158 OPT_GROUP(N_("Type")),
159 OPT_CALLBACK('t', "type", &type
, N_("type"), N_("value is given this type"), option_parse_type
),
160 OPT_CALLBACK_VALUE(0, "bool", &type
, N_("value is \"true\" or \"false\""), TYPE_BOOL
),
161 OPT_CALLBACK_VALUE(0, "int", &type
, N_("value is decimal number"), TYPE_INT
),
162 OPT_CALLBACK_VALUE(0, "bool-or-int", &type
, N_("value is --bool or --int"), TYPE_BOOL_OR_INT
),
163 OPT_CALLBACK_VALUE(0, "bool-or-str", &type
, N_("value is --bool or string"), TYPE_BOOL_OR_STR
),
164 OPT_CALLBACK_VALUE(0, "path", &type
, N_("value is a path (file or directory name)"), TYPE_PATH
),
165 OPT_CALLBACK_VALUE(0, "expiry-date", &type
, N_("value is an expiry date"), TYPE_EXPIRY_DATE
),
166 OPT_GROUP(N_("Other")),
167 OPT_BOOL('z', "null", &end_nul
, N_("terminate values with NUL byte")),
168 OPT_BOOL(0, "name-only", &omit_values
, N_("show variable names only")),
169 OPT_BOOL(0, "includes", &respect_includes_opt
, N_("respect include directives on lookup")),
170 OPT_BOOL(0, "show-origin", &show_origin
, N_("show origin of config (file, standard input, blob, command line)")),
171 OPT_BOOL(0, "show-scope", &show_scope
, N_("show scope of config (worktree, local, global, system, command)")),
172 OPT_STRING(0, "default", &default_value
, N_("value"), N_("with --get, use default value when missing entry")),
176 static NORETURN
void usage_builtin_config(void)
178 usage_with_options(builtin_config_usage
, builtin_config_options
);
181 static void check_argc(int argc
, int min
, int max
)
183 if (argc
>= min
&& argc
<= max
)
186 error(_("wrong number of arguments, should be %d"), min
);
188 error(_("wrong number of arguments, should be from %d to %d"),
190 usage_builtin_config();
193 static void show_config_origin(struct strbuf
*buf
)
195 const char term
= end_nul
? '\0' : '\t';
197 strbuf_addstr(buf
, current_config_origin_type());
198 strbuf_addch(buf
, ':');
200 strbuf_addstr(buf
, current_config_name());
202 quote_c_style(current_config_name(), buf
, NULL
, 0);
203 strbuf_addch(buf
, term
);
206 static void show_config_scope(struct strbuf
*buf
)
208 const char term
= end_nul
? '\0' : '\t';
209 const char *scope
= config_scope_name(current_config_scope());
211 strbuf_addstr(buf
, N_(scope
));
212 strbuf_addch(buf
, term
);
215 static int show_all_config(const char *key_
, const char *value_
,
218 if (show_origin
|| show_scope
) {
219 struct strbuf buf
= STRBUF_INIT
;
221 show_config_scope(&buf
);
223 show_config_origin(&buf
);
224 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
225 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
226 strbuf_release(&buf
);
228 if (!omit_values
&& value_
)
229 printf("%s%c%s%c", key_
, delim
, value_
, term
);
231 printf("%s%c", key_
, term
);
236 struct strbuf
*items
;
241 static int format_config(struct strbuf
*buf
, const char *key_
, const char *value_
)
244 show_config_scope(buf
);
246 show_config_origin(buf
);
248 strbuf_addstr(buf
, key_
);
251 strbuf_addch(buf
, key_delim
);
253 if (type
== TYPE_INT
)
254 strbuf_addf(buf
, "%"PRId64
,
255 git_config_int64(key_
, value_
? value_
: ""));
256 else if (type
== TYPE_BOOL
)
257 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
259 else if (type
== TYPE_BOOL_OR_INT
) {
261 v
= git_config_bool_or_int(key_
, value_
, &is_bool
);
263 strbuf_addstr(buf
, v
? "true" : "false");
265 strbuf_addf(buf
, "%d", v
);
266 } else if (type
== TYPE_BOOL_OR_STR
) {
267 int v
= git_parse_maybe_bool(value_
);
269 strbuf_addstr(buf
, value_
);
271 strbuf_addstr(buf
, v
? "true" : "false");
272 } else if (type
== TYPE_PATH
) {
274 if (git_config_pathname(&v
, key_
, value_
) < 0)
276 strbuf_addstr(buf
, v
);
278 } else if (type
== TYPE_EXPIRY_DATE
) {
280 if (git_config_expiry_date(&t
, key_
, value_
) < 0)
282 strbuf_addf(buf
, "%"PRItime
, t
);
283 } else if (type
== TYPE_COLOR
) {
284 char v
[COLOR_MAXLEN
];
285 if (git_config_color(v
, key_
, value_
) < 0)
287 strbuf_addstr(buf
, v
);
289 strbuf_addstr(buf
, value_
);
291 /* Just show the key name; back out delimiter */
293 strbuf_setlen(buf
, buf
->len
- 1);
296 strbuf_addch(buf
, term
);
300 static int collect_config(const char *key_
, const char *value_
, void *cb
)
302 struct strbuf_list
*values
= cb
;
304 if (!use_key_regexp
&& strcmp(key_
, key
))
306 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
308 if (fixed_value
&& strcmp(value_pattern
, (value_
?value_
:"")))
310 if (regexp
!= NULL
&&
311 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
314 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
315 strbuf_init(&values
->items
[values
->nr
], 0);
317 return format_config(&values
->items
[values
->nr
++], key_
, value_
);
320 static int get_value(const char *key_
, const char *regex_
, unsigned flags
)
322 int ret
= CONFIG_GENERIC_ERROR
;
323 struct strbuf_list values
= {NULL
};
326 if (use_key_regexp
) {
330 * NEEDSWORK: this naive pattern lowercasing obviously does not
331 * work for more complex patterns like "^[^.]*Foo.*bar".
332 * Perhaps we should deprecate this altogether someday.
336 for (tl
= key
+ strlen(key
) - 1;
337 tl
>= key
&& *tl
!= '.';
340 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
343 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
344 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
345 error(_("invalid key pattern: %s"), key_
);
346 FREE_AND_NULL(key_regexp
);
347 ret
= CONFIG_INVALID_PATTERN
;
351 if (git_config_parse_key(key_
, &key
, NULL
)) {
352 ret
= CONFIG_INVALID_KEY
;
357 if (regex_
&& (flags
& CONFIG_FLAGS_FIXED_VALUE
))
358 value_pattern
= regex_
;
360 if (regex_
[0] == '!') {
365 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
366 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
367 error(_("invalid pattern: %s"), regex_
);
368 FREE_AND_NULL(regexp
);
369 ret
= CONFIG_INVALID_PATTERN
;
374 config_with_options(collect_config
, &values
,
375 &given_config_source
, &config_options
);
377 if (!values
.nr
&& default_value
) {
379 ALLOC_GROW(values
.items
, values
.nr
+ 1, values
.alloc
);
380 item
= &values
.items
[values
.nr
++];
381 strbuf_init(item
, 0);
382 if (format_config(item
, key_
, default_value
) < 0)
383 die(_("failed to format default config value: %s"),
389 for (i
= 0; i
< values
.nr
; i
++) {
390 struct strbuf
*buf
= values
.items
+ i
;
391 if (do_all
|| i
== values
.nr
- 1)
392 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
411 static char *normalize_value(const char *key
, const char *value
)
416 if (type
== 0 || type
== TYPE_PATH
|| type
== TYPE_EXPIRY_DATE
)
418 * We don't do normalization for TYPE_PATH here: If
419 * the path is like ~/foobar/, we prefer to store
420 * "~/foobar/" in the config file, and to expand the ~
421 * when retrieving the value.
422 * Also don't do normalization for expiry dates.
424 return xstrdup(value
);
425 if (type
== TYPE_INT
)
426 return xstrfmt("%"PRId64
, git_config_int64(key
, value
));
427 if (type
== TYPE_BOOL
)
428 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
429 if (type
== TYPE_BOOL_OR_INT
) {
431 v
= git_config_bool_or_int(key
, value
, &is_bool
);
433 return xstrfmt("%d", v
);
435 return xstrdup(v
? "true" : "false");
437 if (type
== TYPE_BOOL_OR_STR
) {
438 int v
= git_parse_maybe_bool(value
);
440 return xstrdup(value
);
442 return xstrdup(v
? "true" : "false");
444 if (type
== TYPE_COLOR
) {
445 char v
[COLOR_MAXLEN
];
446 if (git_config_color(v
, key
, value
))
447 die(_("cannot parse color '%s'"), value
);
450 * The contents of `v` now contain an ANSI escape
451 * sequence, not suitable for including within a
452 * configuration file. Treat the above as a
453 * "sanity-check", and return the given value, which we
454 * know is representable as valid color code.
456 return xstrdup(value
);
459 BUG("cannot normalize type %d", type
);
462 static int get_color_found
;
463 static const char *get_color_slot
;
464 static const char *get_colorbool_slot
;
465 static char parsed_color
[COLOR_MAXLEN
];
467 static int git_get_color_config(const char *var
, const char *value
,
470 if (!strcmp(var
, get_color_slot
)) {
472 config_error_nonbool(var
);
473 if (color_parse(value
, parsed_color
) < 0)
480 static void get_color(const char *var
, const char *def_color
)
482 get_color_slot
= var
;
484 parsed_color
[0] = '\0';
485 config_with_options(git_get_color_config
, NULL
,
486 &given_config_source
, &config_options
);
488 if (!get_color_found
&& def_color
) {
489 if (color_parse(def_color
, parsed_color
) < 0)
490 die(_("unable to parse default color value"));
493 fputs(parsed_color
, stdout
);
496 static int get_colorbool_found
;
497 static int get_diff_color_found
;
498 static int get_color_ui_found
;
499 static int git_get_colorbool_config(const char *var
, const char *value
,
502 if (!strcmp(var
, get_colorbool_slot
))
503 get_colorbool_found
= git_config_colorbool(var
, value
);
504 else if (!strcmp(var
, "diff.color"))
505 get_diff_color_found
= git_config_colorbool(var
, value
);
506 else if (!strcmp(var
, "color.ui"))
507 get_color_ui_found
= git_config_colorbool(var
, value
);
511 static int get_colorbool(const char *var
, int print
)
513 get_colorbool_slot
= var
;
514 get_colorbool_found
= -1;
515 get_diff_color_found
= -1;
516 get_color_ui_found
= -1;
517 config_with_options(git_get_colorbool_config
, NULL
,
518 &given_config_source
, &config_options
);
520 if (get_colorbool_found
< 0) {
521 if (!strcmp(get_colorbool_slot
, "color.diff"))
522 get_colorbool_found
= get_diff_color_found
;
523 if (get_colorbool_found
< 0)
524 get_colorbool_found
= get_color_ui_found
;
527 if (get_colorbool_found
< 0)
528 /* default value if none found in config */
529 get_colorbool_found
= GIT_COLOR_AUTO
;
531 get_colorbool_found
= want_color(get_colorbool_found
);
534 printf("%s\n", get_colorbool_found
? "true" : "false");
537 return get_colorbool_found
? 0 : 1;
540 static void check_write(void)
542 if (!given_config_source
.file
&& !startup_info
->have_repository
)
543 die(_("not in a git directory"));
545 if (given_config_source
.use_stdin
)
546 die(_("writing to stdin is not supported"));
548 if (given_config_source
.blob
)
549 die(_("writing config blobs is not supported"));
552 struct urlmatch_current_candidate_value
{
557 static int urlmatch_collect_fn(const char *var
, const char *value
, void *cb
)
559 struct string_list
*values
= cb
;
560 struct string_list_item
*item
= string_list_insert(values
, var
);
561 struct urlmatch_current_candidate_value
*matched
= item
->util
;
564 matched
= xmalloc(sizeof(*matched
));
565 strbuf_init(&matched
->value
, 0);
566 item
->util
= matched
;
568 strbuf_reset(&matched
->value
);
572 strbuf_addstr(&matched
->value
, value
);
573 matched
->value_is_null
= 0;
575 matched
->value_is_null
= 1;
580 static int get_urlmatch(const char *var
, const char *url
)
584 struct string_list_item
*item
;
585 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
586 struct string_list values
= STRING_LIST_INIT_DUP
;
588 config
.collect_fn
= urlmatch_collect_fn
;
589 config
.cascade_fn
= NULL
;
592 if (!url_normalize(url
, &config
.url
))
593 die("%s", config
.url
.err
);
595 config
.section
= xstrdup_tolower(var
);
596 section_tail
= strchr(config
.section
, '.');
598 *section_tail
= '\0';
599 config
.key
= section_tail
+ 1;
606 config_with_options(urlmatch_config_entry
, &config
,
607 &given_config_source
, &config_options
);
611 for_each_string_list_item(item
, &values
) {
612 struct urlmatch_current_candidate_value
*matched
= item
->util
;
613 struct strbuf buf
= STRBUF_INIT
;
615 format_config(&buf
, item
->string
,
616 matched
->value_is_null
? NULL
: matched
->value
.buf
);
617 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
618 strbuf_release(&buf
);
620 strbuf_release(&matched
->value
);
622 urlmatch_config_release(&config
);
623 string_list_clear(&values
, 1);
624 free(config
.url
.url
);
626 free((void *)config
.section
);
630 static char *default_user_config(void)
632 struct strbuf buf
= STRBUF_INIT
;
634 _("# This is Git's per-user configuration file.\n"
636 "# Please adapt and uncomment the following lines:\n"
639 ident_default_name(),
640 ident_default_email());
641 return strbuf_detach(&buf
, NULL
);
644 int cmd_config(int argc
, const char **argv
, const char *prefix
)
646 int nongit
= !startup_info
->have_repository
;
651 given_config_source
.file
= xstrdup_or_null(getenv(CONFIG_ENVIRONMENT
));
653 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
654 builtin_config_usage
,
655 PARSE_OPT_STOP_AT_NON_OPTION
);
657 if (use_global_config
+ use_system_config
+ use_local_config
+
658 use_worktree_config
+
659 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
660 error(_("only one config file at a time"));
661 usage_builtin_config();
665 if (use_local_config
)
666 die(_("--local can only be used inside a git repository"));
667 if (given_config_source
.blob
)
668 die(_("--blob can only be used inside a git repository"));
669 if (use_worktree_config
)
670 die(_("--worktree can only be used inside a git repository"));
674 if (given_config_source
.file
&&
675 !strcmp(given_config_source
.file
, "-")) {
676 given_config_source
.file
= NULL
;
677 given_config_source
.use_stdin
= 1;
678 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
681 if (use_global_config
) {
682 char *user_config
, *xdg_config
;
684 git_global_config(&user_config
, &xdg_config
);
687 * It is unknown if HOME/.gitconfig exists, so
688 * we do not know if we should write to XDG
689 * location; error out even if XDG_CONFIG_HOME
690 * is set and points at a sane location.
692 die(_("$HOME not set"));
694 given_config_source
.scope
= CONFIG_SCOPE_GLOBAL
;
696 if (access_or_warn(user_config
, R_OK
, 0) &&
697 xdg_config
&& !access_or_warn(xdg_config
, R_OK
, 0)) {
698 given_config_source
.file
= xdg_config
;
701 given_config_source
.file
= user_config
;
705 else if (use_system_config
) {
706 given_config_source
.file
= git_system_config();
707 given_config_source
.scope
= CONFIG_SCOPE_SYSTEM
;
708 } else if (use_local_config
) {
709 given_config_source
.file
= git_pathdup("config");
710 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
711 } else if (use_worktree_config
) {
712 struct worktree
**worktrees
= get_worktrees();
713 if (repository_format_worktree_config
)
714 given_config_source
.file
= git_pathdup("config.worktree");
715 else if (worktrees
[0] && worktrees
[1])
716 die(_("--worktree cannot be used with multiple "
717 "working trees unless the config\n"
718 "extension worktreeConfig is enabled. "
719 "Please read \"CONFIGURATION FILE\"\n"
720 "section in \"git help worktree\" for details"));
722 given_config_source
.file
= git_pathdup("config");
723 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
724 free_worktrees(worktrees
);
725 } else if (given_config_source
.file
) {
726 if (!is_absolute_path(given_config_source
.file
) && prefix
)
727 given_config_source
.file
=
728 prefix_filename(prefix
, given_config_source
.file
);
729 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
730 } else if (given_config_source
.blob
) {
731 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
735 if (respect_includes_opt
== -1)
736 config_options
.respect_includes
= !given_config_source
.file
;
738 config_options
.respect_includes
= respect_includes_opt
;
740 config_options
.commondir
= get_git_common_dir();
741 config_options
.git_dir
= get_git_dir();
750 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && type
) {
751 error(_("--get-color and variable type are incoherent"));
752 usage_builtin_config();
755 if (HAS_MULTI_BITS(actions
)) {
756 error(_("only one action at a time"));
757 usage_builtin_config();
761 case 1: actions
= ACTION_GET
; break;
762 case 2: actions
= ACTION_SET
; break;
763 case 3: actions
= ACTION_SET_ALL
; break;
765 usage_builtin_config();
768 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
769 error(_("--name-only is only applicable to --list or --get-regexp"));
770 usage_builtin_config();
773 if (show_origin
&& !(actions
&
774 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
775 error(_("--show-origin is only applicable to --get, --get-all, "
776 "--get-regexp, and --list"));
777 usage_builtin_config();
780 if (default_value
&& !(actions
& ACTION_GET
)) {
781 error(_("--default is only applicable to --get"));
782 usage_builtin_config();
785 /* check usage of --fixed-value */
787 int allowed_usage
= 0;
790 /* git config --get <name> <value-pattern> */
792 /* git config --get-all <name> <value-pattern> */
794 /* git config --get-regexp <name-pattern> <value-pattern> */
795 case ACTION_GET_REGEXP
:
796 /* git config --unset <name> <value-pattern> */
798 /* git config --unset-all <name> <value-pattern> */
799 case ACTION_UNSET_ALL
:
800 allowed_usage
= argc
> 1 && !!argv
[1];
803 /* git config <name> <value> <value-pattern> */
805 /* git config --replace-all <name> <value> <value-pattern> */
806 case ACTION_REPLACE_ALL
:
807 allowed_usage
= argc
> 2 && !!argv
[2];
810 /* other options don't allow --fixed-value */
813 if (!allowed_usage
) {
814 error(_("--fixed-value only applies with 'value-pattern'"));
815 usage_builtin_config();
818 flags
|= CONFIG_FLAGS_FIXED_VALUE
;
821 if (actions
& PAGING_ACTIONS
)
822 setup_auto_pager("config", 1);
824 if (actions
== ACTION_LIST
) {
825 check_argc(argc
, 0, 0);
826 if (config_with_options(show_all_config
, NULL
,
827 &given_config_source
,
828 &config_options
) < 0) {
829 if (given_config_source
.file
)
830 die_errno(_("unable to read config file '%s'"),
831 given_config_source
.file
);
833 die(_("error processing config file(s)"));
836 else if (actions
== ACTION_EDIT
) {
839 check_argc(argc
, 0, 0);
840 if (!given_config_source
.file
&& nongit
)
841 die(_("not in a git directory"));
842 if (given_config_source
.use_stdin
)
843 die(_("editing stdin is not supported"));
844 if (given_config_source
.blob
)
845 die(_("editing blobs is not supported"));
846 git_config(git_default_config
, NULL
);
847 config_file
= given_config_source
.file
?
848 xstrdup(given_config_source
.file
) :
849 git_pathdup("config");
850 if (use_global_config
) {
851 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
853 char *content
= default_user_config();
854 write_str_in_full(fd
, content
);
858 else if (errno
!= EEXIST
)
859 die_errno(_("cannot create configuration file %s"), config_file
);
861 launch_editor(config_file
, NULL
, NULL
);
864 else if (actions
== ACTION_SET
) {
866 check_argc(argc
, 2, 2);
867 value
= normalize_value(argv
[0], argv
[1]);
868 ret
= git_config_set_in_file_gently(given_config_source
.file
, argv
[0], value
);
869 if (ret
== CONFIG_NOTHING_SET
)
870 error(_("cannot overwrite multiple values with a single value\n"
871 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
873 else if (actions
== ACTION_SET_ALL
) {
875 check_argc(argc
, 2, 3);
876 value
= normalize_value(argv
[0], argv
[1]);
877 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
878 argv
[0], value
, argv
[2],
881 else if (actions
== ACTION_ADD
) {
883 check_argc(argc
, 2, 2);
884 value
= normalize_value(argv
[0], argv
[1]);
885 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
890 else if (actions
== ACTION_REPLACE_ALL
) {
892 check_argc(argc
, 2, 3);
893 value
= normalize_value(argv
[0], argv
[1]);
894 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
895 argv
[0], value
, argv
[2],
896 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
898 else if (actions
== ACTION_GET
) {
899 check_argc(argc
, 1, 2);
900 return get_value(argv
[0], argv
[1], flags
);
902 else if (actions
== ACTION_GET_ALL
) {
904 check_argc(argc
, 1, 2);
905 return get_value(argv
[0], argv
[1], flags
);
907 else if (actions
== ACTION_GET_REGEXP
) {
911 check_argc(argc
, 1, 2);
912 return get_value(argv
[0], argv
[1], flags
);
914 else if (actions
== ACTION_GET_URLMATCH
) {
915 check_argc(argc
, 2, 2);
916 return get_urlmatch(argv
[0], argv
[1]);
918 else if (actions
== ACTION_UNSET
) {
920 check_argc(argc
, 1, 2);
922 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
923 argv
[0], NULL
, argv
[1],
926 return git_config_set_in_file_gently(given_config_source
.file
,
929 else if (actions
== ACTION_UNSET_ALL
) {
931 check_argc(argc
, 1, 2);
932 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
933 argv
[0], NULL
, argv
[1],
934 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
936 else if (actions
== ACTION_RENAME_SECTION
) {
938 check_argc(argc
, 2, 2);
939 ret
= git_config_rename_section_in_file(given_config_source
.file
,
944 die(_("no such section: %s"), argv
[0]);
948 else if (actions
== ACTION_REMOVE_SECTION
) {
950 check_argc(argc
, 1, 1);
951 ret
= git_config_rename_section_in_file(given_config_source
.file
,
956 die(_("no such section: %s"), argv
[0]);
960 else if (actions
== ACTION_GET_COLOR
) {
961 check_argc(argc
, 1, 2);
962 get_color(argv
[0], argv
[1]);
964 else if (actions
== ACTION_GET_COLORBOOL
) {
965 check_argc(argc
, 1, 2);
967 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
968 return get_colorbool(argv
[0], argc
== 2);