2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
12 #include "environment.h"
13 #include "repository.h"
19 #include "string-list.h"
20 #include "object-store.h"
27 struct config_source
{
28 struct config_source
*prev
;
37 enum config_origin_type origin_type
;
40 enum config_error_action default_error_action
;
46 unsigned subsection_case_sensitive
: 1;
48 int (*do_fgetc
)(struct config_source
*c
);
49 int (*do_ungetc
)(int c
, struct config_source
*conf
);
50 long (*do_ftell
)(struct config_source
*c
);
54 * These variables record the "current" config source, which
55 * can be accessed by parsing callbacks.
57 * The "cf" variable will be non-NULL only when we are actually parsing a real
58 * config source (file, blob, cmdline, etc).
60 * The "current_config_kvi" variable will be non-NULL only when we are feeding
61 * cached config from a configset into a callback.
63 * They should generally never be non-NULL at the same time. If they are both
64 * NULL, then we aren't parsing anything (and depending on the function looking
65 * at the variables, it's either a bug for it to be called in the first place,
66 * or it's a function which can be reused for non-config purposes, and should
67 * fall back to some sane behavior).
69 static struct config_source
*cf
;
70 static struct key_value_info
*current_config_kvi
;
73 * Similar to the variables above, this gives access to the "scope" of the
74 * current value (repo, global, etc). For cached values, it can be found via
75 * the current_config_kvi as above. During parsing, the current value can be
76 * found in this variable. It's not part of "cf" because it transcends a single
77 * file (i.e., a file included from .git/config is still in "repo" scope).
79 static enum config_scope current_parsing_scope
;
81 static int pack_compression_seen
;
82 static int zlib_compression_seen
;
85 * Config that comes from trusted scopes, namely:
86 * - CONFIG_SCOPE_SYSTEM (e.g. /etc/gitconfig)
87 * - CONFIG_SCOPE_GLOBAL (e.g. $HOME/.gitconfig, $XDG_CONFIG_HOME/git)
88 * - CONFIG_SCOPE_COMMAND (e.g. "-c" option, environment variables)
90 * This is declared here for code cleanliness, but unlike the other
91 * static variables, this does not hold config parser state.
93 static struct config_set protected_config
;
95 static int config_file_fgetc(struct config_source
*conf
)
97 return getc_unlocked(conf
->u
.file
);
100 static int config_file_ungetc(int c
, struct config_source
*conf
)
102 return ungetc(c
, conf
->u
.file
);
105 static long config_file_ftell(struct config_source
*conf
)
107 return ftell(conf
->u
.file
);
111 static int config_buf_fgetc(struct config_source
*conf
)
113 if (conf
->u
.buf
.pos
< conf
->u
.buf
.len
)
114 return conf
->u
.buf
.buf
[conf
->u
.buf
.pos
++];
119 static int config_buf_ungetc(int c
, struct config_source
*conf
)
121 if (conf
->u
.buf
.pos
> 0) {
123 if (conf
->u
.buf
.buf
[conf
->u
.buf
.pos
] != c
)
124 BUG("config_buf can only ungetc the same character");
131 static long config_buf_ftell(struct config_source
*conf
)
133 return conf
->u
.buf
.pos
;
136 struct config_include_data
{
140 const struct config_options
*opts
;
141 struct git_config_source
*config_source
;
144 * All remote URLs discovered when reading all config files.
146 struct string_list
*remote_urls
;
148 #define CONFIG_INCLUDE_INIT { 0 }
150 static int git_config_include(const char *var
, const char *value
, void *data
);
152 #define MAX_INCLUDE_DEPTH 10
153 static const char include_depth_advice
[] = N_(
154 "exceeded maximum include depth (%d) while including\n"
158 "This might be due to circular includes.");
159 static int handle_path_include(const char *path
, struct config_include_data
*inc
)
162 struct strbuf buf
= STRBUF_INIT
;
166 return config_error_nonbool("include.path");
168 expanded
= interpolate_path(path
, 0);
170 return error(_("could not expand include path '%s'"), path
);
174 * Use an absolute path as-is, but interpret relative paths
175 * based on the including config file.
177 if (!is_absolute_path(path
)) {
180 if (!cf
|| !cf
->path
) {
181 ret
= error(_("relative config includes must come from files"));
185 slash
= find_last_dir_sep(cf
->path
);
187 strbuf_add(&buf
, cf
->path
, slash
- cf
->path
+ 1);
188 strbuf_addstr(&buf
, path
);
192 if (!access_or_die(path
, R_OK
, 0)) {
193 if (++inc
->depth
> MAX_INCLUDE_DEPTH
)
194 die(_(include_depth_advice
), MAX_INCLUDE_DEPTH
, path
,
196 cf
->name
? cf
->name
:
198 ret
= git_config_from_file(git_config_include
, path
, inc
);
202 strbuf_release(&buf
);
207 static void add_trailing_starstar_for_dir(struct strbuf
*pat
)
209 if (pat
->len
&& is_dir_sep(pat
->buf
[pat
->len
- 1]))
210 strbuf_addstr(pat
, "**");
213 static int prepare_include_condition_pattern(struct strbuf
*pat
)
215 struct strbuf path
= STRBUF_INIT
;
219 expanded
= interpolate_path(pat
->buf
, 1);
222 strbuf_addstr(pat
, expanded
);
226 if (pat
->buf
[0] == '.' && is_dir_sep(pat
->buf
[1])) {
229 if (!cf
|| !cf
->path
)
230 return error(_("relative config include "
231 "conditionals must come from files"));
233 strbuf_realpath(&path
, cf
->path
, 1);
234 slash
= find_last_dir_sep(path
.buf
);
236 BUG("how is this possible?");
237 strbuf_splice(pat
, 0, 1, path
.buf
, slash
- path
.buf
);
238 prefix
= slash
- path
.buf
+ 1 /* slash */;
239 } else if (!is_absolute_path(pat
->buf
))
240 strbuf_insertstr(pat
, 0, "**/");
242 add_trailing_starstar_for_dir(pat
);
244 strbuf_release(&path
);
248 static int include_by_gitdir(const struct config_options
*opts
,
249 const char *cond
, size_t cond_len
, int icase
)
251 struct strbuf text
= STRBUF_INIT
;
252 struct strbuf pattern
= STRBUF_INIT
;
255 int already_tried_absolute
= 0;
258 git_dir
= opts
->git_dir
;
262 strbuf_realpath(&text
, git_dir
, 1);
263 strbuf_add(&pattern
, cond
, cond_len
);
264 prefix
= prepare_include_condition_pattern(&pattern
);
272 * perform literal matching on the prefix part so that
273 * any wildcard character in it can't create side effects.
275 if (text
.len
< prefix
)
277 if (!icase
&& strncmp(pattern
.buf
, text
.buf
, prefix
))
279 if (icase
&& strncasecmp(pattern
.buf
, text
.buf
, prefix
))
283 ret
= !wildmatch(pattern
.buf
+ prefix
, text
.buf
+ prefix
,
284 WM_PATHNAME
| (icase
? WM_CASEFOLD
: 0));
286 if (!ret
&& !already_tried_absolute
) {
288 * We've tried e.g. matching gitdir:~/work, but if
289 * ~/work is a symlink to /mnt/storage/work
290 * strbuf_realpath() will expand it, so the rule won't
291 * match. Let's match against a
292 * strbuf_add_absolute_path() version of the path,
293 * which'll do the right thing
296 strbuf_add_absolute_path(&text
, git_dir
);
297 already_tried_absolute
= 1;
301 strbuf_release(&pattern
);
302 strbuf_release(&text
);
306 static int include_by_branch(const char *cond
, size_t cond_len
)
310 struct strbuf pattern
= STRBUF_INIT
;
311 const char *refname
= !the_repository
->gitdir
?
312 NULL
: resolve_ref_unsafe("HEAD", 0, NULL
, &flags
);
313 const char *shortname
;
315 if (!refname
|| !(flags
& REF_ISSYMREF
) ||
316 !skip_prefix(refname
, "refs/heads/", &shortname
))
319 strbuf_add(&pattern
, cond
, cond_len
);
320 add_trailing_starstar_for_dir(&pattern
);
321 ret
= !wildmatch(pattern
.buf
, shortname
, WM_PATHNAME
);
322 strbuf_release(&pattern
);
326 static int add_remote_url(const char *var
, const char *value
, void *data
)
328 struct string_list
*remote_urls
= data
;
329 const char *remote_name
;
330 size_t remote_name_len
;
333 if (!parse_config_key(var
, "remote", &remote_name
, &remote_name_len
,
337 string_list_append(remote_urls
, value
);
341 static void populate_remote_urls(struct config_include_data
*inc
)
343 struct config_options opts
;
345 struct config_source
*store_cf
= cf
;
346 struct key_value_info
*store_kvi
= current_config_kvi
;
347 enum config_scope store_scope
= current_parsing_scope
;
350 opts
.unconditional_remote_url
= 1;
353 current_config_kvi
= NULL
;
354 current_parsing_scope
= 0;
356 inc
->remote_urls
= xmalloc(sizeof(*inc
->remote_urls
));
357 string_list_init_dup(inc
->remote_urls
);
358 config_with_options(add_remote_url
, inc
->remote_urls
, inc
->config_source
, &opts
);
361 current_config_kvi
= store_kvi
;
362 current_parsing_scope
= store_scope
;
365 static int forbid_remote_url(const char *var
, const char *value
, void *data
)
367 const char *remote_name
;
368 size_t remote_name_len
;
371 if (!parse_config_key(var
, "remote", &remote_name
, &remote_name_len
,
375 die(_("remote URLs cannot be configured in file directly or indirectly included by includeIf.hasconfig:remote.*.url"));
379 static int at_least_one_url_matches_glob(const char *glob
, int glob_len
,
380 struct string_list
*remote_urls
)
382 struct strbuf pattern
= STRBUF_INIT
;
383 struct string_list_item
*url_item
;
386 strbuf_add(&pattern
, glob
, glob_len
);
387 for_each_string_list_item(url_item
, remote_urls
) {
388 if (!wildmatch(pattern
.buf
, url_item
->string
, WM_PATHNAME
)) {
393 strbuf_release(&pattern
);
397 static int include_by_remote_url(struct config_include_data
*inc
,
398 const char *cond
, size_t cond_len
)
400 if (inc
->opts
->unconditional_remote_url
)
402 if (!inc
->remote_urls
)
403 populate_remote_urls(inc
);
404 return at_least_one_url_matches_glob(cond
, cond_len
,
408 static int include_condition_is_true(struct config_include_data
*inc
,
409 const char *cond
, size_t cond_len
)
411 const struct config_options
*opts
= inc
->opts
;
413 if (skip_prefix_mem(cond
, cond_len
, "gitdir:", &cond
, &cond_len
))
414 return include_by_gitdir(opts
, cond
, cond_len
, 0);
415 else if (skip_prefix_mem(cond
, cond_len
, "gitdir/i:", &cond
, &cond_len
))
416 return include_by_gitdir(opts
, cond
, cond_len
, 1);
417 else if (skip_prefix_mem(cond
, cond_len
, "onbranch:", &cond
, &cond_len
))
418 return include_by_branch(cond
, cond_len
);
419 else if (skip_prefix_mem(cond
, cond_len
, "hasconfig:remote.*.url:", &cond
,
421 return include_by_remote_url(inc
, cond
, cond_len
);
423 /* unknown conditionals are always false */
427 static int git_config_include(const char *var
, const char *value
, void *data
)
429 struct config_include_data
*inc
= data
;
430 const char *cond
, *key
;
435 * Pass along all values, including "include" directives; this makes it
436 * possible to query information on the includes themselves.
438 ret
= inc
->fn(var
, value
, inc
->data
);
442 if (!strcmp(var
, "include.path"))
443 ret
= handle_path_include(value
, inc
);
445 if (!parse_config_key(var
, "includeif", &cond
, &cond_len
, &key
) &&
446 cond
&& include_condition_is_true(inc
, cond
, cond_len
) &&
447 !strcmp(key
, "path")) {
448 config_fn_t old_fn
= inc
->fn
;
450 if (inc
->opts
->unconditional_remote_url
)
451 inc
->fn
= forbid_remote_url
;
452 ret
= handle_path_include(value
, inc
);
459 static void git_config_push_split_parameter(const char *key
, const char *value
)
461 struct strbuf env
= STRBUF_INIT
;
462 const char *old
= getenv(CONFIG_DATA_ENVIRONMENT
);
464 strbuf_addstr(&env
, old
);
465 strbuf_addch(&env
, ' ');
467 sq_quote_buf(&env
, key
);
468 strbuf_addch(&env
, '=');
470 sq_quote_buf(&env
, value
);
471 setenv(CONFIG_DATA_ENVIRONMENT
, env
.buf
, 1);
472 strbuf_release(&env
);
475 void git_config_push_parameter(const char *text
)
482 * section.subsection=with=equals.key=value
484 * we cannot tell if it means:
486 * [section "subsection=with=equals"]
492 * subsection = with=equals.key=value
494 * We parse left-to-right for the first "=", meaning we'll prefer to
495 * keep the value intact over the subsection. This is historical, but
496 * also sensible since values are more likely to contain odd or
497 * untrusted input than a section name.
499 * A missing equals is explicitly allowed (as a bool-only entry).
501 value
= strchr(text
, '=');
503 char *key
= xmemdupz(text
, value
- text
);
504 git_config_push_split_parameter(key
, value
+ 1);
507 git_config_push_split_parameter(text
, NULL
);
511 void git_config_push_env(const char *spec
)
514 const char *env_name
;
515 const char *env_value
;
517 env_name
= strrchr(spec
, '=');
519 die(_("invalid config format: %s"), spec
);
520 key
= xmemdupz(spec
, env_name
- spec
);
523 die(_("missing environment variable name for configuration '%.*s'"),
524 (int)(env_name
- spec
- 1), spec
);
526 env_value
= getenv(env_name
);
528 die(_("missing environment variable '%s' for configuration '%.*s'"),
529 env_name
, (int)(env_name
- spec
- 1), spec
);
531 git_config_push_split_parameter(key
, env_value
);
535 static inline int iskeychar(int c
)
537 return isalnum(c
) || c
== '-';
541 * Auxiliary function to sanity-check and split the key into the section
542 * identifier and variable name.
544 * Returns 0 on success, -1 when there is an invalid character in the key and
545 * -2 if there is no section name in the key.
547 * store_key - pointer to char* which will hold a copy of the key with
548 * lowercase section and variable name
549 * baselen - pointer to size_t which will hold the length of the
550 * section + subsection part, can be NULL
552 int git_config_parse_key(const char *key
, char **store_key
, size_t *baselen_
)
556 const char *last_dot
= strrchr(key
, '.');
559 * Since "key" actually contains the section name and the real
560 * key name separated by a dot, we have to know where the dot is.
563 if (last_dot
== NULL
|| last_dot
== key
) {
564 error(_("key does not contain a section: %s"), key
);
565 return -CONFIG_NO_SECTION_OR_NAME
;
569 error(_("key does not contain variable name: %s"), key
);
570 return -CONFIG_NO_SECTION_OR_NAME
;
573 baselen
= last_dot
- key
;
578 * Validate the key and while at it, lower case it for matching.
580 *store_key
= xmallocz(strlen(key
));
583 for (i
= 0; key
[i
]; i
++) {
584 unsigned char c
= key
[i
];
587 /* Leave the extended basename untouched.. */
588 if (!dot
|| i
> baselen
) {
590 (i
== baselen
+ 1 && !isalpha(c
))) {
591 error(_("invalid key: %s"), key
);
595 } else if (c
== '\n') {
596 error(_("invalid key (newline): %s"), key
);
605 FREE_AND_NULL(*store_key
);
606 return -CONFIG_INVALID_KEY
;
609 static int config_parse_pair(const char *key
, const char *value
,
610 config_fn_t fn
, void *data
)
612 char *canonical_name
;
616 return error(_("empty config key"));
617 if (git_config_parse_key(key
, &canonical_name
, NULL
))
620 ret
= (fn(canonical_name
, value
, data
) < 0) ? -1 : 0;
621 free(canonical_name
);
625 int git_config_parse_parameter(const char *text
,
626 config_fn_t fn
, void *data
)
629 struct strbuf
**pair
;
632 pair
= strbuf_split_str(text
, '=', 2);
634 return error(_("bogus config parameter: %s"), text
);
636 if (pair
[0]->len
&& pair
[0]->buf
[pair
[0]->len
- 1] == '=') {
637 strbuf_setlen(pair
[0], pair
[0]->len
- 1);
638 value
= pair
[1] ? pair
[1]->buf
: "";
643 strbuf_trim(pair
[0]);
645 strbuf_list_free(pair
);
646 return error(_("bogus config parameter: %s"), text
);
649 ret
= config_parse_pair(pair
[0]->buf
, value
, fn
, data
);
650 strbuf_list_free(pair
);
654 static int parse_config_env_list(char *env
, config_fn_t fn
, void *data
)
657 while (cur
&& *cur
) {
658 const char *key
= sq_dequote_step(cur
, &cur
);
660 return error(_("bogus format in %s"),
661 CONFIG_DATA_ENVIRONMENT
);
663 if (!cur
|| isspace(*cur
)) {
664 /* old-style 'key=value' */
665 if (git_config_parse_parameter(key
, fn
, data
) < 0)
668 else if (*cur
== '=') {
669 /* new-style 'key'='value' */
675 value
= sq_dequote_step(cur
, &cur
);
676 if (!value
|| (cur
&& !isspace(*cur
))) {
677 return error(_("bogus format in %s"),
678 CONFIG_DATA_ENVIRONMENT
);
680 } else if (!*cur
|| isspace(*cur
)) {
681 /* implicit bool: 'key'= */
684 return error(_("bogus format in %s"),
685 CONFIG_DATA_ENVIRONMENT
);
688 if (config_parse_pair(key
, value
, fn
, data
) < 0)
693 return error(_("bogus format in %s"),
694 CONFIG_DATA_ENVIRONMENT
);
698 while (isspace(*cur
))
705 int git_config_from_parameters(config_fn_t fn
, void *data
)
708 struct strbuf envvar
= STRBUF_INIT
;
709 struct strvec to_free
= STRVEC_INIT
;
712 struct config_source source
;
714 memset(&source
, 0, sizeof(source
));
716 source
.origin_type
= CONFIG_ORIGIN_CMDLINE
;
719 env
= getenv(CONFIG_COUNT_ENVIRONMENT
);
725 count
= strtoul(env
, &endp
, 10);
727 ret
= error(_("bogus count in %s"), CONFIG_COUNT_ENVIRONMENT
);
730 if (count
> INT_MAX
) {
731 ret
= error(_("too many entries in %s"), CONFIG_COUNT_ENVIRONMENT
);
735 for (i
= 0; i
< count
; i
++) {
736 const char *key
, *value
;
738 strbuf_addf(&envvar
, "GIT_CONFIG_KEY_%d", i
);
739 key
= getenv_safe(&to_free
, envvar
.buf
);
741 ret
= error(_("missing config key %s"), envvar
.buf
);
744 strbuf_reset(&envvar
);
746 strbuf_addf(&envvar
, "GIT_CONFIG_VALUE_%d", i
);
747 value
= getenv_safe(&to_free
, envvar
.buf
);
749 ret
= error(_("missing config value %s"), envvar
.buf
);
752 strbuf_reset(&envvar
);
754 if (config_parse_pair(key
, value
, fn
, data
) < 0) {
761 env
= getenv(CONFIG_DATA_ENVIRONMENT
);
763 /* sq_dequote will write over it */
765 if (parse_config_env_list(envw
, fn
, data
) < 0) {
772 strbuf_release(&envvar
);
773 strvec_clear(&to_free
);
779 static int get_next_char(void)
781 int c
= cf
->do_fgetc(cf
);
784 /* DOS like systems */
785 c
= cf
->do_fgetc(cf
);
788 cf
->do_ungetc(c
, cf
);
793 if (c
!= EOF
&& ++cf
->total_len
> INT_MAX
) {
795 * This is an absurdly long config file; refuse to parse
796 * further in order to protect downstream code from integer
797 * overflows. Note that we can't return an error specifically,
798 * but we can mark EOF and put trash in the return value,
799 * which will trigger a parse error.
815 static char *parse_value(void)
817 int quote
= 0, comment
= 0, space
= 0;
819 strbuf_reset(&cf
->value
);
821 int c
= get_next_char();
827 return cf
->value
.buf
;
831 if (isspace(c
) && !quote
) {
837 if (c
== ';' || c
== '#') {
842 for (; space
; space
--)
843 strbuf_addch(&cf
->value
, ' ');
858 /* Some characters escape as themselves */
861 /* Reject unknown escape sequences */
865 strbuf_addch(&cf
->value
, c
);
872 strbuf_addch(&cf
->value
, c
);
876 static int get_value(config_fn_t fn
, void *data
, struct strbuf
*name
)
882 /* Get the full name */
889 strbuf_addch(name
, tolower(c
));
892 while (c
== ' ' || c
== '\t')
899 value
= parse_value();
904 * We already consumed the \n, but we need linenr to point to
905 * the line we just parsed during the call to fn to get
906 * accurate line number in error messages.
909 ret
= fn(name
->buf
, value
, data
);
915 static int get_extended_base_var(struct strbuf
*name
, int c
)
917 cf
->subsection_case_sensitive
= 0;
920 goto error_incomplete_line
;
922 } while (isspace(c
));
924 /* We require the format to be '[base "extension"]' */
927 strbuf_addch(name
, '.');
930 int c
= get_next_char();
932 goto error_incomplete_line
;
938 goto error_incomplete_line
;
940 strbuf_addch(name
, c
);
944 if (get_next_char() != ']')
947 error_incomplete_line
:
952 static int get_base_var(struct strbuf
*name
)
954 cf
->subsection_case_sensitive
= 1;
956 int c
= get_next_char();
962 return get_extended_base_var(name
, c
);
963 if (!iskeychar(c
) && c
!= '.')
965 strbuf_addch(name
, tolower(c
));
969 struct parse_event_data
{
970 enum config_event_t previous_type
;
971 size_t previous_offset
;
972 const struct config_options
*opts
;
975 static int do_event(enum config_event_t type
, struct parse_event_data
*data
)
979 if (!data
->opts
|| !data
->opts
->event_fn
)
982 if (type
== CONFIG_EVENT_WHITESPACE
&&
983 data
->previous_type
== type
)
986 offset
= cf
->do_ftell(cf
);
988 * At EOF, the parser always "inserts" an extra '\n', therefore
989 * the end offset of the event is the current file position, otherwise
990 * we will already have advanced to the next event.
992 if (type
!= CONFIG_EVENT_EOF
)
995 if (data
->previous_type
!= CONFIG_EVENT_EOF
&&
996 data
->opts
->event_fn(data
->previous_type
, data
->previous_offset
,
997 offset
, data
->opts
->event_fn_data
) < 0)
1000 data
->previous_type
= type
;
1001 data
->previous_offset
= offset
;
1006 static int git_parse_source(config_fn_t fn
, void *data
,
1007 const struct config_options
*opts
)
1011 struct strbuf
*var
= &cf
->var
;
1012 int error_return
= 0;
1013 char *error_msg
= NULL
;
1015 /* U+FEFF Byte Order Mark in UTF8 */
1016 const char *bomptr
= utf8_bom
;
1018 /* For the parser event callback */
1019 struct parse_event_data event_data
= {
1020 CONFIG_EVENT_EOF
, 0, opts
1026 c
= get_next_char();
1027 if (bomptr
&& *bomptr
) {
1028 /* We are at the file beginning; skip UTF8-encoded BOM
1029 * if present. Sane editors won't put this in on their
1030 * own, but e.g. Windows Notepad will do it happily. */
1031 if (c
== (*bomptr
& 0377)) {
1035 /* Do not tolerate partial BOM. */
1036 if (bomptr
!= utf8_bom
)
1038 /* No BOM at file beginning. Cool. */
1044 if (do_event(CONFIG_EVENT_EOF
, &event_data
) < 0)
1048 if (do_event(CONFIG_EVENT_WHITESPACE
, &event_data
) < 0)
1056 if (do_event(CONFIG_EVENT_WHITESPACE
, &event_data
) < 0)
1060 if (c
== '#' || c
== ';') {
1061 if (do_event(CONFIG_EVENT_COMMENT
, &event_data
) < 0)
1067 if (do_event(CONFIG_EVENT_SECTION
, &event_data
) < 0)
1070 /* Reset prior to determining a new stem */
1072 if (get_base_var(var
) < 0 || var
->len
< 1)
1074 strbuf_addch(var
, '.');
1081 if (do_event(CONFIG_EVENT_ENTRY
, &event_data
) < 0)
1085 * Truncate the var name back to the section header
1086 * stem prior to grabbing the suffix part of the name
1089 strbuf_setlen(var
, baselen
);
1090 strbuf_addch(var
, tolower(c
));
1091 if (get_value(fn
, data
, var
) < 0)
1095 if (do_event(CONFIG_EVENT_ERROR
, &event_data
) < 0)
1098 switch (cf
->origin_type
) {
1099 case CONFIG_ORIGIN_BLOB
:
1100 error_msg
= xstrfmt(_("bad config line %d in blob %s"),
1101 cf
->linenr
, cf
->name
);
1103 case CONFIG_ORIGIN_FILE
:
1104 error_msg
= xstrfmt(_("bad config line %d in file %s"),
1105 cf
->linenr
, cf
->name
);
1107 case CONFIG_ORIGIN_STDIN
:
1108 error_msg
= xstrfmt(_("bad config line %d in standard input"),
1111 case CONFIG_ORIGIN_SUBMODULE_BLOB
:
1112 error_msg
= xstrfmt(_("bad config line %d in submodule-blob %s"),
1113 cf
->linenr
, cf
->name
);
1115 case CONFIG_ORIGIN_CMDLINE
:
1116 error_msg
= xstrfmt(_("bad config line %d in command line %s"),
1117 cf
->linenr
, cf
->name
);
1120 error_msg
= xstrfmt(_("bad config line %d in %s"),
1121 cf
->linenr
, cf
->name
);
1124 switch (opts
&& opts
->error_action
?
1125 opts
->error_action
:
1126 cf
->default_error_action
) {
1127 case CONFIG_ERROR_DIE
:
1128 die("%s", error_msg
);
1130 case CONFIG_ERROR_ERROR
:
1131 error_return
= error("%s", error_msg
);
1133 case CONFIG_ERROR_SILENT
:
1136 case CONFIG_ERROR_UNSET
:
1137 BUG("config error action unset");
1141 return error_return
;
1144 static uintmax_t get_unit_factor(const char *end
)
1148 else if (!strcasecmp(end
, "k"))
1150 else if (!strcasecmp(end
, "m"))
1152 else if (!strcasecmp(end
, "g"))
1153 return 1024 * 1024 * 1024;
1157 static int git_parse_signed(const char *value
, intmax_t *ret
, intmax_t max
)
1159 if (value
&& *value
) {
1166 val
= strtoimax(value
, &end
, 0);
1167 if (errno
== ERANGE
)
1169 factor
= get_unit_factor(end
);
1174 uval
= val
< 0 ? -val
: val
;
1175 if (unsigned_mult_overflows(factor
, uval
) ||
1176 factor
* uval
> max
) {
1188 static int git_parse_unsigned(const char *value
, uintmax_t *ret
, uintmax_t max
)
1190 if (value
&& *value
) {
1196 val
= strtoumax(value
, &end
, 0);
1197 if (errno
== ERANGE
)
1199 factor
= get_unit_factor(end
);
1204 if (unsigned_mult_overflows(factor
, val
) ||
1205 factor
* val
> max
) {
1217 static int git_parse_int(const char *value
, int *ret
)
1220 if (!git_parse_signed(value
, &tmp
, maximum_signed_value_of_type(int)))
1226 static int git_parse_int64(const char *value
, int64_t *ret
)
1229 if (!git_parse_signed(value
, &tmp
, maximum_signed_value_of_type(int64_t)))
1235 int git_parse_ulong(const char *value
, unsigned long *ret
)
1238 if (!git_parse_unsigned(value
, &tmp
, maximum_unsigned_value_of_type(long)))
1244 int git_parse_ssize_t(const char *value
, ssize_t
*ret
)
1247 if (!git_parse_signed(value
, &tmp
, maximum_signed_value_of_type(ssize_t
)))
1254 static void die_bad_number(const char *name
, const char *value
)
1256 const char *error_type
= (errno
== ERANGE
) ?
1257 N_("out of range") : N_("invalid unit");
1258 const char *bad_numeric
= N_("bad numeric config value '%s' for '%s': %s");
1263 if (!(cf
&& cf
->name
))
1264 die(_(bad_numeric
), value
, name
, _(error_type
));
1266 switch (cf
->origin_type
) {
1267 case CONFIG_ORIGIN_BLOB
:
1268 die(_("bad numeric config value '%s' for '%s' in blob %s: %s"),
1269 value
, name
, cf
->name
, _(error_type
));
1270 case CONFIG_ORIGIN_FILE
:
1271 die(_("bad numeric config value '%s' for '%s' in file %s: %s"),
1272 value
, name
, cf
->name
, _(error_type
));
1273 case CONFIG_ORIGIN_STDIN
:
1274 die(_("bad numeric config value '%s' for '%s' in standard input: %s"),
1275 value
, name
, _(error_type
));
1276 case CONFIG_ORIGIN_SUBMODULE_BLOB
:
1277 die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"),
1278 value
, name
, cf
->name
, _(error_type
));
1279 case CONFIG_ORIGIN_CMDLINE
:
1280 die(_("bad numeric config value '%s' for '%s' in command line %s: %s"),
1281 value
, name
, cf
->name
, _(error_type
));
1283 die(_("bad numeric config value '%s' for '%s' in %s: %s"),
1284 value
, name
, cf
->name
, _(error_type
));
1288 int git_config_int(const char *name
, const char *value
)
1291 if (!git_parse_int(value
, &ret
))
1292 die_bad_number(name
, value
);
1296 int64_t git_config_int64(const char *name
, const char *value
)
1299 if (!git_parse_int64(value
, &ret
))
1300 die_bad_number(name
, value
);
1304 unsigned long git_config_ulong(const char *name
, const char *value
)
1307 if (!git_parse_ulong(value
, &ret
))
1308 die_bad_number(name
, value
);
1312 ssize_t
git_config_ssize_t(const char *name
, const char *value
)
1315 if (!git_parse_ssize_t(value
, &ret
))
1316 die_bad_number(name
, value
);
1320 static int git_parse_maybe_bool_text(const char *value
)
1326 if (!strcasecmp(value
, "true")
1327 || !strcasecmp(value
, "yes")
1328 || !strcasecmp(value
, "on"))
1330 if (!strcasecmp(value
, "false")
1331 || !strcasecmp(value
, "no")
1332 || !strcasecmp(value
, "off"))
1337 static const struct fsync_component_name
{
1339 enum fsync_component component_bits
;
1340 } fsync_component_names
[] = {
1341 { "loose-object", FSYNC_COMPONENT_LOOSE_OBJECT
},
1342 { "pack", FSYNC_COMPONENT_PACK
},
1343 { "pack-metadata", FSYNC_COMPONENT_PACK_METADATA
},
1344 { "commit-graph", FSYNC_COMPONENT_COMMIT_GRAPH
},
1345 { "index", FSYNC_COMPONENT_INDEX
},
1346 { "objects", FSYNC_COMPONENTS_OBJECTS
},
1347 { "reference", FSYNC_COMPONENT_REFERENCE
},
1348 { "derived-metadata", FSYNC_COMPONENTS_DERIVED_METADATA
},
1349 { "committed", FSYNC_COMPONENTS_COMMITTED
},
1350 { "added", FSYNC_COMPONENTS_ADDED
},
1351 { "all", FSYNC_COMPONENTS_ALL
},
1354 static enum fsync_component
parse_fsync_components(const char *var
, const char *string
)
1356 enum fsync_component current
= FSYNC_COMPONENTS_PLATFORM_DEFAULT
;
1357 enum fsync_component positive
= 0, negative
= 0;
1366 string
= string
+ strspn(string
, ", \t\n\r");
1367 ep
= strchrnul(string
, ',');
1369 if (!strcmp(string
, "none")) {
1370 current
= FSYNC_COMPONENT_NONE
;
1374 if (*string
== '-') {
1379 warning(_("invalid value for variable %s"), var
);
1385 for (i
= 0; i
< ARRAY_SIZE(fsync_component_names
); ++i
) {
1386 const struct fsync_component_name
*n
= &fsync_component_names
[i
];
1388 if (strncmp(n
->name
, string
, len
))
1393 negative
|= n
->component_bits
;
1395 positive
|= n
->component_bits
;
1399 char *component
= xstrndup(string
, len
);
1400 warning(_("ignoring unknown core.fsync component '%s'"), component
);
1408 return (current
& ~negative
) | positive
;
1411 int git_parse_maybe_bool(const char *value
)
1413 int v
= git_parse_maybe_bool_text(value
);
1416 if (git_parse_int(value
, &v
))
1421 int git_config_bool_or_int(const char *name
, const char *value
, int *is_bool
)
1423 int v
= git_parse_maybe_bool_text(value
);
1429 return git_config_int(name
, value
);
1432 int git_config_bool(const char *name
, const char *value
)
1434 int v
= git_parse_maybe_bool(value
);
1436 die(_("bad boolean config value '%s' for '%s'"), value
, name
);
1440 int git_config_string(const char **dest
, const char *var
, const char *value
)
1443 return config_error_nonbool(var
);
1444 *dest
= xstrdup(value
);
1448 int git_config_pathname(const char **dest
, const char *var
, const char *value
)
1451 return config_error_nonbool(var
);
1452 *dest
= interpolate_path(value
, 0);
1454 die(_("failed to expand user dir in: '%s'"), value
);
1458 int git_config_expiry_date(timestamp_t
*timestamp
, const char *var
, const char *value
)
1461 return config_error_nonbool(var
);
1462 if (parse_expiry_date(value
, timestamp
))
1463 return error(_("'%s' for '%s' is not a valid timestamp"),
1468 int git_config_color(char *dest
, const char *var
, const char *value
)
1471 return config_error_nonbool(var
);
1472 if (color_parse(value
, dest
) < 0)
1477 static int git_default_core_config(const char *var
, const char *value
, void *cb
)
1479 /* This needs a better name */
1480 if (!strcmp(var
, "core.filemode")) {
1481 trust_executable_bit
= git_config_bool(var
, value
);
1484 if (!strcmp(var
, "core.trustctime")) {
1485 trust_ctime
= git_config_bool(var
, value
);
1488 if (!strcmp(var
, "core.checkstat")) {
1489 if (!strcasecmp(value
, "default"))
1491 else if (!strcasecmp(value
, "minimal"))
1495 if (!strcmp(var
, "core.quotepath")) {
1496 quote_path_fully
= git_config_bool(var
, value
);
1500 if (!strcmp(var
, "core.symlinks")) {
1501 has_symlinks
= git_config_bool(var
, value
);
1505 if (!strcmp(var
, "core.ignorecase")) {
1506 ignore_case
= git_config_bool(var
, value
);
1510 if (!strcmp(var
, "core.attributesfile"))
1511 return git_config_pathname(&git_attributes_file
, var
, value
);
1513 if (!strcmp(var
, "core.hookspath"))
1514 return git_config_pathname(&git_hooks_path
, var
, value
);
1516 if (!strcmp(var
, "core.bare")) {
1517 is_bare_repository_cfg
= git_config_bool(var
, value
);
1521 if (!strcmp(var
, "core.ignorestat")) {
1522 assume_unchanged
= git_config_bool(var
, value
);
1526 if (!strcmp(var
, "core.prefersymlinkrefs")) {
1527 prefer_symlink_refs
= git_config_bool(var
, value
);
1531 if (!strcmp(var
, "core.logallrefupdates")) {
1532 if (value
&& !strcasecmp(value
, "always"))
1533 log_all_ref_updates
= LOG_REFS_ALWAYS
;
1534 else if (git_config_bool(var
, value
))
1535 log_all_ref_updates
= LOG_REFS_NORMAL
;
1537 log_all_ref_updates
= LOG_REFS_NONE
;
1541 if (!strcmp(var
, "core.warnambiguousrefs")) {
1542 warn_ambiguous_refs
= git_config_bool(var
, value
);
1546 if (!strcmp(var
, "core.abbrev")) {
1548 return config_error_nonbool(var
);
1549 if (!strcasecmp(value
, "auto"))
1550 default_abbrev
= -1;
1551 else if (!git_parse_maybe_bool_text(value
))
1552 default_abbrev
= the_hash_algo
->hexsz
;
1554 int abbrev
= git_config_int(var
, value
);
1555 if (abbrev
< minimum_abbrev
|| abbrev
> the_hash_algo
->hexsz
)
1556 return error(_("abbrev length out of range: %d"), abbrev
);
1557 default_abbrev
= abbrev
;
1562 if (!strcmp(var
, "core.disambiguate"))
1563 return set_disambiguate_hint_config(var
, value
);
1565 if (!strcmp(var
, "core.loosecompression")) {
1566 int level
= git_config_int(var
, value
);
1568 level
= Z_DEFAULT_COMPRESSION
;
1569 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
1570 die(_("bad zlib compression level %d"), level
);
1571 zlib_compression_level
= level
;
1572 zlib_compression_seen
= 1;
1576 if (!strcmp(var
, "core.compression")) {
1577 int level
= git_config_int(var
, value
);
1579 level
= Z_DEFAULT_COMPRESSION
;
1580 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
1581 die(_("bad zlib compression level %d"), level
);
1582 if (!zlib_compression_seen
)
1583 zlib_compression_level
= level
;
1584 if (!pack_compression_seen
)
1585 pack_compression_level
= level
;
1589 if (!strcmp(var
, "core.packedgitwindowsize")) {
1590 int pgsz_x2
= getpagesize() * 2;
1591 packed_git_window_size
= git_config_ulong(var
, value
);
1593 /* This value must be multiple of (pagesize * 2) */
1594 packed_git_window_size
/= pgsz_x2
;
1595 if (packed_git_window_size
< 1)
1596 packed_git_window_size
= 1;
1597 packed_git_window_size
*= pgsz_x2
;
1601 if (!strcmp(var
, "core.bigfilethreshold")) {
1602 big_file_threshold
= git_config_ulong(var
, value
);
1606 if (!strcmp(var
, "core.packedgitlimit")) {
1607 packed_git_limit
= git_config_ulong(var
, value
);
1611 if (!strcmp(var
, "core.deltabasecachelimit")) {
1612 delta_base_cache_limit
= git_config_ulong(var
, value
);
1616 if (!strcmp(var
, "core.autocrlf")) {
1617 if (value
&& !strcasecmp(value
, "input")) {
1618 auto_crlf
= AUTO_CRLF_INPUT
;
1621 auto_crlf
= git_config_bool(var
, value
);
1625 if (!strcmp(var
, "core.safecrlf")) {
1627 if (value
&& !strcasecmp(value
, "warn")) {
1628 global_conv_flags_eol
= CONV_EOL_RNDTRP_WARN
;
1631 eol_rndtrp_die
= git_config_bool(var
, value
);
1632 global_conv_flags_eol
= eol_rndtrp_die
?
1633 CONV_EOL_RNDTRP_DIE
: 0;
1637 if (!strcmp(var
, "core.eol")) {
1638 if (value
&& !strcasecmp(value
, "lf"))
1640 else if (value
&& !strcasecmp(value
, "crlf"))
1641 core_eol
= EOL_CRLF
;
1642 else if (value
&& !strcasecmp(value
, "native"))
1643 core_eol
= EOL_NATIVE
;
1645 core_eol
= EOL_UNSET
;
1649 if (!strcmp(var
, "core.checkroundtripencoding")) {
1650 check_roundtrip_encoding
= xstrdup(value
);
1654 if (!strcmp(var
, "core.notesref")) {
1655 notes_ref_name
= xstrdup(value
);
1659 if (!strcmp(var
, "core.editor"))
1660 return git_config_string(&editor_program
, var
, value
);
1662 if (!strcmp(var
, "core.commentchar")) {
1664 return config_error_nonbool(var
);
1665 else if (!strcasecmp(value
, "auto"))
1666 auto_comment_line_char
= 1;
1667 else if (value
[0] && !value
[1]) {
1668 comment_line_char
= value
[0];
1669 auto_comment_line_char
= 0;
1671 return error(_("core.commentChar should only be one character"));
1675 if (!strcmp(var
, "core.askpass"))
1676 return git_config_string(&askpass_program
, var
, value
);
1678 if (!strcmp(var
, "core.excludesfile"))
1679 return git_config_pathname(&excludes_file
, var
, value
);
1681 if (!strcmp(var
, "core.whitespace")) {
1683 return config_error_nonbool(var
);
1684 whitespace_rule_cfg
= parse_whitespace_rule(value
);
1688 if (!strcmp(var
, "core.fsync")) {
1690 return config_error_nonbool(var
);
1691 fsync_components
= parse_fsync_components(var
, value
);
1695 if (!strcmp(var
, "core.fsyncmethod")) {
1697 return config_error_nonbool(var
);
1698 if (!strcmp(value
, "fsync"))
1699 fsync_method
= FSYNC_METHOD_FSYNC
;
1700 else if (!strcmp(value
, "writeout-only"))
1701 fsync_method
= FSYNC_METHOD_WRITEOUT_ONLY
;
1702 else if (!strcmp(value
, "batch"))
1703 fsync_method
= FSYNC_METHOD_BATCH
;
1705 warning(_("ignoring unknown core.fsyncMethod value '%s'"), value
);
1709 if (!strcmp(var
, "core.fsyncobjectfiles")) {
1710 if (fsync_object_files
< 0)
1711 warning(_("core.fsyncObjectFiles is deprecated; use core.fsync instead"));
1712 fsync_object_files
= git_config_bool(var
, value
);
1716 if (!strcmp(var
, "core.preloadindex")) {
1717 core_preload_index
= git_config_bool(var
, value
);
1721 if (!strcmp(var
, "core.createobject")) {
1722 if (!strcmp(value
, "rename"))
1723 object_creation_mode
= OBJECT_CREATION_USES_RENAMES
;
1724 else if (!strcmp(value
, "link"))
1725 object_creation_mode
= OBJECT_CREATION_USES_HARDLINKS
;
1727 die(_("invalid mode for object creation: %s"), value
);
1731 if (!strcmp(var
, "core.sparsecheckout")) {
1732 core_apply_sparse_checkout
= git_config_bool(var
, value
);
1736 if (!strcmp(var
, "core.sparsecheckoutcone")) {
1737 core_sparse_checkout_cone
= git_config_bool(var
, value
);
1741 if (!strcmp(var
, "core.precomposeunicode")) {
1742 precomposed_unicode
= git_config_bool(var
, value
);
1746 if (!strcmp(var
, "core.protecthfs")) {
1747 protect_hfs
= git_config_bool(var
, value
);
1751 if (!strcmp(var
, "core.protectntfs")) {
1752 protect_ntfs
= git_config_bool(var
, value
);
1756 if (!strcmp(var
, "core.usereplacerefs")) {
1757 read_replace_refs
= git_config_bool(var
, value
);
1761 /* Add other config variables here and to Documentation/config.txt. */
1762 return platform_core_config(var
, value
, cb
);
1765 static int git_default_sparse_config(const char *var
, const char *value
)
1767 if (!strcmp(var
, "sparse.expectfilesoutsideofpatterns")) {
1768 sparse_expect_files_outside_of_patterns
= git_config_bool(var
, value
);
1772 /* Add other config variables here and to Documentation/config/sparse.txt. */
1776 static int git_default_i18n_config(const char *var
, const char *value
)
1778 if (!strcmp(var
, "i18n.commitencoding"))
1779 return git_config_string(&git_commit_encoding
, var
, value
);
1781 if (!strcmp(var
, "i18n.logoutputencoding"))
1782 return git_config_string(&git_log_output_encoding
, var
, value
);
1784 /* Add other config variables here and to Documentation/config.txt. */
1788 static int git_default_branch_config(const char *var
, const char *value
)
1790 if (!strcmp(var
, "branch.autosetupmerge")) {
1791 if (value
&& !strcmp(value
, "always")) {
1792 git_branch_track
= BRANCH_TRACK_ALWAYS
;
1794 } else if (value
&& !strcmp(value
, "inherit")) {
1795 git_branch_track
= BRANCH_TRACK_INHERIT
;
1797 } else if (value
&& !strcmp(value
, "simple")) {
1798 git_branch_track
= BRANCH_TRACK_SIMPLE
;
1801 git_branch_track
= git_config_bool(var
, value
);
1804 if (!strcmp(var
, "branch.autosetuprebase")) {
1806 return config_error_nonbool(var
);
1807 else if (!strcmp(value
, "never"))
1808 autorebase
= AUTOREBASE_NEVER
;
1809 else if (!strcmp(value
, "local"))
1810 autorebase
= AUTOREBASE_LOCAL
;
1811 else if (!strcmp(value
, "remote"))
1812 autorebase
= AUTOREBASE_REMOTE
;
1813 else if (!strcmp(value
, "always"))
1814 autorebase
= AUTOREBASE_ALWAYS
;
1816 return error(_("malformed value for %s"), var
);
1820 /* Add other config variables here and to Documentation/config.txt. */
1824 static int git_default_push_config(const char *var
, const char *value
)
1826 if (!strcmp(var
, "push.default")) {
1828 return config_error_nonbool(var
);
1829 else if (!strcmp(value
, "nothing"))
1830 push_default
= PUSH_DEFAULT_NOTHING
;
1831 else if (!strcmp(value
, "matching"))
1832 push_default
= PUSH_DEFAULT_MATCHING
;
1833 else if (!strcmp(value
, "simple"))
1834 push_default
= PUSH_DEFAULT_SIMPLE
;
1835 else if (!strcmp(value
, "upstream"))
1836 push_default
= PUSH_DEFAULT_UPSTREAM
;
1837 else if (!strcmp(value
, "tracking")) /* deprecated */
1838 push_default
= PUSH_DEFAULT_UPSTREAM
;
1839 else if (!strcmp(value
, "current"))
1840 push_default
= PUSH_DEFAULT_CURRENT
;
1842 error(_("malformed value for %s: %s"), var
, value
);
1843 return error(_("must be one of nothing, matching, simple, "
1844 "upstream or current"));
1849 /* Add other config variables here and to Documentation/config.txt. */
1853 static int git_default_mailmap_config(const char *var
, const char *value
)
1855 if (!strcmp(var
, "mailmap.file"))
1856 return git_config_pathname(&git_mailmap_file
, var
, value
);
1857 if (!strcmp(var
, "mailmap.blob"))
1858 return git_config_string(&git_mailmap_blob
, var
, value
);
1860 /* Add other config variables here and to Documentation/config.txt. */
1864 int git_default_config(const char *var
, const char *value
, void *cb
)
1866 if (starts_with(var
, "core."))
1867 return git_default_core_config(var
, value
, cb
);
1869 if (starts_with(var
, "user.") ||
1870 starts_with(var
, "author.") ||
1871 starts_with(var
, "committer."))
1872 return git_ident_config(var
, value
, cb
);
1874 if (starts_with(var
, "i18n."))
1875 return git_default_i18n_config(var
, value
);
1877 if (starts_with(var
, "branch."))
1878 return git_default_branch_config(var
, value
);
1880 if (starts_with(var
, "push."))
1881 return git_default_push_config(var
, value
);
1883 if (starts_with(var
, "mailmap."))
1884 return git_default_mailmap_config(var
, value
);
1886 if (starts_with(var
, "advice.") || starts_with(var
, "color.advice"))
1887 return git_default_advice_config(var
, value
);
1889 if (!strcmp(var
, "pager.color") || !strcmp(var
, "color.pager")) {
1890 pager_use_color
= git_config_bool(var
,value
);
1894 if (!strcmp(var
, "pack.packsizelimit")) {
1895 pack_size_limit_cfg
= git_config_ulong(var
, value
);
1899 if (!strcmp(var
, "pack.compression")) {
1900 int level
= git_config_int(var
, value
);
1902 level
= Z_DEFAULT_COMPRESSION
;
1903 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
1904 die(_("bad pack compression level %d"), level
);
1905 pack_compression_level
= level
;
1906 pack_compression_seen
= 1;
1910 if (starts_with(var
, "sparse."))
1911 return git_default_sparse_config(var
, value
);
1913 /* Add other config variables here and to Documentation/config.txt. */
1918 * All source specific fields in the union, die_on_error, name and the callbacks
1919 * fgetc, ungetc, ftell of top need to be initialized before calling
1922 static int do_config_from(struct config_source
*top
, config_fn_t fn
, void *data
,
1923 const struct config_options
*opts
)
1927 /* push config-file parsing state stack */
1932 strbuf_init(&top
->value
, 1024);
1933 strbuf_init(&top
->var
, 1024);
1936 ret
= git_parse_source(fn
, data
, opts
);
1938 /* pop config-file parsing state stack */
1939 strbuf_release(&top
->value
);
1940 strbuf_release(&top
->var
);
1946 static int do_config_from_file(config_fn_t fn
,
1947 const enum config_origin_type origin_type
,
1948 const char *name
, const char *path
, FILE *f
,
1949 void *data
, const struct config_options
*opts
)
1951 struct config_source top
;
1955 top
.origin_type
= origin_type
;
1958 top
.default_error_action
= CONFIG_ERROR_DIE
;
1959 top
.do_fgetc
= config_file_fgetc
;
1960 top
.do_ungetc
= config_file_ungetc
;
1961 top
.do_ftell
= config_file_ftell
;
1964 ret
= do_config_from(&top
, fn
, data
, opts
);
1969 static int git_config_from_stdin(config_fn_t fn
, void *data
)
1971 return do_config_from_file(fn
, CONFIG_ORIGIN_STDIN
, "", NULL
, stdin
,
1975 int git_config_from_file_with_options(config_fn_t fn
, const char *filename
,
1977 const struct config_options
*opts
)
1982 f
= fopen_or_warn(filename
, "r");
1984 ret
= do_config_from_file(fn
, CONFIG_ORIGIN_FILE
, filename
,
1985 filename
, f
, data
, opts
);
1991 int git_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
1993 return git_config_from_file_with_options(fn
, filename
, data
, NULL
);
1996 int git_config_from_mem(config_fn_t fn
,
1997 const enum config_origin_type origin_type
,
1998 const char *name
, const char *buf
, size_t len
,
1999 void *data
, const struct config_options
*opts
)
2001 struct config_source top
;
2003 top
.u
.buf
.buf
= buf
;
2004 top
.u
.buf
.len
= len
;
2006 top
.origin_type
= origin_type
;
2009 top
.default_error_action
= CONFIG_ERROR_ERROR
;
2010 top
.do_fgetc
= config_buf_fgetc
;
2011 top
.do_ungetc
= config_buf_ungetc
;
2012 top
.do_ftell
= config_buf_ftell
;
2014 return do_config_from(&top
, fn
, data
, opts
);
2017 int git_config_from_blob_oid(config_fn_t fn
,
2019 struct repository
*repo
,
2020 const struct object_id
*oid
,
2023 enum object_type type
;
2028 buf
= repo_read_object_file(repo
, oid
, &type
, &size
);
2030 return error(_("unable to load config blob object '%s'"), name
);
2031 if (type
!= OBJ_BLOB
) {
2033 return error(_("reference '%s' does not point to a blob"), name
);
2036 ret
= git_config_from_mem(fn
, CONFIG_ORIGIN_BLOB
, name
, buf
, size
,
2043 static int git_config_from_blob_ref(config_fn_t fn
,
2044 struct repository
*repo
,
2048 struct object_id oid
;
2050 if (repo_get_oid(repo
, name
, &oid
) < 0)
2051 return error(_("unable to resolve config blob '%s'"), name
);
2052 return git_config_from_blob_oid(fn
, name
, repo
, &oid
, data
);
2055 char *git_system_config(void)
2057 char *system_config
= xstrdup_or_null(getenv("GIT_CONFIG_SYSTEM"));
2059 system_config
= system_path(ETC_GITCONFIG
);
2060 normalize_path_copy(system_config
, system_config
);
2061 return system_config
;
2064 void git_global_config(char **user_out
, char **xdg_out
)
2066 char *user_config
= xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
2067 char *xdg_config
= NULL
;
2070 user_config
= interpolate_path("~/.gitconfig", 0);
2071 xdg_config
= xdg_config_home("config");
2074 *user_out
= user_config
;
2075 *xdg_out
= xdg_config
;
2079 * Parse environment variable 'k' as a boolean (in various
2080 * possible spellings); if missing, use the default value 'def'.
2082 int git_env_bool(const char *k
, int def
)
2084 const char *v
= getenv(k
);
2085 return v
? git_config_bool(k
, v
) : def
;
2089 * Parse environment variable 'k' as ulong with possibly a unit
2090 * suffix; if missing, use the default value 'val'.
2092 unsigned long git_env_ulong(const char *k
, unsigned long val
)
2094 const char *v
= getenv(k
);
2095 if (v
&& !git_parse_ulong(v
, &val
))
2096 die(_("failed to parse %s"), k
);
2100 int git_config_system(void)
2102 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
2105 static int do_git_config_sequence(const struct config_options
*opts
,
2106 config_fn_t fn
, void *data
)
2109 char *system_config
= git_system_config();
2110 char *xdg_config
= NULL
;
2111 char *user_config
= NULL
;
2113 enum config_scope prev_parsing_scope
= current_parsing_scope
;
2115 if (opts
->commondir
)
2116 repo_config
= mkpathdup("%s/config", opts
->commondir
);
2117 else if (opts
->git_dir
)
2118 BUG("git_dir without commondir");
2122 current_parsing_scope
= CONFIG_SCOPE_SYSTEM
;
2123 if (git_config_system() && system_config
&&
2124 !access_or_die(system_config
, R_OK
,
2125 opts
->system_gently
? ACCESS_EACCES_OK
: 0))
2126 ret
+= git_config_from_file(fn
, system_config
, data
);
2128 current_parsing_scope
= CONFIG_SCOPE_GLOBAL
;
2129 git_global_config(&user_config
, &xdg_config
);
2131 if (xdg_config
&& !access_or_die(xdg_config
, R_OK
, ACCESS_EACCES_OK
))
2132 ret
+= git_config_from_file(fn
, xdg_config
, data
);
2134 if (user_config
&& !access_or_die(user_config
, R_OK
, ACCESS_EACCES_OK
))
2135 ret
+= git_config_from_file(fn
, user_config
, data
);
2137 current_parsing_scope
= CONFIG_SCOPE_LOCAL
;
2138 if (!opts
->ignore_repo
&& repo_config
&&
2139 !access_or_die(repo_config
, R_OK
, 0))
2140 ret
+= git_config_from_file(fn
, repo_config
, data
);
2142 current_parsing_scope
= CONFIG_SCOPE_WORKTREE
;
2143 if (!opts
->ignore_worktree
&& repository_format_worktree_config
) {
2144 char *path
= git_pathdup("config.worktree");
2145 if (!access_or_die(path
, R_OK
, 0))
2146 ret
+= git_config_from_file(fn
, path
, data
);
2150 current_parsing_scope
= CONFIG_SCOPE_COMMAND
;
2151 if (!opts
->ignore_cmdline
&& git_config_from_parameters(fn
, data
) < 0)
2152 die(_("unable to parse command-line config"));
2154 current_parsing_scope
= prev_parsing_scope
;
2155 free(system_config
);
2162 int config_with_options(config_fn_t fn
, void *data
,
2163 struct git_config_source
*config_source
,
2164 const struct config_options
*opts
)
2166 struct config_include_data inc
= CONFIG_INCLUDE_INIT
;
2169 if (opts
->respect_includes
) {
2173 inc
.config_source
= config_source
;
2174 fn
= git_config_include
;
2179 current_parsing_scope
= config_source
->scope
;
2182 * If we have a specific filename, use it. Otherwise, follow the
2183 * regular lookup sequence.
2185 if (config_source
&& config_source
->use_stdin
) {
2186 ret
= git_config_from_stdin(fn
, data
);
2187 } else if (config_source
&& config_source
->file
) {
2188 ret
= git_config_from_file(fn
, config_source
->file
, data
);
2189 } else if (config_source
&& config_source
->blob
) {
2190 struct repository
*repo
= config_source
->repo
?
2191 config_source
->repo
: the_repository
;
2192 ret
= git_config_from_blob_ref(fn
, repo
, config_source
->blob
,
2195 ret
= do_git_config_sequence(opts
, fn
, data
);
2198 if (inc
.remote_urls
) {
2199 string_list_clear(inc
.remote_urls
, 0);
2200 FREE_AND_NULL(inc
.remote_urls
);
2205 static void configset_iter(struct config_set
*cs
, config_fn_t fn
, void *data
)
2208 struct string_list
*values
;
2209 struct config_set_element
*entry
;
2210 struct configset_list
*list
= &cs
->list
;
2212 for (i
= 0; i
< list
->nr
; i
++) {
2213 entry
= list
->items
[i
].e
;
2214 value_index
= list
->items
[i
].value_index
;
2215 values
= &entry
->value_list
;
2217 current_config_kvi
= values
->items
[value_index
].util
;
2219 if (fn(entry
->key
, values
->items
[value_index
].string
, data
) < 0)
2220 git_die_config_linenr(entry
->key
,
2221 current_config_kvi
->filename
,
2222 current_config_kvi
->linenr
);
2224 current_config_kvi
= NULL
;
2228 void read_early_config(config_fn_t cb
, void *data
)
2230 struct config_options opts
= {0};
2231 struct strbuf commondir
= STRBUF_INIT
;
2232 struct strbuf gitdir
= STRBUF_INIT
;
2234 opts
.respect_includes
= 1;
2236 if (have_git_dir()) {
2237 opts
.commondir
= get_git_common_dir();
2238 opts
.git_dir
= get_git_dir();
2240 * When setup_git_directory() was not yet asked to discover the
2241 * GIT_DIR, we ask discover_git_directory() to figure out whether there
2242 * is any repository config we should use (but unlike
2243 * setup_git_directory_gently(), no global state is changed, most
2244 * notably, the current working directory is still the same after the
2247 } else if (!discover_git_directory(&commondir
, &gitdir
)) {
2248 opts
.commondir
= commondir
.buf
;
2249 opts
.git_dir
= gitdir
.buf
;
2252 config_with_options(cb
, data
, NULL
, &opts
);
2254 strbuf_release(&commondir
);
2255 strbuf_release(&gitdir
);
2259 * Read config but only enumerate system and global settings.
2260 * Omit any repo-local, worktree-local, or command-line settings.
2262 void read_very_early_config(config_fn_t cb
, void *data
)
2264 struct config_options opts
= { 0 };
2266 opts
.respect_includes
= 1;
2267 opts
.ignore_repo
= 1;
2268 opts
.ignore_worktree
= 1;
2269 opts
.ignore_cmdline
= 1;
2270 opts
.system_gently
= 1;
2272 config_with_options(cb
, data
, NULL
, &opts
);
2275 static struct config_set_element
*configset_find_element(struct config_set
*cs
, const char *key
)
2277 struct config_set_element k
;
2278 struct config_set_element
*found_entry
;
2279 char *normalized_key
;
2281 * `key` may come from the user, so normalize it before using it
2282 * for querying entries from the hashmap.
2284 if (git_config_parse_key(key
, &normalized_key
, NULL
))
2287 hashmap_entry_init(&k
.ent
, strhash(normalized_key
));
2288 k
.key
= normalized_key
;
2289 found_entry
= hashmap_get_entry(&cs
->config_hash
, &k
, ent
, NULL
);
2290 free(normalized_key
);
2294 static int configset_add_value(struct config_set
*cs
, const char *key
, const char *value
)
2296 struct config_set_element
*e
;
2297 struct string_list_item
*si
;
2298 struct configset_list_item
*l_item
;
2299 struct key_value_info
*kv_info
= xmalloc(sizeof(*kv_info
));
2301 e
= configset_find_element(cs
, key
);
2303 * Since the keys are being fed by git_config*() callback mechanism, they
2304 * are already normalized. So simply add them without any further munging.
2307 e
= xmalloc(sizeof(*e
));
2308 hashmap_entry_init(&e
->ent
, strhash(key
));
2309 e
->key
= xstrdup(key
);
2310 string_list_init_dup(&e
->value_list
);
2311 hashmap_add(&cs
->config_hash
, &e
->ent
);
2313 si
= string_list_append_nodup(&e
->value_list
, xstrdup_or_null(value
));
2315 ALLOC_GROW(cs
->list
.items
, cs
->list
.nr
+ 1, cs
->list
.alloc
);
2316 l_item
= &cs
->list
.items
[cs
->list
.nr
++];
2318 l_item
->value_index
= e
->value_list
.nr
- 1;
2321 BUG("configset_add_value has no source");
2323 kv_info
->filename
= strintern(cf
->name
);
2324 kv_info
->linenr
= cf
->linenr
;
2325 kv_info
->origin_type
= cf
->origin_type
;
2327 /* for values read from `git_config_from_parameters()` */
2328 kv_info
->filename
= NULL
;
2329 kv_info
->linenr
= -1;
2330 kv_info
->origin_type
= CONFIG_ORIGIN_CMDLINE
;
2332 kv_info
->scope
= current_parsing_scope
;
2338 static int config_set_element_cmp(const void *unused_cmp_data
,
2339 const struct hashmap_entry
*eptr
,
2340 const struct hashmap_entry
*entry_or_key
,
2341 const void *unused_keydata
)
2343 const struct config_set_element
*e1
, *e2
;
2345 e1
= container_of(eptr
, const struct config_set_element
, ent
);
2346 e2
= container_of(entry_or_key
, const struct config_set_element
, ent
);
2348 return strcmp(e1
->key
, e2
->key
);
2351 void git_configset_init(struct config_set
*cs
)
2353 hashmap_init(&cs
->config_hash
, config_set_element_cmp
, NULL
, 0);
2354 cs
->hash_initialized
= 1;
2357 cs
->list
.items
= NULL
;
2360 void git_configset_clear(struct config_set
*cs
)
2362 struct config_set_element
*entry
;
2363 struct hashmap_iter iter
;
2364 if (!cs
->hash_initialized
)
2367 hashmap_for_each_entry(&cs
->config_hash
, &iter
, entry
,
2368 ent
/* member name */) {
2370 string_list_clear(&entry
->value_list
, 1);
2372 hashmap_clear_and_free(&cs
->config_hash
, struct config_set_element
, ent
);
2373 cs
->hash_initialized
= 0;
2374 free(cs
->list
.items
);
2377 cs
->list
.items
= NULL
;
2380 static int config_set_callback(const char *key
, const char *value
, void *cb
)
2382 struct config_set
*cs
= cb
;
2383 configset_add_value(cs
, key
, value
);
2387 int git_configset_add_file(struct config_set
*cs
, const char *filename
)
2389 return git_config_from_file(config_set_callback
, filename
, cs
);
2392 int git_configset_add_parameters(struct config_set
*cs
)
2394 return git_config_from_parameters(config_set_callback
, cs
);
2397 int git_configset_get_value(struct config_set
*cs
, const char *key
, const char **value
)
2399 const struct string_list
*values
= NULL
;
2401 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
2402 * queried key in the files of the configset, the value returned will be the last
2403 * value in the value list for that key.
2405 values
= git_configset_get_value_multi(cs
, key
);
2409 assert(values
->nr
> 0);
2410 *value
= values
->items
[values
->nr
- 1].string
;
2414 const struct string_list
*git_configset_get_value_multi(struct config_set
*cs
, const char *key
)
2416 struct config_set_element
*e
= configset_find_element(cs
, key
);
2417 return e
? &e
->value_list
: NULL
;
2420 int git_configset_get_string(struct config_set
*cs
, const char *key
, char **dest
)
2423 if (!git_configset_get_value(cs
, key
, &value
))
2424 return git_config_string((const char **)dest
, key
, value
);
2429 static int git_configset_get_string_tmp(struct config_set
*cs
, const char *key
,
2433 if (!git_configset_get_value(cs
, key
, &value
)) {
2435 return config_error_nonbool(key
);
2443 int git_configset_get_int(struct config_set
*cs
, const char *key
, int *dest
)
2446 if (!git_configset_get_value(cs
, key
, &value
)) {
2447 *dest
= git_config_int(key
, value
);
2453 int git_configset_get_ulong(struct config_set
*cs
, const char *key
, unsigned long *dest
)
2456 if (!git_configset_get_value(cs
, key
, &value
)) {
2457 *dest
= git_config_ulong(key
, value
);
2463 int git_configset_get_bool(struct config_set
*cs
, const char *key
, int *dest
)
2466 if (!git_configset_get_value(cs
, key
, &value
)) {
2467 *dest
= git_config_bool(key
, value
);
2473 int git_configset_get_bool_or_int(struct config_set
*cs
, const char *key
,
2474 int *is_bool
, int *dest
)
2477 if (!git_configset_get_value(cs
, key
, &value
)) {
2478 *dest
= git_config_bool_or_int(key
, value
, is_bool
);
2484 int git_configset_get_maybe_bool(struct config_set
*cs
, const char *key
, int *dest
)
2487 if (!git_configset_get_value(cs
, key
, &value
)) {
2488 *dest
= git_parse_maybe_bool(value
);
2496 int git_configset_get_pathname(struct config_set
*cs
, const char *key
, const char **dest
)
2499 if (!git_configset_get_value(cs
, key
, &value
))
2500 return git_config_pathname(dest
, key
, value
);
2505 /* Functions use to read configuration from a repository */
2506 static void repo_read_config(struct repository
*repo
)
2508 struct config_options opts
= { 0 };
2510 opts
.respect_includes
= 1;
2511 opts
.commondir
= repo
->commondir
;
2512 opts
.git_dir
= repo
->gitdir
;
2515 CALLOC_ARRAY(repo
->config
, 1);
2517 git_configset_clear(repo
->config
);
2519 git_configset_init(repo
->config
);
2521 if (config_with_options(config_set_callback
, repo
->config
, NULL
, &opts
) < 0)
2523 * config_with_options() normally returns only
2524 * zero, as most errors are fatal, and
2525 * non-fatal potential errors are guarded by "if"
2526 * statements that are entered only when no error is
2529 * If we ever encounter a non-fatal error, it means
2530 * something went really wrong and we should stop
2533 die(_("unknown error occurred while reading the configuration files"));
2536 static void git_config_check_init(struct repository
*repo
)
2538 if (repo
->config
&& repo
->config
->hash_initialized
)
2540 repo_read_config(repo
);
2543 static void repo_config_clear(struct repository
*repo
)
2545 if (!repo
->config
|| !repo
->config
->hash_initialized
)
2547 git_configset_clear(repo
->config
);
2550 void repo_config(struct repository
*repo
, config_fn_t fn
, void *data
)
2552 git_config_check_init(repo
);
2553 configset_iter(repo
->config
, fn
, data
);
2556 int repo_config_get_value(struct repository
*repo
,
2557 const char *key
, const char **value
)
2559 git_config_check_init(repo
);
2560 return git_configset_get_value(repo
->config
, key
, value
);
2563 const struct string_list
*repo_config_get_value_multi(struct repository
*repo
,
2566 git_config_check_init(repo
);
2567 return git_configset_get_value_multi(repo
->config
, key
);
2570 int repo_config_get_string(struct repository
*repo
,
2571 const char *key
, char **dest
)
2574 git_config_check_init(repo
);
2575 ret
= git_configset_get_string(repo
->config
, key
, dest
);
2577 git_die_config(key
, NULL
);
2581 int repo_config_get_string_tmp(struct repository
*repo
,
2582 const char *key
, const char **dest
)
2585 git_config_check_init(repo
);
2586 ret
= git_configset_get_string_tmp(repo
->config
, key
, dest
);
2588 git_die_config(key
, NULL
);
2592 int repo_config_get_int(struct repository
*repo
,
2593 const char *key
, int *dest
)
2595 git_config_check_init(repo
);
2596 return git_configset_get_int(repo
->config
, key
, dest
);
2599 int repo_config_get_ulong(struct repository
*repo
,
2600 const char *key
, unsigned long *dest
)
2602 git_config_check_init(repo
);
2603 return git_configset_get_ulong(repo
->config
, key
, dest
);
2606 int repo_config_get_bool(struct repository
*repo
,
2607 const char *key
, int *dest
)
2609 git_config_check_init(repo
);
2610 return git_configset_get_bool(repo
->config
, key
, dest
);
2613 int repo_config_get_bool_or_int(struct repository
*repo
,
2614 const char *key
, int *is_bool
, int *dest
)
2616 git_config_check_init(repo
);
2617 return git_configset_get_bool_or_int(repo
->config
, key
, is_bool
, dest
);
2620 int repo_config_get_maybe_bool(struct repository
*repo
,
2621 const char *key
, int *dest
)
2623 git_config_check_init(repo
);
2624 return git_configset_get_maybe_bool(repo
->config
, key
, dest
);
2627 int repo_config_get_pathname(struct repository
*repo
,
2628 const char *key
, const char **dest
)
2631 git_config_check_init(repo
);
2632 ret
= git_configset_get_pathname(repo
->config
, key
, dest
);
2634 git_die_config(key
, NULL
);
2638 /* Read values into protected_config. */
2639 static void read_protected_config(void)
2641 char *xdg_config
= NULL
, *user_config
= NULL
, *system_config
= NULL
;
2643 git_configset_init(&protected_config
);
2645 system_config
= git_system_config();
2646 git_global_config(&user_config
, &xdg_config
);
2648 git_configset_add_file(&protected_config
, system_config
);
2649 git_configset_add_file(&protected_config
, xdg_config
);
2650 git_configset_add_file(&protected_config
, user_config
);
2651 git_configset_add_parameters(&protected_config
);
2653 free(system_config
);
2658 void git_protected_config(config_fn_t fn
, void *data
)
2660 if (!protected_config
.hash_initialized
)
2661 read_protected_config();
2662 configset_iter(&protected_config
, fn
, data
);
2665 /* Functions used historically to read configuration from 'the_repository' */
2666 void git_config(config_fn_t fn
, void *data
)
2668 repo_config(the_repository
, fn
, data
);
2671 void git_config_clear(void)
2673 repo_config_clear(the_repository
);
2676 int git_config_get_value(const char *key
, const char **value
)
2678 return repo_config_get_value(the_repository
, key
, value
);
2681 const struct string_list
*git_config_get_value_multi(const char *key
)
2683 return repo_config_get_value_multi(the_repository
, key
);
2686 int git_config_get_string(const char *key
, char **dest
)
2688 return repo_config_get_string(the_repository
, key
, dest
);
2691 int git_config_get_string_tmp(const char *key
, const char **dest
)
2693 return repo_config_get_string_tmp(the_repository
, key
, dest
);
2696 int git_config_get_int(const char *key
, int *dest
)
2698 return repo_config_get_int(the_repository
, key
, dest
);
2701 int git_config_get_ulong(const char *key
, unsigned long *dest
)
2703 return repo_config_get_ulong(the_repository
, key
, dest
);
2706 int git_config_get_bool(const char *key
, int *dest
)
2708 return repo_config_get_bool(the_repository
, key
, dest
);
2711 int git_config_get_bool_or_int(const char *key
, int *is_bool
, int *dest
)
2713 return repo_config_get_bool_or_int(the_repository
, key
, is_bool
, dest
);
2716 int git_config_get_maybe_bool(const char *key
, int *dest
)
2718 return repo_config_get_maybe_bool(the_repository
, key
, dest
);
2721 int git_config_get_pathname(const char *key
, const char **dest
)
2723 return repo_config_get_pathname(the_repository
, key
, dest
);
2726 int git_config_get_expiry(const char *key
, const char **output
)
2728 int ret
= git_config_get_string(key
, (char **)output
);
2731 if (strcmp(*output
, "now")) {
2732 timestamp_t now
= approxidate("now");
2733 if (approxidate(*output
) >= now
)
2734 git_die_config(key
, _("Invalid %s: '%s'"), key
, *output
);
2739 int git_config_get_expiry_in_days(const char *key
, timestamp_t
*expiry
, timestamp_t now
)
2741 const char *expiry_string
;
2745 if (git_config_get_string_tmp(key
, &expiry_string
))
2746 return 1; /* no such thing */
2748 if (git_parse_signed(expiry_string
, &days
, maximum_signed_value_of_type(int))) {
2749 const int scale
= 86400;
2750 *expiry
= now
- days
* scale
;
2754 if (!parse_expiry_date(expiry_string
, &when
)) {
2758 return -1; /* thing exists but cannot be parsed */
2761 int git_config_get_split_index(void)
2765 if (!git_config_get_maybe_bool("core.splitindex", &val
))
2768 return -1; /* default value */
2771 int git_config_get_max_percent_split_change(void)
2775 if (!git_config_get_int("splitindex.maxpercentchange", &val
)) {
2776 if (0 <= val
&& val
<= 100)
2779 return error(_("splitIndex.maxPercentChange value '%d' "
2780 "should be between 0 and 100"), val
);
2783 return -1; /* default value */
2786 int git_config_get_index_threads(int *dest
)
2790 val
= git_env_ulong("GIT_TEST_INDEX_THREADS", 0);
2796 if (!git_config_get_bool_or_int("index.threads", &is_bool
, &val
)) {
2798 *dest
= val
? 0 : 1;
2808 void git_die_config_linenr(const char *key
, const char *filename
, int linenr
)
2811 die(_("unable to parse '%s' from command-line config"), key
);
2813 die(_("bad config variable '%s' in file '%s' at line %d"),
2814 key
, filename
, linenr
);
2817 NORETURN
__attribute__((format(printf
, 2, 3)))
2818 void git_die_config(const char *key
, const char *err
, ...)
2820 const struct string_list
*values
;
2821 struct key_value_info
*kv_info
;
2822 report_fn error_fn
= get_error_routine();
2826 va_start(params
, err
);
2827 error_fn(err
, params
);
2830 values
= git_config_get_value_multi(key
);
2831 kv_info
= values
->items
[values
->nr
- 1].util
;
2832 git_die_config_linenr(key
, kv_info
->filename
, kv_info
->linenr
);
2836 * Find all the stuff for git_config_set() below.
2839 struct config_store_data
{
2843 const char *fixed_value
;
2844 regex_t
*value_pattern
;
2848 enum config_event_t type
;
2849 int is_keys_section
;
2851 unsigned int parsed_nr
, parsed_alloc
, *seen
, seen_nr
, seen_alloc
;
2852 unsigned int key_seen
:1, section_seen
:1, is_keys_section
:1;
2855 static void config_store_data_clear(struct config_store_data
*store
)
2858 if (store
->value_pattern
!= NULL
&&
2859 store
->value_pattern
!= CONFIG_REGEX_NONE
) {
2860 regfree(store
->value_pattern
);
2861 free(store
->value_pattern
);
2863 free(store
->parsed
);
2865 memset(store
, 0, sizeof(*store
));
2868 static int matches(const char *key
, const char *value
,
2869 const struct config_store_data
*store
)
2871 if (strcmp(key
, store
->key
))
2872 return 0; /* not ours */
2873 if (store
->fixed_value
)
2874 return !strcmp(store
->fixed_value
, value
);
2875 if (!store
->value_pattern
)
2876 return 1; /* always matches */
2877 if (store
->value_pattern
== CONFIG_REGEX_NONE
)
2878 return 0; /* never matches */
2880 return store
->do_not_match
^
2881 (value
&& !regexec(store
->value_pattern
, value
, 0, NULL
, 0));
2884 static int store_aux_event(enum config_event_t type
,
2885 size_t begin
, size_t end
, void *data
)
2887 struct config_store_data
*store
= data
;
2889 ALLOC_GROW(store
->parsed
, store
->parsed_nr
+ 1, store
->parsed_alloc
);
2890 store
->parsed
[store
->parsed_nr
].begin
= begin
;
2891 store
->parsed
[store
->parsed_nr
].end
= end
;
2892 store
->parsed
[store
->parsed_nr
].type
= type
;
2894 if (type
== CONFIG_EVENT_SECTION
) {
2895 int (*cmpfn
)(const char *, const char *, size_t);
2897 if (cf
->var
.len
< 2 || cf
->var
.buf
[cf
->var
.len
- 1] != '.')
2898 return error(_("invalid section name '%s'"), cf
->var
.buf
);
2900 if (cf
->subsection_case_sensitive
)
2901 cmpfn
= strncasecmp
;
2905 /* Is this the section we were looking for? */
2906 store
->is_keys_section
=
2907 store
->parsed
[store
->parsed_nr
].is_keys_section
=
2908 cf
->var
.len
- 1 == store
->baselen
&&
2909 !cmpfn(cf
->var
.buf
, store
->key
, store
->baselen
);
2910 if (store
->is_keys_section
) {
2911 store
->section_seen
= 1;
2912 ALLOC_GROW(store
->seen
, store
->seen_nr
+ 1,
2914 store
->seen
[store
->seen_nr
] = store
->parsed_nr
;
2923 static int store_aux(const char *key
, const char *value
, void *cb
)
2925 struct config_store_data
*store
= cb
;
2927 if (store
->key_seen
) {
2928 if (matches(key
, value
, store
)) {
2929 if (store
->seen_nr
== 1 && store
->multi_replace
== 0) {
2930 warning(_("%s has multiple values"), key
);
2933 ALLOC_GROW(store
->seen
, store
->seen_nr
+ 1,
2936 store
->seen
[store
->seen_nr
] = store
->parsed_nr
;
2939 } else if (store
->is_keys_section
) {
2941 * Do not increment matches yet: this may not be a match, but we
2942 * are in the desired section.
2944 ALLOC_GROW(store
->seen
, store
->seen_nr
+ 1, store
->seen_alloc
);
2945 store
->seen
[store
->seen_nr
] = store
->parsed_nr
;
2946 store
->section_seen
= 1;
2948 if (matches(key
, value
, store
)) {
2950 store
->key_seen
= 1;
2957 static int write_error(const char *filename
)
2959 error(_("failed to write new configuration file %s"), filename
);
2961 /* Same error code as "failed to rename". */
2965 static struct strbuf
store_create_section(const char *key
,
2966 const struct config_store_data
*store
)
2970 struct strbuf sb
= STRBUF_INIT
;
2972 dot
= memchr(key
, '.', store
->baselen
);
2974 strbuf_addf(&sb
, "[%.*s \"", (int)(dot
- key
), key
);
2975 for (i
= dot
- key
+ 1; i
< store
->baselen
; i
++) {
2976 if (key
[i
] == '"' || key
[i
] == '\\')
2977 strbuf_addch(&sb
, '\\');
2978 strbuf_addch(&sb
, key
[i
]);
2980 strbuf_addstr(&sb
, "\"]\n");
2982 strbuf_addch(&sb
, '[');
2983 strbuf_add(&sb
, key
, store
->baselen
);
2984 strbuf_addstr(&sb
, "]\n");
2990 static ssize_t
write_section(int fd
, const char *key
,
2991 const struct config_store_data
*store
)
2993 struct strbuf sb
= store_create_section(key
, store
);
2996 ret
= write_in_full(fd
, sb
.buf
, sb
.len
);
2997 strbuf_release(&sb
);
3002 static ssize_t
write_pair(int fd
, const char *key
, const char *value
,
3003 const struct config_store_data
*store
)
3007 const char *quote
= "";
3008 struct strbuf sb
= STRBUF_INIT
;
3011 * Check to see if the value needs to be surrounded with a dq pair.
3012 * Note that problematic characters are always backslash-quoted; this
3013 * check is about not losing leading or trailing SP and strings that
3014 * follow beginning-of-comment characters (i.e. ';' and '#') by the
3015 * configuration parser.
3017 if (value
[0] == ' ')
3019 for (i
= 0; value
[i
]; i
++)
3020 if (value
[i
] == ';' || value
[i
] == '#')
3022 if (i
&& value
[i
- 1] == ' ')
3025 strbuf_addf(&sb
, "\t%s = %s", key
+ store
->baselen
+ 1, quote
);
3027 for (i
= 0; value
[i
]; i
++)
3030 strbuf_addstr(&sb
, "\\n");
3033 strbuf_addstr(&sb
, "\\t");
3037 strbuf_addch(&sb
, '\\');
3040 strbuf_addch(&sb
, value
[i
]);
3043 strbuf_addf(&sb
, "%s\n", quote
);
3045 ret
= write_in_full(fd
, sb
.buf
, sb
.len
);
3046 strbuf_release(&sb
);
3052 * If we are about to unset the last key(s) in a section, and if there are
3053 * no comments surrounding (or included in) the section, we will want to
3054 * extend begin/end to remove the entire section.
3056 * Note: the parameter `seen_ptr` points to the index into the store.seen
3057 * array. * This index may be incremented if a section has more than one
3058 * entry (which all are to be removed).
3060 static void maybe_remove_section(struct config_store_data
*store
,
3061 size_t *begin_offset
, size_t *end_offset
,
3065 int i
, seen
, section_seen
= 0;
3068 * First, ensure that this is the first key, and that there are no
3069 * comments before the entry nor before the section header.
3072 for (i
= store
->seen
[seen
]; i
> 0; i
--) {
3073 enum config_event_t type
= store
->parsed
[i
- 1].type
;
3075 if (type
== CONFIG_EVENT_COMMENT
)
3076 /* There is a comment before this entry or section */
3078 if (type
== CONFIG_EVENT_ENTRY
) {
3080 /* This is not the section's first entry. */
3082 /* We encountered no comment before the section. */
3085 if (type
== CONFIG_EVENT_SECTION
) {
3086 if (!store
->parsed
[i
- 1].is_keys_section
)
3091 begin
= store
->parsed
[i
].begin
;
3094 * Next, make sure that we are removing the last key(s) in the section,
3095 * and that there are no comments that are possibly about the current
3098 for (i
= store
->seen
[seen
] + 1; i
< store
->parsed_nr
; i
++) {
3099 enum config_event_t type
= store
->parsed
[i
].type
;
3101 if (type
== CONFIG_EVENT_COMMENT
)
3103 if (type
== CONFIG_EVENT_SECTION
) {
3104 if (store
->parsed
[i
].is_keys_section
)
3108 if (type
== CONFIG_EVENT_ENTRY
) {
3109 if (++seen
< store
->seen_nr
&&
3110 i
== store
->seen
[seen
])
3111 /* We want to remove this entry, too */
3113 /* There is another entry in this section. */
3119 * We are really removing the last entry/entries from this section, and
3120 * there are no enclosed or surrounding comments. Remove the entire,
3121 * now-empty section.
3124 *begin_offset
= begin
;
3125 if (i
< store
->parsed_nr
)
3126 *end_offset
= store
->parsed
[i
].begin
;
3128 *end_offset
= store
->parsed
[store
->parsed_nr
- 1].end
;
3131 int git_config_set_in_file_gently(const char *config_filename
,
3132 const char *key
, const char *value
)
3134 return git_config_set_multivar_in_file_gently(config_filename
, key
, value
, NULL
, 0);
3137 void git_config_set_in_file(const char *config_filename
,
3138 const char *key
, const char *value
)
3140 git_config_set_multivar_in_file(config_filename
, key
, value
, NULL
, 0);
3143 int git_config_set_gently(const char *key
, const char *value
)
3145 return git_config_set_multivar_gently(key
, value
, NULL
, 0);
3148 int repo_config_set_worktree_gently(struct repository
*r
,
3149 const char *key
, const char *value
)
3151 /* Only use worktree-specific config if it is is already enabled. */
3152 if (repository_format_worktree_config
) {
3153 char *file
= repo_git_path(r
, "config.worktree");
3154 int ret
= git_config_set_multivar_in_file_gently(
3155 file
, key
, value
, NULL
, 0);
3159 return repo_config_set_multivar_gently(r
, key
, value
, NULL
, 0);
3162 void git_config_set(const char *key
, const char *value
)
3164 git_config_set_multivar(key
, value
, NULL
, 0);
3166 trace2_cmd_set_config(key
, value
);
3170 * If value==NULL, unset in (remove from) config,
3171 * if value_pattern!=NULL, disregard key/value pairs where value does not match.
3172 * if value_pattern==CONFIG_REGEX_NONE, do not match any existing values
3173 * (only add a new one)
3174 * if flags contains the CONFIG_FLAGS_MULTI_REPLACE flag, all matching
3175 * key/values are removed before a single new pair is written. If the
3176 * flag is not present, then replace only the first match.
3178 * Returns 0 on success.
3180 * This function does this:
3182 * - it locks the config file by creating ".git/config.lock"
3184 * - it then parses the config using store_aux() as validator to find
3185 * the position on the key/value pair to replace. If it is to be unset,
3186 * it must be found exactly once.
3188 * - the config file is mmap()ed and the part before the match (if any) is
3189 * written to the lock file, then the changed part and the rest.
3191 * - the config file is removed and the lock file rename()d to it.
3194 int git_config_set_multivar_in_file_gently(const char *config_filename
,
3195 const char *key
, const char *value
,
3196 const char *value_pattern
,
3199 int fd
= -1, in_fd
= -1;
3201 struct lock_file lock
= LOCK_INIT
;
3202 char *filename_buf
= NULL
;
3203 char *contents
= NULL
;
3205 struct config_store_data store
;
3207 memset(&store
, 0, sizeof(store
));
3209 /* parse-key returns negative; flip the sign to feed exit(3) */
3210 ret
= 0 - git_config_parse_key(key
, &store
.key
, &store
.baselen
);
3214 store
.multi_replace
= (flags
& CONFIG_FLAGS_MULTI_REPLACE
) != 0;
3216 if (!config_filename
)
3217 config_filename
= filename_buf
= git_pathdup("config");
3220 * The lock serves a purpose in addition to locking: the new
3221 * contents of .git/config will be written into it.
3223 fd
= hold_lock_file_for_update(&lock
, config_filename
, 0);
3225 error_errno(_("could not lock config file %s"), config_filename
);
3226 ret
= CONFIG_NO_LOCK
;
3231 * If .git/config does not exist yet, write a minimal version.
3233 in_fd
= open(config_filename
, O_RDONLY
);
3235 if ( ENOENT
!= errno
) {
3236 error_errno(_("opening %s"), config_filename
);
3237 ret
= CONFIG_INVALID_FILE
; /* same as "invalid config file" */
3240 /* if nothing to unset, error out */
3242 ret
= CONFIG_NOTHING_SET
;
3247 store
.key
= xstrdup(key
);
3248 if (write_section(fd
, key
, &store
) < 0 ||
3249 write_pair(fd
, key
, value
, &store
) < 0)
3253 size_t copy_begin
, copy_end
;
3254 int i
, new_line
= 0;
3255 struct config_options opts
;
3258 store
.value_pattern
= NULL
;
3259 else if (value_pattern
== CONFIG_REGEX_NONE
)
3260 store
.value_pattern
= CONFIG_REGEX_NONE
;
3261 else if (flags
& CONFIG_FLAGS_FIXED_VALUE
)
3262 store
.fixed_value
= value_pattern
;
3264 if (value_pattern
[0] == '!') {
3265 store
.do_not_match
= 1;
3268 store
.do_not_match
= 0;
3270 store
.value_pattern
= (regex_t
*)xmalloc(sizeof(regex_t
));
3271 if (regcomp(store
.value_pattern
, value_pattern
,
3273 error(_("invalid pattern: %s"), value_pattern
);
3274 FREE_AND_NULL(store
.value_pattern
);
3275 ret
= CONFIG_INVALID_PATTERN
;
3280 ALLOC_GROW(store
.parsed
, 1, store
.parsed_alloc
);
3281 store
.parsed
[0].end
= 0;
3283 memset(&opts
, 0, sizeof(opts
));
3284 opts
.event_fn
= store_aux_event
;
3285 opts
.event_fn_data
= &store
;
3288 * After this, store.parsed will contain offsets of all the
3289 * parsed elements, and store.seen will contain a list of
3290 * matches, as indices into store.parsed.
3292 * As a side effect, we make sure to transform only a valid
3293 * existing config file.
3295 if (git_config_from_file_with_options(store_aux
,
3298 error(_("invalid config file %s"), config_filename
);
3299 ret
= CONFIG_INVALID_FILE
;
3303 /* if nothing to unset, or too many matches, error out */
3304 if ((store
.seen_nr
== 0 && value
== NULL
) ||
3305 (store
.seen_nr
> 1 && !store
.multi_replace
)) {
3306 ret
= CONFIG_NOTHING_SET
;
3310 if (fstat(in_fd
, &st
) == -1) {
3311 error_errno(_("fstat on %s failed"), config_filename
);
3312 ret
= CONFIG_INVALID_FILE
;
3316 contents_sz
= xsize_t(st
.st_size
);
3317 contents
= xmmap_gently(NULL
, contents_sz
, PROT_READ
,
3318 MAP_PRIVATE
, in_fd
, 0);
3319 if (contents
== MAP_FAILED
) {
3320 if (errno
== ENODEV
&& S_ISDIR(st
.st_mode
))
3322 error_errno(_("unable to mmap '%s'%s"),
3323 config_filename
, mmap_os_err());
3324 ret
= CONFIG_INVALID_FILE
;
3331 if (chmod(get_lock_file_path(&lock
), st
.st_mode
& 07777) < 0) {
3332 error_errno(_("chmod on %s failed"), get_lock_file_path(&lock
));
3333 ret
= CONFIG_NO_WRITE
;
3337 if (store
.seen_nr
== 0) {
3338 if (!store
.seen_alloc
) {
3339 /* Did not see key nor section */
3340 ALLOC_GROW(store
.seen
, 1, store
.seen_alloc
);
3341 store
.seen
[0] = store
.parsed_nr
3342 - !!store
.parsed_nr
;
3347 for (i
= 0, copy_begin
= 0; i
< store
.seen_nr
; i
++) {
3349 int j
= store
.seen
[i
];
3352 if (!store
.key_seen
) {
3353 copy_end
= store
.parsed
[j
].end
;
3354 /* include '\n' when copying section header */
3355 if (copy_end
> 0 && copy_end
< contents_sz
&&
3356 contents
[copy_end
- 1] != '\n' &&
3357 contents
[copy_end
] == '\n')
3359 replace_end
= copy_end
;
3361 replace_end
= store
.parsed
[j
].end
;
3362 copy_end
= store
.parsed
[j
].begin
;
3364 maybe_remove_section(&store
,
3368 * Swallow preceding white-space on the same
3371 while (copy_end
> 0 ) {
3372 char c
= contents
[copy_end
- 1];
3374 if (isspace(c
) && c
!= '\n')
3381 if (copy_end
> 0 && contents
[copy_end
-1] != '\n')
3384 /* write the first part of the config */
3385 if (copy_end
> copy_begin
) {
3386 if (write_in_full(fd
, contents
+ copy_begin
,
3387 copy_end
- copy_begin
) < 0)
3390 write_str_in_full(fd
, "\n") < 0)
3393 copy_begin
= replace_end
;
3396 /* write the pair (value == NULL means unset) */
3398 if (!store
.section_seen
) {
3399 if (write_section(fd
, key
, &store
) < 0)
3402 if (write_pair(fd
, key
, value
, &store
) < 0)
3406 /* write the rest of the config */
3407 if (copy_begin
< contents_sz
)
3408 if (write_in_full(fd
, contents
+ copy_begin
,
3409 contents_sz
- copy_begin
) < 0)
3412 munmap(contents
, contents_sz
);
3416 if (commit_lock_file(&lock
) < 0) {
3417 error_errno(_("could not write config file %s"), config_filename
);
3418 ret
= CONFIG_NO_WRITE
;
3424 /* Invalidate the config cache */
3428 rollback_lock_file(&lock
);
3431 munmap(contents
, contents_sz
);
3434 config_store_data_clear(&store
);
3438 ret
= write_error(get_lock_file_path(&lock
));
3443 void git_config_set_multivar_in_file(const char *config_filename
,
3444 const char *key
, const char *value
,
3445 const char *value_pattern
, unsigned flags
)
3447 if (!git_config_set_multivar_in_file_gently(config_filename
, key
, value
,
3448 value_pattern
, flags
))
3451 die(_("could not set '%s' to '%s'"), key
, value
);
3453 die(_("could not unset '%s'"), key
);
3456 int git_config_set_multivar_gently(const char *key
, const char *value
,
3457 const char *value_pattern
, unsigned flags
)
3459 return repo_config_set_multivar_gently(the_repository
, key
, value
,
3460 value_pattern
, flags
);
3463 int repo_config_set_multivar_gently(struct repository
*r
, const char *key
,
3465 const char *value_pattern
, unsigned flags
)
3467 char *file
= repo_git_path(r
, "config");
3468 int res
= git_config_set_multivar_in_file_gently(file
,
3476 void git_config_set_multivar(const char *key
, const char *value
,
3477 const char *value_pattern
, unsigned flags
)
3479 git_config_set_multivar_in_file(git_path("config"),
3480 key
, value
, value_pattern
,
3484 static int section_name_match (const char *buf
, const char *name
)
3486 int i
= 0, j
= 0, dot
= 0;
3489 for (i
= 1; buf
[i
] && buf
[i
] != ']'; i
++) {
3490 if (!dot
&& isspace(buf
[i
])) {
3492 if (name
[j
++] != '.')
3494 for (i
++; isspace(buf
[i
]); i
++)
3500 if (buf
[i
] == '\\' && dot
)
3502 else if (buf
[i
] == '"' && dot
) {
3503 for (i
++; isspace(buf
[i
]); i
++)
3507 if (buf
[i
] != name
[j
++])
3510 if (buf
[i
] == ']' && name
[j
] == 0) {
3512 * We match, now just find the right length offset by
3513 * gobbling up any whitespace after it, as well
3516 for (; buf
[i
] && isspace(buf
[i
]); i
++)
3523 static int section_name_is_ok(const char *name
)
3525 /* Empty section names are bogus. */
3530 * Before a dot, we must be alphanumeric or dash. After the first dot,
3531 * anything goes, so we can stop checking.
3533 for (; *name
&& *name
!= '.'; name
++)
3534 if (*name
!= '-' && !isalnum(*name
))
3539 /* if new_name == NULL, the section is removed instead */
3540 static int git_config_copy_or_rename_section_in_file(const char *config_filename
,
3541 const char *old_name
,
3542 const char *new_name
, int copy
)
3544 int ret
= 0, remove
= 0;
3545 char *filename_buf
= NULL
;
3546 struct lock_file lock
= LOCK_INIT
;
3549 FILE *config_file
= NULL
;
3551 struct strbuf copystr
= STRBUF_INIT
;
3552 struct config_store_data store
;
3554 memset(&store
, 0, sizeof(store
));
3556 if (new_name
&& !section_name_is_ok(new_name
)) {
3557 ret
= error(_("invalid section name: %s"), new_name
);
3558 goto out_no_rollback
;
3561 if (!config_filename
)
3562 config_filename
= filename_buf
= git_pathdup("config");
3564 out_fd
= hold_lock_file_for_update(&lock
, config_filename
, 0);
3566 ret
= error(_("could not lock config file %s"), config_filename
);
3570 if (!(config_file
= fopen(config_filename
, "rb"))) {
3571 ret
= warn_on_fopen_errors(config_filename
);
3574 /* no config file means nothing to rename, no error */
3575 goto commit_and_out
;
3578 if (fstat(fileno(config_file
), &st
) == -1) {
3579 ret
= error_errno(_("fstat on %s failed"), config_filename
);
3583 if (chmod(get_lock_file_path(&lock
), st
.st_mode
& 07777) < 0) {
3584 ret
= error_errno(_("chmod on %s failed"),
3585 get_lock_file_path(&lock
));
3589 while (fgets(buf
, sizeof(buf
), config_file
)) {
3594 for (i
= 0; buf
[i
] && isspace(buf
[i
]); i
++)
3596 if (buf
[i
] == '[') {
3597 /* it's a section */
3602 * When encountering a new section under -c we
3603 * need to flush out any section we're already
3604 * coping and begin anew. There might be
3605 * multiple [branch "$name"] sections.
3607 if (copystr
.len
> 0) {
3608 if (write_in_full(out_fd
, copystr
.buf
, copystr
.len
) < 0) {
3609 ret
= write_error(get_lock_file_path(&lock
));
3612 strbuf_reset(©str
);
3615 offset
= section_name_match(&buf
[i
], old_name
);
3622 store
.baselen
= strlen(new_name
);
3624 if (write_section(out_fd
, new_name
, &store
) < 0) {
3625 ret
= write_error(get_lock_file_path(&lock
));
3629 * We wrote out the new section, with
3630 * a newline, now skip the old
3633 output
+= offset
+ i
;
3634 if (strlen(output
) > 0) {
3636 * More content means there's
3637 * a declaration to put on the
3638 * next line; indent with a
3645 copystr
= store_create_section(new_name
, &store
);
3652 length
= strlen(output
);
3654 if (!is_section
&& copystr
.len
> 0) {
3655 strbuf_add(©str
, output
, length
);
3658 if (write_in_full(out_fd
, output
, length
) < 0) {
3659 ret
= write_error(get_lock_file_path(&lock
));
3665 * Copy a trailing section at the end of the config, won't be
3666 * flushed by the usual "flush because we have a new section
3667 * logic in the loop above.
3669 if (copystr
.len
> 0) {
3670 if (write_in_full(out_fd
, copystr
.buf
, copystr
.len
) < 0) {
3671 ret
= write_error(get_lock_file_path(&lock
));
3674 strbuf_reset(©str
);
3677 fclose(config_file
);
3680 if (commit_lock_file(&lock
) < 0)
3681 ret
= error_errno(_("could not write config file %s"),
3685 fclose(config_file
);
3686 rollback_lock_file(&lock
);
3689 config_store_data_clear(&store
);
3693 int git_config_rename_section_in_file(const char *config_filename
,
3694 const char *old_name
, const char *new_name
)
3696 return git_config_copy_or_rename_section_in_file(config_filename
,
3697 old_name
, new_name
, 0);
3700 int git_config_rename_section(const char *old_name
, const char *new_name
)
3702 return git_config_rename_section_in_file(NULL
, old_name
, new_name
);
3705 int git_config_copy_section_in_file(const char *config_filename
,
3706 const char *old_name
, const char *new_name
)
3708 return git_config_copy_or_rename_section_in_file(config_filename
,
3709 old_name
, new_name
, 1);
3712 int git_config_copy_section(const char *old_name
, const char *new_name
)
3714 return git_config_copy_section_in_file(NULL
, old_name
, new_name
);
3718 * Call this to report error for your variable that should not
3719 * get a boolean value (i.e. "[my] var" means "true").
3721 #undef config_error_nonbool
3722 int config_error_nonbool(const char *var
)
3724 return error(_("missing value for '%s'"), var
);
3727 int parse_config_key(const char *var
,
3728 const char *section
,
3729 const char **subsection
, size_t *subsection_len
,
3734 /* Does it start with "section." ? */
3735 if (!skip_prefix(var
, section
, &var
) || *var
!= '.')
3739 * Find the key; we don't know yet if we have a subsection, but we must
3740 * parse backwards from the end, since the subsection may have dots in
3743 dot
= strrchr(var
, '.');
3746 /* Did we have a subsection at all? */
3750 *subsection_len
= 0;
3756 *subsection
= var
+ 1;
3757 *subsection_len
= dot
- *subsection
;
3763 const char *current_config_origin_type(void)
3766 if (current_config_kvi
)
3767 type
= current_config_kvi
->origin_type
;
3769 type
= cf
->origin_type
;
3771 BUG("current_config_origin_type called outside config callback");
3774 case CONFIG_ORIGIN_BLOB
:
3776 case CONFIG_ORIGIN_FILE
:
3778 case CONFIG_ORIGIN_STDIN
:
3779 return "standard input";
3780 case CONFIG_ORIGIN_SUBMODULE_BLOB
:
3781 return "submodule-blob";
3782 case CONFIG_ORIGIN_CMDLINE
:
3783 return "command line";
3785 BUG("unknown config origin type");
3789 const char *config_scope_name(enum config_scope scope
)
3792 case CONFIG_SCOPE_SYSTEM
:
3794 case CONFIG_SCOPE_GLOBAL
:
3796 case CONFIG_SCOPE_LOCAL
:
3798 case CONFIG_SCOPE_WORKTREE
:
3800 case CONFIG_SCOPE_COMMAND
:
3802 case CONFIG_SCOPE_SUBMODULE
:
3809 const char *current_config_name(void)
3812 if (current_config_kvi
)
3813 name
= current_config_kvi
->filename
;
3817 BUG("current_config_name called outside config callback");
3818 return name
? name
: "";
3821 enum config_scope
current_config_scope(void)
3823 if (current_config_kvi
)
3824 return current_config_kvi
->scope
;
3826 return current_parsing_scope
;
3829 int current_config_line(void)
3831 if (current_config_kvi
)
3832 return current_config_kvi
->linenr
;
3837 int lookup_config(const char **mapping
, int nr_mapping
, const char *var
)
3841 for (i
= 0; i
< nr_mapping
; i
++) {
3842 const char *name
= mapping
[i
];
3844 if (name
&& !strcasecmp(var
, name
))