2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
14 #include "environment.h"
17 #include "repository.h"
23 #include "string-list.h"
24 #include "object-store.h"
28 #include "replace-object.h"
33 #include "write-or-die.h"
35 struct config_source
{
36 struct config_source
*prev
;
45 enum config_origin_type origin_type
;
48 enum config_error_action default_error_action
;
54 unsigned subsection_case_sensitive
: 1;
56 int (*do_fgetc
)(struct config_source
*c
);
57 int (*do_ungetc
)(int c
, struct config_source
*conf
);
58 long (*do_ftell
)(struct config_source
*c
);
60 #define CONFIG_SOURCE_INIT { 0 }
62 struct config_reader
{
64 * These members record the "current" config source, which can be
65 * accessed by parsing callbacks.
67 * The "source" variable will be non-NULL only when we are actually
68 * parsing a real config source (file, blob, cmdline, etc).
70 * The "config_kvi" variable will be non-NULL only when we are feeding
71 * cached config from a configset into a callback.
73 * They cannot be non-NULL at the same time. If they are both NULL, then
74 * we aren't parsing anything (and depending on the function looking at
75 * the variables, it's either a bug for it to be called in the first
76 * place, or it's a function which can be reused for non-config
77 * purposes, and should fall back to some sane behavior).
79 struct config_source
*source
;
80 struct key_value_info
*config_kvi
;
82 * The "scope" of the current config source being parsed (repo, global,
83 * etc). Like "source", this is only set when parsing a config source.
84 * It's not part of "source" because it transcends a single file (i.e.,
85 * a file included from .git/config is still in "repo" scope).
87 * When iterating through a configset, the equivalent value is
88 * "config_kvi.scope" (see above).
90 enum config_scope parsing_scope
;
93 * Where possible, prefer to accept "struct config_reader" as an arg than to use
94 * "the_reader". "the_reader" should only be used if that is infeasible, e.g. in
97 static struct config_reader the_reader
;
99 static inline void config_reader_push_source(struct config_reader
*reader
,
100 struct config_source
*top
)
102 if (reader
->config_kvi
)
103 BUG("source should not be set while iterating a config set");
104 top
->prev
= reader
->source
;
105 reader
->source
= top
;
108 static inline struct config_source
*config_reader_pop_source(struct config_reader
*reader
)
110 struct config_source
*ret
;
112 BUG("tried to pop config source, but we weren't reading config");
113 ret
= reader
->source
;
114 reader
->source
= reader
->source
->prev
;
118 static inline void config_reader_set_kvi(struct config_reader
*reader
,
119 struct key_value_info
*kvi
)
121 if (kvi
&& (reader
->source
|| reader
->parsing_scope
))
122 BUG("kvi should not be set while parsing a config source");
123 reader
->config_kvi
= kvi
;
126 static inline void config_reader_set_scope(struct config_reader
*reader
,
127 enum config_scope scope
)
129 if (scope
&& reader
->config_kvi
)
130 BUG("scope should only be set when iterating through a config source");
131 reader
->parsing_scope
= scope
;
134 static int pack_compression_seen
;
135 static int zlib_compression_seen
;
138 * Config that comes from trusted scopes, namely:
139 * - CONFIG_SCOPE_SYSTEM (e.g. /etc/gitconfig)
140 * - CONFIG_SCOPE_GLOBAL (e.g. $HOME/.gitconfig, $XDG_CONFIG_HOME/git)
141 * - CONFIG_SCOPE_COMMAND (e.g. "-c" option, environment variables)
143 * This is declared here for code cleanliness, but unlike the other
144 * static variables, this does not hold config parser state.
146 static struct config_set protected_config
;
148 static int config_file_fgetc(struct config_source
*conf
)
150 return getc_unlocked(conf
->u
.file
);
153 static int config_file_ungetc(int c
, struct config_source
*conf
)
155 return ungetc(c
, conf
->u
.file
);
158 static long config_file_ftell(struct config_source
*conf
)
160 return ftell(conf
->u
.file
);
164 static int config_buf_fgetc(struct config_source
*conf
)
166 if (conf
->u
.buf
.pos
< conf
->u
.buf
.len
)
167 return conf
->u
.buf
.buf
[conf
->u
.buf
.pos
++];
172 static int config_buf_ungetc(int c
, struct config_source
*conf
)
174 if (conf
->u
.buf
.pos
> 0) {
176 if (conf
->u
.buf
.buf
[conf
->u
.buf
.pos
] != c
)
177 BUG("config_buf can only ungetc the same character");
184 static long config_buf_ftell(struct config_source
*conf
)
186 return conf
->u
.buf
.pos
;
189 struct config_include_data
{
193 const struct config_options
*opts
;
194 struct git_config_source
*config_source
;
195 struct config_reader
*config_reader
;
198 * All remote URLs discovered when reading all config files.
200 struct string_list
*remote_urls
;
202 #define CONFIG_INCLUDE_INIT { 0 }
204 static int git_config_include(const char *var
, const char *value
, void *data
);
206 #define MAX_INCLUDE_DEPTH 10
207 static const char include_depth_advice
[] = N_(
208 "exceeded maximum include depth (%d) while including\n"
212 "This might be due to circular includes.");
213 static int handle_path_include(struct config_source
*cs
, const char *path
,
214 struct config_include_data
*inc
)
217 struct strbuf buf
= STRBUF_INIT
;
221 return config_error_nonbool("include.path");
223 expanded
= interpolate_path(path
, 0);
225 return error(_("could not expand include path '%s'"), path
);
229 * Use an absolute path as-is, but interpret relative paths
230 * based on the including config file.
232 if (!is_absolute_path(path
)) {
235 if (!cs
|| !cs
->path
) {
236 ret
= error(_("relative config includes must come from files"));
240 slash
= find_last_dir_sep(cs
->path
);
242 strbuf_add(&buf
, cs
->path
, slash
- cs
->path
+ 1);
243 strbuf_addstr(&buf
, path
);
247 if (!access_or_die(path
, R_OK
, 0)) {
248 if (++inc
->depth
> MAX_INCLUDE_DEPTH
)
249 die(_(include_depth_advice
), MAX_INCLUDE_DEPTH
, path
,
251 cs
->name
? cs
->name
:
253 ret
= git_config_from_file(git_config_include
, path
, inc
);
257 strbuf_release(&buf
);
262 static void add_trailing_starstar_for_dir(struct strbuf
*pat
)
264 if (pat
->len
&& is_dir_sep(pat
->buf
[pat
->len
- 1]))
265 strbuf_addstr(pat
, "**");
268 static int prepare_include_condition_pattern(struct config_source
*cs
,
271 struct strbuf path
= STRBUF_INIT
;
275 expanded
= interpolate_path(pat
->buf
, 1);
278 strbuf_addstr(pat
, expanded
);
282 if (pat
->buf
[0] == '.' && is_dir_sep(pat
->buf
[1])) {
285 if (!cs
|| !cs
->path
)
286 return error(_("relative config include "
287 "conditionals must come from files"));
289 strbuf_realpath(&path
, cs
->path
, 1);
290 slash
= find_last_dir_sep(path
.buf
);
292 BUG("how is this possible?");
293 strbuf_splice(pat
, 0, 1, path
.buf
, slash
- path
.buf
);
294 prefix
= slash
- path
.buf
+ 1 /* slash */;
295 } else if (!is_absolute_path(pat
->buf
))
296 strbuf_insertstr(pat
, 0, "**/");
298 add_trailing_starstar_for_dir(pat
);
300 strbuf_release(&path
);
304 static int include_by_gitdir(struct config_source
*cs
,
305 const struct config_options
*opts
,
306 const char *cond
, size_t cond_len
, int icase
)
308 struct strbuf text
= STRBUF_INIT
;
309 struct strbuf pattern
= STRBUF_INIT
;
312 int already_tried_absolute
= 0;
315 git_dir
= opts
->git_dir
;
319 strbuf_realpath(&text
, git_dir
, 1);
320 strbuf_add(&pattern
, cond
, cond_len
);
321 prefix
= prepare_include_condition_pattern(cs
, &pattern
);
329 * perform literal matching on the prefix part so that
330 * any wildcard character in it can't create side effects.
332 if (text
.len
< prefix
)
334 if (!icase
&& strncmp(pattern
.buf
, text
.buf
, prefix
))
336 if (icase
&& strncasecmp(pattern
.buf
, text
.buf
, prefix
))
340 ret
= !wildmatch(pattern
.buf
+ prefix
, text
.buf
+ prefix
,
341 WM_PATHNAME
| (icase
? WM_CASEFOLD
: 0));
343 if (!ret
&& !already_tried_absolute
) {
345 * We've tried e.g. matching gitdir:~/work, but if
346 * ~/work is a symlink to /mnt/storage/work
347 * strbuf_realpath() will expand it, so the rule won't
348 * match. Let's match against a
349 * strbuf_add_absolute_path() version of the path,
350 * which'll do the right thing
353 strbuf_add_absolute_path(&text
, git_dir
);
354 already_tried_absolute
= 1;
358 strbuf_release(&pattern
);
359 strbuf_release(&text
);
363 static int include_by_branch(const char *cond
, size_t cond_len
)
367 struct strbuf pattern
= STRBUF_INIT
;
368 const char *refname
= !the_repository
->gitdir
?
369 NULL
: resolve_ref_unsafe("HEAD", 0, NULL
, &flags
);
370 const char *shortname
;
372 if (!refname
|| !(flags
& REF_ISSYMREF
) ||
373 !skip_prefix(refname
, "refs/heads/", &shortname
))
376 strbuf_add(&pattern
, cond
, cond_len
);
377 add_trailing_starstar_for_dir(&pattern
);
378 ret
= !wildmatch(pattern
.buf
, shortname
, WM_PATHNAME
);
379 strbuf_release(&pattern
);
383 static int add_remote_url(const char *var
, const char *value
, void *data
)
385 struct string_list
*remote_urls
= data
;
386 const char *remote_name
;
387 size_t remote_name_len
;
390 if (!parse_config_key(var
, "remote", &remote_name
, &remote_name_len
,
394 string_list_append(remote_urls
, value
);
398 static void populate_remote_urls(struct config_include_data
*inc
)
400 struct config_options opts
;
402 enum config_scope store_scope
= inc
->config_reader
->parsing_scope
;
405 opts
.unconditional_remote_url
= 1;
407 config_reader_set_scope(inc
->config_reader
, 0);
409 inc
->remote_urls
= xmalloc(sizeof(*inc
->remote_urls
));
410 string_list_init_dup(inc
->remote_urls
);
411 config_with_options(add_remote_url
, inc
->remote_urls
, inc
->config_source
, &opts
);
413 config_reader_set_scope(inc
->config_reader
, store_scope
);
416 static int forbid_remote_url(const char *var
, const char *value UNUSED
,
419 const char *remote_name
;
420 size_t remote_name_len
;
423 if (!parse_config_key(var
, "remote", &remote_name
, &remote_name_len
,
427 die(_("remote URLs cannot be configured in file directly or indirectly included by includeIf.hasconfig:remote.*.url"));
431 static int at_least_one_url_matches_glob(const char *glob
, int glob_len
,
432 struct string_list
*remote_urls
)
434 struct strbuf pattern
= STRBUF_INIT
;
435 struct string_list_item
*url_item
;
438 strbuf_add(&pattern
, glob
, glob_len
);
439 for_each_string_list_item(url_item
, remote_urls
) {
440 if (!wildmatch(pattern
.buf
, url_item
->string
, WM_PATHNAME
)) {
445 strbuf_release(&pattern
);
449 static int include_by_remote_url(struct config_include_data
*inc
,
450 const char *cond
, size_t cond_len
)
452 if (inc
->opts
->unconditional_remote_url
)
454 if (!inc
->remote_urls
)
455 populate_remote_urls(inc
);
456 return at_least_one_url_matches_glob(cond
, cond_len
,
460 static int include_condition_is_true(struct config_source
*cs
,
461 struct config_include_data
*inc
,
462 const char *cond
, size_t cond_len
)
464 const struct config_options
*opts
= inc
->opts
;
466 if (skip_prefix_mem(cond
, cond_len
, "gitdir:", &cond
, &cond_len
))
467 return include_by_gitdir(cs
, opts
, cond
, cond_len
, 0);
468 else if (skip_prefix_mem(cond
, cond_len
, "gitdir/i:", &cond
, &cond_len
))
469 return include_by_gitdir(cs
, opts
, cond
, cond_len
, 1);
470 else if (skip_prefix_mem(cond
, cond_len
, "onbranch:", &cond
, &cond_len
))
471 return include_by_branch(cond
, cond_len
);
472 else if (skip_prefix_mem(cond
, cond_len
, "hasconfig:remote.*.url:", &cond
,
474 return include_by_remote_url(inc
, cond
, cond_len
);
476 /* unknown conditionals are always false */
480 static int git_config_include(const char *var
, const char *value
, void *data
)
482 struct config_include_data
*inc
= data
;
483 struct config_source
*cs
= inc
->config_reader
->source
;
484 const char *cond
, *key
;
489 * Pass along all values, including "include" directives; this makes it
490 * possible to query information on the includes themselves.
492 ret
= inc
->fn(var
, value
, inc
->data
);
496 if (!strcmp(var
, "include.path"))
497 ret
= handle_path_include(cs
, value
, inc
);
499 if (!parse_config_key(var
, "includeif", &cond
, &cond_len
, &key
) &&
500 cond
&& include_condition_is_true(cs
, inc
, cond
, cond_len
) &&
501 !strcmp(key
, "path")) {
502 config_fn_t old_fn
= inc
->fn
;
504 if (inc
->opts
->unconditional_remote_url
)
505 inc
->fn
= forbid_remote_url
;
506 ret
= handle_path_include(cs
, value
, inc
);
513 static void git_config_push_split_parameter(const char *key
, const char *value
)
515 struct strbuf env
= STRBUF_INIT
;
516 const char *old
= getenv(CONFIG_DATA_ENVIRONMENT
);
518 strbuf_addstr(&env
, old
);
519 strbuf_addch(&env
, ' ');
521 sq_quote_buf(&env
, key
);
522 strbuf_addch(&env
, '=');
524 sq_quote_buf(&env
, value
);
525 setenv(CONFIG_DATA_ENVIRONMENT
, env
.buf
, 1);
526 strbuf_release(&env
);
529 void git_config_push_parameter(const char *text
)
536 * section.subsection=with=equals.key=value
538 * we cannot tell if it means:
540 * [section "subsection=with=equals"]
546 * subsection = with=equals.key=value
548 * We parse left-to-right for the first "=", meaning we'll prefer to
549 * keep the value intact over the subsection. This is historical, but
550 * also sensible since values are more likely to contain odd or
551 * untrusted input than a section name.
553 * A missing equals is explicitly allowed (as a bool-only entry).
555 value
= strchr(text
, '=');
557 char *key
= xmemdupz(text
, value
- text
);
558 git_config_push_split_parameter(key
, value
+ 1);
561 git_config_push_split_parameter(text
, NULL
);
565 void git_config_push_env(const char *spec
)
568 const char *env_name
;
569 const char *env_value
;
571 env_name
= strrchr(spec
, '=');
573 die(_("invalid config format: %s"), spec
);
574 key
= xmemdupz(spec
, env_name
- spec
);
577 die(_("missing environment variable name for configuration '%.*s'"),
578 (int)(env_name
- spec
- 1), spec
);
580 env_value
= getenv(env_name
);
582 die(_("missing environment variable '%s' for configuration '%.*s'"),
583 env_name
, (int)(env_name
- spec
- 1), spec
);
585 git_config_push_split_parameter(key
, env_value
);
589 static inline int iskeychar(int c
)
591 return isalnum(c
) || c
== '-';
595 * Auxiliary function to sanity-check and split the key into the section
596 * identifier and variable name.
598 * Returns 0 on success, -1 when there is an invalid character in the key and
599 * -2 if there is no section name in the key.
601 * store_key - pointer to char* which will hold a copy of the key with
602 * lowercase section and variable name
603 * baselen - pointer to size_t which will hold the length of the
604 * section + subsection part, can be NULL
606 int git_config_parse_key(const char *key
, char **store_key
, size_t *baselen_
)
610 const char *last_dot
= strrchr(key
, '.');
613 * Since "key" actually contains the section name and the real
614 * key name separated by a dot, we have to know where the dot is.
617 if (last_dot
== NULL
|| last_dot
== key
) {
618 error(_("key does not contain a section: %s"), key
);
619 return -CONFIG_NO_SECTION_OR_NAME
;
623 error(_("key does not contain variable name: %s"), key
);
624 return -CONFIG_NO_SECTION_OR_NAME
;
627 baselen
= last_dot
- key
;
632 * Validate the key and while at it, lower case it for matching.
634 *store_key
= xmallocz(strlen(key
));
637 for (i
= 0; key
[i
]; i
++) {
638 unsigned char c
= key
[i
];
641 /* Leave the extended basename untouched.. */
642 if (!dot
|| i
> baselen
) {
644 (i
== baselen
+ 1 && !isalpha(c
))) {
645 error(_("invalid key: %s"), key
);
649 } else if (c
== '\n') {
650 error(_("invalid key (newline): %s"), key
);
659 FREE_AND_NULL(*store_key
);
660 return -CONFIG_INVALID_KEY
;
663 static int config_parse_pair(const char *key
, const char *value
,
664 config_fn_t fn
, void *data
)
666 char *canonical_name
;
670 return error(_("empty config key"));
671 if (git_config_parse_key(key
, &canonical_name
, NULL
))
674 ret
= (fn(canonical_name
, value
, data
) < 0) ? -1 : 0;
675 free(canonical_name
);
679 int git_config_parse_parameter(const char *text
,
680 config_fn_t fn
, void *data
)
683 struct strbuf
**pair
;
686 pair
= strbuf_split_str(text
, '=', 2);
688 return error(_("bogus config parameter: %s"), text
);
690 if (pair
[0]->len
&& pair
[0]->buf
[pair
[0]->len
- 1] == '=') {
691 strbuf_setlen(pair
[0], pair
[0]->len
- 1);
692 value
= pair
[1] ? pair
[1]->buf
: "";
697 strbuf_trim(pair
[0]);
699 strbuf_list_free(pair
);
700 return error(_("bogus config parameter: %s"), text
);
703 ret
= config_parse_pair(pair
[0]->buf
, value
, fn
, data
);
704 strbuf_list_free(pair
);
708 static int parse_config_env_list(char *env
, config_fn_t fn
, void *data
)
711 while (cur
&& *cur
) {
712 const char *key
= sq_dequote_step(cur
, &cur
);
714 return error(_("bogus format in %s"),
715 CONFIG_DATA_ENVIRONMENT
);
717 if (!cur
|| isspace(*cur
)) {
718 /* old-style 'key=value' */
719 if (git_config_parse_parameter(key
, fn
, data
) < 0)
722 else if (*cur
== '=') {
723 /* new-style 'key'='value' */
729 value
= sq_dequote_step(cur
, &cur
);
730 if (!value
|| (cur
&& !isspace(*cur
))) {
731 return error(_("bogus format in %s"),
732 CONFIG_DATA_ENVIRONMENT
);
734 } else if (!*cur
|| isspace(*cur
)) {
735 /* implicit bool: 'key'= */
738 return error(_("bogus format in %s"),
739 CONFIG_DATA_ENVIRONMENT
);
742 if (config_parse_pair(key
, value
, fn
, data
) < 0)
747 return error(_("bogus format in %s"),
748 CONFIG_DATA_ENVIRONMENT
);
752 while (isspace(*cur
))
759 int git_config_from_parameters(config_fn_t fn
, void *data
)
762 struct strbuf envvar
= STRBUF_INIT
;
763 struct strvec to_free
= STRVEC_INIT
;
766 struct config_source source
= CONFIG_SOURCE_INIT
;
768 source
.origin_type
= CONFIG_ORIGIN_CMDLINE
;
769 config_reader_push_source(&the_reader
, &source
);
771 env
= getenv(CONFIG_COUNT_ENVIRONMENT
);
777 count
= strtoul(env
, &endp
, 10);
779 ret
= error(_("bogus count in %s"), CONFIG_COUNT_ENVIRONMENT
);
782 if (count
> INT_MAX
) {
783 ret
= error(_("too many entries in %s"), CONFIG_COUNT_ENVIRONMENT
);
787 for (i
= 0; i
< count
; i
++) {
788 const char *key
, *value
;
790 strbuf_addf(&envvar
, "GIT_CONFIG_KEY_%d", i
);
791 key
= getenv_safe(&to_free
, envvar
.buf
);
793 ret
= error(_("missing config key %s"), envvar
.buf
);
796 strbuf_reset(&envvar
);
798 strbuf_addf(&envvar
, "GIT_CONFIG_VALUE_%d", i
);
799 value
= getenv_safe(&to_free
, envvar
.buf
);
801 ret
= error(_("missing config value %s"), envvar
.buf
);
804 strbuf_reset(&envvar
);
806 if (config_parse_pair(key
, value
, fn
, data
) < 0) {
813 env
= getenv(CONFIG_DATA_ENVIRONMENT
);
815 /* sq_dequote will write over it */
817 if (parse_config_env_list(envw
, fn
, data
) < 0) {
824 strbuf_release(&envvar
);
825 strvec_clear(&to_free
);
827 config_reader_pop_source(&the_reader
);
831 static int get_next_char(struct config_source
*cs
)
833 int c
= cs
->do_fgetc(cs
);
836 /* DOS like systems */
837 c
= cs
->do_fgetc(cs
);
840 cs
->do_ungetc(c
, cs
);
845 if (c
!= EOF
&& ++cs
->total_len
> INT_MAX
) {
847 * This is an absurdly long config file; refuse to parse
848 * further in order to protect downstream code from integer
849 * overflows. Note that we can't return an error specifically,
850 * but we can mark EOF and put trash in the return value,
851 * which will trigger a parse error.
867 static char *parse_value(struct config_source
*cs
)
869 int quote
= 0, comment
= 0, space
= 0;
871 strbuf_reset(&cs
->value
);
873 int c
= get_next_char(cs
);
879 return cs
->value
.buf
;
883 if (isspace(c
) && !quote
) {
889 if (c
== ';' || c
== '#') {
894 for (; space
; space
--)
895 strbuf_addch(&cs
->value
, ' ');
897 c
= get_next_char(cs
);
910 /* Some characters escape as themselves */
913 /* Reject unknown escape sequences */
917 strbuf_addch(&cs
->value
, c
);
924 strbuf_addch(&cs
->value
, c
);
928 static int get_value(struct config_source
*cs
, config_fn_t fn
, void *data
,
935 /* Get the full name */
937 c
= get_next_char(cs
);
942 strbuf_addch(name
, tolower(c
));
945 while (c
== ' ' || c
== '\t')
946 c
= get_next_char(cs
);
952 value
= parse_value(cs
);
957 * We already consumed the \n, but we need linenr to point to
958 * the line we just parsed during the call to fn to get
959 * accurate line number in error messages.
962 ret
= fn(name
->buf
, value
, data
);
968 static int get_extended_base_var(struct config_source
*cs
, struct strbuf
*name
,
971 cs
->subsection_case_sensitive
= 0;
974 goto error_incomplete_line
;
975 c
= get_next_char(cs
);
976 } while (isspace(c
));
978 /* We require the format to be '[base "extension"]' */
981 strbuf_addch(name
, '.');
984 int c
= get_next_char(cs
);
986 goto error_incomplete_line
;
990 c
= get_next_char(cs
);
992 goto error_incomplete_line
;
994 strbuf_addch(name
, c
);
998 if (get_next_char(cs
) != ']')
1001 error_incomplete_line
:
1006 static int get_base_var(struct config_source
*cs
, struct strbuf
*name
)
1008 cs
->subsection_case_sensitive
= 1;
1010 int c
= get_next_char(cs
);
1016 return get_extended_base_var(cs
, name
, c
);
1017 if (!iskeychar(c
) && c
!= '.')
1019 strbuf_addch(name
, tolower(c
));
1023 struct parse_event_data
{
1024 enum config_event_t previous_type
;
1025 size_t previous_offset
;
1026 const struct config_options
*opts
;
1029 static int do_event(struct config_source
*cs
, enum config_event_t type
,
1030 struct parse_event_data
*data
)
1034 if (!data
->opts
|| !data
->opts
->event_fn
)
1037 if (type
== CONFIG_EVENT_WHITESPACE
&&
1038 data
->previous_type
== type
)
1041 offset
= cs
->do_ftell(cs
);
1043 * At EOF, the parser always "inserts" an extra '\n', therefore
1044 * the end offset of the event is the current file position, otherwise
1045 * we will already have advanced to the next event.
1047 if (type
!= CONFIG_EVENT_EOF
)
1050 if (data
->previous_type
!= CONFIG_EVENT_EOF
&&
1051 data
->opts
->event_fn(data
->previous_type
, data
->previous_offset
,
1052 offset
, data
->opts
->event_fn_data
) < 0)
1055 data
->previous_type
= type
;
1056 data
->previous_offset
= offset
;
1061 static int git_parse_source(struct config_source
*cs
, config_fn_t fn
,
1062 void *data
, const struct config_options
*opts
)
1066 struct strbuf
*var
= &cs
->var
;
1067 int error_return
= 0;
1068 char *error_msg
= NULL
;
1070 /* U+FEFF Byte Order Mark in UTF8 */
1071 const char *bomptr
= utf8_bom
;
1073 /* For the parser event callback */
1074 struct parse_event_data event_data
= {
1075 CONFIG_EVENT_EOF
, 0, opts
1081 c
= get_next_char(cs
);
1082 if (bomptr
&& *bomptr
) {
1083 /* We are at the file beginning; skip UTF8-encoded BOM
1084 * if present. Sane editors won't put this in on their
1085 * own, but e.g. Windows Notepad will do it happily. */
1086 if (c
== (*bomptr
& 0377)) {
1090 /* Do not tolerate partial BOM. */
1091 if (bomptr
!= utf8_bom
)
1093 /* No BOM at file beginning. Cool. */
1099 if (do_event(cs
, CONFIG_EVENT_EOF
, &event_data
) < 0)
1103 if (do_event(cs
, CONFIG_EVENT_WHITESPACE
, &event_data
) < 0)
1111 if (do_event(cs
, CONFIG_EVENT_WHITESPACE
, &event_data
) < 0)
1115 if (c
== '#' || c
== ';') {
1116 if (do_event(cs
, CONFIG_EVENT_COMMENT
, &event_data
) < 0)
1122 if (do_event(cs
, CONFIG_EVENT_SECTION
, &event_data
) < 0)
1125 /* Reset prior to determining a new stem */
1127 if (get_base_var(cs
, var
) < 0 || var
->len
< 1)
1129 strbuf_addch(var
, '.');
1136 if (do_event(cs
, CONFIG_EVENT_ENTRY
, &event_data
) < 0)
1140 * Truncate the var name back to the section header
1141 * stem prior to grabbing the suffix part of the name
1144 strbuf_setlen(var
, baselen
);
1145 strbuf_addch(var
, tolower(c
));
1146 if (get_value(cs
, fn
, data
, var
) < 0)
1150 if (do_event(cs
, CONFIG_EVENT_ERROR
, &event_data
) < 0)
1153 switch (cs
->origin_type
) {
1154 case CONFIG_ORIGIN_BLOB
:
1155 error_msg
= xstrfmt(_("bad config line %d in blob %s"),
1156 cs
->linenr
, cs
->name
);
1158 case CONFIG_ORIGIN_FILE
:
1159 error_msg
= xstrfmt(_("bad config line %d in file %s"),
1160 cs
->linenr
, cs
->name
);
1162 case CONFIG_ORIGIN_STDIN
:
1163 error_msg
= xstrfmt(_("bad config line %d in standard input"),
1166 case CONFIG_ORIGIN_SUBMODULE_BLOB
:
1167 error_msg
= xstrfmt(_("bad config line %d in submodule-blob %s"),
1168 cs
->linenr
, cs
->name
);
1170 case CONFIG_ORIGIN_CMDLINE
:
1171 error_msg
= xstrfmt(_("bad config line %d in command line %s"),
1172 cs
->linenr
, cs
->name
);
1175 error_msg
= xstrfmt(_("bad config line %d in %s"),
1176 cs
->linenr
, cs
->name
);
1179 switch (opts
&& opts
->error_action
?
1180 opts
->error_action
:
1181 cs
->default_error_action
) {
1182 case CONFIG_ERROR_DIE
:
1183 die("%s", error_msg
);
1185 case CONFIG_ERROR_ERROR
:
1186 error_return
= error("%s", error_msg
);
1188 case CONFIG_ERROR_SILENT
:
1191 case CONFIG_ERROR_UNSET
:
1192 BUG("config error action unset");
1196 return error_return
;
1199 static uintmax_t get_unit_factor(const char *end
)
1203 else if (!strcasecmp(end
, "k"))
1205 else if (!strcasecmp(end
, "m"))
1207 else if (!strcasecmp(end
, "g"))
1208 return 1024 * 1024 * 1024;
1212 static int git_parse_signed(const char *value
, intmax_t *ret
, intmax_t max
)
1214 if (value
&& *value
) {
1220 BUG("max must be a positive integer");
1223 val
= strtoimax(value
, &end
, 0);
1224 if (errno
== ERANGE
)
1230 factor
= get_unit_factor(end
);
1235 if ((val
< 0 && -max
/ factor
> val
) ||
1236 (val
> 0 && max
/ factor
< val
)) {
1248 static int git_parse_unsigned(const char *value
, uintmax_t *ret
, uintmax_t max
)
1250 if (value
&& *value
) {
1255 /* negative values would be accepted by strtoumax */
1256 if (strchr(value
, '-')) {
1261 val
= strtoumax(value
, &end
, 0);
1262 if (errno
== ERANGE
)
1268 factor
= get_unit_factor(end
);
1273 if (unsigned_mult_overflows(factor
, val
) ||
1274 factor
* val
> max
) {
1286 int git_parse_int(const char *value
, int *ret
)
1289 if (!git_parse_signed(value
, &tmp
, maximum_signed_value_of_type(int)))
1295 static int git_parse_int64(const char *value
, int64_t *ret
)
1298 if (!git_parse_signed(value
, &tmp
, maximum_signed_value_of_type(int64_t)))
1304 int git_parse_ulong(const char *value
, unsigned long *ret
)
1307 if (!git_parse_unsigned(value
, &tmp
, maximum_unsigned_value_of_type(long)))
1313 int git_parse_ssize_t(const char *value
, ssize_t
*ret
)
1316 if (!git_parse_signed(value
, &tmp
, maximum_signed_value_of_type(ssize_t
)))
1322 static int reader_config_name(struct config_reader
*reader
, const char **out
);
1323 static int reader_origin_type(struct config_reader
*reader
,
1324 enum config_origin_type
*type
);
1326 static void die_bad_number(struct config_reader
*reader
, const char *name
,
1329 const char *error_type
= (errno
== ERANGE
) ?
1330 N_("out of range") : N_("invalid unit");
1331 const char *bad_numeric
= N_("bad numeric config value '%s' for '%s': %s");
1332 const char *config_name
= NULL
;
1333 enum config_origin_type config_origin
= CONFIG_ORIGIN_UNKNOWN
;
1338 /* Ignoring the return value is okay since we handle missing values. */
1339 reader_config_name(reader
, &config_name
);
1340 reader_origin_type(reader
, &config_origin
);
1343 die(_(bad_numeric
), value
, name
, _(error_type
));
1345 switch (config_origin
) {
1346 case CONFIG_ORIGIN_BLOB
:
1347 die(_("bad numeric config value '%s' for '%s' in blob %s: %s"),
1348 value
, name
, config_name
, _(error_type
));
1349 case CONFIG_ORIGIN_FILE
:
1350 die(_("bad numeric config value '%s' for '%s' in file %s: %s"),
1351 value
, name
, config_name
, _(error_type
));
1352 case CONFIG_ORIGIN_STDIN
:
1353 die(_("bad numeric config value '%s' for '%s' in standard input: %s"),
1354 value
, name
, _(error_type
));
1355 case CONFIG_ORIGIN_SUBMODULE_BLOB
:
1356 die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"),
1357 value
, name
, config_name
, _(error_type
));
1358 case CONFIG_ORIGIN_CMDLINE
:
1359 die(_("bad numeric config value '%s' for '%s' in command line %s: %s"),
1360 value
, name
, config_name
, _(error_type
));
1362 die(_("bad numeric config value '%s' for '%s' in %s: %s"),
1363 value
, name
, config_name
, _(error_type
));
1367 int git_config_int(const char *name
, const char *value
)
1370 if (!git_parse_int(value
, &ret
))
1371 die_bad_number(&the_reader
, name
, value
);
1375 int64_t git_config_int64(const char *name
, const char *value
)
1378 if (!git_parse_int64(value
, &ret
))
1379 die_bad_number(&the_reader
, name
, value
);
1383 unsigned long git_config_ulong(const char *name
, const char *value
)
1386 if (!git_parse_ulong(value
, &ret
))
1387 die_bad_number(&the_reader
, name
, value
);
1391 ssize_t
git_config_ssize_t(const char *name
, const char *value
)
1394 if (!git_parse_ssize_t(value
, &ret
))
1395 die_bad_number(&the_reader
, name
, value
);
1399 static int git_parse_maybe_bool_text(const char *value
)
1405 if (!strcasecmp(value
, "true")
1406 || !strcasecmp(value
, "yes")
1407 || !strcasecmp(value
, "on"))
1409 if (!strcasecmp(value
, "false")
1410 || !strcasecmp(value
, "no")
1411 || !strcasecmp(value
, "off"))
1416 static const struct fsync_component_name
{
1418 enum fsync_component component_bits
;
1419 } fsync_component_names
[] = {
1420 { "loose-object", FSYNC_COMPONENT_LOOSE_OBJECT
},
1421 { "pack", FSYNC_COMPONENT_PACK
},
1422 { "pack-metadata", FSYNC_COMPONENT_PACK_METADATA
},
1423 { "commit-graph", FSYNC_COMPONENT_COMMIT_GRAPH
},
1424 { "index", FSYNC_COMPONENT_INDEX
},
1425 { "objects", FSYNC_COMPONENTS_OBJECTS
},
1426 { "reference", FSYNC_COMPONENT_REFERENCE
},
1427 { "derived-metadata", FSYNC_COMPONENTS_DERIVED_METADATA
},
1428 { "committed", FSYNC_COMPONENTS_COMMITTED
},
1429 { "added", FSYNC_COMPONENTS_ADDED
},
1430 { "all", FSYNC_COMPONENTS_ALL
},
1433 static enum fsync_component
parse_fsync_components(const char *var
, const char *string
)
1435 enum fsync_component current
= FSYNC_COMPONENTS_PLATFORM_DEFAULT
;
1436 enum fsync_component positive
= 0, negative
= 0;
1445 string
= string
+ strspn(string
, ", \t\n\r");
1446 ep
= strchrnul(string
, ',');
1448 if (!strcmp(string
, "none")) {
1449 current
= FSYNC_COMPONENT_NONE
;
1453 if (*string
== '-') {
1458 warning(_("invalid value for variable %s"), var
);
1464 for (i
= 0; i
< ARRAY_SIZE(fsync_component_names
); ++i
) {
1465 const struct fsync_component_name
*n
= &fsync_component_names
[i
];
1467 if (strncmp(n
->name
, string
, len
))
1472 negative
|= n
->component_bits
;
1474 positive
|= n
->component_bits
;
1478 char *component
= xstrndup(string
, len
);
1479 warning(_("ignoring unknown core.fsync component '%s'"), component
);
1487 return (current
& ~negative
) | positive
;
1490 int git_parse_maybe_bool(const char *value
)
1492 int v
= git_parse_maybe_bool_text(value
);
1495 if (git_parse_int(value
, &v
))
1500 int git_config_bool_or_int(const char *name
, const char *value
, int *is_bool
)
1502 int v
= git_parse_maybe_bool_text(value
);
1508 return git_config_int(name
, value
);
1511 int git_config_bool(const char *name
, const char *value
)
1513 int v
= git_parse_maybe_bool(value
);
1515 die(_("bad boolean config value '%s' for '%s'"), value
, name
);
1519 int git_config_string(const char **dest
, const char *var
, const char *value
)
1522 return config_error_nonbool(var
);
1523 *dest
= xstrdup(value
);
1527 int git_config_pathname(const char **dest
, const char *var
, const char *value
)
1530 return config_error_nonbool(var
);
1531 *dest
= interpolate_path(value
, 0);
1533 die(_("failed to expand user dir in: '%s'"), value
);
1537 int git_config_expiry_date(timestamp_t
*timestamp
, const char *var
, const char *value
)
1540 return config_error_nonbool(var
);
1541 if (parse_expiry_date(value
, timestamp
))
1542 return error(_("'%s' for '%s' is not a valid timestamp"),
1547 int git_config_color(char *dest
, const char *var
, const char *value
)
1550 return config_error_nonbool(var
);
1551 if (color_parse(value
, dest
) < 0)
1556 static int git_default_core_config(const char *var
, const char *value
, void *cb
)
1558 /* This needs a better name */
1559 if (!strcmp(var
, "core.filemode")) {
1560 trust_executable_bit
= git_config_bool(var
, value
);
1563 if (!strcmp(var
, "core.trustctime")) {
1564 trust_ctime
= git_config_bool(var
, value
);
1567 if (!strcmp(var
, "core.checkstat")) {
1568 if (!strcasecmp(value
, "default"))
1570 else if (!strcasecmp(value
, "minimal"))
1574 if (!strcmp(var
, "core.quotepath")) {
1575 quote_path_fully
= git_config_bool(var
, value
);
1579 if (!strcmp(var
, "core.symlinks")) {
1580 has_symlinks
= git_config_bool(var
, value
);
1584 if (!strcmp(var
, "core.ignorecase")) {
1585 ignore_case
= git_config_bool(var
, value
);
1589 if (!strcmp(var
, "core.attributesfile"))
1590 return git_config_pathname(&git_attributes_file
, var
, value
);
1592 if (!strcmp(var
, "core.hookspath"))
1593 return git_config_pathname(&git_hooks_path
, var
, value
);
1595 if (!strcmp(var
, "core.bare")) {
1596 is_bare_repository_cfg
= git_config_bool(var
, value
);
1600 if (!strcmp(var
, "core.ignorestat")) {
1601 assume_unchanged
= git_config_bool(var
, value
);
1605 if (!strcmp(var
, "core.prefersymlinkrefs")) {
1606 prefer_symlink_refs
= git_config_bool(var
, value
);
1610 if (!strcmp(var
, "core.logallrefupdates")) {
1611 if (value
&& !strcasecmp(value
, "always"))
1612 log_all_ref_updates
= LOG_REFS_ALWAYS
;
1613 else if (git_config_bool(var
, value
))
1614 log_all_ref_updates
= LOG_REFS_NORMAL
;
1616 log_all_ref_updates
= LOG_REFS_NONE
;
1620 if (!strcmp(var
, "core.warnambiguousrefs")) {
1621 warn_ambiguous_refs
= git_config_bool(var
, value
);
1625 if (!strcmp(var
, "core.abbrev")) {
1627 return config_error_nonbool(var
);
1628 if (!strcasecmp(value
, "auto"))
1629 default_abbrev
= -1;
1630 else if (!git_parse_maybe_bool_text(value
))
1631 default_abbrev
= the_hash_algo
->hexsz
;
1633 int abbrev
= git_config_int(var
, value
);
1634 if (abbrev
< minimum_abbrev
|| abbrev
> the_hash_algo
->hexsz
)
1635 return error(_("abbrev length out of range: %d"), abbrev
);
1636 default_abbrev
= abbrev
;
1641 if (!strcmp(var
, "core.disambiguate"))
1642 return set_disambiguate_hint_config(var
, value
);
1644 if (!strcmp(var
, "core.loosecompression")) {
1645 int level
= git_config_int(var
, value
);
1647 level
= Z_DEFAULT_COMPRESSION
;
1648 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
1649 die(_("bad zlib compression level %d"), level
);
1650 zlib_compression_level
= level
;
1651 zlib_compression_seen
= 1;
1655 if (!strcmp(var
, "core.compression")) {
1656 int level
= git_config_int(var
, value
);
1658 level
= Z_DEFAULT_COMPRESSION
;
1659 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
1660 die(_("bad zlib compression level %d"), level
);
1661 if (!zlib_compression_seen
)
1662 zlib_compression_level
= level
;
1663 if (!pack_compression_seen
)
1664 pack_compression_level
= level
;
1668 if (!strcmp(var
, "core.packedgitwindowsize")) {
1669 int pgsz_x2
= getpagesize() * 2;
1670 packed_git_window_size
= git_config_ulong(var
, value
);
1672 /* This value must be multiple of (pagesize * 2) */
1673 packed_git_window_size
/= pgsz_x2
;
1674 if (packed_git_window_size
< 1)
1675 packed_git_window_size
= 1;
1676 packed_git_window_size
*= pgsz_x2
;
1680 if (!strcmp(var
, "core.bigfilethreshold")) {
1681 big_file_threshold
= git_config_ulong(var
, value
);
1685 if (!strcmp(var
, "core.packedgitlimit")) {
1686 packed_git_limit
= git_config_ulong(var
, value
);
1690 if (!strcmp(var
, "core.deltabasecachelimit")) {
1691 delta_base_cache_limit
= git_config_ulong(var
, value
);
1695 if (!strcmp(var
, "core.autocrlf")) {
1696 if (value
&& !strcasecmp(value
, "input")) {
1697 auto_crlf
= AUTO_CRLF_INPUT
;
1700 auto_crlf
= git_config_bool(var
, value
);
1704 if (!strcmp(var
, "core.safecrlf")) {
1706 if (value
&& !strcasecmp(value
, "warn")) {
1707 global_conv_flags_eol
= CONV_EOL_RNDTRP_WARN
;
1710 eol_rndtrp_die
= git_config_bool(var
, value
);
1711 global_conv_flags_eol
= eol_rndtrp_die
?
1712 CONV_EOL_RNDTRP_DIE
: 0;
1716 if (!strcmp(var
, "core.eol")) {
1717 if (value
&& !strcasecmp(value
, "lf"))
1719 else if (value
&& !strcasecmp(value
, "crlf"))
1720 core_eol
= EOL_CRLF
;
1721 else if (value
&& !strcasecmp(value
, "native"))
1722 core_eol
= EOL_NATIVE
;
1724 core_eol
= EOL_UNSET
;
1728 if (!strcmp(var
, "core.checkroundtripencoding")) {
1729 check_roundtrip_encoding
= xstrdup(value
);
1733 if (!strcmp(var
, "core.notesref")) {
1734 notes_ref_name
= xstrdup(value
);
1738 if (!strcmp(var
, "core.editor"))
1739 return git_config_string(&editor_program
, var
, value
);
1741 if (!strcmp(var
, "core.commentchar")) {
1743 return config_error_nonbool(var
);
1744 else if (!strcasecmp(value
, "auto"))
1745 auto_comment_line_char
= 1;
1746 else if (value
[0] && !value
[1]) {
1747 comment_line_char
= value
[0];
1748 auto_comment_line_char
= 0;
1750 return error(_("core.commentChar should only be one ASCII character"));
1754 if (!strcmp(var
, "core.askpass"))
1755 return git_config_string(&askpass_program
, var
, value
);
1757 if (!strcmp(var
, "core.excludesfile"))
1758 return git_config_pathname(&excludes_file
, var
, value
);
1760 if (!strcmp(var
, "core.whitespace")) {
1762 return config_error_nonbool(var
);
1763 whitespace_rule_cfg
= parse_whitespace_rule(value
);
1767 if (!strcmp(var
, "core.fsync")) {
1769 return config_error_nonbool(var
);
1770 fsync_components
= parse_fsync_components(var
, value
);
1774 if (!strcmp(var
, "core.fsyncmethod")) {
1776 return config_error_nonbool(var
);
1777 if (!strcmp(value
, "fsync"))
1778 fsync_method
= FSYNC_METHOD_FSYNC
;
1779 else if (!strcmp(value
, "writeout-only"))
1780 fsync_method
= FSYNC_METHOD_WRITEOUT_ONLY
;
1781 else if (!strcmp(value
, "batch"))
1782 fsync_method
= FSYNC_METHOD_BATCH
;
1784 warning(_("ignoring unknown core.fsyncMethod value '%s'"), value
);
1788 if (!strcmp(var
, "core.fsyncobjectfiles")) {
1789 if (fsync_object_files
< 0)
1790 warning(_("core.fsyncObjectFiles is deprecated; use core.fsync instead"));
1791 fsync_object_files
= git_config_bool(var
, value
);
1795 if (!strcmp(var
, "core.preloadindex")) {
1796 core_preload_index
= git_config_bool(var
, value
);
1800 if (!strcmp(var
, "core.createobject")) {
1801 if (!strcmp(value
, "rename"))
1802 object_creation_mode
= OBJECT_CREATION_USES_RENAMES
;
1803 else if (!strcmp(value
, "link"))
1804 object_creation_mode
= OBJECT_CREATION_USES_HARDLINKS
;
1806 die(_("invalid mode for object creation: %s"), value
);
1810 if (!strcmp(var
, "core.sparsecheckout")) {
1811 core_apply_sparse_checkout
= git_config_bool(var
, value
);
1815 if (!strcmp(var
, "core.sparsecheckoutcone")) {
1816 core_sparse_checkout_cone
= git_config_bool(var
, value
);
1820 if (!strcmp(var
, "core.precomposeunicode")) {
1821 precomposed_unicode
= git_config_bool(var
, value
);
1825 if (!strcmp(var
, "core.protecthfs")) {
1826 protect_hfs
= git_config_bool(var
, value
);
1830 if (!strcmp(var
, "core.protectntfs")) {
1831 protect_ntfs
= git_config_bool(var
, value
);
1835 if (!strcmp(var
, "core.usereplacerefs")) {
1836 read_replace_refs
= git_config_bool(var
, value
);
1840 /* Add other config variables here and to Documentation/config.txt. */
1841 return platform_core_config(var
, value
, cb
);
1844 static int git_default_sparse_config(const char *var
, const char *value
)
1846 if (!strcmp(var
, "sparse.expectfilesoutsideofpatterns")) {
1847 sparse_expect_files_outside_of_patterns
= git_config_bool(var
, value
);
1851 /* Add other config variables here and to Documentation/config/sparse.txt. */
1855 static int git_default_i18n_config(const char *var
, const char *value
)
1857 if (!strcmp(var
, "i18n.commitencoding"))
1858 return git_config_string(&git_commit_encoding
, var
, value
);
1860 if (!strcmp(var
, "i18n.logoutputencoding"))
1861 return git_config_string(&git_log_output_encoding
, var
, value
);
1863 /* Add other config variables here and to Documentation/config.txt. */
1867 static int git_default_branch_config(const char *var
, const char *value
)
1869 if (!strcmp(var
, "branch.autosetupmerge")) {
1870 if (value
&& !strcmp(value
, "always")) {
1871 git_branch_track
= BRANCH_TRACK_ALWAYS
;
1873 } else if (value
&& !strcmp(value
, "inherit")) {
1874 git_branch_track
= BRANCH_TRACK_INHERIT
;
1876 } else if (value
&& !strcmp(value
, "simple")) {
1877 git_branch_track
= BRANCH_TRACK_SIMPLE
;
1880 git_branch_track
= git_config_bool(var
, value
);
1883 if (!strcmp(var
, "branch.autosetuprebase")) {
1885 return config_error_nonbool(var
);
1886 else if (!strcmp(value
, "never"))
1887 autorebase
= AUTOREBASE_NEVER
;
1888 else if (!strcmp(value
, "local"))
1889 autorebase
= AUTOREBASE_LOCAL
;
1890 else if (!strcmp(value
, "remote"))
1891 autorebase
= AUTOREBASE_REMOTE
;
1892 else if (!strcmp(value
, "always"))
1893 autorebase
= AUTOREBASE_ALWAYS
;
1895 return error(_("malformed value for %s"), var
);
1899 /* Add other config variables here and to Documentation/config.txt. */
1903 static int git_default_push_config(const char *var
, const char *value
)
1905 if (!strcmp(var
, "push.default")) {
1907 return config_error_nonbool(var
);
1908 else if (!strcmp(value
, "nothing"))
1909 push_default
= PUSH_DEFAULT_NOTHING
;
1910 else if (!strcmp(value
, "matching"))
1911 push_default
= PUSH_DEFAULT_MATCHING
;
1912 else if (!strcmp(value
, "simple"))
1913 push_default
= PUSH_DEFAULT_SIMPLE
;
1914 else if (!strcmp(value
, "upstream"))
1915 push_default
= PUSH_DEFAULT_UPSTREAM
;
1916 else if (!strcmp(value
, "tracking")) /* deprecated */
1917 push_default
= PUSH_DEFAULT_UPSTREAM
;
1918 else if (!strcmp(value
, "current"))
1919 push_default
= PUSH_DEFAULT_CURRENT
;
1921 error(_("malformed value for %s: %s"), var
, value
);
1922 return error(_("must be one of nothing, matching, simple, "
1923 "upstream or current"));
1928 /* Add other config variables here and to Documentation/config.txt. */
1932 static int git_default_mailmap_config(const char *var
, const char *value
)
1934 if (!strcmp(var
, "mailmap.file"))
1935 return git_config_pathname(&git_mailmap_file
, var
, value
);
1936 if (!strcmp(var
, "mailmap.blob"))
1937 return git_config_string(&git_mailmap_blob
, var
, value
);
1939 /* Add other config variables here and to Documentation/config.txt. */
1943 int git_default_config(const char *var
, const char *value
, void *cb
)
1945 if (starts_with(var
, "core."))
1946 return git_default_core_config(var
, value
, cb
);
1948 if (starts_with(var
, "user.") ||
1949 starts_with(var
, "author.") ||
1950 starts_with(var
, "committer."))
1951 return git_ident_config(var
, value
, cb
);
1953 if (starts_with(var
, "i18n."))
1954 return git_default_i18n_config(var
, value
);
1956 if (starts_with(var
, "branch."))
1957 return git_default_branch_config(var
, value
);
1959 if (starts_with(var
, "push."))
1960 return git_default_push_config(var
, value
);
1962 if (starts_with(var
, "mailmap."))
1963 return git_default_mailmap_config(var
, value
);
1965 if (starts_with(var
, "advice.") || starts_with(var
, "color.advice"))
1966 return git_default_advice_config(var
, value
);
1968 if (!strcmp(var
, "pager.color") || !strcmp(var
, "color.pager")) {
1969 pager_use_color
= git_config_bool(var
,value
);
1973 if (!strcmp(var
, "pack.packsizelimit")) {
1974 pack_size_limit_cfg
= git_config_ulong(var
, value
);
1978 if (!strcmp(var
, "pack.compression")) {
1979 int level
= git_config_int(var
, value
);
1981 level
= Z_DEFAULT_COMPRESSION
;
1982 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
1983 die(_("bad pack compression level %d"), level
);
1984 pack_compression_level
= level
;
1985 pack_compression_seen
= 1;
1989 if (starts_with(var
, "sparse."))
1990 return git_default_sparse_config(var
, value
);
1992 /* Add other config variables here and to Documentation/config.txt. */
1997 * All source specific fields in the union, die_on_error, name and the callbacks
1998 * fgetc, ungetc, ftell of top need to be initialized before calling
2001 static int do_config_from(struct config_reader
*reader
,
2002 struct config_source
*top
, config_fn_t fn
, void *data
,
2003 const struct config_options
*opts
)
2007 /* push config-file parsing state stack */
2011 strbuf_init(&top
->value
, 1024);
2012 strbuf_init(&top
->var
, 1024);
2013 config_reader_push_source(reader
, top
);
2015 ret
= git_parse_source(top
, fn
, data
, opts
);
2017 /* pop config-file parsing state stack */
2018 strbuf_release(&top
->value
);
2019 strbuf_release(&top
->var
);
2020 config_reader_pop_source(reader
);
2025 static int do_config_from_file(struct config_reader
*reader
,
2027 const enum config_origin_type origin_type
,
2028 const char *name
, const char *path
, FILE *f
,
2029 void *data
, const struct config_options
*opts
)
2031 struct config_source top
= CONFIG_SOURCE_INIT
;
2035 top
.origin_type
= origin_type
;
2038 top
.default_error_action
= CONFIG_ERROR_DIE
;
2039 top
.do_fgetc
= config_file_fgetc
;
2040 top
.do_ungetc
= config_file_ungetc
;
2041 top
.do_ftell
= config_file_ftell
;
2044 ret
= do_config_from(reader
, &top
, fn
, data
, opts
);
2049 static int git_config_from_stdin(config_fn_t fn
, void *data
)
2051 return do_config_from_file(&the_reader
, fn
, CONFIG_ORIGIN_STDIN
, "",
2052 NULL
, stdin
, data
, NULL
);
2055 int git_config_from_file_with_options(config_fn_t fn
, const char *filename
,
2057 const struct config_options
*opts
)
2063 BUG("filename cannot be NULL");
2064 f
= fopen_or_warn(filename
, "r");
2066 ret
= do_config_from_file(&the_reader
, fn
, CONFIG_ORIGIN_FILE
,
2067 filename
, filename
, f
, data
, opts
);
2073 int git_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
2075 return git_config_from_file_with_options(fn
, filename
, data
, NULL
);
2078 int git_config_from_mem(config_fn_t fn
,
2079 const enum config_origin_type origin_type
,
2080 const char *name
, const char *buf
, size_t len
,
2081 void *data
, const struct config_options
*opts
)
2083 struct config_source top
= CONFIG_SOURCE_INIT
;
2085 top
.u
.buf
.buf
= buf
;
2086 top
.u
.buf
.len
= len
;
2088 top
.origin_type
= origin_type
;
2091 top
.default_error_action
= CONFIG_ERROR_ERROR
;
2092 top
.do_fgetc
= config_buf_fgetc
;
2093 top
.do_ungetc
= config_buf_ungetc
;
2094 top
.do_ftell
= config_buf_ftell
;
2096 return do_config_from(&the_reader
, &top
, fn
, data
, opts
);
2099 int git_config_from_blob_oid(config_fn_t fn
,
2101 struct repository
*repo
,
2102 const struct object_id
*oid
,
2105 enum object_type type
;
2110 buf
= repo_read_object_file(repo
, oid
, &type
, &size
);
2112 return error(_("unable to load config blob object '%s'"), name
);
2113 if (type
!= OBJ_BLOB
) {
2115 return error(_("reference '%s' does not point to a blob"), name
);
2118 ret
= git_config_from_mem(fn
, CONFIG_ORIGIN_BLOB
, name
, buf
, size
,
2125 static int git_config_from_blob_ref(config_fn_t fn
,
2126 struct repository
*repo
,
2130 struct object_id oid
;
2132 if (repo_get_oid(repo
, name
, &oid
) < 0)
2133 return error(_("unable to resolve config blob '%s'"), name
);
2134 return git_config_from_blob_oid(fn
, name
, repo
, &oid
, data
);
2137 char *git_system_config(void)
2139 char *system_config
= xstrdup_or_null(getenv("GIT_CONFIG_SYSTEM"));
2141 system_config
= system_path(ETC_GITCONFIG
);
2142 normalize_path_copy(system_config
, system_config
);
2143 return system_config
;
2146 void git_global_config(char **user_out
, char **xdg_out
)
2148 char *user_config
= xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
2149 char *xdg_config
= NULL
;
2152 user_config
= interpolate_path("~/.gitconfig", 0);
2153 xdg_config
= xdg_config_home("config");
2156 *user_out
= user_config
;
2157 *xdg_out
= xdg_config
;
2161 * Parse environment variable 'k' as a boolean (in various
2162 * possible spellings); if missing, use the default value 'def'.
2164 int git_env_bool(const char *k
, int def
)
2166 const char *v
= getenv(k
);
2167 return v
? git_config_bool(k
, v
) : def
;
2171 * Parse environment variable 'k' as ulong with possibly a unit
2172 * suffix; if missing, use the default value 'val'.
2174 unsigned long git_env_ulong(const char *k
, unsigned long val
)
2176 const char *v
= getenv(k
);
2177 if (v
&& !git_parse_ulong(v
, &val
))
2178 die(_("failed to parse %s"), k
);
2182 int git_config_system(void)
2184 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
2187 static int do_git_config_sequence(struct config_reader
*reader
,
2188 const struct config_options
*opts
,
2189 config_fn_t fn
, void *data
)
2192 char *system_config
= git_system_config();
2193 char *xdg_config
= NULL
;
2194 char *user_config
= NULL
;
2196 enum config_scope prev_parsing_scope
= reader
->parsing_scope
;
2198 if (opts
->commondir
)
2199 repo_config
= mkpathdup("%s/config", opts
->commondir
);
2200 else if (opts
->git_dir
)
2201 BUG("git_dir without commondir");
2205 config_reader_set_scope(reader
, CONFIG_SCOPE_SYSTEM
);
2206 if (git_config_system() && system_config
&&
2207 !access_or_die(system_config
, R_OK
,
2208 opts
->system_gently
? ACCESS_EACCES_OK
: 0))
2209 ret
+= git_config_from_file(fn
, system_config
, data
);
2211 config_reader_set_scope(reader
, CONFIG_SCOPE_GLOBAL
);
2212 git_global_config(&user_config
, &xdg_config
);
2214 if (xdg_config
&& !access_or_die(xdg_config
, R_OK
, ACCESS_EACCES_OK
))
2215 ret
+= git_config_from_file(fn
, xdg_config
, data
);
2217 if (user_config
&& !access_or_die(user_config
, R_OK
, ACCESS_EACCES_OK
))
2218 ret
+= git_config_from_file(fn
, user_config
, data
);
2220 config_reader_set_scope(reader
, CONFIG_SCOPE_LOCAL
);
2221 if (!opts
->ignore_repo
&& repo_config
&&
2222 !access_or_die(repo_config
, R_OK
, 0))
2223 ret
+= git_config_from_file(fn
, repo_config
, data
);
2225 config_reader_set_scope(reader
, CONFIG_SCOPE_WORKTREE
);
2226 if (!opts
->ignore_worktree
&& repository_format_worktree_config
) {
2227 char *path
= git_pathdup("config.worktree");
2228 if (!access_or_die(path
, R_OK
, 0))
2229 ret
+= git_config_from_file(fn
, path
, data
);
2233 config_reader_set_scope(reader
, CONFIG_SCOPE_COMMAND
);
2234 if (!opts
->ignore_cmdline
&& git_config_from_parameters(fn
, data
) < 0)
2235 die(_("unable to parse command-line config"));
2237 config_reader_set_scope(reader
, prev_parsing_scope
);
2238 free(system_config
);
2245 int config_with_options(config_fn_t fn
, void *data
,
2246 struct git_config_source
*config_source
,
2247 const struct config_options
*opts
)
2249 struct config_include_data inc
= CONFIG_INCLUDE_INIT
;
2250 enum config_scope prev_scope
= the_reader
.parsing_scope
;
2253 if (opts
->respect_includes
) {
2257 inc
.config_source
= config_source
;
2258 inc
.config_reader
= &the_reader
;
2259 fn
= git_config_include
;
2264 config_reader_set_scope(&the_reader
, config_source
->scope
);
2267 * If we have a specific filename, use it. Otherwise, follow the
2268 * regular lookup sequence.
2270 if (config_source
&& config_source
->use_stdin
) {
2271 ret
= git_config_from_stdin(fn
, data
);
2272 } else if (config_source
&& config_source
->file
) {
2273 ret
= git_config_from_file(fn
, config_source
->file
, data
);
2274 } else if (config_source
&& config_source
->blob
) {
2275 struct repository
*repo
= config_source
->repo
?
2276 config_source
->repo
: the_repository
;
2277 ret
= git_config_from_blob_ref(fn
, repo
, config_source
->blob
,
2280 ret
= do_git_config_sequence(&the_reader
, opts
, fn
, data
);
2283 if (inc
.remote_urls
) {
2284 string_list_clear(inc
.remote_urls
, 0);
2285 FREE_AND_NULL(inc
.remote_urls
);
2287 config_reader_set_scope(&the_reader
, prev_scope
);
2291 static void configset_iter(struct config_reader
*reader
, struct config_set
*set
,
2292 config_fn_t fn
, void *data
)
2295 struct string_list
*values
;
2296 struct config_set_element
*entry
;
2297 struct configset_list
*list
= &set
->list
;
2299 for (i
= 0; i
< list
->nr
; i
++) {
2300 entry
= list
->items
[i
].e
;
2301 value_index
= list
->items
[i
].value_index
;
2302 values
= &entry
->value_list
;
2304 config_reader_set_kvi(reader
, values
->items
[value_index
].util
);
2306 if (fn(entry
->key
, values
->items
[value_index
].string
, data
) < 0)
2307 git_die_config_linenr(entry
->key
,
2308 reader
->config_kvi
->filename
,
2309 reader
->config_kvi
->linenr
);
2311 config_reader_set_kvi(reader
, NULL
);
2315 void read_early_config(config_fn_t cb
, void *data
)
2317 struct config_options opts
= {0};
2318 struct strbuf commondir
= STRBUF_INIT
;
2319 struct strbuf gitdir
= STRBUF_INIT
;
2321 opts
.respect_includes
= 1;
2323 if (have_git_dir()) {
2324 opts
.commondir
= get_git_common_dir();
2325 opts
.git_dir
= get_git_dir();
2327 * When setup_git_directory() was not yet asked to discover the
2328 * GIT_DIR, we ask discover_git_directory() to figure out whether there
2329 * is any repository config we should use (but unlike
2330 * setup_git_directory_gently(), no global state is changed, most
2331 * notably, the current working directory is still the same after the
2334 } else if (!discover_git_directory(&commondir
, &gitdir
)) {
2335 opts
.commondir
= commondir
.buf
;
2336 opts
.git_dir
= gitdir
.buf
;
2339 config_with_options(cb
, data
, NULL
, &opts
);
2341 strbuf_release(&commondir
);
2342 strbuf_release(&gitdir
);
2346 * Read config but only enumerate system and global settings.
2347 * Omit any repo-local, worktree-local, or command-line settings.
2349 void read_very_early_config(config_fn_t cb
, void *data
)
2351 struct config_options opts
= { 0 };
2353 opts
.respect_includes
= 1;
2354 opts
.ignore_repo
= 1;
2355 opts
.ignore_worktree
= 1;
2356 opts
.ignore_cmdline
= 1;
2357 opts
.system_gently
= 1;
2359 config_with_options(cb
, data
, NULL
, &opts
);
2363 static int configset_find_element(struct config_set
*set
, const char *key
,
2364 struct config_set_element
**dest
)
2366 struct config_set_element k
;
2367 struct config_set_element
*found_entry
;
2368 char *normalized_key
;
2372 * `key` may come from the user, so normalize it before using it
2373 * for querying entries from the hashmap.
2375 ret
= git_config_parse_key(key
, &normalized_key
, NULL
);
2379 hashmap_entry_init(&k
.ent
, strhash(normalized_key
));
2380 k
.key
= normalized_key
;
2381 found_entry
= hashmap_get_entry(&set
->config_hash
, &k
, ent
, NULL
);
2382 free(normalized_key
);
2383 *dest
= found_entry
;
2387 static int configset_add_value(struct config_reader
*reader
,
2388 struct config_set
*set
, const char *key
,
2391 struct config_set_element
*e
;
2392 struct string_list_item
*si
;
2393 struct configset_list_item
*l_item
;
2394 struct key_value_info
*kv_info
= xmalloc(sizeof(*kv_info
));
2397 ret
= configset_find_element(set
, key
, &e
);
2401 * Since the keys are being fed by git_config*() callback mechanism, they
2402 * are already normalized. So simply add them without any further munging.
2405 e
= xmalloc(sizeof(*e
));
2406 hashmap_entry_init(&e
->ent
, strhash(key
));
2407 e
->key
= xstrdup(key
);
2408 string_list_init_dup(&e
->value_list
);
2409 hashmap_add(&set
->config_hash
, &e
->ent
);
2411 si
= string_list_append_nodup(&e
->value_list
, xstrdup_or_null(value
));
2413 ALLOC_GROW(set
->list
.items
, set
->list
.nr
+ 1, set
->list
.alloc
);
2414 l_item
= &set
->list
.items
[set
->list
.nr
++];
2416 l_item
->value_index
= e
->value_list
.nr
- 1;
2418 if (!reader
->source
)
2419 BUG("configset_add_value has no source");
2420 if (reader
->source
->name
) {
2421 kv_info
->filename
= strintern(reader
->source
->name
);
2422 kv_info
->linenr
= reader
->source
->linenr
;
2423 kv_info
->origin_type
= reader
->source
->origin_type
;
2425 /* for values read from `git_config_from_parameters()` */
2426 kv_info
->filename
= NULL
;
2427 kv_info
->linenr
= -1;
2428 kv_info
->origin_type
= CONFIG_ORIGIN_CMDLINE
;
2430 kv_info
->scope
= reader
->parsing_scope
;
2436 static int config_set_element_cmp(const void *cmp_data UNUSED
,
2437 const struct hashmap_entry
*eptr
,
2438 const struct hashmap_entry
*entry_or_key
,
2439 const void *keydata UNUSED
)
2441 const struct config_set_element
*e1
, *e2
;
2443 e1
= container_of(eptr
, const struct config_set_element
, ent
);
2444 e2
= container_of(entry_or_key
, const struct config_set_element
, ent
);
2446 return strcmp(e1
->key
, e2
->key
);
2449 void git_configset_init(struct config_set
*set
)
2451 hashmap_init(&set
->config_hash
, config_set_element_cmp
, NULL
, 0);
2452 set
->hash_initialized
= 1;
2454 set
->list
.alloc
= 0;
2455 set
->list
.items
= NULL
;
2458 void git_configset_clear(struct config_set
*set
)
2460 struct config_set_element
*entry
;
2461 struct hashmap_iter iter
;
2462 if (!set
->hash_initialized
)
2465 hashmap_for_each_entry(&set
->config_hash
, &iter
, entry
,
2466 ent
/* member name */) {
2468 string_list_clear(&entry
->value_list
, 1);
2470 hashmap_clear_and_free(&set
->config_hash
, struct config_set_element
, ent
);
2471 set
->hash_initialized
= 0;
2472 free(set
->list
.items
);
2474 set
->list
.alloc
= 0;
2475 set
->list
.items
= NULL
;
2478 struct configset_add_data
{
2479 struct config_set
*config_set
;
2480 struct config_reader
*config_reader
;
2482 #define CONFIGSET_ADD_INIT { 0 }
2484 static int config_set_callback(const char *key
, const char *value
, void *cb
)
2486 struct configset_add_data
*data
= cb
;
2487 configset_add_value(data
->config_reader
, data
->config_set
, key
, value
);
2491 int git_configset_add_file(struct config_set
*set
, const char *filename
)
2493 struct configset_add_data data
= CONFIGSET_ADD_INIT
;
2494 data
.config_reader
= &the_reader
;
2495 data
.config_set
= set
;
2496 return git_config_from_file(config_set_callback
, filename
, &data
);
2499 int git_configset_get_value(struct config_set
*set
, const char *key
, const char **value
)
2501 const struct string_list
*values
= NULL
;
2505 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
2506 * queried key in the files of the configset, the value returned will be the last
2507 * value in the value list for that key.
2509 if ((ret
= git_configset_get_value_multi(set
, key
, &values
)))
2512 assert(values
->nr
> 0);
2513 *value
= values
->items
[values
->nr
- 1].string
;
2517 int git_configset_get_value_multi(struct config_set
*set
, const char *key
,
2518 const struct string_list
**dest
)
2520 struct config_set_element
*e
;
2523 if ((ret
= configset_find_element(set
, key
, &e
)))
2527 *dest
= &e
->value_list
;
2532 static int check_multi_string(struct string_list_item
*item
, void *util
)
2534 return item
->string
? 0 : config_error_nonbool(util
);
2537 int git_configset_get_string_multi(struct config_set
*cs
, const char *key
,
2538 const struct string_list
**dest
)
2542 if ((ret
= git_configset_get_value_multi(cs
, key
, dest
)))
2544 if ((ret
= for_each_string_list((struct string_list
*)*dest
,
2545 check_multi_string
, (void *)key
)))
2551 int git_configset_get(struct config_set
*set
, const char *key
)
2553 struct config_set_element
*e
;
2556 if ((ret
= configset_find_element(set
, key
, &e
)))
2563 int git_configset_get_string(struct config_set
*set
, const char *key
, char **dest
)
2566 if (!git_configset_get_value(set
, key
, &value
))
2567 return git_config_string((const char **)dest
, key
, value
);
2572 static int git_configset_get_string_tmp(struct config_set
*set
, const char *key
,
2576 if (!git_configset_get_value(set
, key
, &value
)) {
2578 return config_error_nonbool(key
);
2586 int git_configset_get_int(struct config_set
*set
, const char *key
, int *dest
)
2589 if (!git_configset_get_value(set
, key
, &value
)) {
2590 *dest
= git_config_int(key
, value
);
2596 int git_configset_get_ulong(struct config_set
*set
, const char *key
, unsigned long *dest
)
2599 if (!git_configset_get_value(set
, key
, &value
)) {
2600 *dest
= git_config_ulong(key
, value
);
2606 int git_configset_get_bool(struct config_set
*set
, const char *key
, int *dest
)
2609 if (!git_configset_get_value(set
, key
, &value
)) {
2610 *dest
= git_config_bool(key
, value
);
2616 int git_configset_get_bool_or_int(struct config_set
*set
, const char *key
,
2617 int *is_bool
, int *dest
)
2620 if (!git_configset_get_value(set
, key
, &value
)) {
2621 *dest
= git_config_bool_or_int(key
, value
, is_bool
);
2627 int git_configset_get_maybe_bool(struct config_set
*set
, const char *key
, int *dest
)
2630 if (!git_configset_get_value(set
, key
, &value
)) {
2631 *dest
= git_parse_maybe_bool(value
);
2639 int git_configset_get_pathname(struct config_set
*set
, const char *key
, const char **dest
)
2642 if (!git_configset_get_value(set
, key
, &value
))
2643 return git_config_pathname(dest
, key
, value
);
2648 /* Functions use to read configuration from a repository */
2649 static void repo_read_config(struct repository
*repo
)
2651 struct config_options opts
= { 0 };
2652 struct configset_add_data data
= CONFIGSET_ADD_INIT
;
2654 opts
.respect_includes
= 1;
2655 opts
.commondir
= repo
->commondir
;
2656 opts
.git_dir
= repo
->gitdir
;
2659 CALLOC_ARRAY(repo
->config
, 1);
2661 git_configset_clear(repo
->config
);
2663 git_configset_init(repo
->config
);
2664 data
.config_set
= repo
->config
;
2665 data
.config_reader
= &the_reader
;
2667 if (config_with_options(config_set_callback
, &data
, NULL
, &opts
) < 0)
2669 * config_with_options() normally returns only
2670 * zero, as most errors are fatal, and
2671 * non-fatal potential errors are guarded by "if"
2672 * statements that are entered only when no error is
2675 * If we ever encounter a non-fatal error, it means
2676 * something went really wrong and we should stop
2679 die(_("unknown error occurred while reading the configuration files"));
2682 static void git_config_check_init(struct repository
*repo
)
2684 if (repo
->config
&& repo
->config
->hash_initialized
)
2686 repo_read_config(repo
);
2689 static void repo_config_clear(struct repository
*repo
)
2691 if (!repo
->config
|| !repo
->config
->hash_initialized
)
2693 git_configset_clear(repo
->config
);
2696 void repo_config(struct repository
*repo
, config_fn_t fn
, void *data
)
2698 git_config_check_init(repo
);
2699 configset_iter(&the_reader
, repo
->config
, fn
, data
);
2702 int repo_config_get(struct repository
*repo
, const char *key
)
2704 git_config_check_init(repo
);
2705 return git_configset_get(repo
->config
, key
);
2708 int repo_config_get_value(struct repository
*repo
,
2709 const char *key
, const char **value
)
2711 git_config_check_init(repo
);
2712 return git_configset_get_value(repo
->config
, key
, value
);
2715 int repo_config_get_value_multi(struct repository
*repo
, const char *key
,
2716 const struct string_list
**dest
)
2718 git_config_check_init(repo
);
2719 return git_configset_get_value_multi(repo
->config
, key
, dest
);
2722 int repo_config_get_string_multi(struct repository
*repo
, const char *key
,
2723 const struct string_list
**dest
)
2725 git_config_check_init(repo
);
2726 return git_configset_get_string_multi(repo
->config
, key
, dest
);
2729 int repo_config_get_string(struct repository
*repo
,
2730 const char *key
, char **dest
)
2733 git_config_check_init(repo
);
2734 ret
= git_configset_get_string(repo
->config
, key
, dest
);
2736 git_die_config(key
, NULL
);
2740 int repo_config_get_string_tmp(struct repository
*repo
,
2741 const char *key
, const char **dest
)
2744 git_config_check_init(repo
);
2745 ret
= git_configset_get_string_tmp(repo
->config
, key
, dest
);
2747 git_die_config(key
, NULL
);
2751 int repo_config_get_int(struct repository
*repo
,
2752 const char *key
, int *dest
)
2754 git_config_check_init(repo
);
2755 return git_configset_get_int(repo
->config
, key
, dest
);
2758 int repo_config_get_ulong(struct repository
*repo
,
2759 const char *key
, unsigned long *dest
)
2761 git_config_check_init(repo
);
2762 return git_configset_get_ulong(repo
->config
, key
, dest
);
2765 int repo_config_get_bool(struct repository
*repo
,
2766 const char *key
, int *dest
)
2768 git_config_check_init(repo
);
2769 return git_configset_get_bool(repo
->config
, key
, dest
);
2772 int repo_config_get_bool_or_int(struct repository
*repo
,
2773 const char *key
, int *is_bool
, int *dest
)
2775 git_config_check_init(repo
);
2776 return git_configset_get_bool_or_int(repo
->config
, key
, is_bool
, dest
);
2779 int repo_config_get_maybe_bool(struct repository
*repo
,
2780 const char *key
, int *dest
)
2782 git_config_check_init(repo
);
2783 return git_configset_get_maybe_bool(repo
->config
, key
, dest
);
2786 int repo_config_get_pathname(struct repository
*repo
,
2787 const char *key
, const char **dest
)
2790 git_config_check_init(repo
);
2791 ret
= git_configset_get_pathname(repo
->config
, key
, dest
);
2793 git_die_config(key
, NULL
);
2797 /* Read values into protected_config. */
2798 static void read_protected_config(void)
2800 struct config_options opts
= {
2801 .respect_includes
= 1,
2803 .ignore_worktree
= 1,
2806 struct configset_add_data data
= CONFIGSET_ADD_INIT
;
2808 git_configset_init(&protected_config
);
2809 data
.config_set
= &protected_config
;
2810 data
.config_reader
= &the_reader
;
2811 config_with_options(config_set_callback
, &data
, NULL
, &opts
);
2814 void git_protected_config(config_fn_t fn
, void *data
)
2816 if (!protected_config
.hash_initialized
)
2817 read_protected_config();
2818 configset_iter(&the_reader
, &protected_config
, fn
, data
);
2821 /* Functions used historically to read configuration from 'the_repository' */
2822 void git_config(config_fn_t fn
, void *data
)
2824 repo_config(the_repository
, fn
, data
);
2827 void git_config_clear(void)
2829 repo_config_clear(the_repository
);
2832 int git_config_get(const char *key
)
2834 return repo_config_get(the_repository
, key
);
2837 int git_config_get_value(const char *key
, const char **value
)
2839 return repo_config_get_value(the_repository
, key
, value
);
2842 int git_config_get_value_multi(const char *key
, const struct string_list
**dest
)
2844 return repo_config_get_value_multi(the_repository
, key
, dest
);
2847 int git_config_get_string_multi(const char *key
,
2848 const struct string_list
**dest
)
2850 return repo_config_get_string_multi(the_repository
, key
, dest
);
2853 int git_config_get_string(const char *key
, char **dest
)
2855 return repo_config_get_string(the_repository
, key
, dest
);
2858 int git_config_get_string_tmp(const char *key
, const char **dest
)
2860 return repo_config_get_string_tmp(the_repository
, key
, dest
);
2863 int git_config_get_int(const char *key
, int *dest
)
2865 return repo_config_get_int(the_repository
, key
, dest
);
2868 int git_config_get_ulong(const char *key
, unsigned long *dest
)
2870 return repo_config_get_ulong(the_repository
, key
, dest
);
2873 int git_config_get_bool(const char *key
, int *dest
)
2875 return repo_config_get_bool(the_repository
, key
, dest
);
2878 int git_config_get_bool_or_int(const char *key
, int *is_bool
, int *dest
)
2880 return repo_config_get_bool_or_int(the_repository
, key
, is_bool
, dest
);
2883 int git_config_get_maybe_bool(const char *key
, int *dest
)
2885 return repo_config_get_maybe_bool(the_repository
, key
, dest
);
2888 int git_config_get_pathname(const char *key
, const char **dest
)
2890 return repo_config_get_pathname(the_repository
, key
, dest
);
2893 int git_config_get_expiry(const char *key
, const char **output
)
2895 int ret
= git_config_get_string(key
, (char **)output
);
2898 if (strcmp(*output
, "now")) {
2899 timestamp_t now
= approxidate("now");
2900 if (approxidate(*output
) >= now
)
2901 git_die_config(key
, _("Invalid %s: '%s'"), key
, *output
);
2906 int git_config_get_expiry_in_days(const char *key
, timestamp_t
*expiry
, timestamp_t now
)
2908 const char *expiry_string
;
2912 if (git_config_get_string_tmp(key
, &expiry_string
))
2913 return 1; /* no such thing */
2915 if (git_parse_signed(expiry_string
, &days
, maximum_signed_value_of_type(int))) {
2916 const int scale
= 86400;
2917 *expiry
= now
- days
* scale
;
2921 if (!parse_expiry_date(expiry_string
, &when
)) {
2925 return -1; /* thing exists but cannot be parsed */
2928 int git_config_get_split_index(void)
2932 if (!git_config_get_maybe_bool("core.splitindex", &val
))
2935 return -1; /* default value */
2938 int git_config_get_max_percent_split_change(void)
2942 if (!git_config_get_int("splitindex.maxpercentchange", &val
)) {
2943 if (0 <= val
&& val
<= 100)
2946 return error(_("splitIndex.maxPercentChange value '%d' "
2947 "should be between 0 and 100"), val
);
2950 return -1; /* default value */
2953 int git_config_get_index_threads(int *dest
)
2957 val
= git_env_ulong("GIT_TEST_INDEX_THREADS", 0);
2963 if (!git_config_get_bool_or_int("index.threads", &is_bool
, &val
)) {
2965 *dest
= val
? 0 : 1;
2975 void git_die_config_linenr(const char *key
, const char *filename
, int linenr
)
2978 die(_("unable to parse '%s' from command-line config"), key
);
2980 die(_("bad config variable '%s' in file '%s' at line %d"),
2981 key
, filename
, linenr
);
2984 NORETURN
__attribute__((format(printf
, 2, 3)))
2985 void git_die_config(const char *key
, const char *err
, ...)
2987 const struct string_list
*values
;
2988 struct key_value_info
*kv_info
;
2989 report_fn error_fn
= get_error_routine();
2993 va_start(params
, err
);
2994 error_fn(err
, params
);
2997 if (git_config_get_value_multi(key
, &values
))
2998 BUG("for key '%s' we must have a value to report on", key
);
2999 kv_info
= values
->items
[values
->nr
- 1].util
;
3000 git_die_config_linenr(key
, kv_info
->filename
, kv_info
->linenr
);
3004 * Find all the stuff for git_config_set() below.
3007 struct config_store_data
{
3008 struct config_reader
*config_reader
;
3012 const char *fixed_value
;
3013 regex_t
*value_pattern
;
3017 enum config_event_t type
;
3018 int is_keys_section
;
3020 unsigned int parsed_nr
, parsed_alloc
, *seen
, seen_nr
, seen_alloc
;
3021 unsigned int key_seen
:1, section_seen
:1, is_keys_section
:1;
3023 #define CONFIG_STORE_INIT { 0 }
3025 static void config_store_data_clear(struct config_store_data
*store
)
3028 if (store
->value_pattern
!= NULL
&&
3029 store
->value_pattern
!= CONFIG_REGEX_NONE
) {
3030 regfree(store
->value_pattern
);
3031 free(store
->value_pattern
);
3033 free(store
->parsed
);
3035 memset(store
, 0, sizeof(*store
));
3038 static int matches(const char *key
, const char *value
,
3039 const struct config_store_data
*store
)
3041 if (strcmp(key
, store
->key
))
3042 return 0; /* not ours */
3043 if (store
->fixed_value
)
3044 return !strcmp(store
->fixed_value
, value
);
3045 if (!store
->value_pattern
)
3046 return 1; /* always matches */
3047 if (store
->value_pattern
== CONFIG_REGEX_NONE
)
3048 return 0; /* never matches */
3050 return store
->do_not_match
^
3051 (value
&& !regexec(store
->value_pattern
, value
, 0, NULL
, 0));
3054 static int store_aux_event(enum config_event_t type
,
3055 size_t begin
, size_t end
, void *data
)
3057 struct config_store_data
*store
= data
;
3058 struct config_source
*cs
= store
->config_reader
->source
;
3060 ALLOC_GROW(store
->parsed
, store
->parsed_nr
+ 1, store
->parsed_alloc
);
3061 store
->parsed
[store
->parsed_nr
].begin
= begin
;
3062 store
->parsed
[store
->parsed_nr
].end
= end
;
3063 store
->parsed
[store
->parsed_nr
].type
= type
;
3065 if (type
== CONFIG_EVENT_SECTION
) {
3066 int (*cmpfn
)(const char *, const char *, size_t);
3068 if (cs
->var
.len
< 2 || cs
->var
.buf
[cs
->var
.len
- 1] != '.')
3069 return error(_("invalid section name '%s'"), cs
->var
.buf
);
3071 if (cs
->subsection_case_sensitive
)
3072 cmpfn
= strncasecmp
;
3076 /* Is this the section we were looking for? */
3077 store
->is_keys_section
=
3078 store
->parsed
[store
->parsed_nr
].is_keys_section
=
3079 cs
->var
.len
- 1 == store
->baselen
&&
3080 !cmpfn(cs
->var
.buf
, store
->key
, store
->baselen
);
3081 if (store
->is_keys_section
) {
3082 store
->section_seen
= 1;
3083 ALLOC_GROW(store
->seen
, store
->seen_nr
+ 1,
3085 store
->seen
[store
->seen_nr
] = store
->parsed_nr
;
3094 static int store_aux(const char *key
, const char *value
, void *cb
)
3096 struct config_store_data
*store
= cb
;
3098 if (store
->key_seen
) {
3099 if (matches(key
, value
, store
)) {
3100 if (store
->seen_nr
== 1 && store
->multi_replace
== 0) {
3101 warning(_("%s has multiple values"), key
);
3104 ALLOC_GROW(store
->seen
, store
->seen_nr
+ 1,
3107 store
->seen
[store
->seen_nr
] = store
->parsed_nr
;
3110 } else if (store
->is_keys_section
) {
3112 * Do not increment matches yet: this may not be a match, but we
3113 * are in the desired section.
3115 ALLOC_GROW(store
->seen
, store
->seen_nr
+ 1, store
->seen_alloc
);
3116 store
->seen
[store
->seen_nr
] = store
->parsed_nr
;
3117 store
->section_seen
= 1;
3119 if (matches(key
, value
, store
)) {
3121 store
->key_seen
= 1;
3128 static int write_error(const char *filename
)
3130 error(_("failed to write new configuration file %s"), filename
);
3132 /* Same error code as "failed to rename". */
3136 static struct strbuf
store_create_section(const char *key
,
3137 const struct config_store_data
*store
)
3141 struct strbuf sb
= STRBUF_INIT
;
3143 dot
= memchr(key
, '.', store
->baselen
);
3145 strbuf_addf(&sb
, "[%.*s \"", (int)(dot
- key
), key
);
3146 for (i
= dot
- key
+ 1; i
< store
->baselen
; i
++) {
3147 if (key
[i
] == '"' || key
[i
] == '\\')
3148 strbuf_addch(&sb
, '\\');
3149 strbuf_addch(&sb
, key
[i
]);
3151 strbuf_addstr(&sb
, "\"]\n");
3153 strbuf_addch(&sb
, '[');
3154 strbuf_add(&sb
, key
, store
->baselen
);
3155 strbuf_addstr(&sb
, "]\n");
3161 static ssize_t
write_section(int fd
, const char *key
,
3162 const struct config_store_data
*store
)
3164 struct strbuf sb
= store_create_section(key
, store
);
3167 ret
= write_in_full(fd
, sb
.buf
, sb
.len
);
3168 strbuf_release(&sb
);
3173 static ssize_t
write_pair(int fd
, const char *key
, const char *value
,
3174 const struct config_store_data
*store
)
3178 const char *quote
= "";
3179 struct strbuf sb
= STRBUF_INIT
;
3182 * Check to see if the value needs to be surrounded with a dq pair.
3183 * Note that problematic characters are always backslash-quoted; this
3184 * check is about not losing leading or trailing SP and strings that
3185 * follow beginning-of-comment characters (i.e. ';' and '#') by the
3186 * configuration parser.
3188 if (value
[0] == ' ')
3190 for (i
= 0; value
[i
]; i
++)
3191 if (value
[i
] == ';' || value
[i
] == '#')
3193 if (i
&& value
[i
- 1] == ' ')
3196 strbuf_addf(&sb
, "\t%s = %s", key
+ store
->baselen
+ 1, quote
);
3198 for (i
= 0; value
[i
]; i
++)
3201 strbuf_addstr(&sb
, "\\n");
3204 strbuf_addstr(&sb
, "\\t");
3208 strbuf_addch(&sb
, '\\');
3211 strbuf_addch(&sb
, value
[i
]);
3214 strbuf_addf(&sb
, "%s\n", quote
);
3216 ret
= write_in_full(fd
, sb
.buf
, sb
.len
);
3217 strbuf_release(&sb
);
3223 * If we are about to unset the last key(s) in a section, and if there are
3224 * no comments surrounding (or included in) the section, we will want to
3225 * extend begin/end to remove the entire section.
3227 * Note: the parameter `seen_ptr` points to the index into the store.seen
3228 * array. * This index may be incremented if a section has more than one
3229 * entry (which all are to be removed).
3231 static void maybe_remove_section(struct config_store_data
*store
,
3232 size_t *begin_offset
, size_t *end_offset
,
3236 int i
, seen
, section_seen
= 0;
3239 * First, ensure that this is the first key, and that there are no
3240 * comments before the entry nor before the section header.
3243 for (i
= store
->seen
[seen
]; i
> 0; i
--) {
3244 enum config_event_t type
= store
->parsed
[i
- 1].type
;
3246 if (type
== CONFIG_EVENT_COMMENT
)
3247 /* There is a comment before this entry or section */
3249 if (type
== CONFIG_EVENT_ENTRY
) {
3251 /* This is not the section's first entry. */
3253 /* We encountered no comment before the section. */
3256 if (type
== CONFIG_EVENT_SECTION
) {
3257 if (!store
->parsed
[i
- 1].is_keys_section
)
3262 begin
= store
->parsed
[i
].begin
;
3265 * Next, make sure that we are removing the last key(s) in the section,
3266 * and that there are no comments that are possibly about the current
3269 for (i
= store
->seen
[seen
] + 1; i
< store
->parsed_nr
; i
++) {
3270 enum config_event_t type
= store
->parsed
[i
].type
;
3272 if (type
== CONFIG_EVENT_COMMENT
)
3274 if (type
== CONFIG_EVENT_SECTION
) {
3275 if (store
->parsed
[i
].is_keys_section
)
3279 if (type
== CONFIG_EVENT_ENTRY
) {
3280 if (++seen
< store
->seen_nr
&&
3281 i
== store
->seen
[seen
])
3282 /* We want to remove this entry, too */
3284 /* There is another entry in this section. */
3290 * We are really removing the last entry/entries from this section, and
3291 * there are no enclosed or surrounding comments. Remove the entire,
3292 * now-empty section.
3295 *begin_offset
= begin
;
3296 if (i
< store
->parsed_nr
)
3297 *end_offset
= store
->parsed
[i
].begin
;
3299 *end_offset
= store
->parsed
[store
->parsed_nr
- 1].end
;
3302 int git_config_set_in_file_gently(const char *config_filename
,
3303 const char *key
, const char *value
)
3305 return git_config_set_multivar_in_file_gently(config_filename
, key
, value
, NULL
, 0);
3308 void git_config_set_in_file(const char *config_filename
,
3309 const char *key
, const char *value
)
3311 git_config_set_multivar_in_file(config_filename
, key
, value
, NULL
, 0);
3314 int git_config_set_gently(const char *key
, const char *value
)
3316 return git_config_set_multivar_gently(key
, value
, NULL
, 0);
3319 int repo_config_set_worktree_gently(struct repository
*r
,
3320 const char *key
, const char *value
)
3322 /* Only use worktree-specific config if it is already enabled. */
3323 if (repository_format_worktree_config
) {
3324 char *file
= repo_git_path(r
, "config.worktree");
3325 int ret
= git_config_set_multivar_in_file_gently(
3326 file
, key
, value
, NULL
, 0);
3330 return repo_config_set_multivar_gently(r
, key
, value
, NULL
, 0);
3333 void git_config_set(const char *key
, const char *value
)
3335 git_config_set_multivar(key
, value
, NULL
, 0);
3337 trace2_cmd_set_config(key
, value
);
3341 * If value==NULL, unset in (remove from) config,
3342 * if value_pattern!=NULL, disregard key/value pairs where value does not match.
3343 * if value_pattern==CONFIG_REGEX_NONE, do not match any existing values
3344 * (only add a new one)
3345 * if flags contains the CONFIG_FLAGS_MULTI_REPLACE flag, all matching
3346 * key/values are removed before a single new pair is written. If the
3347 * flag is not present, then replace only the first match.
3349 * Returns 0 on success.
3351 * This function does this:
3353 * - it locks the config file by creating ".git/config.lock"
3355 * - it then parses the config using store_aux() as validator to find
3356 * the position on the key/value pair to replace. If it is to be unset,
3357 * it must be found exactly once.
3359 * - the config file is mmap()ed and the part before the match (if any) is
3360 * written to the lock file, then the changed part and the rest.
3362 * - the config file is removed and the lock file rename()d to it.
3365 int git_config_set_multivar_in_file_gently(const char *config_filename
,
3366 const char *key
, const char *value
,
3367 const char *value_pattern
,
3370 int fd
= -1, in_fd
= -1;
3372 struct lock_file lock
= LOCK_INIT
;
3373 char *filename_buf
= NULL
;
3374 char *contents
= NULL
;
3376 struct config_store_data store
= CONFIG_STORE_INIT
;
3378 store
.config_reader
= &the_reader
;
3380 /* parse-key returns negative; flip the sign to feed exit(3) */
3381 ret
= 0 - git_config_parse_key(key
, &store
.key
, &store
.baselen
);
3385 store
.multi_replace
= (flags
& CONFIG_FLAGS_MULTI_REPLACE
) != 0;
3387 if (!config_filename
)
3388 config_filename
= filename_buf
= git_pathdup("config");
3391 * The lock serves a purpose in addition to locking: the new
3392 * contents of .git/config will be written into it.
3394 fd
= hold_lock_file_for_update(&lock
, config_filename
, 0);
3396 error_errno(_("could not lock config file %s"), config_filename
);
3397 ret
= CONFIG_NO_LOCK
;
3402 * If .git/config does not exist yet, write a minimal version.
3404 in_fd
= open(config_filename
, O_RDONLY
);
3406 if ( ENOENT
!= errno
) {
3407 error_errno(_("opening %s"), config_filename
);
3408 ret
= CONFIG_INVALID_FILE
; /* same as "invalid config file" */
3411 /* if nothing to unset, error out */
3413 ret
= CONFIG_NOTHING_SET
;
3418 store
.key
= xstrdup(key
);
3419 if (write_section(fd
, key
, &store
) < 0 ||
3420 write_pair(fd
, key
, value
, &store
) < 0)
3424 size_t copy_begin
, copy_end
;
3425 int i
, new_line
= 0;
3426 struct config_options opts
;
3429 store
.value_pattern
= NULL
;
3430 else if (value_pattern
== CONFIG_REGEX_NONE
)
3431 store
.value_pattern
= CONFIG_REGEX_NONE
;
3432 else if (flags
& CONFIG_FLAGS_FIXED_VALUE
)
3433 store
.fixed_value
= value_pattern
;
3435 if (value_pattern
[0] == '!') {
3436 store
.do_not_match
= 1;
3439 store
.do_not_match
= 0;
3441 store
.value_pattern
= (regex_t
*)xmalloc(sizeof(regex_t
));
3442 if (regcomp(store
.value_pattern
, value_pattern
,
3444 error(_("invalid pattern: %s"), value_pattern
);
3445 FREE_AND_NULL(store
.value_pattern
);
3446 ret
= CONFIG_INVALID_PATTERN
;
3451 ALLOC_GROW(store
.parsed
, 1, store
.parsed_alloc
);
3452 store
.parsed
[0].end
= 0;
3454 memset(&opts
, 0, sizeof(opts
));
3455 opts
.event_fn
= store_aux_event
;
3456 opts
.event_fn_data
= &store
;
3459 * After this, store.parsed will contain offsets of all the
3460 * parsed elements, and store.seen will contain a list of
3461 * matches, as indices into store.parsed.
3463 * As a side effect, we make sure to transform only a valid
3464 * existing config file.
3466 if (git_config_from_file_with_options(store_aux
,
3469 error(_("invalid config file %s"), config_filename
);
3470 ret
= CONFIG_INVALID_FILE
;
3474 /* if nothing to unset, or too many matches, error out */
3475 if ((store
.seen_nr
== 0 && value
== NULL
) ||
3476 (store
.seen_nr
> 1 && !store
.multi_replace
)) {
3477 ret
= CONFIG_NOTHING_SET
;
3481 if (fstat(in_fd
, &st
) == -1) {
3482 error_errno(_("fstat on %s failed"), config_filename
);
3483 ret
= CONFIG_INVALID_FILE
;
3487 contents_sz
= xsize_t(st
.st_size
);
3488 contents
= xmmap_gently(NULL
, contents_sz
, PROT_READ
,
3489 MAP_PRIVATE
, in_fd
, 0);
3490 if (contents
== MAP_FAILED
) {
3491 if (errno
== ENODEV
&& S_ISDIR(st
.st_mode
))
3493 error_errno(_("unable to mmap '%s'%s"),
3494 config_filename
, mmap_os_err());
3495 ret
= CONFIG_INVALID_FILE
;
3502 if (chmod(get_lock_file_path(&lock
), st
.st_mode
& 07777) < 0) {
3503 error_errno(_("chmod on %s failed"), get_lock_file_path(&lock
));
3504 ret
= CONFIG_NO_WRITE
;
3508 if (store
.seen_nr
== 0) {
3509 if (!store
.seen_alloc
) {
3510 /* Did not see key nor section */
3511 ALLOC_GROW(store
.seen
, 1, store
.seen_alloc
);
3512 store
.seen
[0] = store
.parsed_nr
3513 - !!store
.parsed_nr
;
3518 for (i
= 0, copy_begin
= 0; i
< store
.seen_nr
; i
++) {
3520 int j
= store
.seen
[i
];
3523 if (!store
.key_seen
) {
3524 copy_end
= store
.parsed
[j
].end
;
3525 /* include '\n' when copying section header */
3526 if (copy_end
> 0 && copy_end
< contents_sz
&&
3527 contents
[copy_end
- 1] != '\n' &&
3528 contents
[copy_end
] == '\n')
3530 replace_end
= copy_end
;
3532 replace_end
= store
.parsed
[j
].end
;
3533 copy_end
= store
.parsed
[j
].begin
;
3535 maybe_remove_section(&store
,
3539 * Swallow preceding white-space on the same
3542 while (copy_end
> 0 ) {
3543 char c
= contents
[copy_end
- 1];
3545 if (isspace(c
) && c
!= '\n')
3552 if (copy_end
> 0 && contents
[copy_end
-1] != '\n')
3555 /* write the first part of the config */
3556 if (copy_end
> copy_begin
) {
3557 if (write_in_full(fd
, contents
+ copy_begin
,
3558 copy_end
- copy_begin
) < 0)
3561 write_str_in_full(fd
, "\n") < 0)
3564 copy_begin
= replace_end
;
3567 /* write the pair (value == NULL means unset) */
3569 if (!store
.section_seen
) {
3570 if (write_section(fd
, key
, &store
) < 0)
3573 if (write_pair(fd
, key
, value
, &store
) < 0)
3577 /* write the rest of the config */
3578 if (copy_begin
< contents_sz
)
3579 if (write_in_full(fd
, contents
+ copy_begin
,
3580 contents_sz
- copy_begin
) < 0)
3583 munmap(contents
, contents_sz
);
3587 if (commit_lock_file(&lock
) < 0) {
3588 error_errno(_("could not write config file %s"), config_filename
);
3589 ret
= CONFIG_NO_WRITE
;
3595 /* Invalidate the config cache */
3599 rollback_lock_file(&lock
);
3602 munmap(contents
, contents_sz
);
3605 config_store_data_clear(&store
);
3609 ret
= write_error(get_lock_file_path(&lock
));
3614 void git_config_set_multivar_in_file(const char *config_filename
,
3615 const char *key
, const char *value
,
3616 const char *value_pattern
, unsigned flags
)
3618 if (!git_config_set_multivar_in_file_gently(config_filename
, key
, value
,
3619 value_pattern
, flags
))
3622 die(_("could not set '%s' to '%s'"), key
, value
);
3624 die(_("could not unset '%s'"), key
);
3627 int git_config_set_multivar_gently(const char *key
, const char *value
,
3628 const char *value_pattern
, unsigned flags
)
3630 return repo_config_set_multivar_gently(the_repository
, key
, value
,
3631 value_pattern
, flags
);
3634 int repo_config_set_multivar_gently(struct repository
*r
, const char *key
,
3636 const char *value_pattern
, unsigned flags
)
3638 char *file
= repo_git_path(r
, "config");
3639 int res
= git_config_set_multivar_in_file_gently(file
,
3647 void git_config_set_multivar(const char *key
, const char *value
,
3648 const char *value_pattern
, unsigned flags
)
3650 git_config_set_multivar_in_file(git_path("config"),
3651 key
, value
, value_pattern
,
3655 static int section_name_match (const char *buf
, const char *name
)
3657 int i
= 0, j
= 0, dot
= 0;
3660 for (i
= 1; buf
[i
] && buf
[i
] != ']'; i
++) {
3661 if (!dot
&& isspace(buf
[i
])) {
3663 if (name
[j
++] != '.')
3665 for (i
++; isspace(buf
[i
]); i
++)
3671 if (buf
[i
] == '\\' && dot
)
3673 else if (buf
[i
] == '"' && dot
) {
3674 for (i
++; isspace(buf
[i
]); i
++)
3678 if (buf
[i
] != name
[j
++])
3681 if (buf
[i
] == ']' && name
[j
] == 0) {
3683 * We match, now just find the right length offset by
3684 * gobbling up any whitespace after it, as well
3687 for (; buf
[i
] && isspace(buf
[i
]); i
++)
3694 static int section_name_is_ok(const char *name
)
3696 /* Empty section names are bogus. */
3701 * Before a dot, we must be alphanumeric or dash. After the first dot,
3702 * anything goes, so we can stop checking.
3704 for (; *name
&& *name
!= '.'; name
++)
3705 if (*name
!= '-' && !isalnum(*name
))
3710 /* if new_name == NULL, the section is removed instead */
3711 static int git_config_copy_or_rename_section_in_file(const char *config_filename
,
3712 const char *old_name
,
3713 const char *new_name
, int copy
)
3715 int ret
= 0, remove
= 0;
3716 char *filename_buf
= NULL
;
3717 struct lock_file lock
= LOCK_INIT
;
3720 FILE *config_file
= NULL
;
3722 struct strbuf copystr
= STRBUF_INIT
;
3723 struct config_store_data store
;
3725 memset(&store
, 0, sizeof(store
));
3727 if (new_name
&& !section_name_is_ok(new_name
)) {
3728 ret
= error(_("invalid section name: %s"), new_name
);
3729 goto out_no_rollback
;
3732 if (!config_filename
)
3733 config_filename
= filename_buf
= git_pathdup("config");
3735 out_fd
= hold_lock_file_for_update(&lock
, config_filename
, 0);
3737 ret
= error(_("could not lock config file %s"), config_filename
);
3741 if (!(config_file
= fopen(config_filename
, "rb"))) {
3742 ret
= warn_on_fopen_errors(config_filename
);
3745 /* no config file means nothing to rename, no error */
3746 goto commit_and_out
;
3749 if (fstat(fileno(config_file
), &st
) == -1) {
3750 ret
= error_errno(_("fstat on %s failed"), config_filename
);
3754 if (chmod(get_lock_file_path(&lock
), st
.st_mode
& 07777) < 0) {
3755 ret
= error_errno(_("chmod on %s failed"),
3756 get_lock_file_path(&lock
));
3760 while (fgets(buf
, sizeof(buf
), config_file
)) {
3765 for (i
= 0; buf
[i
] && isspace(buf
[i
]); i
++)
3767 if (buf
[i
] == '[') {
3768 /* it's a section */
3773 * When encountering a new section under -c we
3774 * need to flush out any section we're already
3775 * coping and begin anew. There might be
3776 * multiple [branch "$name"] sections.
3778 if (copystr
.len
> 0) {
3779 if (write_in_full(out_fd
, copystr
.buf
, copystr
.len
) < 0) {
3780 ret
= write_error(get_lock_file_path(&lock
));
3783 strbuf_reset(©str
);
3786 offset
= section_name_match(&buf
[i
], old_name
);
3793 store
.baselen
= strlen(new_name
);
3795 if (write_section(out_fd
, new_name
, &store
) < 0) {
3796 ret
= write_error(get_lock_file_path(&lock
));
3800 * We wrote out the new section, with
3801 * a newline, now skip the old
3804 output
+= offset
+ i
;
3805 if (strlen(output
) > 0) {
3807 * More content means there's
3808 * a declaration to put on the
3809 * next line; indent with a
3816 copystr
= store_create_section(new_name
, &store
);
3823 length
= strlen(output
);
3825 if (!is_section
&& copystr
.len
> 0) {
3826 strbuf_add(©str
, output
, length
);
3829 if (write_in_full(out_fd
, output
, length
) < 0) {
3830 ret
= write_error(get_lock_file_path(&lock
));
3836 * Copy a trailing section at the end of the config, won't be
3837 * flushed by the usual "flush because we have a new section
3838 * logic in the loop above.
3840 if (copystr
.len
> 0) {
3841 if (write_in_full(out_fd
, copystr
.buf
, copystr
.len
) < 0) {
3842 ret
= write_error(get_lock_file_path(&lock
));
3845 strbuf_reset(©str
);
3848 fclose(config_file
);
3851 if (commit_lock_file(&lock
) < 0)
3852 ret
= error_errno(_("could not write config file %s"),
3856 fclose(config_file
);
3857 rollback_lock_file(&lock
);
3860 config_store_data_clear(&store
);
3864 int git_config_rename_section_in_file(const char *config_filename
,
3865 const char *old_name
, const char *new_name
)
3867 return git_config_copy_or_rename_section_in_file(config_filename
,
3868 old_name
, new_name
, 0);
3871 int git_config_rename_section(const char *old_name
, const char *new_name
)
3873 return git_config_rename_section_in_file(NULL
, old_name
, new_name
);
3876 int git_config_copy_section_in_file(const char *config_filename
,
3877 const char *old_name
, const char *new_name
)
3879 return git_config_copy_or_rename_section_in_file(config_filename
,
3880 old_name
, new_name
, 1);
3883 int git_config_copy_section(const char *old_name
, const char *new_name
)
3885 return git_config_copy_section_in_file(NULL
, old_name
, new_name
);
3889 * Call this to report error for your variable that should not
3890 * get a boolean value (i.e. "[my] var" means "true").
3892 #undef config_error_nonbool
3893 int config_error_nonbool(const char *var
)
3895 return error(_("missing value for '%s'"), var
);
3898 int parse_config_key(const char *var
,
3899 const char *section
,
3900 const char **subsection
, size_t *subsection_len
,
3905 /* Does it start with "section." ? */
3906 if (!skip_prefix(var
, section
, &var
) || *var
!= '.')
3910 * Find the key; we don't know yet if we have a subsection, but we must
3911 * parse backwards from the end, since the subsection may have dots in
3914 dot
= strrchr(var
, '.');
3917 /* Did we have a subsection at all? */
3921 *subsection_len
= 0;
3927 *subsection
= var
+ 1;
3928 *subsection_len
= dot
- *subsection
;
3934 static int reader_origin_type(struct config_reader
*reader
,
3935 enum config_origin_type
*type
)
3937 if (the_reader
.config_kvi
)
3938 *type
= reader
->config_kvi
->origin_type
;
3939 else if(the_reader
.source
)
3940 *type
= reader
->source
->origin_type
;
3946 const char *current_config_origin_type(void)
3948 enum config_origin_type type
= CONFIG_ORIGIN_UNKNOWN
;
3950 if (reader_origin_type(&the_reader
, &type
))
3951 BUG("current_config_origin_type called outside config callback");
3954 case CONFIG_ORIGIN_BLOB
:
3956 case CONFIG_ORIGIN_FILE
:
3958 case CONFIG_ORIGIN_STDIN
:
3959 return "standard input";
3960 case CONFIG_ORIGIN_SUBMODULE_BLOB
:
3961 return "submodule-blob";
3962 case CONFIG_ORIGIN_CMDLINE
:
3963 return "command line";
3965 BUG("unknown config origin type");
3969 const char *config_scope_name(enum config_scope scope
)
3972 case CONFIG_SCOPE_SYSTEM
:
3974 case CONFIG_SCOPE_GLOBAL
:
3976 case CONFIG_SCOPE_LOCAL
:
3978 case CONFIG_SCOPE_WORKTREE
:
3980 case CONFIG_SCOPE_COMMAND
:
3982 case CONFIG_SCOPE_SUBMODULE
:
3989 static int reader_config_name(struct config_reader
*reader
, const char **out
)
3991 if (the_reader
.config_kvi
)
3992 *out
= reader
->config_kvi
->filename
;
3993 else if (the_reader
.source
)
3994 *out
= reader
->source
->name
;
4000 const char *current_config_name(void)
4003 if (reader_config_name(&the_reader
, &name
))
4004 BUG("current_config_name called outside config callback");
4005 return name
? name
: "";
4008 enum config_scope
current_config_scope(void)
4010 if (the_reader
.config_kvi
)
4011 return the_reader
.config_kvi
->scope
;
4013 return the_reader
.parsing_scope
;
4016 int current_config_line(void)
4018 if (the_reader
.config_kvi
)
4019 return the_reader
.config_kvi
->linenr
;
4021 return the_reader
.source
->linenr
;
4024 int lookup_config(const char **mapping
, int nr_mapping
, const char *var
)
4028 for (i
= 0; i
< nr_mapping
; i
++) {
4029 const char *name
= mapping
[i
];
4031 if (name
&& !strcasecmp(var
, name
))