]> git.ipfire.org Git - thirdparty/git.git/blob - config.c
treewide: be explicit about dependence on strbuf.h
[thirdparty/git.git] / config.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
6 *
7 */
8 #include "cache.h"
9 #include "abspath.h"
10 #include "advice.h"
11 #include "alloc.h"
12 #include "date.h"
13 #include "branch.h"
14 #include "config.h"
15 #include "convert.h"
16 #include "environment.h"
17 #include "gettext.h"
18 #include "ident.h"
19 #include "repository.h"
20 #include "lockfile.h"
21 #include "mailmap.h"
22 #include "exec-cmd.h"
23 #include "strbuf.h"
24 #include "quote.h"
25 #include "hashmap.h"
26 #include "string-list.h"
27 #include "object-name.h"
28 #include "object-store.h"
29 #include "pager.h"
30 #include "utf8.h"
31 #include "dir.h"
32 #include "color.h"
33 #include "replace-object.h"
34 #include "refs.h"
35 #include "setup.h"
36 #include "trace2.h"
37 #include "worktree.h"
38 #include "wrapper.h"
39 #include "write-or-die.h"
40
41 struct config_source {
42 struct config_source *prev;
43 union {
44 FILE *file;
45 struct config_buf {
46 const char *buf;
47 size_t len;
48 size_t pos;
49 } buf;
50 } u;
51 enum config_origin_type origin_type;
52 const char *name;
53 const char *path;
54 enum config_error_action default_error_action;
55 int linenr;
56 int eof;
57 size_t total_len;
58 struct strbuf value;
59 struct strbuf var;
60 unsigned subsection_case_sensitive : 1;
61
62 int (*do_fgetc)(struct config_source *c);
63 int (*do_ungetc)(int c, struct config_source *conf);
64 long (*do_ftell)(struct config_source *c);
65 };
66
67 /*
68 * These variables record the "current" config source, which
69 * can be accessed by parsing callbacks.
70 *
71 * The "cf" variable will be non-NULL only when we are actually parsing a real
72 * config source (file, blob, cmdline, etc).
73 *
74 * The "current_config_kvi" variable will be non-NULL only when we are feeding
75 * cached config from a configset into a callback.
76 *
77 * They should generally never be non-NULL at the same time. If they are both
78 * NULL, then we aren't parsing anything (and depending on the function looking
79 * at the variables, it's either a bug for it to be called in the first place,
80 * or it's a function which can be reused for non-config purposes, and should
81 * fall back to some sane behavior).
82 */
83 static struct config_source *cf;
84 static struct key_value_info *current_config_kvi;
85
86 /*
87 * Similar to the variables above, this gives access to the "scope" of the
88 * current value (repo, global, etc). For cached values, it can be found via
89 * the current_config_kvi as above. During parsing, the current value can be
90 * found in this variable. It's not part of "cf" because it transcends a single
91 * file (i.e., a file included from .git/config is still in "repo" scope).
92 */
93 static enum config_scope current_parsing_scope;
94
95 static int pack_compression_seen;
96 static int zlib_compression_seen;
97
98 /*
99 * Config that comes from trusted scopes, namely:
100 * - CONFIG_SCOPE_SYSTEM (e.g. /etc/gitconfig)
101 * - CONFIG_SCOPE_GLOBAL (e.g. $HOME/.gitconfig, $XDG_CONFIG_HOME/git)
102 * - CONFIG_SCOPE_COMMAND (e.g. "-c" option, environment variables)
103 *
104 * This is declared here for code cleanliness, but unlike the other
105 * static variables, this does not hold config parser state.
106 */
107 static struct config_set protected_config;
108
109 static int config_file_fgetc(struct config_source *conf)
110 {
111 return getc_unlocked(conf->u.file);
112 }
113
114 static int config_file_ungetc(int c, struct config_source *conf)
115 {
116 return ungetc(c, conf->u.file);
117 }
118
119 static long config_file_ftell(struct config_source *conf)
120 {
121 return ftell(conf->u.file);
122 }
123
124
125 static int config_buf_fgetc(struct config_source *conf)
126 {
127 if (conf->u.buf.pos < conf->u.buf.len)
128 return conf->u.buf.buf[conf->u.buf.pos++];
129
130 return EOF;
131 }
132
133 static int config_buf_ungetc(int c, struct config_source *conf)
134 {
135 if (conf->u.buf.pos > 0) {
136 conf->u.buf.pos--;
137 if (conf->u.buf.buf[conf->u.buf.pos] != c)
138 BUG("config_buf can only ungetc the same character");
139 return c;
140 }
141
142 return EOF;
143 }
144
145 static long config_buf_ftell(struct config_source *conf)
146 {
147 return conf->u.buf.pos;
148 }
149
150 struct config_include_data {
151 int depth;
152 config_fn_t fn;
153 void *data;
154 const struct config_options *opts;
155 struct git_config_source *config_source;
156
157 /*
158 * All remote URLs discovered when reading all config files.
159 */
160 struct string_list *remote_urls;
161 };
162 #define CONFIG_INCLUDE_INIT { 0 }
163
164 static int git_config_include(const char *var, const char *value, void *data);
165
166 #define MAX_INCLUDE_DEPTH 10
167 static const char include_depth_advice[] = N_(
168 "exceeded maximum include depth (%d) while including\n"
169 " %s\n"
170 "from\n"
171 " %s\n"
172 "This might be due to circular includes.");
173 static int handle_path_include(const char *path, struct config_include_data *inc)
174 {
175 int ret = 0;
176 struct strbuf buf = STRBUF_INIT;
177 char *expanded;
178
179 if (!path)
180 return config_error_nonbool("include.path");
181
182 expanded = interpolate_path(path, 0);
183 if (!expanded)
184 return error(_("could not expand include path '%s'"), path);
185 path = expanded;
186
187 /*
188 * Use an absolute path as-is, but interpret relative paths
189 * based on the including config file.
190 */
191 if (!is_absolute_path(path)) {
192 char *slash;
193
194 if (!cf || !cf->path) {
195 ret = error(_("relative config includes must come from files"));
196 goto cleanup;
197 }
198
199 slash = find_last_dir_sep(cf->path);
200 if (slash)
201 strbuf_add(&buf, cf->path, slash - cf->path + 1);
202 strbuf_addstr(&buf, path);
203 path = buf.buf;
204 }
205
206 if (!access_or_die(path, R_OK, 0)) {
207 if (++inc->depth > MAX_INCLUDE_DEPTH)
208 die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path,
209 !cf ? "<unknown>" :
210 cf->name ? cf->name :
211 "the command line");
212 ret = git_config_from_file(git_config_include, path, inc);
213 inc->depth--;
214 }
215 cleanup:
216 strbuf_release(&buf);
217 free(expanded);
218 return ret;
219 }
220
221 static void add_trailing_starstar_for_dir(struct strbuf *pat)
222 {
223 if (pat->len && is_dir_sep(pat->buf[pat->len - 1]))
224 strbuf_addstr(pat, "**");
225 }
226
227 static int prepare_include_condition_pattern(struct strbuf *pat)
228 {
229 struct strbuf path = STRBUF_INIT;
230 char *expanded;
231 int prefix = 0;
232
233 expanded = interpolate_path(pat->buf, 1);
234 if (expanded) {
235 strbuf_reset(pat);
236 strbuf_addstr(pat, expanded);
237 free(expanded);
238 }
239
240 if (pat->buf[0] == '.' && is_dir_sep(pat->buf[1])) {
241 const char *slash;
242
243 if (!cf || !cf->path)
244 return error(_("relative config include "
245 "conditionals must come from files"));
246
247 strbuf_realpath(&path, cf->path, 1);
248 slash = find_last_dir_sep(path.buf);
249 if (!slash)
250 BUG("how is this possible?");
251 strbuf_splice(pat, 0, 1, path.buf, slash - path.buf);
252 prefix = slash - path.buf + 1 /* slash */;
253 } else if (!is_absolute_path(pat->buf))
254 strbuf_insertstr(pat, 0, "**/");
255
256 add_trailing_starstar_for_dir(pat);
257
258 strbuf_release(&path);
259 return prefix;
260 }
261
262 static int include_by_gitdir(const struct config_options *opts,
263 const char *cond, size_t cond_len, int icase)
264 {
265 struct strbuf text = STRBUF_INIT;
266 struct strbuf pattern = STRBUF_INIT;
267 int ret = 0, prefix;
268 const char *git_dir;
269 int already_tried_absolute = 0;
270
271 if (opts->git_dir)
272 git_dir = opts->git_dir;
273 else
274 goto done;
275
276 strbuf_realpath(&text, git_dir, 1);
277 strbuf_add(&pattern, cond, cond_len);
278 prefix = prepare_include_condition_pattern(&pattern);
279
280 again:
281 if (prefix < 0)
282 goto done;
283
284 if (prefix > 0) {
285 /*
286 * perform literal matching on the prefix part so that
287 * any wildcard character in it can't create side effects.
288 */
289 if (text.len < prefix)
290 goto done;
291 if (!icase && strncmp(pattern.buf, text.buf, prefix))
292 goto done;
293 if (icase && strncasecmp(pattern.buf, text.buf, prefix))
294 goto done;
295 }
296
297 ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
298 WM_PATHNAME | (icase ? WM_CASEFOLD : 0));
299
300 if (!ret && !already_tried_absolute) {
301 /*
302 * We've tried e.g. matching gitdir:~/work, but if
303 * ~/work is a symlink to /mnt/storage/work
304 * strbuf_realpath() will expand it, so the rule won't
305 * match. Let's match against a
306 * strbuf_add_absolute_path() version of the path,
307 * which'll do the right thing
308 */
309 strbuf_reset(&text);
310 strbuf_add_absolute_path(&text, git_dir);
311 already_tried_absolute = 1;
312 goto again;
313 }
314 done:
315 strbuf_release(&pattern);
316 strbuf_release(&text);
317 return ret;
318 }
319
320 static int include_by_branch(const char *cond, size_t cond_len)
321 {
322 int flags;
323 int ret;
324 struct strbuf pattern = STRBUF_INIT;
325 const char *refname = !the_repository->gitdir ?
326 NULL : resolve_ref_unsafe("HEAD", 0, NULL, &flags);
327 const char *shortname;
328
329 if (!refname || !(flags & REF_ISSYMREF) ||
330 !skip_prefix(refname, "refs/heads/", &shortname))
331 return 0;
332
333 strbuf_add(&pattern, cond, cond_len);
334 add_trailing_starstar_for_dir(&pattern);
335 ret = !wildmatch(pattern.buf, shortname, WM_PATHNAME);
336 strbuf_release(&pattern);
337 return ret;
338 }
339
340 static int add_remote_url(const char *var, const char *value, void *data)
341 {
342 struct string_list *remote_urls = data;
343 const char *remote_name;
344 size_t remote_name_len;
345 const char *key;
346
347 if (!parse_config_key(var, "remote", &remote_name, &remote_name_len,
348 &key) &&
349 remote_name &&
350 !strcmp(key, "url"))
351 string_list_append(remote_urls, value);
352 return 0;
353 }
354
355 static void populate_remote_urls(struct config_include_data *inc)
356 {
357 struct config_options opts;
358
359 struct config_source *store_cf = cf;
360 struct key_value_info *store_kvi = current_config_kvi;
361 enum config_scope store_scope = current_parsing_scope;
362
363 opts = *inc->opts;
364 opts.unconditional_remote_url = 1;
365
366 cf = NULL;
367 current_config_kvi = NULL;
368 current_parsing_scope = 0;
369
370 inc->remote_urls = xmalloc(sizeof(*inc->remote_urls));
371 string_list_init_dup(inc->remote_urls);
372 config_with_options(add_remote_url, inc->remote_urls, inc->config_source, &opts);
373
374 cf = store_cf;
375 current_config_kvi = store_kvi;
376 current_parsing_scope = store_scope;
377 }
378
379 static int forbid_remote_url(const char *var, const char *value UNUSED,
380 void *data UNUSED)
381 {
382 const char *remote_name;
383 size_t remote_name_len;
384 const char *key;
385
386 if (!parse_config_key(var, "remote", &remote_name, &remote_name_len,
387 &key) &&
388 remote_name &&
389 !strcmp(key, "url"))
390 die(_("remote URLs cannot be configured in file directly or indirectly included by includeIf.hasconfig:remote.*.url"));
391 return 0;
392 }
393
394 static int at_least_one_url_matches_glob(const char *glob, int glob_len,
395 struct string_list *remote_urls)
396 {
397 struct strbuf pattern = STRBUF_INIT;
398 struct string_list_item *url_item;
399 int found = 0;
400
401 strbuf_add(&pattern, glob, glob_len);
402 for_each_string_list_item(url_item, remote_urls) {
403 if (!wildmatch(pattern.buf, url_item->string, WM_PATHNAME)) {
404 found = 1;
405 break;
406 }
407 }
408 strbuf_release(&pattern);
409 return found;
410 }
411
412 static int include_by_remote_url(struct config_include_data *inc,
413 const char *cond, size_t cond_len)
414 {
415 if (inc->opts->unconditional_remote_url)
416 return 1;
417 if (!inc->remote_urls)
418 populate_remote_urls(inc);
419 return at_least_one_url_matches_glob(cond, cond_len,
420 inc->remote_urls);
421 }
422
423 static int include_condition_is_true(struct config_include_data *inc,
424 const char *cond, size_t cond_len)
425 {
426 const struct config_options *opts = inc->opts;
427
428 if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len))
429 return include_by_gitdir(opts, cond, cond_len, 0);
430 else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
431 return include_by_gitdir(opts, cond, cond_len, 1);
432 else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))
433 return include_by_branch(cond, cond_len);
434 else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond,
435 &cond_len))
436 return include_by_remote_url(inc, cond, cond_len);
437
438 /* unknown conditionals are always false */
439 return 0;
440 }
441
442 static int git_config_include(const char *var, const char *value, void *data)
443 {
444 struct config_include_data *inc = data;
445 const char *cond, *key;
446 size_t cond_len;
447 int ret;
448
449 /*
450 * Pass along all values, including "include" directives; this makes it
451 * possible to query information on the includes themselves.
452 */
453 ret = inc->fn(var, value, inc->data);
454 if (ret < 0)
455 return ret;
456
457 if (!strcmp(var, "include.path"))
458 ret = handle_path_include(value, inc);
459
460 if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
461 cond && include_condition_is_true(inc, cond, cond_len) &&
462 !strcmp(key, "path")) {
463 config_fn_t old_fn = inc->fn;
464
465 if (inc->opts->unconditional_remote_url)
466 inc->fn = forbid_remote_url;
467 ret = handle_path_include(value, inc);
468 inc->fn = old_fn;
469 }
470
471 return ret;
472 }
473
474 static void git_config_push_split_parameter(const char *key, const char *value)
475 {
476 struct strbuf env = STRBUF_INIT;
477 const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
478 if (old && *old) {
479 strbuf_addstr(&env, old);
480 strbuf_addch(&env, ' ');
481 }
482 sq_quote_buf(&env, key);
483 strbuf_addch(&env, '=');
484 if (value)
485 sq_quote_buf(&env, value);
486 setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
487 strbuf_release(&env);
488 }
489
490 void git_config_push_parameter(const char *text)
491 {
492 const char *value;
493
494 /*
495 * When we see:
496 *
497 * section.subsection=with=equals.key=value
498 *
499 * we cannot tell if it means:
500 *
501 * [section "subsection=with=equals"]
502 * key = value
503 *
504 * or:
505 *
506 * [section]
507 * subsection = with=equals.key=value
508 *
509 * We parse left-to-right for the first "=", meaning we'll prefer to
510 * keep the value intact over the subsection. This is historical, but
511 * also sensible since values are more likely to contain odd or
512 * untrusted input than a section name.
513 *
514 * A missing equals is explicitly allowed (as a bool-only entry).
515 */
516 value = strchr(text, '=');
517 if (value) {
518 char *key = xmemdupz(text, value - text);
519 git_config_push_split_parameter(key, value + 1);
520 free(key);
521 } else {
522 git_config_push_split_parameter(text, NULL);
523 }
524 }
525
526 void git_config_push_env(const char *spec)
527 {
528 char *key;
529 const char *env_name;
530 const char *env_value;
531
532 env_name = strrchr(spec, '=');
533 if (!env_name)
534 die(_("invalid config format: %s"), spec);
535 key = xmemdupz(spec, env_name - spec);
536 env_name++;
537 if (!*env_name)
538 die(_("missing environment variable name for configuration '%.*s'"),
539 (int)(env_name - spec - 1), spec);
540
541 env_value = getenv(env_name);
542 if (!env_value)
543 die(_("missing environment variable '%s' for configuration '%.*s'"),
544 env_name, (int)(env_name - spec - 1), spec);
545
546 git_config_push_split_parameter(key, env_value);
547 free(key);
548 }
549
550 static inline int iskeychar(int c)
551 {
552 return isalnum(c) || c == '-';
553 }
554
555 /*
556 * Auxiliary function to sanity-check and split the key into the section
557 * identifier and variable name.
558 *
559 * Returns 0 on success, -1 when there is an invalid character in the key and
560 * -2 if there is no section name in the key.
561 *
562 * store_key - pointer to char* which will hold a copy of the key with
563 * lowercase section and variable name
564 * baselen - pointer to size_t which will hold the length of the
565 * section + subsection part, can be NULL
566 */
567 int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
568 {
569 size_t i, baselen;
570 int dot;
571 const char *last_dot = strrchr(key, '.');
572
573 /*
574 * Since "key" actually contains the section name and the real
575 * key name separated by a dot, we have to know where the dot is.
576 */
577
578 if (last_dot == NULL || last_dot == key) {
579 error(_("key does not contain a section: %s"), key);
580 return -CONFIG_NO_SECTION_OR_NAME;
581 }
582
583 if (!last_dot[1]) {
584 error(_("key does not contain variable name: %s"), key);
585 return -CONFIG_NO_SECTION_OR_NAME;
586 }
587
588 baselen = last_dot - key;
589 if (baselen_)
590 *baselen_ = baselen;
591
592 /*
593 * Validate the key and while at it, lower case it for matching.
594 */
595 *store_key = xmallocz(strlen(key));
596
597 dot = 0;
598 for (i = 0; key[i]; i++) {
599 unsigned char c = key[i];
600 if (c == '.')
601 dot = 1;
602 /* Leave the extended basename untouched.. */
603 if (!dot || i > baselen) {
604 if (!iskeychar(c) ||
605 (i == baselen + 1 && !isalpha(c))) {
606 error(_("invalid key: %s"), key);
607 goto out_free_ret_1;
608 }
609 c = tolower(c);
610 } else if (c == '\n') {
611 error(_("invalid key (newline): %s"), key);
612 goto out_free_ret_1;
613 }
614 (*store_key)[i] = c;
615 }
616
617 return 0;
618
619 out_free_ret_1:
620 FREE_AND_NULL(*store_key);
621 return -CONFIG_INVALID_KEY;
622 }
623
624 static int config_parse_pair(const char *key, const char *value,
625 config_fn_t fn, void *data)
626 {
627 char *canonical_name;
628 int ret;
629
630 if (!strlen(key))
631 return error(_("empty config key"));
632 if (git_config_parse_key(key, &canonical_name, NULL))
633 return -1;
634
635 ret = (fn(canonical_name, value, data) < 0) ? -1 : 0;
636 free(canonical_name);
637 return ret;
638 }
639
640 int git_config_parse_parameter(const char *text,
641 config_fn_t fn, void *data)
642 {
643 const char *value;
644 struct strbuf **pair;
645 int ret;
646
647 pair = strbuf_split_str(text, '=', 2);
648 if (!pair[0])
649 return error(_("bogus config parameter: %s"), text);
650
651 if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') {
652 strbuf_setlen(pair[0], pair[0]->len - 1);
653 value = pair[1] ? pair[1]->buf : "";
654 } else {
655 value = NULL;
656 }
657
658 strbuf_trim(pair[0]);
659 if (!pair[0]->len) {
660 strbuf_list_free(pair);
661 return error(_("bogus config parameter: %s"), text);
662 }
663
664 ret = config_parse_pair(pair[0]->buf, value, fn, data);
665 strbuf_list_free(pair);
666 return ret;
667 }
668
669 static int parse_config_env_list(char *env, config_fn_t fn, void *data)
670 {
671 char *cur = env;
672 while (cur && *cur) {
673 const char *key = sq_dequote_step(cur, &cur);
674 if (!key)
675 return error(_("bogus format in %s"),
676 CONFIG_DATA_ENVIRONMENT);
677
678 if (!cur || isspace(*cur)) {
679 /* old-style 'key=value' */
680 if (git_config_parse_parameter(key, fn, data) < 0)
681 return -1;
682 }
683 else if (*cur == '=') {
684 /* new-style 'key'='value' */
685 const char *value;
686
687 cur++;
688 if (*cur == '\'') {
689 /* quoted value */
690 value = sq_dequote_step(cur, &cur);
691 if (!value || (cur && !isspace(*cur))) {
692 return error(_("bogus format in %s"),
693 CONFIG_DATA_ENVIRONMENT);
694 }
695 } else if (!*cur || isspace(*cur)) {
696 /* implicit bool: 'key'= */
697 value = NULL;
698 } else {
699 return error(_("bogus format in %s"),
700 CONFIG_DATA_ENVIRONMENT);
701 }
702
703 if (config_parse_pair(key, value, fn, data) < 0)
704 return -1;
705 }
706 else {
707 /* unknown format */
708 return error(_("bogus format in %s"),
709 CONFIG_DATA_ENVIRONMENT);
710 }
711
712 if (cur) {
713 while (isspace(*cur))
714 cur++;
715 }
716 }
717 return 0;
718 }
719
720 int git_config_from_parameters(config_fn_t fn, void *data)
721 {
722 const char *env;
723 struct strbuf envvar = STRBUF_INIT;
724 struct strvec to_free = STRVEC_INIT;
725 int ret = 0;
726 char *envw = NULL;
727 struct config_source source;
728
729 memset(&source, 0, sizeof(source));
730 source.prev = cf;
731 source.origin_type = CONFIG_ORIGIN_CMDLINE;
732 cf = &source;
733
734 env = getenv(CONFIG_COUNT_ENVIRONMENT);
735 if (env) {
736 unsigned long count;
737 char *endp;
738 int i;
739
740 count = strtoul(env, &endp, 10);
741 if (*endp) {
742 ret = error(_("bogus count in %s"), CONFIG_COUNT_ENVIRONMENT);
743 goto out;
744 }
745 if (count > INT_MAX) {
746 ret = error(_("too many entries in %s"), CONFIG_COUNT_ENVIRONMENT);
747 goto out;
748 }
749
750 for (i = 0; i < count; i++) {
751 const char *key, *value;
752
753 strbuf_addf(&envvar, "GIT_CONFIG_KEY_%d", i);
754 key = getenv_safe(&to_free, envvar.buf);
755 if (!key) {
756 ret = error(_("missing config key %s"), envvar.buf);
757 goto out;
758 }
759 strbuf_reset(&envvar);
760
761 strbuf_addf(&envvar, "GIT_CONFIG_VALUE_%d", i);
762 value = getenv_safe(&to_free, envvar.buf);
763 if (!value) {
764 ret = error(_("missing config value %s"), envvar.buf);
765 goto out;
766 }
767 strbuf_reset(&envvar);
768
769 if (config_parse_pair(key, value, fn, data) < 0) {
770 ret = -1;
771 goto out;
772 }
773 }
774 }
775
776 env = getenv(CONFIG_DATA_ENVIRONMENT);
777 if (env) {
778 /* sq_dequote will write over it */
779 envw = xstrdup(env);
780 if (parse_config_env_list(envw, fn, data) < 0) {
781 ret = -1;
782 goto out;
783 }
784 }
785
786 out:
787 strbuf_release(&envvar);
788 strvec_clear(&to_free);
789 free(envw);
790 cf = source.prev;
791 return ret;
792 }
793
794 static int get_next_char(void)
795 {
796 int c = cf->do_fgetc(cf);
797
798 if (c == '\r') {
799 /* DOS like systems */
800 c = cf->do_fgetc(cf);
801 if (c != '\n') {
802 if (c != EOF)
803 cf->do_ungetc(c, cf);
804 c = '\r';
805 }
806 }
807
808 if (c != EOF && ++cf->total_len > INT_MAX) {
809 /*
810 * This is an absurdly long config file; refuse to parse
811 * further in order to protect downstream code from integer
812 * overflows. Note that we can't return an error specifically,
813 * but we can mark EOF and put trash in the return value,
814 * which will trigger a parse error.
815 */
816 cf->eof = 1;
817 return 0;
818 }
819
820 if (c == '\n')
821 cf->linenr++;
822 if (c == EOF) {
823 cf->eof = 1;
824 cf->linenr++;
825 c = '\n';
826 }
827 return c;
828 }
829
830 static char *parse_value(void)
831 {
832 int quote = 0, comment = 0, space = 0;
833
834 strbuf_reset(&cf->value);
835 for (;;) {
836 int c = get_next_char();
837 if (c == '\n') {
838 if (quote) {
839 cf->linenr--;
840 return NULL;
841 }
842 return cf->value.buf;
843 }
844 if (comment)
845 continue;
846 if (isspace(c) && !quote) {
847 if (cf->value.len)
848 space++;
849 continue;
850 }
851 if (!quote) {
852 if (c == ';' || c == '#') {
853 comment = 1;
854 continue;
855 }
856 }
857 for (; space; space--)
858 strbuf_addch(&cf->value, ' ');
859 if (c == '\\') {
860 c = get_next_char();
861 switch (c) {
862 case '\n':
863 continue;
864 case 't':
865 c = '\t';
866 break;
867 case 'b':
868 c = '\b';
869 break;
870 case 'n':
871 c = '\n';
872 break;
873 /* Some characters escape as themselves */
874 case '\\': case '"':
875 break;
876 /* Reject unknown escape sequences */
877 default:
878 return NULL;
879 }
880 strbuf_addch(&cf->value, c);
881 continue;
882 }
883 if (c == '"') {
884 quote = 1-quote;
885 continue;
886 }
887 strbuf_addch(&cf->value, c);
888 }
889 }
890
891 static int get_value(config_fn_t fn, void *data, struct strbuf *name)
892 {
893 int c;
894 char *value;
895 int ret;
896
897 /* Get the full name */
898 for (;;) {
899 c = get_next_char();
900 if (cf->eof)
901 break;
902 if (!iskeychar(c))
903 break;
904 strbuf_addch(name, tolower(c));
905 }
906
907 while (c == ' ' || c == '\t')
908 c = get_next_char();
909
910 value = NULL;
911 if (c != '\n') {
912 if (c != '=')
913 return -1;
914 value = parse_value();
915 if (!value)
916 return -1;
917 }
918 /*
919 * We already consumed the \n, but we need linenr to point to
920 * the line we just parsed during the call to fn to get
921 * accurate line number in error messages.
922 */
923 cf->linenr--;
924 ret = fn(name->buf, value, data);
925 if (ret >= 0)
926 cf->linenr++;
927 return ret;
928 }
929
930 static int get_extended_base_var(struct strbuf *name, int c)
931 {
932 cf->subsection_case_sensitive = 0;
933 do {
934 if (c == '\n')
935 goto error_incomplete_line;
936 c = get_next_char();
937 } while (isspace(c));
938
939 /* We require the format to be '[base "extension"]' */
940 if (c != '"')
941 return -1;
942 strbuf_addch(name, '.');
943
944 for (;;) {
945 int c = get_next_char();
946 if (c == '\n')
947 goto error_incomplete_line;
948 if (c == '"')
949 break;
950 if (c == '\\') {
951 c = get_next_char();
952 if (c == '\n')
953 goto error_incomplete_line;
954 }
955 strbuf_addch(name, c);
956 }
957
958 /* Final ']' */
959 if (get_next_char() != ']')
960 return -1;
961 return 0;
962 error_incomplete_line:
963 cf->linenr--;
964 return -1;
965 }
966
967 static int get_base_var(struct strbuf *name)
968 {
969 cf->subsection_case_sensitive = 1;
970 for (;;) {
971 int c = get_next_char();
972 if (cf->eof)
973 return -1;
974 if (c == ']')
975 return 0;
976 if (isspace(c))
977 return get_extended_base_var(name, c);
978 if (!iskeychar(c) && c != '.')
979 return -1;
980 strbuf_addch(name, tolower(c));
981 }
982 }
983
984 struct parse_event_data {
985 enum config_event_t previous_type;
986 size_t previous_offset;
987 const struct config_options *opts;
988 };
989
990 static int do_event(enum config_event_t type, struct parse_event_data *data)
991 {
992 size_t offset;
993
994 if (!data->opts || !data->opts->event_fn)
995 return 0;
996
997 if (type == CONFIG_EVENT_WHITESPACE &&
998 data->previous_type == type)
999 return 0;
1000
1001 offset = cf->do_ftell(cf);
1002 /*
1003 * At EOF, the parser always "inserts" an extra '\n', therefore
1004 * the end offset of the event is the current file position, otherwise
1005 * we will already have advanced to the next event.
1006 */
1007 if (type != CONFIG_EVENT_EOF)
1008 offset--;
1009
1010 if (data->previous_type != CONFIG_EVENT_EOF &&
1011 data->opts->event_fn(data->previous_type, data->previous_offset,
1012 offset, data->opts->event_fn_data) < 0)
1013 return -1;
1014
1015 data->previous_type = type;
1016 data->previous_offset = offset;
1017
1018 return 0;
1019 }
1020
1021 static int git_parse_source(config_fn_t fn, void *data,
1022 const struct config_options *opts)
1023 {
1024 int comment = 0;
1025 size_t baselen = 0;
1026 struct strbuf *var = &cf->var;
1027 int error_return = 0;
1028 char *error_msg = NULL;
1029
1030 /* U+FEFF Byte Order Mark in UTF8 */
1031 const char *bomptr = utf8_bom;
1032
1033 /* For the parser event callback */
1034 struct parse_event_data event_data = {
1035 CONFIG_EVENT_EOF, 0, opts
1036 };
1037
1038 for (;;) {
1039 int c;
1040
1041 c = get_next_char();
1042 if (bomptr && *bomptr) {
1043 /* We are at the file beginning; skip UTF8-encoded BOM
1044 * if present. Sane editors won't put this in on their
1045 * own, but e.g. Windows Notepad will do it happily. */
1046 if (c == (*bomptr & 0377)) {
1047 bomptr++;
1048 continue;
1049 } else {
1050 /* Do not tolerate partial BOM. */
1051 if (bomptr != utf8_bom)
1052 break;
1053 /* No BOM at file beginning. Cool. */
1054 bomptr = NULL;
1055 }
1056 }
1057 if (c == '\n') {
1058 if (cf->eof) {
1059 if (do_event(CONFIG_EVENT_EOF, &event_data) < 0)
1060 return -1;
1061 return 0;
1062 }
1063 if (do_event(CONFIG_EVENT_WHITESPACE, &event_data) < 0)
1064 return -1;
1065 comment = 0;
1066 continue;
1067 }
1068 if (comment)
1069 continue;
1070 if (isspace(c)) {
1071 if (do_event(CONFIG_EVENT_WHITESPACE, &event_data) < 0)
1072 return -1;
1073 continue;
1074 }
1075 if (c == '#' || c == ';') {
1076 if (do_event(CONFIG_EVENT_COMMENT, &event_data) < 0)
1077 return -1;
1078 comment = 1;
1079 continue;
1080 }
1081 if (c == '[') {
1082 if (do_event(CONFIG_EVENT_SECTION, &event_data) < 0)
1083 return -1;
1084
1085 /* Reset prior to determining a new stem */
1086 strbuf_reset(var);
1087 if (get_base_var(var) < 0 || var->len < 1)
1088 break;
1089 strbuf_addch(var, '.');
1090 baselen = var->len;
1091 continue;
1092 }
1093 if (!isalpha(c))
1094 break;
1095
1096 if (do_event(CONFIG_EVENT_ENTRY, &event_data) < 0)
1097 return -1;
1098
1099 /*
1100 * Truncate the var name back to the section header
1101 * stem prior to grabbing the suffix part of the name
1102 * and the value.
1103 */
1104 strbuf_setlen(var, baselen);
1105 strbuf_addch(var, tolower(c));
1106 if (get_value(fn, data, var) < 0)
1107 break;
1108 }
1109
1110 if (do_event(CONFIG_EVENT_ERROR, &event_data) < 0)
1111 return -1;
1112
1113 switch (cf->origin_type) {
1114 case CONFIG_ORIGIN_BLOB:
1115 error_msg = xstrfmt(_("bad config line %d in blob %s"),
1116 cf->linenr, cf->name);
1117 break;
1118 case CONFIG_ORIGIN_FILE:
1119 error_msg = xstrfmt(_("bad config line %d in file %s"),
1120 cf->linenr, cf->name);
1121 break;
1122 case CONFIG_ORIGIN_STDIN:
1123 error_msg = xstrfmt(_("bad config line %d in standard input"),
1124 cf->linenr);
1125 break;
1126 case CONFIG_ORIGIN_SUBMODULE_BLOB:
1127 error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"),
1128 cf->linenr, cf->name);
1129 break;
1130 case CONFIG_ORIGIN_CMDLINE:
1131 error_msg = xstrfmt(_("bad config line %d in command line %s"),
1132 cf->linenr, cf->name);
1133 break;
1134 default:
1135 error_msg = xstrfmt(_("bad config line %d in %s"),
1136 cf->linenr, cf->name);
1137 }
1138
1139 switch (opts && opts->error_action ?
1140 opts->error_action :
1141 cf->default_error_action) {
1142 case CONFIG_ERROR_DIE:
1143 die("%s", error_msg);
1144 break;
1145 case CONFIG_ERROR_ERROR:
1146 error_return = error("%s", error_msg);
1147 break;
1148 case CONFIG_ERROR_SILENT:
1149 error_return = -1;
1150 break;
1151 case CONFIG_ERROR_UNSET:
1152 BUG("config error action unset");
1153 }
1154
1155 free(error_msg);
1156 return error_return;
1157 }
1158
1159 static uintmax_t get_unit_factor(const char *end)
1160 {
1161 if (!*end)
1162 return 1;
1163 else if (!strcasecmp(end, "k"))
1164 return 1024;
1165 else if (!strcasecmp(end, "m"))
1166 return 1024 * 1024;
1167 else if (!strcasecmp(end, "g"))
1168 return 1024 * 1024 * 1024;
1169 return 0;
1170 }
1171
1172 static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
1173 {
1174 if (value && *value) {
1175 char *end;
1176 intmax_t val;
1177 intmax_t factor;
1178
1179 if (max < 0)
1180 BUG("max must be a positive integer");
1181
1182 errno = 0;
1183 val = strtoimax(value, &end, 0);
1184 if (errno == ERANGE)
1185 return 0;
1186 if (end == value) {
1187 errno = EINVAL;
1188 return 0;
1189 }
1190 factor = get_unit_factor(end);
1191 if (!factor) {
1192 errno = EINVAL;
1193 return 0;
1194 }
1195 if ((val < 0 && -max / factor > val) ||
1196 (val > 0 && max / factor < val)) {
1197 errno = ERANGE;
1198 return 0;
1199 }
1200 val *= factor;
1201 *ret = val;
1202 return 1;
1203 }
1204 errno = EINVAL;
1205 return 0;
1206 }
1207
1208 static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
1209 {
1210 if (value && *value) {
1211 char *end;
1212 uintmax_t val;
1213 uintmax_t factor;
1214
1215 /* negative values would be accepted by strtoumax */
1216 if (strchr(value, '-')) {
1217 errno = EINVAL;
1218 return 0;
1219 }
1220 errno = 0;
1221 val = strtoumax(value, &end, 0);
1222 if (errno == ERANGE)
1223 return 0;
1224 if (end == value) {
1225 errno = EINVAL;
1226 return 0;
1227 }
1228 factor = get_unit_factor(end);
1229 if (!factor) {
1230 errno = EINVAL;
1231 return 0;
1232 }
1233 if (unsigned_mult_overflows(factor, val) ||
1234 factor * val > max) {
1235 errno = ERANGE;
1236 return 0;
1237 }
1238 val *= factor;
1239 *ret = val;
1240 return 1;
1241 }
1242 errno = EINVAL;
1243 return 0;
1244 }
1245
1246 int git_parse_int(const char *value, int *ret)
1247 {
1248 intmax_t tmp;
1249 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
1250 return 0;
1251 *ret = tmp;
1252 return 1;
1253 }
1254
1255 static int git_parse_int64(const char *value, int64_t *ret)
1256 {
1257 intmax_t tmp;
1258 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t)))
1259 return 0;
1260 *ret = tmp;
1261 return 1;
1262 }
1263
1264 int git_parse_ulong(const char *value, unsigned long *ret)
1265 {
1266 uintmax_t tmp;
1267 if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long)))
1268 return 0;
1269 *ret = tmp;
1270 return 1;
1271 }
1272
1273 int git_parse_ssize_t(const char *value, ssize_t *ret)
1274 {
1275 intmax_t tmp;
1276 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(ssize_t)))
1277 return 0;
1278 *ret = tmp;
1279 return 1;
1280 }
1281
1282 NORETURN
1283 static void die_bad_number(const char *name, const char *value)
1284 {
1285 const char *error_type = (errno == ERANGE) ?
1286 N_("out of range") : N_("invalid unit");
1287 const char *bad_numeric = N_("bad numeric config value '%s' for '%s': %s");
1288
1289 if (!value)
1290 value = "";
1291
1292 if (!(cf && cf->name))
1293 die(_(bad_numeric), value, name, _(error_type));
1294
1295 switch (cf->origin_type) {
1296 case CONFIG_ORIGIN_BLOB:
1297 die(_("bad numeric config value '%s' for '%s' in blob %s: %s"),
1298 value, name, cf->name, _(error_type));
1299 case CONFIG_ORIGIN_FILE:
1300 die(_("bad numeric config value '%s' for '%s' in file %s: %s"),
1301 value, name, cf->name, _(error_type));
1302 case CONFIG_ORIGIN_STDIN:
1303 die(_("bad numeric config value '%s' for '%s' in standard input: %s"),
1304 value, name, _(error_type));
1305 case CONFIG_ORIGIN_SUBMODULE_BLOB:
1306 die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"),
1307 value, name, cf->name, _(error_type));
1308 case CONFIG_ORIGIN_CMDLINE:
1309 die(_("bad numeric config value '%s' for '%s' in command line %s: %s"),
1310 value, name, cf->name, _(error_type));
1311 default:
1312 die(_("bad numeric config value '%s' for '%s' in %s: %s"),
1313 value, name, cf->name, _(error_type));
1314 }
1315 }
1316
1317 int git_config_int(const char *name, const char *value)
1318 {
1319 int ret;
1320 if (!git_parse_int(value, &ret))
1321 die_bad_number(name, value);
1322 return ret;
1323 }
1324
1325 int64_t git_config_int64(const char *name, const char *value)
1326 {
1327 int64_t ret;
1328 if (!git_parse_int64(value, &ret))
1329 die_bad_number(name, value);
1330 return ret;
1331 }
1332
1333 unsigned long git_config_ulong(const char *name, const char *value)
1334 {
1335 unsigned long ret;
1336 if (!git_parse_ulong(value, &ret))
1337 die_bad_number(name, value);
1338 return ret;
1339 }
1340
1341 ssize_t git_config_ssize_t(const char *name, const char *value)
1342 {
1343 ssize_t ret;
1344 if (!git_parse_ssize_t(value, &ret))
1345 die_bad_number(name, value);
1346 return ret;
1347 }
1348
1349 static int git_parse_maybe_bool_text(const char *value)
1350 {
1351 if (!value)
1352 return 1;
1353 if (!*value)
1354 return 0;
1355 if (!strcasecmp(value, "true")
1356 || !strcasecmp(value, "yes")
1357 || !strcasecmp(value, "on"))
1358 return 1;
1359 if (!strcasecmp(value, "false")
1360 || !strcasecmp(value, "no")
1361 || !strcasecmp(value, "off"))
1362 return 0;
1363 return -1;
1364 }
1365
1366 static const struct fsync_component_name {
1367 const char *name;
1368 enum fsync_component component_bits;
1369 } fsync_component_names[] = {
1370 { "loose-object", FSYNC_COMPONENT_LOOSE_OBJECT },
1371 { "pack", FSYNC_COMPONENT_PACK },
1372 { "pack-metadata", FSYNC_COMPONENT_PACK_METADATA },
1373 { "commit-graph", FSYNC_COMPONENT_COMMIT_GRAPH },
1374 { "index", FSYNC_COMPONENT_INDEX },
1375 { "objects", FSYNC_COMPONENTS_OBJECTS },
1376 { "reference", FSYNC_COMPONENT_REFERENCE },
1377 { "derived-metadata", FSYNC_COMPONENTS_DERIVED_METADATA },
1378 { "committed", FSYNC_COMPONENTS_COMMITTED },
1379 { "added", FSYNC_COMPONENTS_ADDED },
1380 { "all", FSYNC_COMPONENTS_ALL },
1381 };
1382
1383 static enum fsync_component parse_fsync_components(const char *var, const char *string)
1384 {
1385 enum fsync_component current = FSYNC_COMPONENTS_PLATFORM_DEFAULT;
1386 enum fsync_component positive = 0, negative = 0;
1387
1388 while (string) {
1389 int i;
1390 size_t len;
1391 const char *ep;
1392 int negated = 0;
1393 int found = 0;
1394
1395 string = string + strspn(string, ", \t\n\r");
1396 ep = strchrnul(string, ',');
1397 len = ep - string;
1398 if (!strcmp(string, "none")) {
1399 current = FSYNC_COMPONENT_NONE;
1400 goto next_name;
1401 }
1402
1403 if (*string == '-') {
1404 negated = 1;
1405 string++;
1406 len--;
1407 if (!len)
1408 warning(_("invalid value for variable %s"), var);
1409 }
1410
1411 if (!len)
1412 break;
1413
1414 for (i = 0; i < ARRAY_SIZE(fsync_component_names); ++i) {
1415 const struct fsync_component_name *n = &fsync_component_names[i];
1416
1417 if (strncmp(n->name, string, len))
1418 continue;
1419
1420 found = 1;
1421 if (negated)
1422 negative |= n->component_bits;
1423 else
1424 positive |= n->component_bits;
1425 }
1426
1427 if (!found) {
1428 char *component = xstrndup(string, len);
1429 warning(_("ignoring unknown core.fsync component '%s'"), component);
1430 free(component);
1431 }
1432
1433 next_name:
1434 string = ep;
1435 }
1436
1437 return (current & ~negative) | positive;
1438 }
1439
1440 int git_parse_maybe_bool(const char *value)
1441 {
1442 int v = git_parse_maybe_bool_text(value);
1443 if (0 <= v)
1444 return v;
1445 if (git_parse_int(value, &v))
1446 return !!v;
1447 return -1;
1448 }
1449
1450 int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
1451 {
1452 int v = git_parse_maybe_bool_text(value);
1453 if (0 <= v) {
1454 *is_bool = 1;
1455 return v;
1456 }
1457 *is_bool = 0;
1458 return git_config_int(name, value);
1459 }
1460
1461 int git_config_bool(const char *name, const char *value)
1462 {
1463 int v = git_parse_maybe_bool(value);
1464 if (v < 0)
1465 die(_("bad boolean config value '%s' for '%s'"), value, name);
1466 return v;
1467 }
1468
1469 int git_config_string(const char **dest, const char *var, const char *value)
1470 {
1471 if (!value)
1472 return config_error_nonbool(var);
1473 *dest = xstrdup(value);
1474 return 0;
1475 }
1476
1477 int git_config_pathname(const char **dest, const char *var, const char *value)
1478 {
1479 if (!value)
1480 return config_error_nonbool(var);
1481 *dest = interpolate_path(value, 0);
1482 if (!*dest)
1483 die(_("failed to expand user dir in: '%s'"), value);
1484 return 0;
1485 }
1486
1487 int git_config_expiry_date(timestamp_t *timestamp, const char *var, const char *value)
1488 {
1489 if (!value)
1490 return config_error_nonbool(var);
1491 if (parse_expiry_date(value, timestamp))
1492 return error(_("'%s' for '%s' is not a valid timestamp"),
1493 value, var);
1494 return 0;
1495 }
1496
1497 int git_config_color(char *dest, const char *var, const char *value)
1498 {
1499 if (!value)
1500 return config_error_nonbool(var);
1501 if (color_parse(value, dest) < 0)
1502 return -1;
1503 return 0;
1504 }
1505
1506 static int git_default_core_config(const char *var, const char *value, void *cb)
1507 {
1508 /* This needs a better name */
1509 if (!strcmp(var, "core.filemode")) {
1510 trust_executable_bit = git_config_bool(var, value);
1511 return 0;
1512 }
1513 if (!strcmp(var, "core.trustctime")) {
1514 trust_ctime = git_config_bool(var, value);
1515 return 0;
1516 }
1517 if (!strcmp(var, "core.checkstat")) {
1518 if (!strcasecmp(value, "default"))
1519 check_stat = 1;
1520 else if (!strcasecmp(value, "minimal"))
1521 check_stat = 0;
1522 }
1523
1524 if (!strcmp(var, "core.quotepath")) {
1525 quote_path_fully = git_config_bool(var, value);
1526 return 0;
1527 }
1528
1529 if (!strcmp(var, "core.symlinks")) {
1530 has_symlinks = git_config_bool(var, value);
1531 return 0;
1532 }
1533
1534 if (!strcmp(var, "core.ignorecase")) {
1535 ignore_case = git_config_bool(var, value);
1536 return 0;
1537 }
1538
1539 if (!strcmp(var, "core.attributesfile"))
1540 return git_config_pathname(&git_attributes_file, var, value);
1541
1542 if (!strcmp(var, "core.hookspath"))
1543 return git_config_pathname(&git_hooks_path, var, value);
1544
1545 if (!strcmp(var, "core.bare")) {
1546 is_bare_repository_cfg = git_config_bool(var, value);
1547 return 0;
1548 }
1549
1550 if (!strcmp(var, "core.ignorestat")) {
1551 assume_unchanged = git_config_bool(var, value);
1552 return 0;
1553 }
1554
1555 if (!strcmp(var, "core.prefersymlinkrefs")) {
1556 prefer_symlink_refs = git_config_bool(var, value);
1557 return 0;
1558 }
1559
1560 if (!strcmp(var, "core.logallrefupdates")) {
1561 if (value && !strcasecmp(value, "always"))
1562 log_all_ref_updates = LOG_REFS_ALWAYS;
1563 else if (git_config_bool(var, value))
1564 log_all_ref_updates = LOG_REFS_NORMAL;
1565 else
1566 log_all_ref_updates = LOG_REFS_NONE;
1567 return 0;
1568 }
1569
1570 if (!strcmp(var, "core.warnambiguousrefs")) {
1571 warn_ambiguous_refs = git_config_bool(var, value);
1572 return 0;
1573 }
1574
1575 if (!strcmp(var, "core.abbrev")) {
1576 if (!value)
1577 return config_error_nonbool(var);
1578 if (!strcasecmp(value, "auto"))
1579 default_abbrev = -1;
1580 else if (!git_parse_maybe_bool_text(value))
1581 default_abbrev = the_hash_algo->hexsz;
1582 else {
1583 int abbrev = git_config_int(var, value);
1584 if (abbrev < minimum_abbrev || abbrev > the_hash_algo->hexsz)
1585 return error(_("abbrev length out of range: %d"), abbrev);
1586 default_abbrev = abbrev;
1587 }
1588 return 0;
1589 }
1590
1591 if (!strcmp(var, "core.disambiguate"))
1592 return set_disambiguate_hint_config(var, value);
1593
1594 if (!strcmp(var, "core.loosecompression")) {
1595 int level = git_config_int(var, value);
1596 if (level == -1)
1597 level = Z_DEFAULT_COMPRESSION;
1598 else if (level < 0 || level > Z_BEST_COMPRESSION)
1599 die(_("bad zlib compression level %d"), level);
1600 zlib_compression_level = level;
1601 zlib_compression_seen = 1;
1602 return 0;
1603 }
1604
1605 if (!strcmp(var, "core.compression")) {
1606 int level = git_config_int(var, value);
1607 if (level == -1)
1608 level = Z_DEFAULT_COMPRESSION;
1609 else if (level < 0 || level > Z_BEST_COMPRESSION)
1610 die(_("bad zlib compression level %d"), level);
1611 if (!zlib_compression_seen)
1612 zlib_compression_level = level;
1613 if (!pack_compression_seen)
1614 pack_compression_level = level;
1615 return 0;
1616 }
1617
1618 if (!strcmp(var, "core.packedgitwindowsize")) {
1619 int pgsz_x2 = getpagesize() * 2;
1620 packed_git_window_size = git_config_ulong(var, value);
1621
1622 /* This value must be multiple of (pagesize * 2) */
1623 packed_git_window_size /= pgsz_x2;
1624 if (packed_git_window_size < 1)
1625 packed_git_window_size = 1;
1626 packed_git_window_size *= pgsz_x2;
1627 return 0;
1628 }
1629
1630 if (!strcmp(var, "core.bigfilethreshold")) {
1631 big_file_threshold = git_config_ulong(var, value);
1632 return 0;
1633 }
1634
1635 if (!strcmp(var, "core.packedgitlimit")) {
1636 packed_git_limit = git_config_ulong(var, value);
1637 return 0;
1638 }
1639
1640 if (!strcmp(var, "core.deltabasecachelimit")) {
1641 delta_base_cache_limit = git_config_ulong(var, value);
1642 return 0;
1643 }
1644
1645 if (!strcmp(var, "core.autocrlf")) {
1646 if (value && !strcasecmp(value, "input")) {
1647 auto_crlf = AUTO_CRLF_INPUT;
1648 return 0;
1649 }
1650 auto_crlf = git_config_bool(var, value);
1651 return 0;
1652 }
1653
1654 if (!strcmp(var, "core.safecrlf")) {
1655 int eol_rndtrp_die;
1656 if (value && !strcasecmp(value, "warn")) {
1657 global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
1658 return 0;
1659 }
1660 eol_rndtrp_die = git_config_bool(var, value);
1661 global_conv_flags_eol = eol_rndtrp_die ?
1662 CONV_EOL_RNDTRP_DIE : 0;
1663 return 0;
1664 }
1665
1666 if (!strcmp(var, "core.eol")) {
1667 if (value && !strcasecmp(value, "lf"))
1668 core_eol = EOL_LF;
1669 else if (value && !strcasecmp(value, "crlf"))
1670 core_eol = EOL_CRLF;
1671 else if (value && !strcasecmp(value, "native"))
1672 core_eol = EOL_NATIVE;
1673 else
1674 core_eol = EOL_UNSET;
1675 return 0;
1676 }
1677
1678 if (!strcmp(var, "core.checkroundtripencoding")) {
1679 check_roundtrip_encoding = xstrdup(value);
1680 return 0;
1681 }
1682
1683 if (!strcmp(var, "core.notesref")) {
1684 notes_ref_name = xstrdup(value);
1685 return 0;
1686 }
1687
1688 if (!strcmp(var, "core.editor"))
1689 return git_config_string(&editor_program, var, value);
1690
1691 if (!strcmp(var, "core.commentchar")) {
1692 if (!value)
1693 return config_error_nonbool(var);
1694 else if (!strcasecmp(value, "auto"))
1695 auto_comment_line_char = 1;
1696 else if (value[0] && !value[1]) {
1697 comment_line_char = value[0];
1698 auto_comment_line_char = 0;
1699 } else
1700 return error(_("core.commentChar should only be one character"));
1701 return 0;
1702 }
1703
1704 if (!strcmp(var, "core.askpass"))
1705 return git_config_string(&askpass_program, var, value);
1706
1707 if (!strcmp(var, "core.excludesfile"))
1708 return git_config_pathname(&excludes_file, var, value);
1709
1710 if (!strcmp(var, "core.whitespace")) {
1711 if (!value)
1712 return config_error_nonbool(var);
1713 whitespace_rule_cfg = parse_whitespace_rule(value);
1714 return 0;
1715 }
1716
1717 if (!strcmp(var, "core.fsync")) {
1718 if (!value)
1719 return config_error_nonbool(var);
1720 fsync_components = parse_fsync_components(var, value);
1721 return 0;
1722 }
1723
1724 if (!strcmp(var, "core.fsyncmethod")) {
1725 if (!value)
1726 return config_error_nonbool(var);
1727 if (!strcmp(value, "fsync"))
1728 fsync_method = FSYNC_METHOD_FSYNC;
1729 else if (!strcmp(value, "writeout-only"))
1730 fsync_method = FSYNC_METHOD_WRITEOUT_ONLY;
1731 else if (!strcmp(value, "batch"))
1732 fsync_method = FSYNC_METHOD_BATCH;
1733 else
1734 warning(_("ignoring unknown core.fsyncMethod value '%s'"), value);
1735
1736 }
1737
1738 if (!strcmp(var, "core.fsyncobjectfiles")) {
1739 if (fsync_object_files < 0)
1740 warning(_("core.fsyncObjectFiles is deprecated; use core.fsync instead"));
1741 fsync_object_files = git_config_bool(var, value);
1742 return 0;
1743 }
1744
1745 if (!strcmp(var, "core.preloadindex")) {
1746 core_preload_index = git_config_bool(var, value);
1747 return 0;
1748 }
1749
1750 if (!strcmp(var, "core.createobject")) {
1751 if (!strcmp(value, "rename"))
1752 object_creation_mode = OBJECT_CREATION_USES_RENAMES;
1753 else if (!strcmp(value, "link"))
1754 object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
1755 else
1756 die(_("invalid mode for object creation: %s"), value);
1757 return 0;
1758 }
1759
1760 if (!strcmp(var, "core.sparsecheckout")) {
1761 core_apply_sparse_checkout = git_config_bool(var, value);
1762 return 0;
1763 }
1764
1765 if (!strcmp(var, "core.sparsecheckoutcone")) {
1766 core_sparse_checkout_cone = git_config_bool(var, value);
1767 return 0;
1768 }
1769
1770 if (!strcmp(var, "core.precomposeunicode")) {
1771 precomposed_unicode = git_config_bool(var, value);
1772 return 0;
1773 }
1774
1775 if (!strcmp(var, "core.protecthfs")) {
1776 protect_hfs = git_config_bool(var, value);
1777 return 0;
1778 }
1779
1780 if (!strcmp(var, "core.protectntfs")) {
1781 protect_ntfs = git_config_bool(var, value);
1782 return 0;
1783 }
1784
1785 if (!strcmp(var, "core.usereplacerefs")) {
1786 read_replace_refs = git_config_bool(var, value);
1787 return 0;
1788 }
1789
1790 /* Add other config variables here and to Documentation/config.txt. */
1791 return platform_core_config(var, value, cb);
1792 }
1793
1794 static int git_default_sparse_config(const char *var, const char *value)
1795 {
1796 if (!strcmp(var, "sparse.expectfilesoutsideofpatterns")) {
1797 sparse_expect_files_outside_of_patterns = git_config_bool(var, value);
1798 return 0;
1799 }
1800
1801 /* Add other config variables here and to Documentation/config/sparse.txt. */
1802 return 0;
1803 }
1804
1805 static int git_default_i18n_config(const char *var, const char *value)
1806 {
1807 if (!strcmp(var, "i18n.commitencoding"))
1808 return git_config_string(&git_commit_encoding, var, value);
1809
1810 if (!strcmp(var, "i18n.logoutputencoding"))
1811 return git_config_string(&git_log_output_encoding, var, value);
1812
1813 /* Add other config variables here and to Documentation/config.txt. */
1814 return 0;
1815 }
1816
1817 static int git_default_branch_config(const char *var, const char *value)
1818 {
1819 if (!strcmp(var, "branch.autosetupmerge")) {
1820 if (value && !strcmp(value, "always")) {
1821 git_branch_track = BRANCH_TRACK_ALWAYS;
1822 return 0;
1823 } else if (value && !strcmp(value, "inherit")) {
1824 git_branch_track = BRANCH_TRACK_INHERIT;
1825 return 0;
1826 } else if (value && !strcmp(value, "simple")) {
1827 git_branch_track = BRANCH_TRACK_SIMPLE;
1828 return 0;
1829 }
1830 git_branch_track = git_config_bool(var, value);
1831 return 0;
1832 }
1833 if (!strcmp(var, "branch.autosetuprebase")) {
1834 if (!value)
1835 return config_error_nonbool(var);
1836 else if (!strcmp(value, "never"))
1837 autorebase = AUTOREBASE_NEVER;
1838 else if (!strcmp(value, "local"))
1839 autorebase = AUTOREBASE_LOCAL;
1840 else if (!strcmp(value, "remote"))
1841 autorebase = AUTOREBASE_REMOTE;
1842 else if (!strcmp(value, "always"))
1843 autorebase = AUTOREBASE_ALWAYS;
1844 else
1845 return error(_("malformed value for %s"), var);
1846 return 0;
1847 }
1848
1849 /* Add other config variables here and to Documentation/config.txt. */
1850 return 0;
1851 }
1852
1853 static int git_default_push_config(const char *var, const char *value)
1854 {
1855 if (!strcmp(var, "push.default")) {
1856 if (!value)
1857 return config_error_nonbool(var);
1858 else if (!strcmp(value, "nothing"))
1859 push_default = PUSH_DEFAULT_NOTHING;
1860 else if (!strcmp(value, "matching"))
1861 push_default = PUSH_DEFAULT_MATCHING;
1862 else if (!strcmp(value, "simple"))
1863 push_default = PUSH_DEFAULT_SIMPLE;
1864 else if (!strcmp(value, "upstream"))
1865 push_default = PUSH_DEFAULT_UPSTREAM;
1866 else if (!strcmp(value, "tracking")) /* deprecated */
1867 push_default = PUSH_DEFAULT_UPSTREAM;
1868 else if (!strcmp(value, "current"))
1869 push_default = PUSH_DEFAULT_CURRENT;
1870 else {
1871 error(_("malformed value for %s: %s"), var, value);
1872 return error(_("must be one of nothing, matching, simple, "
1873 "upstream or current"));
1874 }
1875 return 0;
1876 }
1877
1878 /* Add other config variables here and to Documentation/config.txt. */
1879 return 0;
1880 }
1881
1882 static int git_default_mailmap_config(const char *var, const char *value)
1883 {
1884 if (!strcmp(var, "mailmap.file"))
1885 return git_config_pathname(&git_mailmap_file, var, value);
1886 if (!strcmp(var, "mailmap.blob"))
1887 return git_config_string(&git_mailmap_blob, var, value);
1888
1889 /* Add other config variables here and to Documentation/config.txt. */
1890 return 0;
1891 }
1892
1893 int git_default_config(const char *var, const char *value, void *cb)
1894 {
1895 if (starts_with(var, "core."))
1896 return git_default_core_config(var, value, cb);
1897
1898 if (starts_with(var, "user.") ||
1899 starts_with(var, "author.") ||
1900 starts_with(var, "committer."))
1901 return git_ident_config(var, value, cb);
1902
1903 if (starts_with(var, "i18n."))
1904 return git_default_i18n_config(var, value);
1905
1906 if (starts_with(var, "branch."))
1907 return git_default_branch_config(var, value);
1908
1909 if (starts_with(var, "push."))
1910 return git_default_push_config(var, value);
1911
1912 if (starts_with(var, "mailmap."))
1913 return git_default_mailmap_config(var, value);
1914
1915 if (starts_with(var, "advice.") || starts_with(var, "color.advice"))
1916 return git_default_advice_config(var, value);
1917
1918 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
1919 pager_use_color = git_config_bool(var,value);
1920 return 0;
1921 }
1922
1923 if (!strcmp(var, "pack.packsizelimit")) {
1924 pack_size_limit_cfg = git_config_ulong(var, value);
1925 return 0;
1926 }
1927
1928 if (!strcmp(var, "pack.compression")) {
1929 int level = git_config_int(var, value);
1930 if (level == -1)
1931 level = Z_DEFAULT_COMPRESSION;
1932 else if (level < 0 || level > Z_BEST_COMPRESSION)
1933 die(_("bad pack compression level %d"), level);
1934 pack_compression_level = level;
1935 pack_compression_seen = 1;
1936 return 0;
1937 }
1938
1939 if (starts_with(var, "sparse."))
1940 return git_default_sparse_config(var, value);
1941
1942 /* Add other config variables here and to Documentation/config.txt. */
1943 return 0;
1944 }
1945
1946 /*
1947 * All source specific fields in the union, die_on_error, name and the callbacks
1948 * fgetc, ungetc, ftell of top need to be initialized before calling
1949 * this function.
1950 */
1951 static int do_config_from(struct config_source *top, config_fn_t fn, void *data,
1952 const struct config_options *opts)
1953 {
1954 int ret;
1955
1956 /* push config-file parsing state stack */
1957 top->prev = cf;
1958 top->linenr = 1;
1959 top->eof = 0;
1960 top->total_len = 0;
1961 strbuf_init(&top->value, 1024);
1962 strbuf_init(&top->var, 1024);
1963 cf = top;
1964
1965 ret = git_parse_source(fn, data, opts);
1966
1967 /* pop config-file parsing state stack */
1968 strbuf_release(&top->value);
1969 strbuf_release(&top->var);
1970 cf = top->prev;
1971
1972 return ret;
1973 }
1974
1975 static int do_config_from_file(config_fn_t fn,
1976 const enum config_origin_type origin_type,
1977 const char *name, const char *path, FILE *f,
1978 void *data, const struct config_options *opts)
1979 {
1980 struct config_source top;
1981 int ret;
1982
1983 top.u.file = f;
1984 top.origin_type = origin_type;
1985 top.name = name;
1986 top.path = path;
1987 top.default_error_action = CONFIG_ERROR_DIE;
1988 top.do_fgetc = config_file_fgetc;
1989 top.do_ungetc = config_file_ungetc;
1990 top.do_ftell = config_file_ftell;
1991
1992 flockfile(f);
1993 ret = do_config_from(&top, fn, data, opts);
1994 funlockfile(f);
1995 return ret;
1996 }
1997
1998 static int git_config_from_stdin(config_fn_t fn, void *data)
1999 {
2000 return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin,
2001 data, NULL);
2002 }
2003
2004 int git_config_from_file_with_options(config_fn_t fn, const char *filename,
2005 void *data,
2006 const struct config_options *opts)
2007 {
2008 int ret = -1;
2009 FILE *f;
2010
2011 if (!filename)
2012 BUG("filename cannot be NULL");
2013 f = fopen_or_warn(filename, "r");
2014 if (f) {
2015 ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
2016 filename, f, data, opts);
2017 fclose(f);
2018 }
2019 return ret;
2020 }
2021
2022 int git_config_from_file(config_fn_t fn, const char *filename, void *data)
2023 {
2024 return git_config_from_file_with_options(fn, filename, data, NULL);
2025 }
2026
2027 int git_config_from_mem(config_fn_t fn,
2028 const enum config_origin_type origin_type,
2029 const char *name, const char *buf, size_t len,
2030 void *data, const struct config_options *opts)
2031 {
2032 struct config_source top;
2033
2034 top.u.buf.buf = buf;
2035 top.u.buf.len = len;
2036 top.u.buf.pos = 0;
2037 top.origin_type = origin_type;
2038 top.name = name;
2039 top.path = NULL;
2040 top.default_error_action = CONFIG_ERROR_ERROR;
2041 top.do_fgetc = config_buf_fgetc;
2042 top.do_ungetc = config_buf_ungetc;
2043 top.do_ftell = config_buf_ftell;
2044
2045 return do_config_from(&top, fn, data, opts);
2046 }
2047
2048 int git_config_from_blob_oid(config_fn_t fn,
2049 const char *name,
2050 struct repository *repo,
2051 const struct object_id *oid,
2052 void *data)
2053 {
2054 enum object_type type;
2055 char *buf;
2056 unsigned long size;
2057 int ret;
2058
2059 buf = repo_read_object_file(repo, oid, &type, &size);
2060 if (!buf)
2061 return error(_("unable to load config blob object '%s'"), name);
2062 if (type != OBJ_BLOB) {
2063 free(buf);
2064 return error(_("reference '%s' does not point to a blob"), name);
2065 }
2066
2067 ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size,
2068 data, NULL);
2069 free(buf);
2070
2071 return ret;
2072 }
2073
2074 static int git_config_from_blob_ref(config_fn_t fn,
2075 struct repository *repo,
2076 const char *name,
2077 void *data)
2078 {
2079 struct object_id oid;
2080
2081 if (repo_get_oid(repo, name, &oid) < 0)
2082 return error(_("unable to resolve config blob '%s'"), name);
2083 return git_config_from_blob_oid(fn, name, repo, &oid, data);
2084 }
2085
2086 char *git_system_config(void)
2087 {
2088 char *system_config = xstrdup_or_null(getenv("GIT_CONFIG_SYSTEM"));
2089 if (!system_config)
2090 system_config = system_path(ETC_GITCONFIG);
2091 normalize_path_copy(system_config, system_config);
2092 return system_config;
2093 }
2094
2095 void git_global_config(char **user_out, char **xdg_out)
2096 {
2097 char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
2098 char *xdg_config = NULL;
2099
2100 if (!user_config) {
2101 user_config = interpolate_path("~/.gitconfig", 0);
2102 xdg_config = xdg_config_home("config");
2103 }
2104
2105 *user_out = user_config;
2106 *xdg_out = xdg_config;
2107 }
2108
2109 /*
2110 * Parse environment variable 'k' as a boolean (in various
2111 * possible spellings); if missing, use the default value 'def'.
2112 */
2113 int git_env_bool(const char *k, int def)
2114 {
2115 const char *v = getenv(k);
2116 return v ? git_config_bool(k, v) : def;
2117 }
2118
2119 /*
2120 * Parse environment variable 'k' as ulong with possibly a unit
2121 * suffix; if missing, use the default value 'val'.
2122 */
2123 unsigned long git_env_ulong(const char *k, unsigned long val)
2124 {
2125 const char *v = getenv(k);
2126 if (v && !git_parse_ulong(v, &val))
2127 die(_("failed to parse %s"), k);
2128 return val;
2129 }
2130
2131 int git_config_system(void)
2132 {
2133 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
2134 }
2135
2136 static int do_git_config_sequence(const struct config_options *opts,
2137 config_fn_t fn, void *data)
2138 {
2139 int ret = 0;
2140 char *system_config = git_system_config();
2141 char *xdg_config = NULL;
2142 char *user_config = NULL;
2143 char *repo_config;
2144 enum config_scope prev_parsing_scope = current_parsing_scope;
2145
2146 if (opts->commondir)
2147 repo_config = mkpathdup("%s/config", opts->commondir);
2148 else if (opts->git_dir)
2149 BUG("git_dir without commondir");
2150 else
2151 repo_config = NULL;
2152
2153 current_parsing_scope = CONFIG_SCOPE_SYSTEM;
2154 if (git_config_system() && system_config &&
2155 !access_or_die(system_config, R_OK,
2156 opts->system_gently ? ACCESS_EACCES_OK : 0))
2157 ret += git_config_from_file(fn, system_config, data);
2158
2159 current_parsing_scope = CONFIG_SCOPE_GLOBAL;
2160 git_global_config(&user_config, &xdg_config);
2161
2162 if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
2163 ret += git_config_from_file(fn, xdg_config, data);
2164
2165 if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
2166 ret += git_config_from_file(fn, user_config, data);
2167
2168 current_parsing_scope = CONFIG_SCOPE_LOCAL;
2169 if (!opts->ignore_repo && repo_config &&
2170 !access_or_die(repo_config, R_OK, 0))
2171 ret += git_config_from_file(fn, repo_config, data);
2172
2173 current_parsing_scope = CONFIG_SCOPE_WORKTREE;
2174 if (!opts->ignore_worktree && repository_format_worktree_config) {
2175 char *path = git_pathdup("config.worktree");
2176 if (!access_or_die(path, R_OK, 0))
2177 ret += git_config_from_file(fn, path, data);
2178 free(path);
2179 }
2180
2181 current_parsing_scope = CONFIG_SCOPE_COMMAND;
2182 if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
2183 die(_("unable to parse command-line config"));
2184
2185 current_parsing_scope = prev_parsing_scope;
2186 free(system_config);
2187 free(xdg_config);
2188 free(user_config);
2189 free(repo_config);
2190 return ret;
2191 }
2192
2193 int config_with_options(config_fn_t fn, void *data,
2194 struct git_config_source *config_source,
2195 const struct config_options *opts)
2196 {
2197 struct config_include_data inc = CONFIG_INCLUDE_INIT;
2198 int ret;
2199
2200 if (opts->respect_includes) {
2201 inc.fn = fn;
2202 inc.data = data;
2203 inc.opts = opts;
2204 inc.config_source = config_source;
2205 fn = git_config_include;
2206 data = &inc;
2207 }
2208
2209 if (config_source)
2210 current_parsing_scope = config_source->scope;
2211
2212 /*
2213 * If we have a specific filename, use it. Otherwise, follow the
2214 * regular lookup sequence.
2215 */
2216 if (config_source && config_source->use_stdin) {
2217 ret = git_config_from_stdin(fn, data);
2218 } else if (config_source && config_source->file) {
2219 ret = git_config_from_file(fn, config_source->file, data);
2220 } else if (config_source && config_source->blob) {
2221 struct repository *repo = config_source->repo ?
2222 config_source->repo : the_repository;
2223 ret = git_config_from_blob_ref(fn, repo, config_source->blob,
2224 data);
2225 } else {
2226 ret = do_git_config_sequence(opts, fn, data);
2227 }
2228
2229 if (inc.remote_urls) {
2230 string_list_clear(inc.remote_urls, 0);
2231 FREE_AND_NULL(inc.remote_urls);
2232 }
2233 return ret;
2234 }
2235
2236 static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
2237 {
2238 int i, value_index;
2239 struct string_list *values;
2240 struct config_set_element *entry;
2241 struct configset_list *list = &cs->list;
2242
2243 for (i = 0; i < list->nr; i++) {
2244 entry = list->items[i].e;
2245 value_index = list->items[i].value_index;
2246 values = &entry->value_list;
2247
2248 current_config_kvi = values->items[value_index].util;
2249
2250 if (fn(entry->key, values->items[value_index].string, data) < 0)
2251 git_die_config_linenr(entry->key,
2252 current_config_kvi->filename,
2253 current_config_kvi->linenr);
2254
2255 current_config_kvi = NULL;
2256 }
2257 }
2258
2259 void read_early_config(config_fn_t cb, void *data)
2260 {
2261 struct config_options opts = {0};
2262 struct strbuf commondir = STRBUF_INIT;
2263 struct strbuf gitdir = STRBUF_INIT;
2264
2265 opts.respect_includes = 1;
2266
2267 if (have_git_dir()) {
2268 opts.commondir = get_git_common_dir();
2269 opts.git_dir = get_git_dir();
2270 /*
2271 * When setup_git_directory() was not yet asked to discover the
2272 * GIT_DIR, we ask discover_git_directory() to figure out whether there
2273 * is any repository config we should use (but unlike
2274 * setup_git_directory_gently(), no global state is changed, most
2275 * notably, the current working directory is still the same after the
2276 * call).
2277 */
2278 } else if (!discover_git_directory(&commondir, &gitdir)) {
2279 opts.commondir = commondir.buf;
2280 opts.git_dir = gitdir.buf;
2281 }
2282
2283 config_with_options(cb, data, NULL, &opts);
2284
2285 strbuf_release(&commondir);
2286 strbuf_release(&gitdir);
2287 }
2288
2289 /*
2290 * Read config but only enumerate system and global settings.
2291 * Omit any repo-local, worktree-local, or command-line settings.
2292 */
2293 void read_very_early_config(config_fn_t cb, void *data)
2294 {
2295 struct config_options opts = { 0 };
2296
2297 opts.respect_includes = 1;
2298 opts.ignore_repo = 1;
2299 opts.ignore_worktree = 1;
2300 opts.ignore_cmdline = 1;
2301 opts.system_gently = 1;
2302
2303 config_with_options(cb, data, NULL, &opts);
2304 }
2305
2306 static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
2307 {
2308 struct config_set_element k;
2309 struct config_set_element *found_entry;
2310 char *normalized_key;
2311 /*
2312 * `key` may come from the user, so normalize it before using it
2313 * for querying entries from the hashmap.
2314 */
2315 if (git_config_parse_key(key, &normalized_key, NULL))
2316 return NULL;
2317
2318 hashmap_entry_init(&k.ent, strhash(normalized_key));
2319 k.key = normalized_key;
2320 found_entry = hashmap_get_entry(&cs->config_hash, &k, ent, NULL);
2321 free(normalized_key);
2322 return found_entry;
2323 }
2324
2325 static int configset_add_value(struct config_set *cs, const char *key, const char *value)
2326 {
2327 struct config_set_element *e;
2328 struct string_list_item *si;
2329 struct configset_list_item *l_item;
2330 struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
2331
2332 e = configset_find_element(cs, key);
2333 /*
2334 * Since the keys are being fed by git_config*() callback mechanism, they
2335 * are already normalized. So simply add them without any further munging.
2336 */
2337 if (!e) {
2338 e = xmalloc(sizeof(*e));
2339 hashmap_entry_init(&e->ent, strhash(key));
2340 e->key = xstrdup(key);
2341 string_list_init_dup(&e->value_list);
2342 hashmap_add(&cs->config_hash, &e->ent);
2343 }
2344 si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
2345
2346 ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc);
2347 l_item = &cs->list.items[cs->list.nr++];
2348 l_item->e = e;
2349 l_item->value_index = e->value_list.nr - 1;
2350
2351 if (!cf)
2352 BUG("configset_add_value has no source");
2353 if (cf->name) {
2354 kv_info->filename = strintern(cf->name);
2355 kv_info->linenr = cf->linenr;
2356 kv_info->origin_type = cf->origin_type;
2357 } else {
2358 /* for values read from `git_config_from_parameters()` */
2359 kv_info->filename = NULL;
2360 kv_info->linenr = -1;
2361 kv_info->origin_type = CONFIG_ORIGIN_CMDLINE;
2362 }
2363 kv_info->scope = current_parsing_scope;
2364 si->util = kv_info;
2365
2366 return 0;
2367 }
2368
2369 static int config_set_element_cmp(const void *cmp_data UNUSED,
2370 const struct hashmap_entry *eptr,
2371 const struct hashmap_entry *entry_or_key,
2372 const void *keydata UNUSED)
2373 {
2374 const struct config_set_element *e1, *e2;
2375
2376 e1 = container_of(eptr, const struct config_set_element, ent);
2377 e2 = container_of(entry_or_key, const struct config_set_element, ent);
2378
2379 return strcmp(e1->key, e2->key);
2380 }
2381
2382 void git_configset_init(struct config_set *cs)
2383 {
2384 hashmap_init(&cs->config_hash, config_set_element_cmp, NULL, 0);
2385 cs->hash_initialized = 1;
2386 cs->list.nr = 0;
2387 cs->list.alloc = 0;
2388 cs->list.items = NULL;
2389 }
2390
2391 void git_configset_clear(struct config_set *cs)
2392 {
2393 struct config_set_element *entry;
2394 struct hashmap_iter iter;
2395 if (!cs->hash_initialized)
2396 return;
2397
2398 hashmap_for_each_entry(&cs->config_hash, &iter, entry,
2399 ent /* member name */) {
2400 free(entry->key);
2401 string_list_clear(&entry->value_list, 1);
2402 }
2403 hashmap_clear_and_free(&cs->config_hash, struct config_set_element, ent);
2404 cs->hash_initialized = 0;
2405 free(cs->list.items);
2406 cs->list.nr = 0;
2407 cs->list.alloc = 0;
2408 cs->list.items = NULL;
2409 }
2410
2411 static int config_set_callback(const char *key, const char *value, void *cb)
2412 {
2413 struct config_set *cs = cb;
2414 configset_add_value(cs, key, value);
2415 return 0;
2416 }
2417
2418 int git_configset_add_file(struct config_set *cs, const char *filename)
2419 {
2420 return git_config_from_file(config_set_callback, filename, cs);
2421 }
2422
2423 int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
2424 {
2425 const struct string_list *values = NULL;
2426 /*
2427 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
2428 * queried key in the files of the configset, the value returned will be the last
2429 * value in the value list for that key.
2430 */
2431 values = git_configset_get_value_multi(cs, key);
2432
2433 if (!values)
2434 return 1;
2435 assert(values->nr > 0);
2436 *value = values->items[values->nr - 1].string;
2437 return 0;
2438 }
2439
2440 const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
2441 {
2442 struct config_set_element *e = configset_find_element(cs, key);
2443 return e ? &e->value_list : NULL;
2444 }
2445
2446 int git_configset_get_string(struct config_set *cs, const char *key, char **dest)
2447 {
2448 const char *value;
2449 if (!git_configset_get_value(cs, key, &value))
2450 return git_config_string((const char **)dest, key, value);
2451 else
2452 return 1;
2453 }
2454
2455 static int git_configset_get_string_tmp(struct config_set *cs, const char *key,
2456 const char **dest)
2457 {
2458 const char *value;
2459 if (!git_configset_get_value(cs, key, &value)) {
2460 if (!value)
2461 return config_error_nonbool(key);
2462 *dest = value;
2463 return 0;
2464 } else {
2465 return 1;
2466 }
2467 }
2468
2469 int git_configset_get_int(struct config_set *cs, const char *key, int *dest)
2470 {
2471 const char *value;
2472 if (!git_configset_get_value(cs, key, &value)) {
2473 *dest = git_config_int(key, value);
2474 return 0;
2475 } else
2476 return 1;
2477 }
2478
2479 int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest)
2480 {
2481 const char *value;
2482 if (!git_configset_get_value(cs, key, &value)) {
2483 *dest = git_config_ulong(key, value);
2484 return 0;
2485 } else
2486 return 1;
2487 }
2488
2489 int git_configset_get_bool(struct config_set *cs, const char *key, int *dest)
2490 {
2491 const char *value;
2492 if (!git_configset_get_value(cs, key, &value)) {
2493 *dest = git_config_bool(key, value);
2494 return 0;
2495 } else
2496 return 1;
2497 }
2498
2499 int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
2500 int *is_bool, int *dest)
2501 {
2502 const char *value;
2503 if (!git_configset_get_value(cs, key, &value)) {
2504 *dest = git_config_bool_or_int(key, value, is_bool);
2505 return 0;
2506 } else
2507 return 1;
2508 }
2509
2510 int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest)
2511 {
2512 const char *value;
2513 if (!git_configset_get_value(cs, key, &value)) {
2514 *dest = git_parse_maybe_bool(value);
2515 if (*dest == -1)
2516 return -1;
2517 return 0;
2518 } else
2519 return 1;
2520 }
2521
2522 int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest)
2523 {
2524 const char *value;
2525 if (!git_configset_get_value(cs, key, &value))
2526 return git_config_pathname(dest, key, value);
2527 else
2528 return 1;
2529 }
2530
2531 /* Functions use to read configuration from a repository */
2532 static void repo_read_config(struct repository *repo)
2533 {
2534 struct config_options opts = { 0 };
2535
2536 opts.respect_includes = 1;
2537 opts.commondir = repo->commondir;
2538 opts.git_dir = repo->gitdir;
2539
2540 if (!repo->config)
2541 CALLOC_ARRAY(repo->config, 1);
2542 else
2543 git_configset_clear(repo->config);
2544
2545 git_configset_init(repo->config);
2546
2547 if (config_with_options(config_set_callback, repo->config, NULL, &opts) < 0)
2548 /*
2549 * config_with_options() normally returns only
2550 * zero, as most errors are fatal, and
2551 * non-fatal potential errors are guarded by "if"
2552 * statements that are entered only when no error is
2553 * possible.
2554 *
2555 * If we ever encounter a non-fatal error, it means
2556 * something went really wrong and we should stop
2557 * immediately.
2558 */
2559 die(_("unknown error occurred while reading the configuration files"));
2560 }
2561
2562 static void git_config_check_init(struct repository *repo)
2563 {
2564 if (repo->config && repo->config->hash_initialized)
2565 return;
2566 repo_read_config(repo);
2567 }
2568
2569 static void repo_config_clear(struct repository *repo)
2570 {
2571 if (!repo->config || !repo->config->hash_initialized)
2572 return;
2573 git_configset_clear(repo->config);
2574 }
2575
2576 void repo_config(struct repository *repo, config_fn_t fn, void *data)
2577 {
2578 git_config_check_init(repo);
2579 configset_iter(repo->config, fn, data);
2580 }
2581
2582 int repo_config_get_value(struct repository *repo,
2583 const char *key, const char **value)
2584 {
2585 git_config_check_init(repo);
2586 return git_configset_get_value(repo->config, key, value);
2587 }
2588
2589 const struct string_list *repo_config_get_value_multi(struct repository *repo,
2590 const char *key)
2591 {
2592 git_config_check_init(repo);
2593 return git_configset_get_value_multi(repo->config, key);
2594 }
2595
2596 int repo_config_get_string(struct repository *repo,
2597 const char *key, char **dest)
2598 {
2599 int ret;
2600 git_config_check_init(repo);
2601 ret = git_configset_get_string(repo->config, key, dest);
2602 if (ret < 0)
2603 git_die_config(key, NULL);
2604 return ret;
2605 }
2606
2607 int repo_config_get_string_tmp(struct repository *repo,
2608 const char *key, const char **dest)
2609 {
2610 int ret;
2611 git_config_check_init(repo);
2612 ret = git_configset_get_string_tmp(repo->config, key, dest);
2613 if (ret < 0)
2614 git_die_config(key, NULL);
2615 return ret;
2616 }
2617
2618 int repo_config_get_int(struct repository *repo,
2619 const char *key, int *dest)
2620 {
2621 git_config_check_init(repo);
2622 return git_configset_get_int(repo->config, key, dest);
2623 }
2624
2625 int repo_config_get_ulong(struct repository *repo,
2626 const char *key, unsigned long *dest)
2627 {
2628 git_config_check_init(repo);
2629 return git_configset_get_ulong(repo->config, key, dest);
2630 }
2631
2632 int repo_config_get_bool(struct repository *repo,
2633 const char *key, int *dest)
2634 {
2635 git_config_check_init(repo);
2636 return git_configset_get_bool(repo->config, key, dest);
2637 }
2638
2639 int repo_config_get_bool_or_int(struct repository *repo,
2640 const char *key, int *is_bool, int *dest)
2641 {
2642 git_config_check_init(repo);
2643 return git_configset_get_bool_or_int(repo->config, key, is_bool, dest);
2644 }
2645
2646 int repo_config_get_maybe_bool(struct repository *repo,
2647 const char *key, int *dest)
2648 {
2649 git_config_check_init(repo);
2650 return git_configset_get_maybe_bool(repo->config, key, dest);
2651 }
2652
2653 int repo_config_get_pathname(struct repository *repo,
2654 const char *key, const char **dest)
2655 {
2656 int ret;
2657 git_config_check_init(repo);
2658 ret = git_configset_get_pathname(repo->config, key, dest);
2659 if (ret < 0)
2660 git_die_config(key, NULL);
2661 return ret;
2662 }
2663
2664 /* Read values into protected_config. */
2665 static void read_protected_config(void)
2666 {
2667 struct config_options opts = {
2668 .respect_includes = 1,
2669 .ignore_repo = 1,
2670 .ignore_worktree = 1,
2671 .system_gently = 1,
2672 };
2673 git_configset_init(&protected_config);
2674 config_with_options(config_set_callback, &protected_config,
2675 NULL, &opts);
2676 }
2677
2678 void git_protected_config(config_fn_t fn, void *data)
2679 {
2680 if (!protected_config.hash_initialized)
2681 read_protected_config();
2682 configset_iter(&protected_config, fn, data);
2683 }
2684
2685 /* Functions used historically to read configuration from 'the_repository' */
2686 void git_config(config_fn_t fn, void *data)
2687 {
2688 repo_config(the_repository, fn, data);
2689 }
2690
2691 void git_config_clear(void)
2692 {
2693 repo_config_clear(the_repository);
2694 }
2695
2696 int git_config_get_value(const char *key, const char **value)
2697 {
2698 return repo_config_get_value(the_repository, key, value);
2699 }
2700
2701 const struct string_list *git_config_get_value_multi(const char *key)
2702 {
2703 return repo_config_get_value_multi(the_repository, key);
2704 }
2705
2706 int git_config_get_string(const char *key, char **dest)
2707 {
2708 return repo_config_get_string(the_repository, key, dest);
2709 }
2710
2711 int git_config_get_string_tmp(const char *key, const char **dest)
2712 {
2713 return repo_config_get_string_tmp(the_repository, key, dest);
2714 }
2715
2716 int git_config_get_int(const char *key, int *dest)
2717 {
2718 return repo_config_get_int(the_repository, key, dest);
2719 }
2720
2721 int git_config_get_ulong(const char *key, unsigned long *dest)
2722 {
2723 return repo_config_get_ulong(the_repository, key, dest);
2724 }
2725
2726 int git_config_get_bool(const char *key, int *dest)
2727 {
2728 return repo_config_get_bool(the_repository, key, dest);
2729 }
2730
2731 int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
2732 {
2733 return repo_config_get_bool_or_int(the_repository, key, is_bool, dest);
2734 }
2735
2736 int git_config_get_maybe_bool(const char *key, int *dest)
2737 {
2738 return repo_config_get_maybe_bool(the_repository, key, dest);
2739 }
2740
2741 int git_config_get_pathname(const char *key, const char **dest)
2742 {
2743 return repo_config_get_pathname(the_repository, key, dest);
2744 }
2745
2746 int git_config_get_expiry(const char *key, const char **output)
2747 {
2748 int ret = git_config_get_string(key, (char **)output);
2749 if (ret)
2750 return ret;
2751 if (strcmp(*output, "now")) {
2752 timestamp_t now = approxidate("now");
2753 if (approxidate(*output) >= now)
2754 git_die_config(key, _("Invalid %s: '%s'"), key, *output);
2755 }
2756 return ret;
2757 }
2758
2759 int git_config_get_expiry_in_days(const char *key, timestamp_t *expiry, timestamp_t now)
2760 {
2761 const char *expiry_string;
2762 intmax_t days;
2763 timestamp_t when;
2764
2765 if (git_config_get_string_tmp(key, &expiry_string))
2766 return 1; /* no such thing */
2767
2768 if (git_parse_signed(expiry_string, &days, maximum_signed_value_of_type(int))) {
2769 const int scale = 86400;
2770 *expiry = now - days * scale;
2771 return 0;
2772 }
2773
2774 if (!parse_expiry_date(expiry_string, &when)) {
2775 *expiry = when;
2776 return 0;
2777 }
2778 return -1; /* thing exists but cannot be parsed */
2779 }
2780
2781 int git_config_get_split_index(void)
2782 {
2783 int val;
2784
2785 if (!git_config_get_maybe_bool("core.splitindex", &val))
2786 return val;
2787
2788 return -1; /* default value */
2789 }
2790
2791 int git_config_get_max_percent_split_change(void)
2792 {
2793 int val = -1;
2794
2795 if (!git_config_get_int("splitindex.maxpercentchange", &val)) {
2796 if (0 <= val && val <= 100)
2797 return val;
2798
2799 return error(_("splitIndex.maxPercentChange value '%d' "
2800 "should be between 0 and 100"), val);
2801 }
2802
2803 return -1; /* default value */
2804 }
2805
2806 int git_config_get_index_threads(int *dest)
2807 {
2808 int is_bool, val;
2809
2810 val = git_env_ulong("GIT_TEST_INDEX_THREADS", 0);
2811 if (val) {
2812 *dest = val;
2813 return 0;
2814 }
2815
2816 if (!git_config_get_bool_or_int("index.threads", &is_bool, &val)) {
2817 if (is_bool)
2818 *dest = val ? 0 : 1;
2819 else
2820 *dest = val;
2821 return 0;
2822 }
2823
2824 return 1;
2825 }
2826
2827 NORETURN
2828 void git_die_config_linenr(const char *key, const char *filename, int linenr)
2829 {
2830 if (!filename)
2831 die(_("unable to parse '%s' from command-line config"), key);
2832 else
2833 die(_("bad config variable '%s' in file '%s' at line %d"),
2834 key, filename, linenr);
2835 }
2836
2837 NORETURN __attribute__((format(printf, 2, 3)))
2838 void git_die_config(const char *key, const char *err, ...)
2839 {
2840 const struct string_list *values;
2841 struct key_value_info *kv_info;
2842 report_fn error_fn = get_error_routine();
2843
2844 if (err) {
2845 va_list params;
2846 va_start(params, err);
2847 error_fn(err, params);
2848 va_end(params);
2849 }
2850 values = git_config_get_value_multi(key);
2851 kv_info = values->items[values->nr - 1].util;
2852 git_die_config_linenr(key, kv_info->filename, kv_info->linenr);
2853 }
2854
2855 /*
2856 * Find all the stuff for git_config_set() below.
2857 */
2858
2859 struct config_store_data {
2860 size_t baselen;
2861 char *key;
2862 int do_not_match;
2863 const char *fixed_value;
2864 regex_t *value_pattern;
2865 int multi_replace;
2866 struct {
2867 size_t begin, end;
2868 enum config_event_t type;
2869 int is_keys_section;
2870 } *parsed;
2871 unsigned int parsed_nr, parsed_alloc, *seen, seen_nr, seen_alloc;
2872 unsigned int key_seen:1, section_seen:1, is_keys_section:1;
2873 };
2874
2875 static void config_store_data_clear(struct config_store_data *store)
2876 {
2877 free(store->key);
2878 if (store->value_pattern != NULL &&
2879 store->value_pattern != CONFIG_REGEX_NONE) {
2880 regfree(store->value_pattern);
2881 free(store->value_pattern);
2882 }
2883 free(store->parsed);
2884 free(store->seen);
2885 memset(store, 0, sizeof(*store));
2886 }
2887
2888 static int matches(const char *key, const char *value,
2889 const struct config_store_data *store)
2890 {
2891 if (strcmp(key, store->key))
2892 return 0; /* not ours */
2893 if (store->fixed_value)
2894 return !strcmp(store->fixed_value, value);
2895 if (!store->value_pattern)
2896 return 1; /* always matches */
2897 if (store->value_pattern == CONFIG_REGEX_NONE)
2898 return 0; /* never matches */
2899
2900 return store->do_not_match ^
2901 (value && !regexec(store->value_pattern, value, 0, NULL, 0));
2902 }
2903
2904 static int store_aux_event(enum config_event_t type,
2905 size_t begin, size_t end, void *data)
2906 {
2907 struct config_store_data *store = data;
2908
2909 ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc);
2910 store->parsed[store->parsed_nr].begin = begin;
2911 store->parsed[store->parsed_nr].end = end;
2912 store->parsed[store->parsed_nr].type = type;
2913
2914 if (type == CONFIG_EVENT_SECTION) {
2915 int (*cmpfn)(const char *, const char *, size_t);
2916
2917 if (cf->var.len < 2 || cf->var.buf[cf->var.len - 1] != '.')
2918 return error(_("invalid section name '%s'"), cf->var.buf);
2919
2920 if (cf->subsection_case_sensitive)
2921 cmpfn = strncasecmp;
2922 else
2923 cmpfn = strncmp;
2924
2925 /* Is this the section we were looking for? */
2926 store->is_keys_section =
2927 store->parsed[store->parsed_nr].is_keys_section =
2928 cf->var.len - 1 == store->baselen &&
2929 !cmpfn(cf->var.buf, store->key, store->baselen);
2930 if (store->is_keys_section) {
2931 store->section_seen = 1;
2932 ALLOC_GROW(store->seen, store->seen_nr + 1,
2933 store->seen_alloc);
2934 store->seen[store->seen_nr] = store->parsed_nr;
2935 }
2936 }
2937
2938 store->parsed_nr++;
2939
2940 return 0;
2941 }
2942
2943 static int store_aux(const char *key, const char *value, void *cb)
2944 {
2945 struct config_store_data *store = cb;
2946
2947 if (store->key_seen) {
2948 if (matches(key, value, store)) {
2949 if (store->seen_nr == 1 && store->multi_replace == 0) {
2950 warning(_("%s has multiple values"), key);
2951 }
2952
2953 ALLOC_GROW(store->seen, store->seen_nr + 1,
2954 store->seen_alloc);
2955
2956 store->seen[store->seen_nr] = store->parsed_nr;
2957 store->seen_nr++;
2958 }
2959 } else if (store->is_keys_section) {
2960 /*
2961 * Do not increment matches yet: this may not be a match, but we
2962 * are in the desired section.
2963 */
2964 ALLOC_GROW(store->seen, store->seen_nr + 1, store->seen_alloc);
2965 store->seen[store->seen_nr] = store->parsed_nr;
2966 store->section_seen = 1;
2967
2968 if (matches(key, value, store)) {
2969 store->seen_nr++;
2970 store->key_seen = 1;
2971 }
2972 }
2973
2974 return 0;
2975 }
2976
2977 static int write_error(const char *filename)
2978 {
2979 error(_("failed to write new configuration file %s"), filename);
2980
2981 /* Same error code as "failed to rename". */
2982 return 4;
2983 }
2984
2985 static struct strbuf store_create_section(const char *key,
2986 const struct config_store_data *store)
2987 {
2988 const char *dot;
2989 size_t i;
2990 struct strbuf sb = STRBUF_INIT;
2991
2992 dot = memchr(key, '.', store->baselen);
2993 if (dot) {
2994 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
2995 for (i = dot - key + 1; i < store->baselen; i++) {
2996 if (key[i] == '"' || key[i] == '\\')
2997 strbuf_addch(&sb, '\\');
2998 strbuf_addch(&sb, key[i]);
2999 }
3000 strbuf_addstr(&sb, "\"]\n");
3001 } else {
3002 strbuf_addch(&sb, '[');
3003 strbuf_add(&sb, key, store->baselen);
3004 strbuf_addstr(&sb, "]\n");
3005 }
3006
3007 return sb;
3008 }
3009
3010 static ssize_t write_section(int fd, const char *key,
3011 const struct config_store_data *store)
3012 {
3013 struct strbuf sb = store_create_section(key, store);
3014 ssize_t ret;
3015
3016 ret = write_in_full(fd, sb.buf, sb.len);
3017 strbuf_release(&sb);
3018
3019 return ret;
3020 }
3021
3022 static ssize_t write_pair(int fd, const char *key, const char *value,
3023 const struct config_store_data *store)
3024 {
3025 int i;
3026 ssize_t ret;
3027 const char *quote = "";
3028 struct strbuf sb = STRBUF_INIT;
3029
3030 /*
3031 * Check to see if the value needs to be surrounded with a dq pair.
3032 * Note that problematic characters are always backslash-quoted; this
3033 * check is about not losing leading or trailing SP and strings that
3034 * follow beginning-of-comment characters (i.e. ';' and '#') by the
3035 * configuration parser.
3036 */
3037 if (value[0] == ' ')
3038 quote = "\"";
3039 for (i = 0; value[i]; i++)
3040 if (value[i] == ';' || value[i] == '#')
3041 quote = "\"";
3042 if (i && value[i - 1] == ' ')
3043 quote = "\"";
3044
3045 strbuf_addf(&sb, "\t%s = %s", key + store->baselen + 1, quote);
3046
3047 for (i = 0; value[i]; i++)
3048 switch (value[i]) {
3049 case '\n':
3050 strbuf_addstr(&sb, "\\n");
3051 break;
3052 case '\t':
3053 strbuf_addstr(&sb, "\\t");
3054 break;
3055 case '"':
3056 case '\\':
3057 strbuf_addch(&sb, '\\');
3058 /* fallthrough */
3059 default:
3060 strbuf_addch(&sb, value[i]);
3061 break;
3062 }
3063 strbuf_addf(&sb, "%s\n", quote);
3064
3065 ret = write_in_full(fd, sb.buf, sb.len);
3066 strbuf_release(&sb);
3067
3068 return ret;
3069 }
3070
3071 /*
3072 * If we are about to unset the last key(s) in a section, and if there are
3073 * no comments surrounding (or included in) the section, we will want to
3074 * extend begin/end to remove the entire section.
3075 *
3076 * Note: the parameter `seen_ptr` points to the index into the store.seen
3077 * array. * This index may be incremented if a section has more than one
3078 * entry (which all are to be removed).
3079 */
3080 static void maybe_remove_section(struct config_store_data *store,
3081 size_t *begin_offset, size_t *end_offset,
3082 int *seen_ptr)
3083 {
3084 size_t begin;
3085 int i, seen, section_seen = 0;
3086
3087 /*
3088 * First, ensure that this is the first key, and that there are no
3089 * comments before the entry nor before the section header.
3090 */
3091 seen = *seen_ptr;
3092 for (i = store->seen[seen]; i > 0; i--) {
3093 enum config_event_t type = store->parsed[i - 1].type;
3094
3095 if (type == CONFIG_EVENT_COMMENT)
3096 /* There is a comment before this entry or section */
3097 return;
3098 if (type == CONFIG_EVENT_ENTRY) {
3099 if (!section_seen)
3100 /* This is not the section's first entry. */
3101 return;
3102 /* We encountered no comment before the section. */
3103 break;
3104 }
3105 if (type == CONFIG_EVENT_SECTION) {
3106 if (!store->parsed[i - 1].is_keys_section)
3107 break;
3108 section_seen = 1;
3109 }
3110 }
3111 begin = store->parsed[i].begin;
3112
3113 /*
3114 * Next, make sure that we are removing the last key(s) in the section,
3115 * and that there are no comments that are possibly about the current
3116 * section.
3117 */
3118 for (i = store->seen[seen] + 1; i < store->parsed_nr; i++) {
3119 enum config_event_t type = store->parsed[i].type;
3120
3121 if (type == CONFIG_EVENT_COMMENT)
3122 return;
3123 if (type == CONFIG_EVENT_SECTION) {
3124 if (store->parsed[i].is_keys_section)
3125 continue;
3126 break;
3127 }
3128 if (type == CONFIG_EVENT_ENTRY) {
3129 if (++seen < store->seen_nr &&
3130 i == store->seen[seen])
3131 /* We want to remove this entry, too */
3132 continue;
3133 /* There is another entry in this section. */
3134 return;
3135 }
3136 }
3137
3138 /*
3139 * We are really removing the last entry/entries from this section, and
3140 * there are no enclosed or surrounding comments. Remove the entire,
3141 * now-empty section.
3142 */
3143 *seen_ptr = seen;
3144 *begin_offset = begin;
3145 if (i < store->parsed_nr)
3146 *end_offset = store->parsed[i].begin;
3147 else
3148 *end_offset = store->parsed[store->parsed_nr - 1].end;
3149 }
3150
3151 int git_config_set_in_file_gently(const char *config_filename,
3152 const char *key, const char *value)
3153 {
3154 return git_config_set_multivar_in_file_gently(config_filename, key, value, NULL, 0);
3155 }
3156
3157 void git_config_set_in_file(const char *config_filename,
3158 const char *key, const char *value)
3159 {
3160 git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
3161 }
3162
3163 int git_config_set_gently(const char *key, const char *value)
3164 {
3165 return git_config_set_multivar_gently(key, value, NULL, 0);
3166 }
3167
3168 int repo_config_set_worktree_gently(struct repository *r,
3169 const char *key, const char *value)
3170 {
3171 /* Only use worktree-specific config if it is already enabled. */
3172 if (repository_format_worktree_config) {
3173 char *file = repo_git_path(r, "config.worktree");
3174 int ret = git_config_set_multivar_in_file_gently(
3175 file, key, value, NULL, 0);
3176 free(file);
3177 return ret;
3178 }
3179 return repo_config_set_multivar_gently(r, key, value, NULL, 0);
3180 }
3181
3182 void git_config_set(const char *key, const char *value)
3183 {
3184 git_config_set_multivar(key, value, NULL, 0);
3185
3186 trace2_cmd_set_config(key, value);
3187 }
3188
3189 /*
3190 * If value==NULL, unset in (remove from) config,
3191 * if value_pattern!=NULL, disregard key/value pairs where value does not match.
3192 * if value_pattern==CONFIG_REGEX_NONE, do not match any existing values
3193 * (only add a new one)
3194 * if flags contains the CONFIG_FLAGS_MULTI_REPLACE flag, all matching
3195 * key/values are removed before a single new pair is written. If the
3196 * flag is not present, then replace only the first match.
3197 *
3198 * Returns 0 on success.
3199 *
3200 * This function does this:
3201 *
3202 * - it locks the config file by creating ".git/config.lock"
3203 *
3204 * - it then parses the config using store_aux() as validator to find
3205 * the position on the key/value pair to replace. If it is to be unset,
3206 * it must be found exactly once.
3207 *
3208 * - the config file is mmap()ed and the part before the match (if any) is
3209 * written to the lock file, then the changed part and the rest.
3210 *
3211 * - the config file is removed and the lock file rename()d to it.
3212 *
3213 */
3214 int git_config_set_multivar_in_file_gently(const char *config_filename,
3215 const char *key, const char *value,
3216 const char *value_pattern,
3217 unsigned flags)
3218 {
3219 int fd = -1, in_fd = -1;
3220 int ret;
3221 struct lock_file lock = LOCK_INIT;
3222 char *filename_buf = NULL;
3223 char *contents = NULL;
3224 size_t contents_sz;
3225 struct config_store_data store;
3226
3227 memset(&store, 0, sizeof(store));
3228
3229 /* parse-key returns negative; flip the sign to feed exit(3) */
3230 ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
3231 if (ret)
3232 goto out_free;
3233
3234 store.multi_replace = (flags & CONFIG_FLAGS_MULTI_REPLACE) != 0;
3235
3236 if (!config_filename)
3237 config_filename = filename_buf = git_pathdup("config");
3238
3239 /*
3240 * The lock serves a purpose in addition to locking: the new
3241 * contents of .git/config will be written into it.
3242 */
3243 fd = hold_lock_file_for_update(&lock, config_filename, 0);
3244 if (fd < 0) {
3245 error_errno(_("could not lock config file %s"), config_filename);
3246 ret = CONFIG_NO_LOCK;
3247 goto out_free;
3248 }
3249
3250 /*
3251 * If .git/config does not exist yet, write a minimal version.
3252 */
3253 in_fd = open(config_filename, O_RDONLY);
3254 if ( in_fd < 0 ) {
3255 if ( ENOENT != errno ) {
3256 error_errno(_("opening %s"), config_filename);
3257 ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
3258 goto out_free;
3259 }
3260 /* if nothing to unset, error out */
3261 if (!value) {
3262 ret = CONFIG_NOTHING_SET;
3263 goto out_free;
3264 }
3265
3266 free(store.key);
3267 store.key = xstrdup(key);
3268 if (write_section(fd, key, &store) < 0 ||
3269 write_pair(fd, key, value, &store) < 0)
3270 goto write_err_out;
3271 } else {
3272 struct stat st;
3273 size_t copy_begin, copy_end;
3274 int i, new_line = 0;
3275 struct config_options opts;
3276
3277 if (!value_pattern)
3278 store.value_pattern = NULL;
3279 else if (value_pattern == CONFIG_REGEX_NONE)
3280 store.value_pattern = CONFIG_REGEX_NONE;
3281 else if (flags & CONFIG_FLAGS_FIXED_VALUE)
3282 store.fixed_value = value_pattern;
3283 else {
3284 if (value_pattern[0] == '!') {
3285 store.do_not_match = 1;
3286 value_pattern++;
3287 } else
3288 store.do_not_match = 0;
3289
3290 store.value_pattern = (regex_t*)xmalloc(sizeof(regex_t));
3291 if (regcomp(store.value_pattern, value_pattern,
3292 REG_EXTENDED)) {
3293 error(_("invalid pattern: %s"), value_pattern);
3294 FREE_AND_NULL(store.value_pattern);
3295 ret = CONFIG_INVALID_PATTERN;
3296 goto out_free;
3297 }
3298 }
3299
3300 ALLOC_GROW(store.parsed, 1, store.parsed_alloc);
3301 store.parsed[0].end = 0;
3302
3303 memset(&opts, 0, sizeof(opts));
3304 opts.event_fn = store_aux_event;
3305 opts.event_fn_data = &store;
3306
3307 /*
3308 * After this, store.parsed will contain offsets of all the
3309 * parsed elements, and store.seen will contain a list of
3310 * matches, as indices into store.parsed.
3311 *
3312 * As a side effect, we make sure to transform only a valid
3313 * existing config file.
3314 */
3315 if (git_config_from_file_with_options(store_aux,
3316 config_filename,
3317 &store, &opts)) {
3318 error(_("invalid config file %s"), config_filename);
3319 ret = CONFIG_INVALID_FILE;
3320 goto out_free;
3321 }
3322
3323 /* if nothing to unset, or too many matches, error out */
3324 if ((store.seen_nr == 0 && value == NULL) ||
3325 (store.seen_nr > 1 && !store.multi_replace)) {
3326 ret = CONFIG_NOTHING_SET;
3327 goto out_free;
3328 }
3329
3330 if (fstat(in_fd, &st) == -1) {
3331 error_errno(_("fstat on %s failed"), config_filename);
3332 ret = CONFIG_INVALID_FILE;
3333 goto out_free;
3334 }
3335
3336 contents_sz = xsize_t(st.st_size);
3337 contents = xmmap_gently(NULL, contents_sz, PROT_READ,
3338 MAP_PRIVATE, in_fd, 0);
3339 if (contents == MAP_FAILED) {
3340 if (errno == ENODEV && S_ISDIR(st.st_mode))
3341 errno = EISDIR;
3342 error_errno(_("unable to mmap '%s'%s"),
3343 config_filename, mmap_os_err());
3344 ret = CONFIG_INVALID_FILE;
3345 contents = NULL;
3346 goto out_free;
3347 }
3348 close(in_fd);
3349 in_fd = -1;
3350
3351 if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
3352 error_errno(_("chmod on %s failed"), get_lock_file_path(&lock));
3353 ret = CONFIG_NO_WRITE;
3354 goto out_free;
3355 }
3356
3357 if (store.seen_nr == 0) {
3358 if (!store.seen_alloc) {
3359 /* Did not see key nor section */
3360 ALLOC_GROW(store.seen, 1, store.seen_alloc);
3361 store.seen[0] = store.parsed_nr
3362 - !!store.parsed_nr;
3363 }
3364 store.seen_nr = 1;
3365 }
3366
3367 for (i = 0, copy_begin = 0; i < store.seen_nr; i++) {
3368 size_t replace_end;
3369 int j = store.seen[i];
3370
3371 new_line = 0;
3372 if (!store.key_seen) {
3373 copy_end = store.parsed[j].end;
3374 /* include '\n' when copying section header */
3375 if (copy_end > 0 && copy_end < contents_sz &&
3376 contents[copy_end - 1] != '\n' &&
3377 contents[copy_end] == '\n')
3378 copy_end++;
3379 replace_end = copy_end;
3380 } else {
3381 replace_end = store.parsed[j].end;
3382 copy_end = store.parsed[j].begin;
3383 if (!value)
3384 maybe_remove_section(&store,
3385 &copy_end,
3386 &replace_end, &i);
3387 /*
3388 * Swallow preceding white-space on the same
3389 * line.
3390 */
3391 while (copy_end > 0 ) {
3392 char c = contents[copy_end - 1];
3393
3394 if (isspace(c) && c != '\n')
3395 copy_end--;
3396 else
3397 break;
3398 }
3399 }
3400
3401 if (copy_end > 0 && contents[copy_end-1] != '\n')
3402 new_line = 1;
3403
3404 /* write the first part of the config */
3405 if (copy_end > copy_begin) {
3406 if (write_in_full(fd, contents + copy_begin,
3407 copy_end - copy_begin) < 0)
3408 goto write_err_out;
3409 if (new_line &&
3410 write_str_in_full(fd, "\n") < 0)
3411 goto write_err_out;
3412 }
3413 copy_begin = replace_end;
3414 }
3415
3416 /* write the pair (value == NULL means unset) */
3417 if (value) {
3418 if (!store.section_seen) {
3419 if (write_section(fd, key, &store) < 0)
3420 goto write_err_out;
3421 }
3422 if (write_pair(fd, key, value, &store) < 0)
3423 goto write_err_out;
3424 }
3425
3426 /* write the rest of the config */
3427 if (copy_begin < contents_sz)
3428 if (write_in_full(fd, contents + copy_begin,
3429 contents_sz - copy_begin) < 0)
3430 goto write_err_out;
3431
3432 munmap(contents, contents_sz);
3433 contents = NULL;
3434 }
3435
3436 if (commit_lock_file(&lock) < 0) {
3437 error_errno(_("could not write config file %s"), config_filename);
3438 ret = CONFIG_NO_WRITE;
3439 goto out_free;
3440 }
3441
3442 ret = 0;
3443
3444 /* Invalidate the config cache */
3445 git_config_clear();
3446
3447 out_free:
3448 rollback_lock_file(&lock);
3449 free(filename_buf);
3450 if (contents)
3451 munmap(contents, contents_sz);
3452 if (in_fd >= 0)
3453 close(in_fd);
3454 config_store_data_clear(&store);
3455 return ret;
3456
3457 write_err_out:
3458 ret = write_error(get_lock_file_path(&lock));
3459 goto out_free;
3460
3461 }
3462
3463 void git_config_set_multivar_in_file(const char *config_filename,
3464 const char *key, const char *value,
3465 const char *value_pattern, unsigned flags)
3466 {
3467 if (!git_config_set_multivar_in_file_gently(config_filename, key, value,
3468 value_pattern, flags))
3469 return;
3470 if (value)
3471 die(_("could not set '%s' to '%s'"), key, value);
3472 else
3473 die(_("could not unset '%s'"), key);
3474 }
3475
3476 int git_config_set_multivar_gently(const char *key, const char *value,
3477 const char *value_pattern, unsigned flags)
3478 {
3479 return repo_config_set_multivar_gently(the_repository, key, value,
3480 value_pattern, flags);
3481 }
3482
3483 int repo_config_set_multivar_gently(struct repository *r, const char *key,
3484 const char *value,
3485 const char *value_pattern, unsigned flags)
3486 {
3487 char *file = repo_git_path(r, "config");
3488 int res = git_config_set_multivar_in_file_gently(file,
3489 key, value,
3490 value_pattern,
3491 flags);
3492 free(file);
3493 return res;
3494 }
3495
3496 void git_config_set_multivar(const char *key, const char *value,
3497 const char *value_pattern, unsigned flags)
3498 {
3499 git_config_set_multivar_in_file(git_path("config"),
3500 key, value, value_pattern,
3501 flags);
3502 }
3503
3504 static int section_name_match (const char *buf, const char *name)
3505 {
3506 int i = 0, j = 0, dot = 0;
3507 if (buf[i] != '[')
3508 return 0;
3509 for (i = 1; buf[i] && buf[i] != ']'; i++) {
3510 if (!dot && isspace(buf[i])) {
3511 dot = 1;
3512 if (name[j++] != '.')
3513 break;
3514 for (i++; isspace(buf[i]); i++)
3515 ; /* do nothing */
3516 if (buf[i] != '"')
3517 break;
3518 continue;
3519 }
3520 if (buf[i] == '\\' && dot)
3521 i++;
3522 else if (buf[i] == '"' && dot) {
3523 for (i++; isspace(buf[i]); i++)
3524 ; /* do_nothing */
3525 break;
3526 }
3527 if (buf[i] != name[j++])
3528 break;
3529 }
3530 if (buf[i] == ']' && name[j] == 0) {
3531 /*
3532 * We match, now just find the right length offset by
3533 * gobbling up any whitespace after it, as well
3534 */
3535 i++;
3536 for (; buf[i] && isspace(buf[i]); i++)
3537 ; /* do nothing */
3538 return i;
3539 }
3540 return 0;
3541 }
3542
3543 static int section_name_is_ok(const char *name)
3544 {
3545 /* Empty section names are bogus. */
3546 if (!*name)
3547 return 0;
3548
3549 /*
3550 * Before a dot, we must be alphanumeric or dash. After the first dot,
3551 * anything goes, so we can stop checking.
3552 */
3553 for (; *name && *name != '.'; name++)
3554 if (*name != '-' && !isalnum(*name))
3555 return 0;
3556 return 1;
3557 }
3558
3559 /* if new_name == NULL, the section is removed instead */
3560 static int git_config_copy_or_rename_section_in_file(const char *config_filename,
3561 const char *old_name,
3562 const char *new_name, int copy)
3563 {
3564 int ret = 0, remove = 0;
3565 char *filename_buf = NULL;
3566 struct lock_file lock = LOCK_INIT;
3567 int out_fd;
3568 char buf[1024];
3569 FILE *config_file = NULL;
3570 struct stat st;
3571 struct strbuf copystr = STRBUF_INIT;
3572 struct config_store_data store;
3573
3574 memset(&store, 0, sizeof(store));
3575
3576 if (new_name && !section_name_is_ok(new_name)) {
3577 ret = error(_("invalid section name: %s"), new_name);
3578 goto out_no_rollback;
3579 }
3580
3581 if (!config_filename)
3582 config_filename = filename_buf = git_pathdup("config");
3583
3584 out_fd = hold_lock_file_for_update(&lock, config_filename, 0);
3585 if (out_fd < 0) {
3586 ret = error(_("could not lock config file %s"), config_filename);
3587 goto out;
3588 }
3589
3590 if (!(config_file = fopen(config_filename, "rb"))) {
3591 ret = warn_on_fopen_errors(config_filename);
3592 if (ret)
3593 goto out;
3594 /* no config file means nothing to rename, no error */
3595 goto commit_and_out;
3596 }
3597
3598 if (fstat(fileno(config_file), &st) == -1) {
3599 ret = error_errno(_("fstat on %s failed"), config_filename);
3600 goto out;
3601 }
3602
3603 if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
3604 ret = error_errno(_("chmod on %s failed"),
3605 get_lock_file_path(&lock));
3606 goto out;
3607 }
3608
3609 while (fgets(buf, sizeof(buf), config_file)) {
3610 unsigned i;
3611 int length;
3612 int is_section = 0;
3613 char *output = buf;
3614 for (i = 0; buf[i] && isspace(buf[i]); i++)
3615 ; /* do nothing */
3616 if (buf[i] == '[') {
3617 /* it's a section */
3618 int offset;
3619 is_section = 1;
3620
3621 /*
3622 * When encountering a new section under -c we
3623 * need to flush out any section we're already
3624 * coping and begin anew. There might be
3625 * multiple [branch "$name"] sections.
3626 */
3627 if (copystr.len > 0) {
3628 if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) {
3629 ret = write_error(get_lock_file_path(&lock));
3630 goto out;
3631 }
3632 strbuf_reset(&copystr);
3633 }
3634
3635 offset = section_name_match(&buf[i], old_name);
3636 if (offset > 0) {
3637 ret++;
3638 if (!new_name) {
3639 remove = 1;
3640 continue;
3641 }
3642 store.baselen = strlen(new_name);
3643 if (!copy) {
3644 if (write_section(out_fd, new_name, &store) < 0) {
3645 ret = write_error(get_lock_file_path(&lock));
3646 goto out;
3647 }
3648 /*
3649 * We wrote out the new section, with
3650 * a newline, now skip the old
3651 * section's length
3652 */
3653 output += offset + i;
3654 if (strlen(output) > 0) {
3655 /*
3656 * More content means there's
3657 * a declaration to put on the
3658 * next line; indent with a
3659 * tab
3660 */
3661 output -= 1;
3662 output[0] = '\t';
3663 }
3664 } else {
3665 copystr = store_create_section(new_name, &store);
3666 }
3667 }
3668 remove = 0;
3669 }
3670 if (remove)
3671 continue;
3672 length = strlen(output);
3673
3674 if (!is_section && copystr.len > 0) {
3675 strbuf_add(&copystr, output, length);
3676 }
3677
3678 if (write_in_full(out_fd, output, length) < 0) {
3679 ret = write_error(get_lock_file_path(&lock));
3680 goto out;
3681 }
3682 }
3683
3684 /*
3685 * Copy a trailing section at the end of the config, won't be
3686 * flushed by the usual "flush because we have a new section
3687 * logic in the loop above.
3688 */
3689 if (copystr.len > 0) {
3690 if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) {
3691 ret = write_error(get_lock_file_path(&lock));
3692 goto out;
3693 }
3694 strbuf_reset(&copystr);
3695 }
3696
3697 fclose(config_file);
3698 config_file = NULL;
3699 commit_and_out:
3700 if (commit_lock_file(&lock) < 0)
3701 ret = error_errno(_("could not write config file %s"),
3702 config_filename);
3703 out:
3704 if (config_file)
3705 fclose(config_file);
3706 rollback_lock_file(&lock);
3707 out_no_rollback:
3708 free(filename_buf);
3709 config_store_data_clear(&store);
3710 return ret;
3711 }
3712
3713 int git_config_rename_section_in_file(const char *config_filename,
3714 const char *old_name, const char *new_name)
3715 {
3716 return git_config_copy_or_rename_section_in_file(config_filename,
3717 old_name, new_name, 0);
3718 }
3719
3720 int git_config_rename_section(const char *old_name, const char *new_name)
3721 {
3722 return git_config_rename_section_in_file(NULL, old_name, new_name);
3723 }
3724
3725 int git_config_copy_section_in_file(const char *config_filename,
3726 const char *old_name, const char *new_name)
3727 {
3728 return git_config_copy_or_rename_section_in_file(config_filename,
3729 old_name, new_name, 1);
3730 }
3731
3732 int git_config_copy_section(const char *old_name, const char *new_name)
3733 {
3734 return git_config_copy_section_in_file(NULL, old_name, new_name);
3735 }
3736
3737 /*
3738 * Call this to report error for your variable that should not
3739 * get a boolean value (i.e. "[my] var" means "true").
3740 */
3741 #undef config_error_nonbool
3742 int config_error_nonbool(const char *var)
3743 {
3744 return error(_("missing value for '%s'"), var);
3745 }
3746
3747 int parse_config_key(const char *var,
3748 const char *section,
3749 const char **subsection, size_t *subsection_len,
3750 const char **key)
3751 {
3752 const char *dot;
3753
3754 /* Does it start with "section." ? */
3755 if (!skip_prefix(var, section, &var) || *var != '.')
3756 return -1;
3757
3758 /*
3759 * Find the key; we don't know yet if we have a subsection, but we must
3760 * parse backwards from the end, since the subsection may have dots in
3761 * it, too.
3762 */
3763 dot = strrchr(var, '.');
3764 *key = dot + 1;
3765
3766 /* Did we have a subsection at all? */
3767 if (dot == var) {
3768 if (subsection) {
3769 *subsection = NULL;
3770 *subsection_len = 0;
3771 }
3772 }
3773 else {
3774 if (!subsection)
3775 return -1;
3776 *subsection = var + 1;
3777 *subsection_len = dot - *subsection;
3778 }
3779
3780 return 0;
3781 }
3782
3783 const char *current_config_origin_type(void)
3784 {
3785 int type;
3786 if (current_config_kvi)
3787 type = current_config_kvi->origin_type;
3788 else if(cf)
3789 type = cf->origin_type;
3790 else
3791 BUG("current_config_origin_type called outside config callback");
3792
3793 switch (type) {
3794 case CONFIG_ORIGIN_BLOB:
3795 return "blob";
3796 case CONFIG_ORIGIN_FILE:
3797 return "file";
3798 case CONFIG_ORIGIN_STDIN:
3799 return "standard input";
3800 case CONFIG_ORIGIN_SUBMODULE_BLOB:
3801 return "submodule-blob";
3802 case CONFIG_ORIGIN_CMDLINE:
3803 return "command line";
3804 default:
3805 BUG("unknown config origin type");
3806 }
3807 }
3808
3809 const char *config_scope_name(enum config_scope scope)
3810 {
3811 switch (scope) {
3812 case CONFIG_SCOPE_SYSTEM:
3813 return "system";
3814 case CONFIG_SCOPE_GLOBAL:
3815 return "global";
3816 case CONFIG_SCOPE_LOCAL:
3817 return "local";
3818 case CONFIG_SCOPE_WORKTREE:
3819 return "worktree";
3820 case CONFIG_SCOPE_COMMAND:
3821 return "command";
3822 case CONFIG_SCOPE_SUBMODULE:
3823 return "submodule";
3824 default:
3825 return "unknown";
3826 }
3827 }
3828
3829 const char *current_config_name(void)
3830 {
3831 const char *name;
3832 if (current_config_kvi)
3833 name = current_config_kvi->filename;
3834 else if (cf)
3835 name = cf->name;
3836 else
3837 BUG("current_config_name called outside config callback");
3838 return name ? name : "";
3839 }
3840
3841 enum config_scope current_config_scope(void)
3842 {
3843 if (current_config_kvi)
3844 return current_config_kvi->scope;
3845 else
3846 return current_parsing_scope;
3847 }
3848
3849 int current_config_line(void)
3850 {
3851 if (current_config_kvi)
3852 return current_config_kvi->linenr;
3853 else
3854 return cf->linenr;
3855 }
3856
3857 int lookup_config(const char **mapping, int nr_mapping, const char *var)
3858 {
3859 int i;
3860
3861 for (i = 0; i < nr_mapping; i++) {
3862 const char *name = mapping[i];
3863
3864 if (name && !strcasecmp(var, name))
3865 return i;
3866 }
3867 return -1;
3868 }