7 #include "environment.h"
8 #include "repository.h"
11 #include "parse-options.h"
19 static const char *const builtin_config_usage
[] = {
20 N_("git config [<options>]"),
25 static regex_t
*key_regexp
;
26 static const char *value_pattern
;
27 static regex_t
*regexp
;
29 static int omit_values
;
30 static int use_key_regexp
;
32 static int do_not_match
;
33 static char delim
= '=';
34 static char key_delim
= ' ';
35 static char term
= '\n';
37 static int use_global_config
, use_system_config
, use_local_config
;
38 static int use_worktree_config
;
39 static struct git_config_source given_config_source
;
40 static int actions
, type
;
41 static char *default_value
;
43 static int respect_includes_opt
= -1;
44 static struct config_options config_options
;
45 static int show_origin
;
46 static int show_scope
;
47 static int fixed_value
;
49 #define ACTION_GET (1<<0)
50 #define ACTION_GET_ALL (1<<1)
51 #define ACTION_GET_REGEXP (1<<2)
52 #define ACTION_REPLACE_ALL (1<<3)
53 #define ACTION_ADD (1<<4)
54 #define ACTION_UNSET (1<<5)
55 #define ACTION_UNSET_ALL (1<<6)
56 #define ACTION_RENAME_SECTION (1<<7)
57 #define ACTION_REMOVE_SECTION (1<<8)
58 #define ACTION_LIST (1<<9)
59 #define ACTION_EDIT (1<<10)
60 #define ACTION_SET (1<<11)
61 #define ACTION_SET_ALL (1<<12)
62 #define ACTION_GET_COLOR (1<<13)
63 #define ACTION_GET_COLORBOOL (1<<14)
64 #define ACTION_GET_URLMATCH (1<<15)
67 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
68 * one line of output and which should therefore be paged.
70 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
71 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
75 #define TYPE_BOOL_OR_INT 3
77 #define TYPE_EXPIRY_DATE 5
79 #define TYPE_BOOL_OR_STR 7
81 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
82 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
83 PARSE_OPT_NONEG, option_parse_type, (i) }
85 static NORETURN
void usage_builtin_config(void);
87 static int option_parse_type(const struct option
*opt
, const char *arg
,
90 int new_type
, *to_type
;
93 *((int *) opt
->value
) = 0;
98 * To support '--<type>' style flags, begin with new_type equal to
101 new_type
= opt
->defval
;
103 if (!strcmp(arg
, "bool"))
104 new_type
= TYPE_BOOL
;
105 else if (!strcmp(arg
, "int"))
107 else if (!strcmp(arg
, "bool-or-int"))
108 new_type
= TYPE_BOOL_OR_INT
;
109 else if (!strcmp(arg
, "bool-or-str"))
110 new_type
= TYPE_BOOL_OR_STR
;
111 else if (!strcmp(arg
, "path"))
112 new_type
= TYPE_PATH
;
113 else if (!strcmp(arg
, "expiry-date"))
114 new_type
= TYPE_EXPIRY_DATE
;
115 else if (!strcmp(arg
, "color"))
116 new_type
= TYPE_COLOR
;
118 die(_("unrecognized --type argument, %s"), arg
);
121 to_type
= opt
->value
;
122 if (*to_type
&& *to_type
!= new_type
) {
124 * Complain when there is a new type not equal to the old type.
125 * This allows for combinations like '--int --type=int' and
126 * '--type=int --type=int', but disallows ones like '--type=bool
127 * --int' and '--type=bool
130 error(_("only one type at a time"));
131 usage_builtin_config();
138 static struct option builtin_config_options
[] = {
139 OPT_GROUP(N_("Config file location")),
140 OPT_BOOL(0, "global", &use_global_config
, N_("use global config file")),
141 OPT_BOOL(0, "system", &use_system_config
, N_("use system config file")),
142 OPT_BOOL(0, "local", &use_local_config
, N_("use repository config file")),
143 OPT_BOOL(0, "worktree", &use_worktree_config
, N_("use per-worktree config file")),
144 OPT_STRING('f', "file", &given_config_source
.file
, N_("file"), N_("use given config file")),
145 OPT_STRING(0, "blob", &given_config_source
.blob
, N_("blob-id"), N_("read config from given blob object")),
146 OPT_GROUP(N_("Action")),
147 OPT_BIT(0, "get", &actions
, N_("get value: name [value-pattern]"), ACTION_GET
),
148 OPT_BIT(0, "get-all", &actions
, N_("get all values: key [value-pattern]"), ACTION_GET_ALL
),
149 OPT_BIT(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP
),
150 OPT_BIT(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
151 OPT_BIT(0, "replace-all", &actions
, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL
),
152 OPT_BIT(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
153 OPT_BIT(0, "unset", &actions
, N_("remove a variable: name [value-pattern]"), ACTION_UNSET
),
154 OPT_BIT(0, "unset-all", &actions
, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL
),
155 OPT_BIT(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
156 OPT_BIT(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
157 OPT_BIT('l', "list", &actions
, N_("list all"), ACTION_LIST
),
158 OPT_BOOL(0, "fixed-value", &fixed_value
, N_("use string equality when comparing values to 'value-pattern'")),
159 OPT_BIT('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
160 OPT_BIT(0, "get-color", &actions
, N_("find the color configured: slot [default]"), ACTION_GET_COLOR
),
161 OPT_BIT(0, "get-colorbool", &actions
, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL
),
162 OPT_GROUP(N_("Type")),
163 OPT_CALLBACK('t', "type", &type
, N_("type"), N_("value is given this type"), option_parse_type
),
164 OPT_CALLBACK_VALUE(0, "bool", &type
, N_("value is \"true\" or \"false\""), TYPE_BOOL
),
165 OPT_CALLBACK_VALUE(0, "int", &type
, N_("value is decimal number"), TYPE_INT
),
166 OPT_CALLBACK_VALUE(0, "bool-or-int", &type
, N_("value is --bool or --int"), TYPE_BOOL_OR_INT
),
167 OPT_CALLBACK_VALUE(0, "bool-or-str", &type
, N_("value is --bool or string"), TYPE_BOOL_OR_STR
),
168 OPT_CALLBACK_VALUE(0, "path", &type
, N_("value is a path (file or directory name)"), TYPE_PATH
),
169 OPT_CALLBACK_VALUE(0, "expiry-date", &type
, N_("value is an expiry date"), TYPE_EXPIRY_DATE
),
170 OPT_GROUP(N_("Other")),
171 OPT_BOOL('z', "null", &end_nul
, N_("terminate values with NUL byte")),
172 OPT_BOOL(0, "name-only", &omit_values
, N_("show variable names only")),
173 OPT_BOOL(0, "includes", &respect_includes_opt
, N_("respect include directives on lookup")),
174 OPT_BOOL(0, "show-origin", &show_origin
, N_("show origin of config (file, standard input, blob, command line)")),
175 OPT_BOOL(0, "show-scope", &show_scope
, N_("show scope of config (worktree, local, global, system, command)")),
176 OPT_STRING(0, "default", &default_value
, N_("value"), N_("with --get, use default value when missing entry")),
180 static NORETURN
void usage_builtin_config(void)
182 usage_with_options(builtin_config_usage
, builtin_config_options
);
185 static void check_argc(int argc
, int min
, int max
)
187 if (argc
>= min
&& argc
<= max
)
190 error(_("wrong number of arguments, should be %d"), min
);
192 error(_("wrong number of arguments, should be from %d to %d"),
194 usage_builtin_config();
197 static void show_config_origin(struct strbuf
*buf
)
199 const char term
= end_nul
? '\0' : '\t';
201 strbuf_addstr(buf
, current_config_origin_type());
202 strbuf_addch(buf
, ':');
204 strbuf_addstr(buf
, current_config_name());
206 quote_c_style(current_config_name(), buf
, NULL
, 0);
207 strbuf_addch(buf
, term
);
210 static void show_config_scope(struct strbuf
*buf
)
212 const char term
= end_nul
? '\0' : '\t';
213 const char *scope
= config_scope_name(current_config_scope());
215 strbuf_addstr(buf
, N_(scope
));
216 strbuf_addch(buf
, term
);
219 static int show_all_config(const char *key_
, const char *value_
,
222 if (show_origin
|| show_scope
) {
223 struct strbuf buf
= STRBUF_INIT
;
225 show_config_scope(&buf
);
227 show_config_origin(&buf
);
228 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
229 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
230 strbuf_release(&buf
);
232 if (!omit_values
&& value_
)
233 printf("%s%c%s%c", key_
, delim
, value_
, term
);
235 printf("%s%c", key_
, term
);
240 struct strbuf
*items
;
245 static int format_config(struct strbuf
*buf
, const char *key_
, const char *value_
)
248 show_config_scope(buf
);
250 show_config_origin(buf
);
252 strbuf_addstr(buf
, key_
);
255 strbuf_addch(buf
, key_delim
);
257 if (type
== TYPE_INT
)
258 strbuf_addf(buf
, "%"PRId64
,
259 git_config_int64(key_
, value_
? value_
: ""));
260 else if (type
== TYPE_BOOL
)
261 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
263 else if (type
== TYPE_BOOL_OR_INT
) {
265 v
= git_config_bool_or_int(key_
, value_
, &is_bool
);
267 strbuf_addstr(buf
, v
? "true" : "false");
269 strbuf_addf(buf
, "%d", v
);
270 } else if (type
== TYPE_BOOL_OR_STR
) {
271 int v
= git_parse_maybe_bool(value_
);
273 strbuf_addstr(buf
, value_
);
275 strbuf_addstr(buf
, v
? "true" : "false");
276 } else if (type
== TYPE_PATH
) {
278 if (git_config_pathname(&v
, key_
, value_
) < 0)
280 strbuf_addstr(buf
, v
);
282 } else if (type
== TYPE_EXPIRY_DATE
) {
284 if (git_config_expiry_date(&t
, key_
, value_
) < 0)
286 strbuf_addf(buf
, "%"PRItime
, t
);
287 } else if (type
== TYPE_COLOR
) {
288 char v
[COLOR_MAXLEN
];
289 if (git_config_color(v
, key_
, value_
) < 0)
291 strbuf_addstr(buf
, v
);
293 strbuf_addstr(buf
, value_
);
295 /* Just show the key name; back out delimiter */
297 strbuf_setlen(buf
, buf
->len
- 1);
300 strbuf_addch(buf
, term
);
304 static int collect_config(const char *key_
, const char *value_
, void *cb
)
306 struct strbuf_list
*values
= cb
;
308 if (!use_key_regexp
&& strcmp(key_
, key
))
310 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
312 if (fixed_value
&& strcmp(value_pattern
, (value_
?value_
:"")))
314 if (regexp
!= NULL
&&
315 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
318 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
319 strbuf_init(&values
->items
[values
->nr
], 0);
321 return format_config(&values
->items
[values
->nr
++], key_
, value_
);
324 static int get_value(const char *key_
, const char *regex_
, unsigned flags
)
326 int ret
= CONFIG_GENERIC_ERROR
;
327 struct strbuf_list values
= {NULL
};
330 if (use_key_regexp
) {
334 * NEEDSWORK: this naive pattern lowercasing obviously does not
335 * work for more complex patterns like "^[^.]*Foo.*bar".
336 * Perhaps we should deprecate this altogether someday.
340 for (tl
= key
+ strlen(key
) - 1;
341 tl
>= key
&& *tl
!= '.';
344 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
347 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
348 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
349 error(_("invalid key pattern: %s"), key_
);
350 FREE_AND_NULL(key_regexp
);
351 ret
= CONFIG_INVALID_PATTERN
;
355 if (git_config_parse_key(key_
, &key
, NULL
)) {
356 ret
= CONFIG_INVALID_KEY
;
361 if (regex_
&& (flags
& CONFIG_FLAGS_FIXED_VALUE
))
362 value_pattern
= regex_
;
364 if (regex_
[0] == '!') {
369 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
370 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
371 error(_("invalid pattern: %s"), regex_
);
372 FREE_AND_NULL(regexp
);
373 ret
= CONFIG_INVALID_PATTERN
;
378 config_with_options(collect_config
, &values
,
379 &given_config_source
, the_repository
,
382 if (!values
.nr
&& default_value
) {
384 ALLOC_GROW(values
.items
, values
.nr
+ 1, values
.alloc
);
385 item
= &values
.items
[values
.nr
++];
386 strbuf_init(item
, 0);
387 if (format_config(item
, key_
, default_value
) < 0)
388 die(_("failed to format default config value: %s"),
394 for (i
= 0; i
< values
.nr
; i
++) {
395 struct strbuf
*buf
= values
.items
+ i
;
396 if (do_all
|| i
== values
.nr
- 1)
397 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
416 static char *normalize_value(const char *key
, const char *value
)
421 if (type
== 0 || type
== TYPE_PATH
|| type
== TYPE_EXPIRY_DATE
)
423 * We don't do normalization for TYPE_PATH here: If
424 * the path is like ~/foobar/, we prefer to store
425 * "~/foobar/" in the config file, and to expand the ~
426 * when retrieving the value.
427 * Also don't do normalization for expiry dates.
429 return xstrdup(value
);
430 if (type
== TYPE_INT
)
431 return xstrfmt("%"PRId64
, git_config_int64(key
, value
));
432 if (type
== TYPE_BOOL
)
433 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
434 if (type
== TYPE_BOOL_OR_INT
) {
436 v
= git_config_bool_or_int(key
, value
, &is_bool
);
438 return xstrfmt("%d", v
);
440 return xstrdup(v
? "true" : "false");
442 if (type
== TYPE_BOOL_OR_STR
) {
443 int v
= git_parse_maybe_bool(value
);
445 return xstrdup(value
);
447 return xstrdup(v
? "true" : "false");
449 if (type
== TYPE_COLOR
) {
450 char v
[COLOR_MAXLEN
];
451 if (git_config_color(v
, key
, value
))
452 die(_("cannot parse color '%s'"), value
);
455 * The contents of `v` now contain an ANSI escape
456 * sequence, not suitable for including within a
457 * configuration file. Treat the above as a
458 * "sanity-check", and return the given value, which we
459 * know is representable as valid color code.
461 return xstrdup(value
);
464 BUG("cannot normalize type %d", type
);
467 static int get_color_found
;
468 static const char *get_color_slot
;
469 static const char *get_colorbool_slot
;
470 static char parsed_color
[COLOR_MAXLEN
];
472 static int git_get_color_config(const char *var
, const char *value
,
475 if (!strcmp(var
, get_color_slot
)) {
477 config_error_nonbool(var
);
478 if (color_parse(value
, parsed_color
) < 0)
485 static void get_color(const char *var
, const char *def_color
)
487 get_color_slot
= var
;
489 parsed_color
[0] = '\0';
490 config_with_options(git_get_color_config
, NULL
,
491 &given_config_source
, the_repository
,
494 if (!get_color_found
&& def_color
) {
495 if (color_parse(def_color
, parsed_color
) < 0)
496 die(_("unable to parse default color value"));
499 fputs(parsed_color
, stdout
);
502 static int get_colorbool_found
;
503 static int get_diff_color_found
;
504 static int get_color_ui_found
;
505 static int git_get_colorbool_config(const char *var
, const char *value
,
508 if (!strcmp(var
, get_colorbool_slot
))
509 get_colorbool_found
= git_config_colorbool(var
, value
);
510 else if (!strcmp(var
, "diff.color"))
511 get_diff_color_found
= git_config_colorbool(var
, value
);
512 else if (!strcmp(var
, "color.ui"))
513 get_color_ui_found
= git_config_colorbool(var
, value
);
517 static int get_colorbool(const char *var
, int print
)
519 get_colorbool_slot
= var
;
520 get_colorbool_found
= -1;
521 get_diff_color_found
= -1;
522 get_color_ui_found
= -1;
523 config_with_options(git_get_colorbool_config
, NULL
,
524 &given_config_source
, the_repository
,
527 if (get_colorbool_found
< 0) {
528 if (!strcmp(get_colorbool_slot
, "color.diff"))
529 get_colorbool_found
= get_diff_color_found
;
530 if (get_colorbool_found
< 0)
531 get_colorbool_found
= get_color_ui_found
;
534 if (get_colorbool_found
< 0)
535 /* default value if none found in config */
536 get_colorbool_found
= GIT_COLOR_AUTO
;
538 get_colorbool_found
= want_color(get_colorbool_found
);
541 printf("%s\n", get_colorbool_found
? "true" : "false");
544 return get_colorbool_found
? 0 : 1;
547 static void check_write(void)
549 if (!given_config_source
.file
&& !startup_info
->have_repository
)
550 die(_("not in a git directory"));
552 if (given_config_source
.use_stdin
)
553 die(_("writing to stdin is not supported"));
555 if (given_config_source
.blob
)
556 die(_("writing config blobs is not supported"));
559 struct urlmatch_current_candidate_value
{
564 static int urlmatch_collect_fn(const char *var
, const char *value
, void *cb
)
566 struct string_list
*values
= cb
;
567 struct string_list_item
*item
= string_list_insert(values
, var
);
568 struct urlmatch_current_candidate_value
*matched
= item
->util
;
571 matched
= xmalloc(sizeof(*matched
));
572 strbuf_init(&matched
->value
, 0);
573 item
->util
= matched
;
575 strbuf_reset(&matched
->value
);
579 strbuf_addstr(&matched
->value
, value
);
580 matched
->value_is_null
= 0;
582 matched
->value_is_null
= 1;
587 static int get_urlmatch(const char *var
, const char *url
)
591 struct string_list_item
*item
;
592 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
593 struct string_list values
= STRING_LIST_INIT_DUP
;
595 config
.collect_fn
= urlmatch_collect_fn
;
596 config
.cascade_fn
= NULL
;
599 if (!url_normalize(url
, &config
.url
))
600 die("%s", config
.url
.err
);
602 config
.section
= xstrdup_tolower(var
);
603 section_tail
= strchr(config
.section
, '.');
605 *section_tail
= '\0';
606 config
.key
= section_tail
+ 1;
613 config_with_options(urlmatch_config_entry
, &config
,
614 &given_config_source
, the_repository
,
619 for_each_string_list_item(item
, &values
) {
620 struct urlmatch_current_candidate_value
*matched
= item
->util
;
621 struct strbuf buf
= STRBUF_INIT
;
623 format_config(&buf
, item
->string
,
624 matched
->value_is_null
? NULL
: matched
->value
.buf
);
625 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
626 strbuf_release(&buf
);
628 strbuf_release(&matched
->value
);
630 urlmatch_config_release(&config
);
631 string_list_clear(&values
, 1);
632 free(config
.url
.url
);
634 free((void *)config
.section
);
638 static char *default_user_config(void)
640 struct strbuf buf
= STRBUF_INIT
;
642 _("# This is Git's per-user configuration file.\n"
644 "# Please adapt and uncomment the following lines:\n"
647 ident_default_name(),
648 ident_default_email());
649 return strbuf_detach(&buf
, NULL
);
652 int cmd_config(int argc
, const char **argv
, const char *prefix
)
654 int nongit
= !startup_info
->have_repository
;
659 given_config_source
.file
= xstrdup_or_null(getenv(CONFIG_ENVIRONMENT
));
661 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
662 builtin_config_usage
,
663 PARSE_OPT_STOP_AT_NON_OPTION
);
665 if (use_global_config
+ use_system_config
+ use_local_config
+
666 use_worktree_config
+
667 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
668 error(_("only one config file at a time"));
669 usage_builtin_config();
673 if (use_local_config
)
674 die(_("--local can only be used inside a git repository"));
675 if (given_config_source
.blob
)
676 die(_("--blob can only be used inside a git repository"));
677 if (use_worktree_config
)
678 die(_("--worktree can only be used inside a git repository"));
682 if (given_config_source
.file
&&
683 !strcmp(given_config_source
.file
, "-")) {
684 given_config_source
.file
= NULL
;
685 given_config_source
.use_stdin
= 1;
686 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
689 if (use_global_config
) {
690 char *user_config
, *xdg_config
;
692 git_global_config(&user_config
, &xdg_config
);
695 * It is unknown if HOME/.gitconfig exists, so
696 * we do not know if we should write to XDG
697 * location; error out even if XDG_CONFIG_HOME
698 * is set and points at a sane location.
700 die(_("$HOME not set"));
702 given_config_source
.scope
= CONFIG_SCOPE_GLOBAL
;
704 if (access_or_warn(user_config
, R_OK
, 0) &&
705 xdg_config
&& !access_or_warn(xdg_config
, R_OK
, 0)) {
706 given_config_source
.file
= xdg_config
;
709 given_config_source
.file
= user_config
;
713 else if (use_system_config
) {
714 given_config_source
.file
= git_system_config();
715 given_config_source
.scope
= CONFIG_SCOPE_SYSTEM
;
716 } else if (use_local_config
) {
717 given_config_source
.file
= git_pathdup("config");
718 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
719 } else if (use_worktree_config
) {
720 struct worktree
**worktrees
= get_worktrees();
721 if (the_repository
->repository_format_worktree_config
)
722 given_config_source
.file
= git_pathdup("config.worktree");
723 else if (worktrees
[0] && worktrees
[1])
724 die(_("--worktree cannot be used with multiple "
725 "working trees unless the config\n"
726 "extension worktreeConfig is enabled. "
727 "Please read \"CONFIGURATION FILE\"\n"
728 "section in \"git help worktree\" for details"));
730 given_config_source
.file
= git_pathdup("config");
731 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
732 free_worktrees(worktrees
);
733 } else if (given_config_source
.file
) {
734 if (!is_absolute_path(given_config_source
.file
) && prefix
)
735 given_config_source
.file
=
736 prefix_filename(prefix
, given_config_source
.file
);
737 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
738 } else if (given_config_source
.blob
) {
739 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
743 if (respect_includes_opt
== -1)
744 config_options
.respect_includes
= !given_config_source
.file
;
746 config_options
.respect_includes
= respect_includes_opt
;
748 config_options
.commondir
= get_git_common_dir();
749 config_options
.git_dir
= get_git_dir();
758 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && type
) {
759 error(_("--get-color and variable type are incoherent"));
760 usage_builtin_config();
763 if (HAS_MULTI_BITS(actions
)) {
764 error(_("only one action at a time"));
765 usage_builtin_config();
769 case 1: actions
= ACTION_GET
; break;
770 case 2: actions
= ACTION_SET
; break;
771 case 3: actions
= ACTION_SET_ALL
; break;
773 usage_builtin_config();
776 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
777 error(_("--name-only is only applicable to --list or --get-regexp"));
778 usage_builtin_config();
781 if (show_origin
&& !(actions
&
782 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
783 error(_("--show-origin is only applicable to --get, --get-all, "
784 "--get-regexp, and --list"));
785 usage_builtin_config();
788 if (default_value
&& !(actions
& ACTION_GET
)) {
789 error(_("--default is only applicable to --get"));
790 usage_builtin_config();
793 /* check usage of --fixed-value */
795 int allowed_usage
= 0;
798 /* git config --get <name> <value-pattern> */
800 /* git config --get-all <name> <value-pattern> */
802 /* git config --get-regexp <name-pattern> <value-pattern> */
803 case ACTION_GET_REGEXP
:
804 /* git config --unset <name> <value-pattern> */
806 /* git config --unset-all <name> <value-pattern> */
807 case ACTION_UNSET_ALL
:
808 allowed_usage
= argc
> 1 && !!argv
[1];
811 /* git config <name> <value> <value-pattern> */
813 /* git config --replace-all <name> <value> <value-pattern> */
814 case ACTION_REPLACE_ALL
:
815 allowed_usage
= argc
> 2 && !!argv
[2];
818 /* other options don't allow --fixed-value */
821 if (!allowed_usage
) {
822 error(_("--fixed-value only applies with 'value-pattern'"));
823 usage_builtin_config();
826 flags
|= CONFIG_FLAGS_FIXED_VALUE
;
829 if (actions
& PAGING_ACTIONS
)
830 setup_auto_pager("config", 1);
832 if (actions
== ACTION_LIST
) {
833 check_argc(argc
, 0, 0);
834 if (config_with_options(show_all_config
, NULL
,
835 &given_config_source
, the_repository
,
836 &config_options
) < 0) {
837 if (given_config_source
.file
)
838 die_errno(_("unable to read config file '%s'"),
839 given_config_source
.file
);
841 die(_("error processing config file(s)"));
844 else if (actions
== ACTION_EDIT
) {
847 check_argc(argc
, 0, 0);
848 if (!given_config_source
.file
&& nongit
)
849 die(_("not in a git directory"));
850 if (given_config_source
.use_stdin
)
851 die(_("editing stdin is not supported"));
852 if (given_config_source
.blob
)
853 die(_("editing blobs is not supported"));
854 git_config(git_default_config
, NULL
);
855 config_file
= given_config_source
.file
?
856 xstrdup(given_config_source
.file
) :
857 git_pathdup("config");
858 if (use_global_config
) {
859 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
861 char *content
= default_user_config();
862 write_str_in_full(fd
, content
);
866 else if (errno
!= EEXIST
)
867 die_errno(_("cannot create configuration file %s"), config_file
);
869 launch_editor(config_file
, NULL
, NULL
);
872 else if (actions
== ACTION_SET
) {
874 check_argc(argc
, 2, 2);
875 value
= normalize_value(argv
[0], argv
[1]);
876 ret
= git_config_set_in_file_gently(given_config_source
.file
, argv
[0], value
);
877 if (ret
== CONFIG_NOTHING_SET
)
878 error(_("cannot overwrite multiple values with a single value\n"
879 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
881 else if (actions
== ACTION_SET_ALL
) {
883 check_argc(argc
, 2, 3);
884 value
= normalize_value(argv
[0], argv
[1]);
885 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
886 argv
[0], value
, argv
[2],
889 else if (actions
== ACTION_ADD
) {
891 check_argc(argc
, 2, 2);
892 value
= normalize_value(argv
[0], argv
[1]);
893 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
898 else if (actions
== ACTION_REPLACE_ALL
) {
900 check_argc(argc
, 2, 3);
901 value
= normalize_value(argv
[0], argv
[1]);
902 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
903 argv
[0], value
, argv
[2],
904 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
906 else if (actions
== ACTION_GET
) {
907 check_argc(argc
, 1, 2);
908 return get_value(argv
[0], argv
[1], flags
);
910 else if (actions
== ACTION_GET_ALL
) {
912 check_argc(argc
, 1, 2);
913 return get_value(argv
[0], argv
[1], flags
);
915 else if (actions
== ACTION_GET_REGEXP
) {
919 check_argc(argc
, 1, 2);
920 return get_value(argv
[0], argv
[1], flags
);
922 else if (actions
== ACTION_GET_URLMATCH
) {
923 check_argc(argc
, 2, 2);
924 return get_urlmatch(argv
[0], argv
[1]);
926 else if (actions
== ACTION_UNSET
) {
928 check_argc(argc
, 1, 2);
930 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
931 argv
[0], NULL
, argv
[1],
934 return git_config_set_in_file_gently(given_config_source
.file
,
937 else if (actions
== ACTION_UNSET_ALL
) {
939 check_argc(argc
, 1, 2);
940 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
941 argv
[0], NULL
, argv
[1],
942 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
944 else if (actions
== ACTION_RENAME_SECTION
) {
946 check_argc(argc
, 2, 2);
947 ret
= git_config_rename_section_in_file(given_config_source
.file
,
952 die(_("no such section: %s"), argv
[0]);
956 else if (actions
== ACTION_REMOVE_SECTION
) {
958 check_argc(argc
, 1, 1);
959 ret
= git_config_rename_section_in_file(given_config_source
.file
,
964 die(_("no such section: %s"), argv
[0]);
968 else if (actions
== ACTION_GET_COLOR
) {
969 check_argc(argc
, 1, 2);
970 get_color(argv
[0], argv
[1]);
972 else if (actions
== ACTION_GET_COLORBOOL
) {
973 check_argc(argc
, 1, 2);
975 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
976 return get_colorbool(argv
[0], argc
== 2);