6 #include "environment.h"
9 #include "parse-options.h"
16 static const char *const builtin_config_usage
[] = {
17 N_("git config [<options>]"),
22 static regex_t
*key_regexp
;
23 static const char *value_pattern
;
24 static regex_t
*regexp
;
26 static int omit_values
;
27 static int use_key_regexp
;
29 static int do_not_match
;
30 static char delim
= '=';
31 static char key_delim
= ' ';
32 static char term
= '\n';
34 static int use_global_config
, use_system_config
, use_local_config
;
35 static int use_worktree_config
;
36 static struct git_config_source given_config_source
;
37 static int actions
, type
;
38 static char *default_value
;
40 static int respect_includes_opt
= -1;
41 static struct config_options config_options
;
42 static int show_origin
;
43 static int show_scope
;
44 static int fixed_value
;
46 #define ACTION_GET (1<<0)
47 #define ACTION_GET_ALL (1<<1)
48 #define ACTION_GET_REGEXP (1<<2)
49 #define ACTION_REPLACE_ALL (1<<3)
50 #define ACTION_ADD (1<<4)
51 #define ACTION_UNSET (1<<5)
52 #define ACTION_UNSET_ALL (1<<6)
53 #define ACTION_RENAME_SECTION (1<<7)
54 #define ACTION_REMOVE_SECTION (1<<8)
55 #define ACTION_LIST (1<<9)
56 #define ACTION_EDIT (1<<10)
57 #define ACTION_SET (1<<11)
58 #define ACTION_SET_ALL (1<<12)
59 #define ACTION_GET_COLOR (1<<13)
60 #define ACTION_GET_COLORBOOL (1<<14)
61 #define ACTION_GET_URLMATCH (1<<15)
64 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
65 * one line of output and which should therefore be paged.
67 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
68 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
72 #define TYPE_BOOL_OR_INT 3
74 #define TYPE_EXPIRY_DATE 5
76 #define TYPE_BOOL_OR_STR 7
78 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
79 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
80 PARSE_OPT_NONEG, option_parse_type, (i) }
82 static NORETURN
void usage_builtin_config(void);
84 static int option_parse_type(const struct option
*opt
, const char *arg
,
87 int new_type
, *to_type
;
90 *((int *) opt
->value
) = 0;
95 * To support '--<type>' style flags, begin with new_type equal to
98 new_type
= opt
->defval
;
100 if (!strcmp(arg
, "bool"))
101 new_type
= TYPE_BOOL
;
102 else if (!strcmp(arg
, "int"))
104 else if (!strcmp(arg
, "bool-or-int"))
105 new_type
= TYPE_BOOL_OR_INT
;
106 else if (!strcmp(arg
, "bool-or-str"))
107 new_type
= TYPE_BOOL_OR_STR
;
108 else if (!strcmp(arg
, "path"))
109 new_type
= TYPE_PATH
;
110 else if (!strcmp(arg
, "expiry-date"))
111 new_type
= TYPE_EXPIRY_DATE
;
112 else if (!strcmp(arg
, "color"))
113 new_type
= TYPE_COLOR
;
115 die(_("unrecognized --type argument, %s"), arg
);
118 to_type
= opt
->value
;
119 if (*to_type
&& *to_type
!= new_type
) {
121 * Complain when there is a new type not equal to the old type.
122 * This allows for combinations like '--int --type=int' and
123 * '--type=int --type=int', but disallows ones like '--type=bool
124 * --int' and '--type=bool
127 error(_("only one type at a time"));
128 usage_builtin_config();
135 static struct option builtin_config_options
[] = {
136 OPT_GROUP(N_("Config file location")),
137 OPT_BOOL(0, "global", &use_global_config
, N_("use global config file")),
138 OPT_BOOL(0, "system", &use_system_config
, N_("use system config file")),
139 OPT_BOOL(0, "local", &use_local_config
, N_("use repository config file")),
140 OPT_BOOL(0, "worktree", &use_worktree_config
, N_("use per-worktree config file")),
141 OPT_STRING('f', "file", &given_config_source
.file
, N_("file"), N_("use given config file")),
142 OPT_STRING(0, "blob", &given_config_source
.blob
, N_("blob-id"), N_("read config from given blob object")),
143 OPT_GROUP(N_("Action")),
144 OPT_BIT(0, "get", &actions
, N_("get value: name [value-pattern]"), ACTION_GET
),
145 OPT_BIT(0, "get-all", &actions
, N_("get all values: key [value-pattern]"), ACTION_GET_ALL
),
146 OPT_BIT(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP
),
147 OPT_BIT(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
148 OPT_BIT(0, "replace-all", &actions
, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL
),
149 OPT_BIT(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
150 OPT_BIT(0, "unset", &actions
, N_("remove a variable: name [value-pattern]"), ACTION_UNSET
),
151 OPT_BIT(0, "unset-all", &actions
, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL
),
152 OPT_BIT(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
153 OPT_BIT(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
154 OPT_BIT('l', "list", &actions
, N_("list all"), ACTION_LIST
),
155 OPT_BOOL(0, "fixed-value", &fixed_value
, N_("use string equality when comparing values to 'value-pattern'")),
156 OPT_BIT('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
157 OPT_BIT(0, "get-color", &actions
, N_("find the color configured: slot [default]"), ACTION_GET_COLOR
),
158 OPT_BIT(0, "get-colorbool", &actions
, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL
),
159 OPT_GROUP(N_("Type")),
160 OPT_CALLBACK('t', "type", &type
, N_("type"), N_("value is given this type"), option_parse_type
),
161 OPT_CALLBACK_VALUE(0, "bool", &type
, N_("value is \"true\" or \"false\""), TYPE_BOOL
),
162 OPT_CALLBACK_VALUE(0, "int", &type
, N_("value is decimal number"), TYPE_INT
),
163 OPT_CALLBACK_VALUE(0, "bool-or-int", &type
, N_("value is --bool or --int"), TYPE_BOOL_OR_INT
),
164 OPT_CALLBACK_VALUE(0, "bool-or-str", &type
, N_("value is --bool or string"), TYPE_BOOL_OR_STR
),
165 OPT_CALLBACK_VALUE(0, "path", &type
, N_("value is a path (file or directory name)"), TYPE_PATH
),
166 OPT_CALLBACK_VALUE(0, "expiry-date", &type
, N_("value is an expiry date"), TYPE_EXPIRY_DATE
),
167 OPT_GROUP(N_("Other")),
168 OPT_BOOL('z', "null", &end_nul
, N_("terminate values with NUL byte")),
169 OPT_BOOL(0, "name-only", &omit_values
, N_("show variable names only")),
170 OPT_BOOL(0, "includes", &respect_includes_opt
, N_("respect include directives on lookup")),
171 OPT_BOOL(0, "show-origin", &show_origin
, N_("show origin of config (file, standard input, blob, command line)")),
172 OPT_BOOL(0, "show-scope", &show_scope
, N_("show scope of config (worktree, local, global, system, command)")),
173 OPT_STRING(0, "default", &default_value
, N_("value"), N_("with --get, use default value when missing entry")),
177 static NORETURN
void usage_builtin_config(void)
179 usage_with_options(builtin_config_usage
, builtin_config_options
);
182 static void check_argc(int argc
, int min
, int max
)
184 if (argc
>= min
&& argc
<= max
)
187 error(_("wrong number of arguments, should be %d"), min
);
189 error(_("wrong number of arguments, should be from %d to %d"),
191 usage_builtin_config();
194 static void show_config_origin(struct strbuf
*buf
)
196 const char term
= end_nul
? '\0' : '\t';
198 strbuf_addstr(buf
, current_config_origin_type());
199 strbuf_addch(buf
, ':');
201 strbuf_addstr(buf
, current_config_name());
203 quote_c_style(current_config_name(), buf
, NULL
, 0);
204 strbuf_addch(buf
, term
);
207 static void show_config_scope(struct strbuf
*buf
)
209 const char term
= end_nul
? '\0' : '\t';
210 const char *scope
= config_scope_name(current_config_scope());
212 strbuf_addstr(buf
, N_(scope
));
213 strbuf_addch(buf
, term
);
216 static int show_all_config(const char *key_
, const char *value_
,
219 if (show_origin
|| show_scope
) {
220 struct strbuf buf
= STRBUF_INIT
;
222 show_config_scope(&buf
);
224 show_config_origin(&buf
);
225 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
226 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
227 strbuf_release(&buf
);
229 if (!omit_values
&& value_
)
230 printf("%s%c%s%c", key_
, delim
, value_
, term
);
232 printf("%s%c", key_
, term
);
237 struct strbuf
*items
;
242 static int format_config(struct strbuf
*buf
, const char *key_
, const char *value_
)
245 show_config_scope(buf
);
247 show_config_origin(buf
);
249 strbuf_addstr(buf
, key_
);
252 strbuf_addch(buf
, key_delim
);
254 if (type
== TYPE_INT
)
255 strbuf_addf(buf
, "%"PRId64
,
256 git_config_int64(key_
, value_
? value_
: ""));
257 else if (type
== TYPE_BOOL
)
258 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
260 else if (type
== TYPE_BOOL_OR_INT
) {
262 v
= git_config_bool_or_int(key_
, value_
, &is_bool
);
264 strbuf_addstr(buf
, v
? "true" : "false");
266 strbuf_addf(buf
, "%d", v
);
267 } else if (type
== TYPE_BOOL_OR_STR
) {
268 int v
= git_parse_maybe_bool(value_
);
270 strbuf_addstr(buf
, value_
);
272 strbuf_addstr(buf
, v
? "true" : "false");
273 } else if (type
== TYPE_PATH
) {
275 if (git_config_pathname(&v
, key_
, value_
) < 0)
277 strbuf_addstr(buf
, v
);
279 } else if (type
== TYPE_EXPIRY_DATE
) {
281 if (git_config_expiry_date(&t
, key_
, value_
) < 0)
283 strbuf_addf(buf
, "%"PRItime
, t
);
284 } else if (type
== TYPE_COLOR
) {
285 char v
[COLOR_MAXLEN
];
286 if (git_config_color(v
, key_
, value_
) < 0)
288 strbuf_addstr(buf
, v
);
290 strbuf_addstr(buf
, value_
);
292 /* Just show the key name; back out delimiter */
294 strbuf_setlen(buf
, buf
->len
- 1);
297 strbuf_addch(buf
, term
);
301 static int collect_config(const char *key_
, const char *value_
, void *cb
)
303 struct strbuf_list
*values
= cb
;
305 if (!use_key_regexp
&& strcmp(key_
, key
))
307 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
309 if (fixed_value
&& strcmp(value_pattern
, (value_
?value_
:"")))
311 if (regexp
!= NULL
&&
312 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
315 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
316 strbuf_init(&values
->items
[values
->nr
], 0);
318 return format_config(&values
->items
[values
->nr
++], key_
, value_
);
321 static int get_value(const char *key_
, const char *regex_
, unsigned flags
)
323 int ret
= CONFIG_GENERIC_ERROR
;
324 struct strbuf_list values
= {NULL
};
327 if (use_key_regexp
) {
331 * NEEDSWORK: this naive pattern lowercasing obviously does not
332 * work for more complex patterns like "^[^.]*Foo.*bar".
333 * Perhaps we should deprecate this altogether someday.
337 for (tl
= key
+ strlen(key
) - 1;
338 tl
>= key
&& *tl
!= '.';
341 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
344 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
345 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
346 error(_("invalid key pattern: %s"), key_
);
347 FREE_AND_NULL(key_regexp
);
348 ret
= CONFIG_INVALID_PATTERN
;
352 if (git_config_parse_key(key_
, &key
, NULL
)) {
353 ret
= CONFIG_INVALID_KEY
;
358 if (regex_
&& (flags
& CONFIG_FLAGS_FIXED_VALUE
))
359 value_pattern
= regex_
;
361 if (regex_
[0] == '!') {
366 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
367 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
368 error(_("invalid pattern: %s"), regex_
);
369 FREE_AND_NULL(regexp
);
370 ret
= CONFIG_INVALID_PATTERN
;
375 config_with_options(collect_config
, &values
,
376 &given_config_source
, &config_options
);
378 if (!values
.nr
&& default_value
) {
380 ALLOC_GROW(values
.items
, values
.nr
+ 1, values
.alloc
);
381 item
= &values
.items
[values
.nr
++];
382 strbuf_init(item
, 0);
383 if (format_config(item
, key_
, default_value
) < 0)
384 die(_("failed to format default config value: %s"),
390 for (i
= 0; i
< values
.nr
; i
++) {
391 struct strbuf
*buf
= values
.items
+ i
;
392 if (do_all
|| i
== values
.nr
- 1)
393 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
412 static char *normalize_value(const char *key
, const char *value
)
417 if (type
== 0 || type
== TYPE_PATH
|| type
== TYPE_EXPIRY_DATE
)
419 * We don't do normalization for TYPE_PATH here: If
420 * the path is like ~/foobar/, we prefer to store
421 * "~/foobar/" in the config file, and to expand the ~
422 * when retrieving the value.
423 * Also don't do normalization for expiry dates.
425 return xstrdup(value
);
426 if (type
== TYPE_INT
)
427 return xstrfmt("%"PRId64
, git_config_int64(key
, value
));
428 if (type
== TYPE_BOOL
)
429 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
430 if (type
== TYPE_BOOL_OR_INT
) {
432 v
= git_config_bool_or_int(key
, value
, &is_bool
);
434 return xstrfmt("%d", v
);
436 return xstrdup(v
? "true" : "false");
438 if (type
== TYPE_BOOL_OR_STR
) {
439 int v
= git_parse_maybe_bool(value
);
441 return xstrdup(value
);
443 return xstrdup(v
? "true" : "false");
445 if (type
== TYPE_COLOR
) {
446 char v
[COLOR_MAXLEN
];
447 if (git_config_color(v
, key
, value
))
448 die(_("cannot parse color '%s'"), value
);
451 * The contents of `v` now contain an ANSI escape
452 * sequence, not suitable for including within a
453 * configuration file. Treat the above as a
454 * "sanity-check", and return the given value, which we
455 * know is representable as valid color code.
457 return xstrdup(value
);
460 BUG("cannot normalize type %d", type
);
463 static int get_color_found
;
464 static const char *get_color_slot
;
465 static const char *get_colorbool_slot
;
466 static char parsed_color
[COLOR_MAXLEN
];
468 static int git_get_color_config(const char *var
, const char *value
,
471 if (!strcmp(var
, get_color_slot
)) {
473 config_error_nonbool(var
);
474 if (color_parse(value
, parsed_color
) < 0)
481 static void get_color(const char *var
, const char *def_color
)
483 get_color_slot
= var
;
485 parsed_color
[0] = '\0';
486 config_with_options(git_get_color_config
, NULL
,
487 &given_config_source
, &config_options
);
489 if (!get_color_found
&& def_color
) {
490 if (color_parse(def_color
, parsed_color
) < 0)
491 die(_("unable to parse default color value"));
494 fputs(parsed_color
, stdout
);
497 static int get_colorbool_found
;
498 static int get_diff_color_found
;
499 static int get_color_ui_found
;
500 static int git_get_colorbool_config(const char *var
, const char *value
,
503 if (!strcmp(var
, get_colorbool_slot
))
504 get_colorbool_found
= git_config_colorbool(var
, value
);
505 else if (!strcmp(var
, "diff.color"))
506 get_diff_color_found
= git_config_colorbool(var
, value
);
507 else if (!strcmp(var
, "color.ui"))
508 get_color_ui_found
= git_config_colorbool(var
, value
);
512 static int get_colorbool(const char *var
, int print
)
514 get_colorbool_slot
= var
;
515 get_colorbool_found
= -1;
516 get_diff_color_found
= -1;
517 get_color_ui_found
= -1;
518 config_with_options(git_get_colorbool_config
, NULL
,
519 &given_config_source
, &config_options
);
521 if (get_colorbool_found
< 0) {
522 if (!strcmp(get_colorbool_slot
, "color.diff"))
523 get_colorbool_found
= get_diff_color_found
;
524 if (get_colorbool_found
< 0)
525 get_colorbool_found
= get_color_ui_found
;
528 if (get_colorbool_found
< 0)
529 /* default value if none found in config */
530 get_colorbool_found
= GIT_COLOR_AUTO
;
532 get_colorbool_found
= want_color(get_colorbool_found
);
535 printf("%s\n", get_colorbool_found
? "true" : "false");
538 return get_colorbool_found
? 0 : 1;
541 static void check_write(void)
543 if (!given_config_source
.file
&& !startup_info
->have_repository
)
544 die(_("not in a git directory"));
546 if (given_config_source
.use_stdin
)
547 die(_("writing to stdin is not supported"));
549 if (given_config_source
.blob
)
550 die(_("writing config blobs is not supported"));
553 struct urlmatch_current_candidate_value
{
558 static int urlmatch_collect_fn(const char *var
, const char *value
, void *cb
)
560 struct string_list
*values
= cb
;
561 struct string_list_item
*item
= string_list_insert(values
, var
);
562 struct urlmatch_current_candidate_value
*matched
= item
->util
;
565 matched
= xmalloc(sizeof(*matched
));
566 strbuf_init(&matched
->value
, 0);
567 item
->util
= matched
;
569 strbuf_reset(&matched
->value
);
573 strbuf_addstr(&matched
->value
, value
);
574 matched
->value_is_null
= 0;
576 matched
->value_is_null
= 1;
581 static int get_urlmatch(const char *var
, const char *url
)
585 struct string_list_item
*item
;
586 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
587 struct string_list values
= STRING_LIST_INIT_DUP
;
589 config
.collect_fn
= urlmatch_collect_fn
;
590 config
.cascade_fn
= NULL
;
593 if (!url_normalize(url
, &config
.url
))
594 die("%s", config
.url
.err
);
596 config
.section
= xstrdup_tolower(var
);
597 section_tail
= strchr(config
.section
, '.');
599 *section_tail
= '\0';
600 config
.key
= section_tail
+ 1;
607 config_with_options(urlmatch_config_entry
, &config
,
608 &given_config_source
, &config_options
);
612 for_each_string_list_item(item
, &values
) {
613 struct urlmatch_current_candidate_value
*matched
= item
->util
;
614 struct strbuf buf
= STRBUF_INIT
;
616 format_config(&buf
, item
->string
,
617 matched
->value_is_null
? NULL
: matched
->value
.buf
);
618 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
619 strbuf_release(&buf
);
621 strbuf_release(&matched
->value
);
623 urlmatch_config_release(&config
);
624 string_list_clear(&values
, 1);
625 free(config
.url
.url
);
627 free((void *)config
.section
);
631 static char *default_user_config(void)
633 struct strbuf buf
= STRBUF_INIT
;
635 _("# This is Git's per-user configuration file.\n"
637 "# Please adapt and uncomment the following lines:\n"
640 ident_default_name(),
641 ident_default_email());
642 return strbuf_detach(&buf
, NULL
);
645 int cmd_config(int argc
, const char **argv
, const char *prefix
)
647 int nongit
= !startup_info
->have_repository
;
652 given_config_source
.file
= xstrdup_or_null(getenv(CONFIG_ENVIRONMENT
));
654 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
655 builtin_config_usage
,
656 PARSE_OPT_STOP_AT_NON_OPTION
);
658 if (use_global_config
+ use_system_config
+ use_local_config
+
659 use_worktree_config
+
660 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
661 error(_("only one config file at a time"));
662 usage_builtin_config();
666 if (use_local_config
)
667 die(_("--local can only be used inside a git repository"));
668 if (given_config_source
.blob
)
669 die(_("--blob can only be used inside a git repository"));
670 if (use_worktree_config
)
671 die(_("--worktree can only be used inside a git repository"));
675 if (given_config_source
.file
&&
676 !strcmp(given_config_source
.file
, "-")) {
677 given_config_source
.file
= NULL
;
678 given_config_source
.use_stdin
= 1;
679 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
682 if (use_global_config
) {
683 char *user_config
, *xdg_config
;
685 git_global_config(&user_config
, &xdg_config
);
688 * It is unknown if HOME/.gitconfig exists, so
689 * we do not know if we should write to XDG
690 * location; error out even if XDG_CONFIG_HOME
691 * is set and points at a sane location.
693 die(_("$HOME not set"));
695 given_config_source
.scope
= CONFIG_SCOPE_GLOBAL
;
697 if (access_or_warn(user_config
, R_OK
, 0) &&
698 xdg_config
&& !access_or_warn(xdg_config
, R_OK
, 0)) {
699 given_config_source
.file
= xdg_config
;
702 given_config_source
.file
= user_config
;
706 else if (use_system_config
) {
707 given_config_source
.file
= git_system_config();
708 given_config_source
.scope
= CONFIG_SCOPE_SYSTEM
;
709 } else if (use_local_config
) {
710 given_config_source
.file
= git_pathdup("config");
711 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
712 } else if (use_worktree_config
) {
713 struct worktree
**worktrees
= get_worktrees();
714 if (repository_format_worktree_config
)
715 given_config_source
.file
= git_pathdup("config.worktree");
716 else if (worktrees
[0] && worktrees
[1])
717 die(_("--worktree cannot be used with multiple "
718 "working trees unless the config\n"
719 "extension worktreeConfig is enabled. "
720 "Please read \"CONFIGURATION FILE\"\n"
721 "section in \"git help worktree\" for details"));
723 given_config_source
.file
= git_pathdup("config");
724 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
725 free_worktrees(worktrees
);
726 } else if (given_config_source
.file
) {
727 if (!is_absolute_path(given_config_source
.file
) && prefix
)
728 given_config_source
.file
=
729 prefix_filename(prefix
, given_config_source
.file
);
730 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
731 } else if (given_config_source
.blob
) {
732 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
736 if (respect_includes_opt
== -1)
737 config_options
.respect_includes
= !given_config_source
.file
;
739 config_options
.respect_includes
= respect_includes_opt
;
741 config_options
.commondir
= get_git_common_dir();
742 config_options
.git_dir
= get_git_dir();
751 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && type
) {
752 error(_("--get-color and variable type are incoherent"));
753 usage_builtin_config();
756 if (HAS_MULTI_BITS(actions
)) {
757 error(_("only one action at a time"));
758 usage_builtin_config();
762 case 1: actions
= ACTION_GET
; break;
763 case 2: actions
= ACTION_SET
; break;
764 case 3: actions
= ACTION_SET_ALL
; break;
766 usage_builtin_config();
769 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
770 error(_("--name-only is only applicable to --list or --get-regexp"));
771 usage_builtin_config();
774 if (show_origin
&& !(actions
&
775 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
776 error(_("--show-origin is only applicable to --get, --get-all, "
777 "--get-regexp, and --list"));
778 usage_builtin_config();
781 if (default_value
&& !(actions
& ACTION_GET
)) {
782 error(_("--default is only applicable to --get"));
783 usage_builtin_config();
786 /* check usage of --fixed-value */
788 int allowed_usage
= 0;
791 /* git config --get <name> <value-pattern> */
793 /* git config --get-all <name> <value-pattern> */
795 /* git config --get-regexp <name-pattern> <value-pattern> */
796 case ACTION_GET_REGEXP
:
797 /* git config --unset <name> <value-pattern> */
799 /* git config --unset-all <name> <value-pattern> */
800 case ACTION_UNSET_ALL
:
801 allowed_usage
= argc
> 1 && !!argv
[1];
804 /* git config <name> <value> <value-pattern> */
806 /* git config --replace-all <name> <value> <value-pattern> */
807 case ACTION_REPLACE_ALL
:
808 allowed_usage
= argc
> 2 && !!argv
[2];
811 /* other options don't allow --fixed-value */
814 if (!allowed_usage
) {
815 error(_("--fixed-value only applies with 'value-pattern'"));
816 usage_builtin_config();
819 flags
|= CONFIG_FLAGS_FIXED_VALUE
;
822 if (actions
& PAGING_ACTIONS
)
823 setup_auto_pager("config", 1);
825 if (actions
== ACTION_LIST
) {
826 check_argc(argc
, 0, 0);
827 if (config_with_options(show_all_config
, NULL
,
828 &given_config_source
,
829 &config_options
) < 0) {
830 if (given_config_source
.file
)
831 die_errno(_("unable to read config file '%s'"),
832 given_config_source
.file
);
834 die(_("error processing config file(s)"));
837 else if (actions
== ACTION_EDIT
) {
840 check_argc(argc
, 0, 0);
841 if (!given_config_source
.file
&& nongit
)
842 die(_("not in a git directory"));
843 if (given_config_source
.use_stdin
)
844 die(_("editing stdin is not supported"));
845 if (given_config_source
.blob
)
846 die(_("editing blobs is not supported"));
847 git_config(git_default_config
, NULL
);
848 config_file
= given_config_source
.file
?
849 xstrdup(given_config_source
.file
) :
850 git_pathdup("config");
851 if (use_global_config
) {
852 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
854 char *content
= default_user_config();
855 write_str_in_full(fd
, content
);
859 else if (errno
!= EEXIST
)
860 die_errno(_("cannot create configuration file %s"), config_file
);
862 launch_editor(config_file
, NULL
, NULL
);
865 else if (actions
== ACTION_SET
) {
867 check_argc(argc
, 2, 2);
868 value
= normalize_value(argv
[0], argv
[1]);
869 ret
= git_config_set_in_file_gently(given_config_source
.file
, argv
[0], value
);
870 if (ret
== CONFIG_NOTHING_SET
)
871 error(_("cannot overwrite multiple values with a single value\n"
872 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
874 else if (actions
== ACTION_SET_ALL
) {
876 check_argc(argc
, 2, 3);
877 value
= normalize_value(argv
[0], argv
[1]);
878 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
879 argv
[0], value
, argv
[2],
882 else if (actions
== ACTION_ADD
) {
884 check_argc(argc
, 2, 2);
885 value
= normalize_value(argv
[0], argv
[1]);
886 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
891 else if (actions
== ACTION_REPLACE_ALL
) {
893 check_argc(argc
, 2, 3);
894 value
= normalize_value(argv
[0], argv
[1]);
895 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
896 argv
[0], value
, argv
[2],
897 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
899 else if (actions
== ACTION_GET
) {
900 check_argc(argc
, 1, 2);
901 return get_value(argv
[0], argv
[1], flags
);
903 else if (actions
== ACTION_GET_ALL
) {
905 check_argc(argc
, 1, 2);
906 return get_value(argv
[0], argv
[1], flags
);
908 else if (actions
== ACTION_GET_REGEXP
) {
912 check_argc(argc
, 1, 2);
913 return get_value(argv
[0], argv
[1], flags
);
915 else if (actions
== ACTION_GET_URLMATCH
) {
916 check_argc(argc
, 2, 2);
917 return get_urlmatch(argv
[0], argv
[1]);
919 else if (actions
== ACTION_UNSET
) {
921 check_argc(argc
, 1, 2);
923 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
924 argv
[0], NULL
, argv
[1],
927 return git_config_set_in_file_gently(given_config_source
.file
,
930 else if (actions
== ACTION_UNSET_ALL
) {
932 check_argc(argc
, 1, 2);
933 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
934 argv
[0], NULL
, argv
[1],
935 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
937 else if (actions
== ACTION_RENAME_SECTION
) {
939 check_argc(argc
, 2, 2);
940 ret
= git_config_rename_section_in_file(given_config_source
.file
,
945 die(_("no such section: %s"), argv
[0]);
949 else if (actions
== ACTION_REMOVE_SECTION
) {
951 check_argc(argc
, 1, 1);
952 ret
= git_config_rename_section_in_file(given_config_source
.file
,
957 die(_("no such section: %s"), argv
[0]);
961 else if (actions
== ACTION_GET_COLOR
) {
962 check_argc(argc
, 1, 2);
963 get_color(argv
[0], argv
[1]);
965 else if (actions
== ACTION_GET_COLORBOOL
) {
966 check_argc(argc
, 1, 2);
968 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
969 return get_colorbool(argv
[0], argc
== 2);