6 #include "environment.h"
7 #include "repository.h"
10 #include "parse-options.h"
18 static const char *const builtin_config_usage
[] = {
19 N_("git config [<options>]"),
24 static regex_t
*key_regexp
;
25 static const char *value_pattern
;
26 static regex_t
*regexp
;
28 static int omit_values
;
29 static int use_key_regexp
;
31 static int do_not_match
;
32 static char delim
= '=';
33 static char key_delim
= ' ';
34 static char term
= '\n';
36 static int use_global_config
, use_system_config
, use_local_config
;
37 static int use_worktree_config
;
38 static struct git_config_source given_config_source
;
39 static int actions
, type
;
40 static char *default_value
;
42 static int respect_includes_opt
= -1;
43 static struct config_options config_options
;
44 static int show_origin
;
45 static int show_scope
;
46 static int fixed_value
;
48 #define ACTION_GET (1<<0)
49 #define ACTION_GET_ALL (1<<1)
50 #define ACTION_GET_REGEXP (1<<2)
51 #define ACTION_REPLACE_ALL (1<<3)
52 #define ACTION_ADD (1<<4)
53 #define ACTION_UNSET (1<<5)
54 #define ACTION_UNSET_ALL (1<<6)
55 #define ACTION_RENAME_SECTION (1<<7)
56 #define ACTION_REMOVE_SECTION (1<<8)
57 #define ACTION_LIST (1<<9)
58 #define ACTION_EDIT (1<<10)
59 #define ACTION_SET (1<<11)
60 #define ACTION_SET_ALL (1<<12)
61 #define ACTION_GET_COLOR (1<<13)
62 #define ACTION_GET_COLORBOOL (1<<14)
63 #define ACTION_GET_URLMATCH (1<<15)
66 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
67 * one line of output and which should therefore be paged.
69 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
70 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
74 #define TYPE_BOOL_OR_INT 3
76 #define TYPE_EXPIRY_DATE 5
78 #define TYPE_BOOL_OR_STR 7
80 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
81 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
82 PARSE_OPT_NONEG, option_parse_type, (i) }
84 static NORETURN
void usage_builtin_config(void);
86 static int option_parse_type(const struct option
*opt
, const char *arg
,
89 int new_type
, *to_type
;
92 *((int *) opt
->value
) = 0;
97 * To support '--<type>' style flags, begin with new_type equal to
100 new_type
= opt
->defval
;
102 if (!strcmp(arg
, "bool"))
103 new_type
= TYPE_BOOL
;
104 else if (!strcmp(arg
, "int"))
106 else if (!strcmp(arg
, "bool-or-int"))
107 new_type
= TYPE_BOOL_OR_INT
;
108 else if (!strcmp(arg
, "bool-or-str"))
109 new_type
= TYPE_BOOL_OR_STR
;
110 else if (!strcmp(arg
, "path"))
111 new_type
= TYPE_PATH
;
112 else if (!strcmp(arg
, "expiry-date"))
113 new_type
= TYPE_EXPIRY_DATE
;
114 else if (!strcmp(arg
, "color"))
115 new_type
= TYPE_COLOR
;
117 die(_("unrecognized --type argument, %s"), arg
);
120 to_type
= opt
->value
;
121 if (*to_type
&& *to_type
!= new_type
) {
123 * Complain when there is a new type not equal to the old type.
124 * This allows for combinations like '--int --type=int' and
125 * '--type=int --type=int', but disallows ones like '--type=bool
126 * --int' and '--type=bool
129 error(_("only one type at a time"));
130 usage_builtin_config();
137 static struct option builtin_config_options
[] = {
138 OPT_GROUP(N_("Config file location")),
139 OPT_BOOL(0, "global", &use_global_config
, N_("use global config file")),
140 OPT_BOOL(0, "system", &use_system_config
, N_("use system config file")),
141 OPT_BOOL(0, "local", &use_local_config
, N_("use repository config file")),
142 OPT_BOOL(0, "worktree", &use_worktree_config
, N_("use per-worktree config file")),
143 OPT_STRING('f', "file", &given_config_source
.file
, N_("file"), N_("use given config file")),
144 OPT_STRING(0, "blob", &given_config_source
.blob
, N_("blob-id"), N_("read config from given blob object")),
145 OPT_GROUP(N_("Action")),
146 OPT_BIT(0, "get", &actions
, N_("get value: name [value-pattern]"), ACTION_GET
),
147 OPT_BIT(0, "get-all", &actions
, N_("get all values: key [value-pattern]"), ACTION_GET_ALL
),
148 OPT_BIT(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP
),
149 OPT_BIT(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
150 OPT_BIT(0, "replace-all", &actions
, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL
),
151 OPT_BIT(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
152 OPT_BIT(0, "unset", &actions
, N_("remove a variable: name [value-pattern]"), ACTION_UNSET
),
153 OPT_BIT(0, "unset-all", &actions
, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL
),
154 OPT_BIT(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
155 OPT_BIT(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
156 OPT_BIT('l', "list", &actions
, N_("list all"), ACTION_LIST
),
157 OPT_BOOL(0, "fixed-value", &fixed_value
, N_("use string equality when comparing values to 'value-pattern'")),
158 OPT_BIT('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
159 OPT_BIT(0, "get-color", &actions
, N_("find the color configured: slot [default]"), ACTION_GET_COLOR
),
160 OPT_BIT(0, "get-colorbool", &actions
, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL
),
161 OPT_GROUP(N_("Type")),
162 OPT_CALLBACK('t', "type", &type
, N_("type"), N_("value is given this type"), option_parse_type
),
163 OPT_CALLBACK_VALUE(0, "bool", &type
, N_("value is \"true\" or \"false\""), TYPE_BOOL
),
164 OPT_CALLBACK_VALUE(0, "int", &type
, N_("value is decimal number"), TYPE_INT
),
165 OPT_CALLBACK_VALUE(0, "bool-or-int", &type
, N_("value is --bool or --int"), TYPE_BOOL_OR_INT
),
166 OPT_CALLBACK_VALUE(0, "bool-or-str", &type
, N_("value is --bool or string"), TYPE_BOOL_OR_STR
),
167 OPT_CALLBACK_VALUE(0, "path", &type
, N_("value is a path (file or directory name)"), TYPE_PATH
),
168 OPT_CALLBACK_VALUE(0, "expiry-date", &type
, N_("value is an expiry date"), TYPE_EXPIRY_DATE
),
169 OPT_GROUP(N_("Other")),
170 OPT_BOOL('z', "null", &end_nul
, N_("terminate values with NUL byte")),
171 OPT_BOOL(0, "name-only", &omit_values
, N_("show variable names only")),
172 OPT_BOOL(0, "includes", &respect_includes_opt
, N_("respect include directives on lookup")),
173 OPT_BOOL(0, "show-origin", &show_origin
, N_("show origin of config (file, standard input, blob, command line)")),
174 OPT_BOOL(0, "show-scope", &show_scope
, N_("show scope of config (worktree, local, global, system, command)")),
175 OPT_STRING(0, "default", &default_value
, N_("value"), N_("with --get, use default value when missing entry")),
179 static NORETURN
void usage_builtin_config(void)
181 usage_with_options(builtin_config_usage
, builtin_config_options
);
184 static void check_argc(int argc
, int min
, int max
)
186 if (argc
>= min
&& argc
<= max
)
189 error(_("wrong number of arguments, should be %d"), min
);
191 error(_("wrong number of arguments, should be from %d to %d"),
193 usage_builtin_config();
196 static void show_config_origin(const struct key_value_info
*kvi
,
199 const char term
= end_nul
? '\0' : '\t';
201 strbuf_addstr(buf
, config_origin_type_name(kvi
->origin_type
));
202 strbuf_addch(buf
, ':');
204 strbuf_addstr(buf
, kvi
->filename
? kvi
->filename
: "");
206 quote_c_style(kvi
->filename
? kvi
->filename
: "", buf
, NULL
, 0);
207 strbuf_addch(buf
, term
);
210 static void show_config_scope(const struct key_value_info
*kvi
,
213 const char term
= end_nul
? '\0' : '\t';
214 const char *scope
= config_scope_name(kvi
->scope
);
216 strbuf_addstr(buf
, N_(scope
));
217 strbuf_addch(buf
, term
);
220 static int show_all_config(const char *key_
, const char *value_
,
221 const struct config_context
*ctx
,
224 const struct key_value_info
*kvi
= ctx
->kvi
;
226 if (show_origin
|| show_scope
) {
227 struct strbuf buf
= STRBUF_INIT
;
229 show_config_scope(kvi
, &buf
);
231 show_config_origin(kvi
, &buf
);
232 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
233 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
234 strbuf_release(&buf
);
236 if (!omit_values
&& value_
)
237 printf("%s%c%s%c", key_
, delim
, value_
, term
);
239 printf("%s%c", key_
, term
);
244 struct strbuf
*items
;
249 static int format_config(struct strbuf
*buf
, const char *key_
,
250 const char *value_
, const struct key_value_info
*kvi
)
253 show_config_scope(kvi
, buf
);
255 show_config_origin(kvi
, buf
);
257 strbuf_addstr(buf
, key_
);
260 strbuf_addch(buf
, key_delim
);
262 if (type
== TYPE_INT
)
263 strbuf_addf(buf
, "%"PRId64
,
264 git_config_int64(key_
, value_
? value_
: "", kvi
));
265 else if (type
== TYPE_BOOL
)
266 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
268 else if (type
== TYPE_BOOL_OR_INT
) {
270 v
= git_config_bool_or_int(key_
, value_
, kvi
,
273 strbuf_addstr(buf
, v
? "true" : "false");
275 strbuf_addf(buf
, "%d", v
);
276 } else if (type
== TYPE_BOOL_OR_STR
) {
277 int v
= git_parse_maybe_bool(value_
);
279 strbuf_addstr(buf
, value_
);
281 strbuf_addstr(buf
, v
? "true" : "false");
282 } else if (type
== TYPE_PATH
) {
284 if (git_config_pathname(&v
, key_
, value_
) < 0)
286 strbuf_addstr(buf
, v
);
288 } else if (type
== TYPE_EXPIRY_DATE
) {
290 if (git_config_expiry_date(&t
, key_
, value_
) < 0)
292 strbuf_addf(buf
, "%"PRItime
, t
);
293 } else if (type
== TYPE_COLOR
) {
294 char v
[COLOR_MAXLEN
];
295 if (git_config_color(v
, key_
, value_
) < 0)
297 strbuf_addstr(buf
, v
);
299 strbuf_addstr(buf
, value_
);
301 /* Just show the key name; back out delimiter */
303 strbuf_setlen(buf
, buf
->len
- 1);
306 strbuf_addch(buf
, term
);
310 static int collect_config(const char *key_
, const char *value_
,
311 const struct config_context
*ctx
, void *cb
)
313 struct strbuf_list
*values
= cb
;
314 const struct key_value_info
*kvi
= ctx
->kvi
;
316 if (!use_key_regexp
&& strcmp(key_
, key
))
318 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
320 if (fixed_value
&& strcmp(value_pattern
, (value_
?value_
:"")))
322 if (regexp
!= NULL
&&
323 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
326 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
327 strbuf_init(&values
->items
[values
->nr
], 0);
329 return format_config(&values
->items
[values
->nr
++], key_
, value_
, kvi
);
332 static int get_value(const char *key_
, const char *regex_
, unsigned flags
)
334 int ret
= CONFIG_GENERIC_ERROR
;
335 struct strbuf_list values
= {NULL
};
338 if (use_key_regexp
) {
342 * NEEDSWORK: this naive pattern lowercasing obviously does not
343 * work for more complex patterns like "^[^.]*Foo.*bar".
344 * Perhaps we should deprecate this altogether someday.
348 for (tl
= key
+ strlen(key
) - 1;
349 tl
>= key
&& *tl
!= '.';
352 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
355 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
356 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
357 error(_("invalid key pattern: %s"), key_
);
358 FREE_AND_NULL(key_regexp
);
359 ret
= CONFIG_INVALID_PATTERN
;
363 if (git_config_parse_key(key_
, &key
, NULL
)) {
364 ret
= CONFIG_INVALID_KEY
;
369 if (regex_
&& (flags
& CONFIG_FLAGS_FIXED_VALUE
))
370 value_pattern
= regex_
;
372 if (regex_
[0] == '!') {
377 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
378 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
379 error(_("invalid pattern: %s"), regex_
);
380 FREE_AND_NULL(regexp
);
381 ret
= CONFIG_INVALID_PATTERN
;
386 config_with_options(collect_config
, &values
,
387 &given_config_source
, the_repository
,
390 if (!values
.nr
&& default_value
) {
391 struct key_value_info kvi
= KVI_INIT
;
394 kvi_from_param(&kvi
);
395 ALLOC_GROW(values
.items
, values
.nr
+ 1, values
.alloc
);
396 item
= &values
.items
[values
.nr
++];
397 strbuf_init(item
, 0);
398 if (format_config(item
, key_
, default_value
, &kvi
) < 0)
399 die(_("failed to format default config value: %s"),
405 for (i
= 0; i
< values
.nr
; i
++) {
406 struct strbuf
*buf
= values
.items
+ i
;
407 if (do_all
|| i
== values
.nr
- 1)
408 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
427 static char *normalize_value(const char *key
, const char *value
,
428 struct key_value_info
*kvi
)
433 if (type
== 0 || type
== TYPE_PATH
|| type
== TYPE_EXPIRY_DATE
)
435 * We don't do normalization for TYPE_PATH here: If
436 * the path is like ~/foobar/, we prefer to store
437 * "~/foobar/" in the config file, and to expand the ~
438 * when retrieving the value.
439 * Also don't do normalization for expiry dates.
441 return xstrdup(value
);
442 if (type
== TYPE_INT
)
443 return xstrfmt("%"PRId64
, git_config_int64(key
, value
, kvi
));
444 if (type
== TYPE_BOOL
)
445 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
446 if (type
== TYPE_BOOL_OR_INT
) {
448 v
= git_config_bool_or_int(key
, value
, kvi
, &is_bool
);
450 return xstrfmt("%d", v
);
452 return xstrdup(v
? "true" : "false");
454 if (type
== TYPE_BOOL_OR_STR
) {
455 int v
= git_parse_maybe_bool(value
);
457 return xstrdup(value
);
459 return xstrdup(v
? "true" : "false");
461 if (type
== TYPE_COLOR
) {
462 char v
[COLOR_MAXLEN
];
463 if (git_config_color(v
, key
, value
))
464 die(_("cannot parse color '%s'"), value
);
467 * The contents of `v` now contain an ANSI escape
468 * sequence, not suitable for including within a
469 * configuration file. Treat the above as a
470 * "sanity-check", and return the given value, which we
471 * know is representable as valid color code.
473 return xstrdup(value
);
476 BUG("cannot normalize type %d", type
);
479 static int get_color_found
;
480 static const char *get_color_slot
;
481 static const char *get_colorbool_slot
;
482 static char parsed_color
[COLOR_MAXLEN
];
484 static int git_get_color_config(const char *var
, const char *value
,
485 const struct config_context
*ctx UNUSED
,
488 if (!strcmp(var
, get_color_slot
)) {
490 config_error_nonbool(var
);
491 if (color_parse(value
, parsed_color
) < 0)
498 static void get_color(const char *var
, const char *def_color
)
500 get_color_slot
= var
;
502 parsed_color
[0] = '\0';
503 config_with_options(git_get_color_config
, NULL
,
504 &given_config_source
, the_repository
,
507 if (!get_color_found
&& def_color
) {
508 if (color_parse(def_color
, parsed_color
) < 0)
509 die(_("unable to parse default color value"));
512 fputs(parsed_color
, stdout
);
515 static int get_colorbool_found
;
516 static int get_diff_color_found
;
517 static int get_color_ui_found
;
518 static int git_get_colorbool_config(const char *var
, const char *value
,
519 const struct config_context
*ctx UNUSED
,
522 if (!strcmp(var
, get_colorbool_slot
))
523 get_colorbool_found
= git_config_colorbool(var
, value
);
524 else if (!strcmp(var
, "diff.color"))
525 get_diff_color_found
= git_config_colorbool(var
, value
);
526 else if (!strcmp(var
, "color.ui"))
527 get_color_ui_found
= git_config_colorbool(var
, value
);
531 static int get_colorbool(const char *var
, int print
)
533 get_colorbool_slot
= var
;
534 get_colorbool_found
= -1;
535 get_diff_color_found
= -1;
536 get_color_ui_found
= -1;
537 config_with_options(git_get_colorbool_config
, NULL
,
538 &given_config_source
, the_repository
,
541 if (get_colorbool_found
< 0) {
542 if (!strcmp(get_colorbool_slot
, "color.diff"))
543 get_colorbool_found
= get_diff_color_found
;
544 if (get_colorbool_found
< 0)
545 get_colorbool_found
= get_color_ui_found
;
548 if (get_colorbool_found
< 0)
549 /* default value if none found in config */
550 get_colorbool_found
= GIT_COLOR_AUTO
;
552 get_colorbool_found
= want_color(get_colorbool_found
);
555 printf("%s\n", get_colorbool_found
? "true" : "false");
558 return get_colorbool_found
? 0 : 1;
561 static void check_write(void)
563 if (!given_config_source
.file
&& !startup_info
->have_repository
)
564 die(_("not in a git directory"));
566 if (given_config_source
.use_stdin
)
567 die(_("writing to stdin is not supported"));
569 if (given_config_source
.blob
)
570 die(_("writing config blobs is not supported"));
573 struct urlmatch_current_candidate_value
{
576 struct key_value_info kvi
;
579 static int urlmatch_collect_fn(const char *var
, const char *value
,
580 const struct config_context
*ctx
,
583 struct string_list
*values
= cb
;
584 struct string_list_item
*item
= string_list_insert(values
, var
);
585 struct urlmatch_current_candidate_value
*matched
= item
->util
;
586 const struct key_value_info
*kvi
= ctx
->kvi
;
589 matched
= xmalloc(sizeof(*matched
));
590 strbuf_init(&matched
->value
, 0);
591 item
->util
= matched
;
593 strbuf_reset(&matched
->value
);
598 strbuf_addstr(&matched
->value
, value
);
599 matched
->value_is_null
= 0;
601 matched
->value_is_null
= 1;
606 static int get_urlmatch(const char *var
, const char *url
)
610 struct string_list_item
*item
;
611 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
612 struct string_list values
= STRING_LIST_INIT_DUP
;
614 config
.collect_fn
= urlmatch_collect_fn
;
615 config
.cascade_fn
= NULL
;
618 if (!url_normalize(url
, &config
.url
))
619 die("%s", config
.url
.err
);
621 config
.section
= xstrdup_tolower(var
);
622 section_tail
= strchr(config
.section
, '.');
624 *section_tail
= '\0';
625 config
.key
= section_tail
+ 1;
632 config_with_options(urlmatch_config_entry
, &config
,
633 &given_config_source
, the_repository
,
638 for_each_string_list_item(item
, &values
) {
639 struct urlmatch_current_candidate_value
*matched
= item
->util
;
640 struct strbuf buf
= STRBUF_INIT
;
642 format_config(&buf
, item
->string
,
643 matched
->value_is_null
? NULL
: matched
->value
.buf
,
645 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
646 strbuf_release(&buf
);
648 strbuf_release(&matched
->value
);
650 urlmatch_config_release(&config
);
651 string_list_clear(&values
, 1);
652 free(config
.url
.url
);
654 free((void *)config
.section
);
658 static char *default_user_config(void)
660 struct strbuf buf
= STRBUF_INIT
;
662 _("# This is Git's per-user configuration file.\n"
664 "# Please adapt and uncomment the following lines:\n"
667 ident_default_name(),
668 ident_default_email());
669 return strbuf_detach(&buf
, NULL
);
672 int cmd_config(int argc
, const char **argv
, const char *prefix
)
674 int nongit
= !startup_info
->have_repository
;
678 struct key_value_info default_kvi
= KVI_INIT
;
680 given_config_source
.file
= xstrdup_or_null(getenv(CONFIG_ENVIRONMENT
));
682 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
683 builtin_config_usage
,
684 PARSE_OPT_STOP_AT_NON_OPTION
);
686 if (use_global_config
+ use_system_config
+ use_local_config
+
687 use_worktree_config
+
688 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
689 error(_("only one config file at a time"));
690 usage_builtin_config();
694 if (use_local_config
)
695 die(_("--local can only be used inside a git repository"));
696 if (given_config_source
.blob
)
697 die(_("--blob can only be used inside a git repository"));
698 if (use_worktree_config
)
699 die(_("--worktree can only be used inside a git repository"));
703 if (given_config_source
.file
&&
704 !strcmp(given_config_source
.file
, "-")) {
705 given_config_source
.file
= NULL
;
706 given_config_source
.use_stdin
= 1;
707 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
710 if (use_global_config
) {
711 given_config_source
.file
= git_global_config();
712 if (!given_config_source
.file
)
714 * It is unknown if HOME/.gitconfig exists, so
715 * we do not know if we should write to XDG
716 * location; error out even if XDG_CONFIG_HOME
717 * is set and points at a sane location.
719 die(_("$HOME not set"));
720 given_config_source
.scope
= CONFIG_SCOPE_GLOBAL
;
721 } else if (use_system_config
) {
722 given_config_source
.file
= git_system_config();
723 given_config_source
.scope
= CONFIG_SCOPE_SYSTEM
;
724 } else if (use_local_config
) {
725 given_config_source
.file
= git_pathdup("config");
726 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
727 } else if (use_worktree_config
) {
728 struct worktree
**worktrees
= get_worktrees();
729 if (the_repository
->repository_format_worktree_config
)
730 given_config_source
.file
= git_pathdup("config.worktree");
731 else if (worktrees
[0] && worktrees
[1])
732 die(_("--worktree cannot be used with multiple "
733 "working trees unless the config\n"
734 "extension worktreeConfig is enabled. "
735 "Please read \"CONFIGURATION FILE\"\n"
736 "section in \"git help worktree\" for details"));
738 given_config_source
.file
= git_pathdup("config");
739 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
740 free_worktrees(worktrees
);
741 } else if (given_config_source
.file
) {
742 if (!is_absolute_path(given_config_source
.file
) && prefix
)
743 given_config_source
.file
=
744 prefix_filename(prefix
, given_config_source
.file
);
745 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
746 } else if (given_config_source
.blob
) {
747 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
750 if (respect_includes_opt
== -1)
751 config_options
.respect_includes
= !given_config_source
.file
;
753 config_options
.respect_includes
= respect_includes_opt
;
755 config_options
.commondir
= get_git_common_dir();
756 config_options
.git_dir
= get_git_dir();
765 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && type
) {
766 error(_("--get-color and variable type are incoherent"));
767 usage_builtin_config();
770 if (HAS_MULTI_BITS(actions
)) {
771 error(_("only one action at a time"));
772 usage_builtin_config();
776 case 1: actions
= ACTION_GET
; break;
777 case 2: actions
= ACTION_SET
; break;
778 case 3: actions
= ACTION_SET_ALL
; break;
780 usage_builtin_config();
783 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
784 error(_("--name-only is only applicable to --list or --get-regexp"));
785 usage_builtin_config();
788 if (show_origin
&& !(actions
&
789 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
790 error(_("--show-origin is only applicable to --get, --get-all, "
791 "--get-regexp, and --list"));
792 usage_builtin_config();
795 if (default_value
&& !(actions
& ACTION_GET
)) {
796 error(_("--default is only applicable to --get"));
797 usage_builtin_config();
800 /* check usage of --fixed-value */
802 int allowed_usage
= 0;
805 /* git config --get <name> <value-pattern> */
807 /* git config --get-all <name> <value-pattern> */
809 /* git config --get-regexp <name-pattern> <value-pattern> */
810 case ACTION_GET_REGEXP
:
811 /* git config --unset <name> <value-pattern> */
813 /* git config --unset-all <name> <value-pattern> */
814 case ACTION_UNSET_ALL
:
815 allowed_usage
= argc
> 1 && !!argv
[1];
818 /* git config <name> <value> <value-pattern> */
820 /* git config --replace-all <name> <value> <value-pattern> */
821 case ACTION_REPLACE_ALL
:
822 allowed_usage
= argc
> 2 && !!argv
[2];
825 /* other options don't allow --fixed-value */
828 if (!allowed_usage
) {
829 error(_("--fixed-value only applies with 'value-pattern'"));
830 usage_builtin_config();
833 flags
|= CONFIG_FLAGS_FIXED_VALUE
;
836 if (actions
& PAGING_ACTIONS
)
837 setup_auto_pager("config", 1);
839 if (actions
== ACTION_LIST
) {
840 check_argc(argc
, 0, 0);
841 if (config_with_options(show_all_config
, NULL
,
842 &given_config_source
, the_repository
,
843 &config_options
) < 0) {
844 if (given_config_source
.file
)
845 die_errno(_("unable to read config file '%s'"),
846 given_config_source
.file
);
848 die(_("error processing config file(s)"));
851 else if (actions
== ACTION_EDIT
) {
854 check_argc(argc
, 0, 0);
855 if (!given_config_source
.file
&& nongit
)
856 die(_("not in a git directory"));
857 if (given_config_source
.use_stdin
)
858 die(_("editing stdin is not supported"));
859 if (given_config_source
.blob
)
860 die(_("editing blobs is not supported"));
861 git_config(git_default_config
, NULL
);
862 config_file
= given_config_source
.file
?
863 xstrdup(given_config_source
.file
) :
864 git_pathdup("config");
865 if (use_global_config
) {
866 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
868 char *content
= default_user_config();
869 write_str_in_full(fd
, content
);
873 else if (errno
!= EEXIST
)
874 die_errno(_("cannot create configuration file %s"), config_file
);
876 launch_editor(config_file
, NULL
, NULL
);
879 else if (actions
== ACTION_SET
) {
881 check_argc(argc
, 2, 2);
882 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
883 ret
= git_config_set_in_file_gently(given_config_source
.file
, argv
[0], value
);
884 if (ret
== CONFIG_NOTHING_SET
)
885 error(_("cannot overwrite multiple values with a single value\n"
886 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
888 else if (actions
== ACTION_SET_ALL
) {
890 check_argc(argc
, 2, 3);
891 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
892 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
893 argv
[0], value
, argv
[2],
896 else if (actions
== ACTION_ADD
) {
898 check_argc(argc
, 2, 2);
899 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
900 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
905 else if (actions
== ACTION_REPLACE_ALL
) {
907 check_argc(argc
, 2, 3);
908 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
909 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
910 argv
[0], value
, argv
[2],
911 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
913 else if (actions
== ACTION_GET
) {
914 check_argc(argc
, 1, 2);
915 return get_value(argv
[0], argv
[1], flags
);
917 else if (actions
== ACTION_GET_ALL
) {
919 check_argc(argc
, 1, 2);
920 return get_value(argv
[0], argv
[1], flags
);
922 else if (actions
== ACTION_GET_REGEXP
) {
926 check_argc(argc
, 1, 2);
927 return get_value(argv
[0], argv
[1], flags
);
929 else if (actions
== ACTION_GET_URLMATCH
) {
930 check_argc(argc
, 2, 2);
931 return get_urlmatch(argv
[0], argv
[1]);
933 else if (actions
== ACTION_UNSET
) {
935 check_argc(argc
, 1, 2);
937 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
938 argv
[0], NULL
, argv
[1],
941 return git_config_set_in_file_gently(given_config_source
.file
,
944 else if (actions
== ACTION_UNSET_ALL
) {
946 check_argc(argc
, 1, 2);
947 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
948 argv
[0], NULL
, argv
[1],
949 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
951 else if (actions
== ACTION_RENAME_SECTION
) {
953 check_argc(argc
, 2, 2);
954 ret
= git_config_rename_section_in_file(given_config_source
.file
,
959 die(_("no such section: %s"), argv
[0]);
963 else if (actions
== ACTION_REMOVE_SECTION
) {
965 check_argc(argc
, 1, 1);
966 ret
= git_config_rename_section_in_file(given_config_source
.file
,
971 die(_("no such section: %s"), argv
[0]);
975 else if (actions
== ACTION_GET_COLOR
) {
976 check_argc(argc
, 1, 2);
977 get_color(argv
[0], argv
[1]);
979 else if (actions
== ACTION_GET_COLORBOOL
) {
980 check_argc(argc
, 1, 2);
982 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
983 return get_colorbool(argv
[0], argc
== 2);