d555deeb9a976bbb1640513d4c81e7aacc9cc785
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 "lockfile.h"
10#include "exec_cmd.h"
11#include "strbuf.h"
12#include "quote.h"
13#include "hashmap.h"
14#include "string-list.h"
15#include "utf8.h"
16
17struct config_source {
18 struct config_source *prev;
19 union {
20 FILE *file;
21 struct config_buf {
22 const char *buf;
23 size_t len;
24 size_t pos;
25 } buf;
26 } u;
27 const char *origin_type;
28 const char *name;
29 const char *path;
30 int die_on_error;
31 int linenr;
32 int eof;
33 struct strbuf value;
34 struct strbuf var;
35
36 int (*do_fgetc)(struct config_source *c);
37 int (*do_ungetc)(int c, struct config_source *conf);
38 long (*do_ftell)(struct config_source *c);
39};
40
41/*
42 * These variables record the "current" config source, which
43 * can be accessed by parsing callbacks.
44 *
45 * The "cf" variable will be non-NULL only when we are actually parsing a real
46 * config source (file, blob, cmdline, etc).
47 *
48 * The "current_config_kvi" variable will be non-NULL only when we are feeding
49 * cached config from a configset into a callback.
50 *
51 * They should generally never be non-NULL at the same time. If they are both
52 * NULL, then we aren't parsing anything (and depending on the function looking
53 * at the variables, it's either a bug for it to be called in the first place,
54 * or it's a function which can be reused for non-config purposes, and should
55 * fall back to some sane behavior).
56 */
57static struct config_source *cf;
58static struct key_value_info *current_config_kvi;
59
60static int zlib_compression_seen;
61
62/*
63 * Default config_set that contains key-value pairs from the usual set of config
64 * config files (i.e repo specific .git/config, user wide ~/.gitconfig, XDG
65 * config file and the global /etc/gitconfig)
66 */
67static struct config_set the_config_set;
68
69static int config_file_fgetc(struct config_source *conf)
70{
71 return getc_unlocked(conf->u.file);
72}
73
74static int config_file_ungetc(int c, struct config_source *conf)
75{
76 return ungetc(c, conf->u.file);
77}
78
79static long config_file_ftell(struct config_source *conf)
80{
81 return ftell(conf->u.file);
82}
83
84
85static int config_buf_fgetc(struct config_source *conf)
86{
87 if (conf->u.buf.pos < conf->u.buf.len)
88 return conf->u.buf.buf[conf->u.buf.pos++];
89
90 return EOF;
91}
92
93static int config_buf_ungetc(int c, struct config_source *conf)
94{
95 if (conf->u.buf.pos > 0) {
96 conf->u.buf.pos--;
97 if (conf->u.buf.buf[conf->u.buf.pos] != c)
98 die("BUG: config_buf can only ungetc the same character");
99 return c;
100 }
101
102 return EOF;
103}
104
105static long config_buf_ftell(struct config_source *conf)
106{
107 return conf->u.buf.pos;
108}
109
110#define MAX_INCLUDE_DEPTH 10
111static const char include_depth_advice[] =
112"exceeded maximum include depth (%d) while including\n"
113" %s\n"
114"from\n"
115" %s\n"
116"Do you have circular includes?";
117static int handle_path_include(const char *path, struct config_include_data *inc)
118{
119 int ret = 0;
120 struct strbuf buf = STRBUF_INIT;
121 char *expanded;
122
123 if (!path)
124 return config_error_nonbool("include.path");
125
126 expanded = expand_user_path(path);
127 if (!expanded)
128 return error("could not expand include path '%s'", path);
129 path = expanded;
130
131 /*
132 * Use an absolute path as-is, but interpret relative paths
133 * based on the including config file.
134 */
135 if (!is_absolute_path(path)) {
136 char *slash;
137
138 if (!cf || !cf->path)
139 return error("relative config includes must come from files");
140
141 slash = find_last_dir_sep(cf->path);
142 if (slash)
143 strbuf_add(&buf, cf->path, slash - cf->path + 1);
144 strbuf_addstr(&buf, path);
145 path = buf.buf;
146 }
147
148 if (!access_or_die(path, R_OK, 0)) {
149 if (++inc->depth > MAX_INCLUDE_DEPTH)
150 die(include_depth_advice, MAX_INCLUDE_DEPTH, path,
151 !cf ? "<unknown>" :
152 cf->name ? cf->name :
153 "the command line");
154 ret = git_config_from_file(git_config_include, path, inc);
155 inc->depth--;
156 }
157 strbuf_release(&buf);
158 free(expanded);
159 return ret;
160}
161
162int git_config_include(const char *var, const char *value, void *data)
163{
164 struct config_include_data *inc = data;
165 int ret;
166
167 /*
168 * Pass along all values, including "include" directives; this makes it
169 * possible to query information on the includes themselves.
170 */
171 ret = inc->fn(var, value, inc->data);
172 if (ret < 0)
173 return ret;
174
175 if (!strcmp(var, "include.path"))
176 ret = handle_path_include(value, inc);
177 return ret;
178}
179
180void git_config_push_parameter(const char *text)
181{
182 struct strbuf env = STRBUF_INIT;
183 const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
184 if (old && *old) {
185 strbuf_addstr(&env, old);
186 strbuf_addch(&env, ' ');
187 }
188 sq_quote_buf(&env, text);
189 setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
190 strbuf_release(&env);
191}
192
193int git_config_parse_parameter(const char *text,
194 config_fn_t fn, void *data)
195{
196 const char *value;
197 struct strbuf **pair;
198
199 pair = strbuf_split_str(text, '=', 2);
200 if (!pair[0])
201 return error("bogus config parameter: %s", text);
202
203 if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') {
204 strbuf_setlen(pair[0], pair[0]->len - 1);
205 value = pair[1] ? pair[1]->buf : "";
206 } else {
207 value = NULL;
208 }
209
210 strbuf_trim(pair[0]);
211 if (!pair[0]->len) {
212 strbuf_list_free(pair);
213 return error("bogus config parameter: %s", text);
214 }
215 strbuf_tolower(pair[0]);
216 if (fn(pair[0]->buf, value, data) < 0) {
217 strbuf_list_free(pair);
218 return -1;
219 }
220 strbuf_list_free(pair);
221 return 0;
222}
223
224int git_config_from_parameters(config_fn_t fn, void *data)
225{
226 const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
227 int ret = 0;
228 char *envw;
229 const char **argv = NULL;
230 int nr = 0, alloc = 0;
231 int i;
232 struct config_source source;
233
234 if (!env)
235 return 0;
236
237 memset(&source, 0, sizeof(source));
238 source.prev = cf;
239 cf = &source;
240
241 /* sq_dequote will write over it */
242 envw = xstrdup(env);
243
244 if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
245 ret = error("bogus format in " CONFIG_DATA_ENVIRONMENT);
246 goto out;
247 }
248
249 for (i = 0; i < nr; i++) {
250 if (git_config_parse_parameter(argv[i], fn, data) < 0) {
251 ret = -1;
252 goto out;
253 }
254 }
255
256out:
257 free(argv);
258 free(envw);
259 cf = source.prev;
260 return ret;
261}
262
263static int get_next_char(void)
264{
265 int c = cf->do_fgetc(cf);
266
267 if (c == '\r') {
268 /* DOS like systems */
269 c = cf->do_fgetc(cf);
270 if (c != '\n') {
271 if (c != EOF)
272 cf->do_ungetc(c, cf);
273 c = '\r';
274 }
275 }
276 if (c == '\n')
277 cf->linenr++;
278 if (c == EOF) {
279 cf->eof = 1;
280 cf->linenr++;
281 c = '\n';
282 }
283 return c;
284}
285
286static char *parse_value(void)
287{
288 int quote = 0, comment = 0, space = 0;
289
290 strbuf_reset(&cf->value);
291 for (;;) {
292 int c = get_next_char();
293 if (c == '\n') {
294 if (quote) {
295 cf->linenr--;
296 return NULL;
297 }
298 return cf->value.buf;
299 }
300 if (comment)
301 continue;
302 if (isspace(c) && !quote) {
303 if (cf->value.len)
304 space++;
305 continue;
306 }
307 if (!quote) {
308 if (c == ';' || c == '#') {
309 comment = 1;
310 continue;
311 }
312 }
313 for (; space; space--)
314 strbuf_addch(&cf->value, ' ');
315 if (c == '\\') {
316 c = get_next_char();
317 switch (c) {
318 case '\n':
319 continue;
320 case 't':
321 c = '\t';
322 break;
323 case 'b':
324 c = '\b';
325 break;
326 case 'n':
327 c = '\n';
328 break;
329 /* Some characters escape as themselves */
330 case '\\': case '"':
331 break;
332 /* Reject unknown escape sequences */
333 default:
334 return NULL;
335 }
336 strbuf_addch(&cf->value, c);
337 continue;
338 }
339 if (c == '"') {
340 quote = 1-quote;
341 continue;
342 }
343 strbuf_addch(&cf->value, c);
344 }
345}
346
347static inline int iskeychar(int c)
348{
349 return isalnum(c) || c == '-';
350}
351
352static int get_value(config_fn_t fn, void *data, struct strbuf *name)
353{
354 int c;
355 char *value;
356 int ret;
357
358 /* Get the full name */
359 for (;;) {
360 c = get_next_char();
361 if (cf->eof)
362 break;
363 if (!iskeychar(c))
364 break;
365 strbuf_addch(name, tolower(c));
366 }
367
368 while (c == ' ' || c == '\t')
369 c = get_next_char();
370
371 value = NULL;
372 if (c != '\n') {
373 if (c != '=')
374 return -1;
375 value = parse_value();
376 if (!value)
377 return -1;
378 }
379 /*
380 * We already consumed the \n, but we need linenr to point to
381 * the line we just parsed during the call to fn to get
382 * accurate line number in error messages.
383 */
384 cf->linenr--;
385 ret = fn(name->buf, value, data);
386 cf->linenr++;
387 return ret;
388}
389
390static int get_extended_base_var(struct strbuf *name, int c)
391{
392 do {
393 if (c == '\n')
394 goto error_incomplete_line;
395 c = get_next_char();
396 } while (isspace(c));
397
398 /* We require the format to be '[base "extension"]' */
399 if (c != '"')
400 return -1;
401 strbuf_addch(name, '.');
402
403 for (;;) {
404 int c = get_next_char();
405 if (c == '\n')
406 goto error_incomplete_line;
407 if (c == '"')
408 break;
409 if (c == '\\') {
410 c = get_next_char();
411 if (c == '\n')
412 goto error_incomplete_line;
413 }
414 strbuf_addch(name, c);
415 }
416
417 /* Final ']' */
418 if (get_next_char() != ']')
419 return -1;
420 return 0;
421error_incomplete_line:
422 cf->linenr--;
423 return -1;
424}
425
426static int get_base_var(struct strbuf *name)
427{
428 for (;;) {
429 int c = get_next_char();
430 if (cf->eof)
431 return -1;
432 if (c == ']')
433 return 0;
434 if (isspace(c))
435 return get_extended_base_var(name, c);
436 if (!iskeychar(c) && c != '.')
437 return -1;
438 strbuf_addch(name, tolower(c));
439 }
440}
441
442static int git_parse_source(config_fn_t fn, void *data)
443{
444 int comment = 0;
445 int baselen = 0;
446 struct strbuf *var = &cf->var;
447
448 /* U+FEFF Byte Order Mark in UTF8 */
449 const char *bomptr = utf8_bom;
450
451 for (;;) {
452 int c = get_next_char();
453 if (bomptr && *bomptr) {
454 /* We are at the file beginning; skip UTF8-encoded BOM
455 * if present. Sane editors won't put this in on their
456 * own, but e.g. Windows Notepad will do it happily. */
457 if (c == (*bomptr & 0377)) {
458 bomptr++;
459 continue;
460 } else {
461 /* Do not tolerate partial BOM. */
462 if (bomptr != utf8_bom)
463 break;
464 /* No BOM at file beginning. Cool. */
465 bomptr = NULL;
466 }
467 }
468 if (c == '\n') {
469 if (cf->eof)
470 return 0;
471 comment = 0;
472 continue;
473 }
474 if (comment || isspace(c))
475 continue;
476 if (c == '#' || c == ';') {
477 comment = 1;
478 continue;
479 }
480 if (c == '[') {
481 /* Reset prior to determining a new stem */
482 strbuf_reset(var);
483 if (get_base_var(var) < 0 || var->len < 1)
484 break;
485 strbuf_addch(var, '.');
486 baselen = var->len;
487 continue;
488 }
489 if (!isalpha(c))
490 break;
491 /*
492 * Truncate the var name back to the section header
493 * stem prior to grabbing the suffix part of the name
494 * and the value.
495 */
496 strbuf_setlen(var, baselen);
497 strbuf_addch(var, tolower(c));
498 if (get_value(fn, data, var) < 0)
499 break;
500 }
501 if (cf->die_on_error)
502 die(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
503 else
504 return error(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
505}
506
507static int parse_unit_factor(const char *end, uintmax_t *val)
508{
509 if (!*end)
510 return 1;
511 else if (!strcasecmp(end, "k")) {
512 *val *= 1024;
513 return 1;
514 }
515 else if (!strcasecmp(end, "m")) {
516 *val *= 1024 * 1024;
517 return 1;
518 }
519 else if (!strcasecmp(end, "g")) {
520 *val *= 1024 * 1024 * 1024;
521 return 1;
522 }
523 return 0;
524}
525
526static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
527{
528 if (value && *value) {
529 char *end;
530 intmax_t val;
531 uintmax_t uval;
532 uintmax_t factor = 1;
533
534 errno = 0;
535 val = strtoimax(value, &end, 0);
536 if (errno == ERANGE)
537 return 0;
538 if (!parse_unit_factor(end, &factor)) {
539 errno = EINVAL;
540 return 0;
541 }
542 uval = labs(val);
543 uval *= factor;
544 if (uval > max || labs(val) > uval) {
545 errno = ERANGE;
546 return 0;
547 }
548 val *= factor;
549 *ret = val;
550 return 1;
551 }
552 errno = EINVAL;
553 return 0;
554}
555
556static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
557{
558 if (value && *value) {
559 char *end;
560 uintmax_t val;
561 uintmax_t oldval;
562
563 errno = 0;
564 val = strtoumax(value, &end, 0);
565 if (errno == ERANGE)
566 return 0;
567 oldval = val;
568 if (!parse_unit_factor(end, &val)) {
569 errno = EINVAL;
570 return 0;
571 }
572 if (val > max || oldval > val) {
573 errno = ERANGE;
574 return 0;
575 }
576 *ret = val;
577 return 1;
578 }
579 errno = EINVAL;
580 return 0;
581}
582
583static int git_parse_int(const char *value, int *ret)
584{
585 intmax_t tmp;
586 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
587 return 0;
588 *ret = tmp;
589 return 1;
590}
591
592static int git_parse_int64(const char *value, int64_t *ret)
593{
594 intmax_t tmp;
595 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t)))
596 return 0;
597 *ret = tmp;
598 return 1;
599}
600
601int git_parse_ulong(const char *value, unsigned long *ret)
602{
603 uintmax_t tmp;
604 if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long)))
605 return 0;
606 *ret = tmp;
607 return 1;
608}
609
610NORETURN
611static void die_bad_number(const char *name, const char *value)
612{
613 const char *reason = errno == ERANGE ?
614 "out of range" :
615 "invalid unit";
616 if (!value)
617 value = "";
618
619 if (cf && cf->origin_type && cf->name)
620 die(_("bad numeric config value '%s' for '%s' in %s %s: %s"),
621 value, name, cf->origin_type, cf->name, reason);
622 die(_("bad numeric config value '%s' for '%s': %s"), value, name, reason);
623}
624
625int git_config_int(const char *name, const char *value)
626{
627 int ret;
628 if (!git_parse_int(value, &ret))
629 die_bad_number(name, value);
630 return ret;
631}
632
633int64_t git_config_int64(const char *name, const char *value)
634{
635 int64_t ret;
636 if (!git_parse_int64(value, &ret))
637 die_bad_number(name, value);
638 return ret;
639}
640
641unsigned long git_config_ulong(const char *name, const char *value)
642{
643 unsigned long ret;
644 if (!git_parse_ulong(value, &ret))
645 die_bad_number(name, value);
646 return ret;
647}
648
649int git_parse_maybe_bool(const char *value)
650{
651 if (!value)
652 return 1;
653 if (!*value)
654 return 0;
655 if (!strcasecmp(value, "true")
656 || !strcasecmp(value, "yes")
657 || !strcasecmp(value, "on"))
658 return 1;
659 if (!strcasecmp(value, "false")
660 || !strcasecmp(value, "no")
661 || !strcasecmp(value, "off"))
662 return 0;
663 return -1;
664}
665
666int git_config_maybe_bool(const char *name, const char *value)
667{
668 int v = git_parse_maybe_bool(value);
669 if (0 <= v)
670 return v;
671 if (git_parse_int(value, &v))
672 return !!v;
673 return -1;
674}
675
676int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
677{
678 int v = git_parse_maybe_bool(value);
679 if (0 <= v) {
680 *is_bool = 1;
681 return v;
682 }
683 *is_bool = 0;
684 return git_config_int(name, value);
685}
686
687int git_config_bool(const char *name, const char *value)
688{
689 int discard;
690 return !!git_config_bool_or_int(name, value, &discard);
691}
692
693int git_config_string(const char **dest, const char *var, const char *value)
694{
695 if (!value)
696 return config_error_nonbool(var);
697 *dest = xstrdup(value);
698 return 0;
699}
700
701int git_config_pathname(const char **dest, const char *var, const char *value)
702{
703 if (!value)
704 return config_error_nonbool(var);
705 *dest = expand_user_path(value);
706 if (!*dest)
707 die(_("failed to expand user dir in: '%s'"), value);
708 return 0;
709}
710
711static int git_default_core_config(const char *var, const char *value)
712{
713 /* This needs a better name */
714 if (!strcmp(var, "core.filemode")) {
715 trust_executable_bit = git_config_bool(var, value);
716 return 0;
717 }
718 if (!strcmp(var, "core.trustctime")) {
719 trust_ctime = git_config_bool(var, value);
720 return 0;
721 }
722 if (!strcmp(var, "core.checkstat")) {
723 if (!strcasecmp(value, "default"))
724 check_stat = 1;
725 else if (!strcasecmp(value, "minimal"))
726 check_stat = 0;
727 }
728
729 if (!strcmp(var, "core.quotepath")) {
730 quote_path_fully = git_config_bool(var, value);
731 return 0;
732 }
733
734 if (!strcmp(var, "core.symlinks")) {
735 has_symlinks = git_config_bool(var, value);
736 return 0;
737 }
738
739 if (!strcmp(var, "core.ignorecase")) {
740 ignore_case = git_config_bool(var, value);
741 return 0;
742 }
743
744 if (!strcmp(var, "core.attributesfile"))
745 return git_config_pathname(&git_attributes_file, var, value);
746
747 if (!strcmp(var, "core.hookspath"))
748 return git_config_pathname(&git_hooks_path, var, value);
749
750 if (!strcmp(var, "core.bare")) {
751 is_bare_repository_cfg = git_config_bool(var, value);
752 return 0;
753 }
754
755 if (!strcmp(var, "core.ignorestat")) {
756 assume_unchanged = git_config_bool(var, value);
757 return 0;
758 }
759
760 if (!strcmp(var, "core.prefersymlinkrefs")) {
761 prefer_symlink_refs = git_config_bool(var, value);
762 return 0;
763 }
764
765 if (!strcmp(var, "core.logallrefupdates")) {
766 log_all_ref_updates = git_config_bool(var, value);
767 return 0;
768 }
769
770 if (!strcmp(var, "core.warnambiguousrefs")) {
771 warn_ambiguous_refs = git_config_bool(var, value);
772 return 0;
773 }
774
775 if (!strcmp(var, "core.abbrev")) {
776 int abbrev = git_config_int(var, value);
777 if (abbrev < minimum_abbrev || abbrev > 40)
778 return -1;
779 default_abbrev = abbrev;
780 return 0;
781 }
782
783 if (!strcmp(var, "core.loosecompression")) {
784 int level = git_config_int(var, value);
785 if (level == -1)
786 level = Z_DEFAULT_COMPRESSION;
787 else if (level < 0 || level > Z_BEST_COMPRESSION)
788 die(_("bad zlib compression level %d"), level);
789 zlib_compression_level = level;
790 zlib_compression_seen = 1;
791 return 0;
792 }
793
794 if (!strcmp(var, "core.compression")) {
795 int level = git_config_int(var, value);
796 if (level == -1)
797 level = Z_DEFAULT_COMPRESSION;
798 else if (level < 0 || level > Z_BEST_COMPRESSION)
799 die(_("bad zlib compression level %d"), level);
800 core_compression_level = level;
801 core_compression_seen = 1;
802 if (!zlib_compression_seen)
803 zlib_compression_level = level;
804 return 0;
805 }
806
807 if (!strcmp(var, "core.packedgitwindowsize")) {
808 int pgsz_x2 = getpagesize() * 2;
809 packed_git_window_size = git_config_ulong(var, value);
810
811 /* This value must be multiple of (pagesize * 2) */
812 packed_git_window_size /= pgsz_x2;
813 if (packed_git_window_size < 1)
814 packed_git_window_size = 1;
815 packed_git_window_size *= pgsz_x2;
816 return 0;
817 }
818
819 if (!strcmp(var, "core.bigfilethreshold")) {
820 big_file_threshold = git_config_ulong(var, value);
821 return 0;
822 }
823
824 if (!strcmp(var, "core.packedgitlimit")) {
825 packed_git_limit = git_config_ulong(var, value);
826 return 0;
827 }
828
829 if (!strcmp(var, "core.deltabasecachelimit")) {
830 delta_base_cache_limit = git_config_ulong(var, value);
831 return 0;
832 }
833
834 if (!strcmp(var, "core.autocrlf")) {
835 if (value && !strcasecmp(value, "input")) {
836 auto_crlf = AUTO_CRLF_INPUT;
837 return 0;
838 }
839 auto_crlf = git_config_bool(var, value);
840 return 0;
841 }
842
843 if (!strcmp(var, "core.safecrlf")) {
844 if (value && !strcasecmp(value, "warn")) {
845 safe_crlf = SAFE_CRLF_WARN;
846 return 0;
847 }
848 safe_crlf = git_config_bool(var, value);
849 return 0;
850 }
851
852 if (!strcmp(var, "core.eol")) {
853 if (value && !strcasecmp(value, "lf"))
854 core_eol = EOL_LF;
855 else if (value && !strcasecmp(value, "crlf"))
856 core_eol = EOL_CRLF;
857 else if (value && !strcasecmp(value, "native"))
858 core_eol = EOL_NATIVE;
859 else
860 core_eol = EOL_UNSET;
861 return 0;
862 }
863
864 if (!strcmp(var, "core.notesref")) {
865 notes_ref_name = xstrdup(value);
866 return 0;
867 }
868
869 if (!strcmp(var, "core.pager"))
870 return git_config_string(&pager_program, var, value);
871
872 if (!strcmp(var, "core.editor"))
873 return git_config_string(&editor_program, var, value);
874
875 if (!strcmp(var, "core.commentchar")) {
876 if (!value)
877 return config_error_nonbool(var);
878 else if (!strcasecmp(value, "auto"))
879 auto_comment_line_char = 1;
880 else if (value[0] && !value[1]) {
881 comment_line_char = value[0];
882 auto_comment_line_char = 0;
883 } else
884 return error("core.commentChar should only be one character");
885 return 0;
886 }
887
888 if (!strcmp(var, "core.askpass"))
889 return git_config_string(&askpass_program, var, value);
890
891 if (!strcmp(var, "core.excludesfile"))
892 return git_config_pathname(&excludes_file, var, value);
893
894 if (!strcmp(var, "core.whitespace")) {
895 if (!value)
896 return config_error_nonbool(var);
897 whitespace_rule_cfg = parse_whitespace_rule(value);
898 return 0;
899 }
900
901 if (!strcmp(var, "core.fsyncobjectfiles")) {
902 fsync_object_files = git_config_bool(var, value);
903 return 0;
904 }
905
906 if (!strcmp(var, "core.preloadindex")) {
907 core_preload_index = git_config_bool(var, value);
908 return 0;
909 }
910
911 if (!strcmp(var, "core.createobject")) {
912 if (!strcmp(value, "rename"))
913 object_creation_mode = OBJECT_CREATION_USES_RENAMES;
914 else if (!strcmp(value, "link"))
915 object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
916 else
917 die(_("invalid mode for object creation: %s"), value);
918 return 0;
919 }
920
921 if (!strcmp(var, "core.sparsecheckout")) {
922 core_apply_sparse_checkout = git_config_bool(var, value);
923 return 0;
924 }
925
926 if (!strcmp(var, "core.precomposeunicode")) {
927 precomposed_unicode = git_config_bool(var, value);
928 return 0;
929 }
930
931 if (!strcmp(var, "core.protecthfs")) {
932 protect_hfs = git_config_bool(var, value);
933 return 0;
934 }
935
936 if (!strcmp(var, "core.protectntfs")) {
937 protect_ntfs = git_config_bool(var, value);
938 return 0;
939 }
940
941 if (!strcmp(var, "core.hidedotfiles")) {
942 if (value && !strcasecmp(value, "dotgitonly"))
943 hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
944 else
945 hide_dotfiles = git_config_bool(var, value);
946 return 0;
947 }
948
949 /* Add other config variables here and to Documentation/config.txt. */
950 return 0;
951}
952
953static int git_default_i18n_config(const char *var, const char *value)
954{
955 if (!strcmp(var, "i18n.commitencoding"))
956 return git_config_string(&git_commit_encoding, var, value);
957
958 if (!strcmp(var, "i18n.logoutputencoding"))
959 return git_config_string(&git_log_output_encoding, var, value);
960
961 /* Add other config variables here and to Documentation/config.txt. */
962 return 0;
963}
964
965static int git_default_branch_config(const char *var, const char *value)
966{
967 if (!strcmp(var, "branch.autosetupmerge")) {
968 if (value && !strcasecmp(value, "always")) {
969 git_branch_track = BRANCH_TRACK_ALWAYS;
970 return 0;
971 }
972 git_branch_track = git_config_bool(var, value);
973 return 0;
974 }
975 if (!strcmp(var, "branch.autosetuprebase")) {
976 if (!value)
977 return config_error_nonbool(var);
978 else if (!strcmp(value, "never"))
979 autorebase = AUTOREBASE_NEVER;
980 else if (!strcmp(value, "local"))
981 autorebase = AUTOREBASE_LOCAL;
982 else if (!strcmp(value, "remote"))
983 autorebase = AUTOREBASE_REMOTE;
984 else if (!strcmp(value, "always"))
985 autorebase = AUTOREBASE_ALWAYS;
986 else
987 return error("malformed value for %s", var);
988 return 0;
989 }
990
991 /* Add other config variables here and to Documentation/config.txt. */
992 return 0;
993}
994
995static int git_default_push_config(const char *var, const char *value)
996{
997 if (!strcmp(var, "push.default")) {
998 if (!value)
999 return config_error_nonbool(var);
1000 else if (!strcmp(value, "nothing"))
1001 push_default = PUSH_DEFAULT_NOTHING;
1002 else if (!strcmp(value, "matching"))
1003 push_default = PUSH_DEFAULT_MATCHING;
1004 else if (!strcmp(value, "simple"))
1005 push_default = PUSH_DEFAULT_SIMPLE;
1006 else if (!strcmp(value, "upstream"))
1007 push_default = PUSH_DEFAULT_UPSTREAM;
1008 else if (!strcmp(value, "tracking")) /* deprecated */
1009 push_default = PUSH_DEFAULT_UPSTREAM;
1010 else if (!strcmp(value, "current"))
1011 push_default = PUSH_DEFAULT_CURRENT;
1012 else {
1013 error("malformed value for %s: %s", var, value);
1014 return error("Must be one of nothing, matching, simple, "
1015 "upstream or current.");
1016 }
1017 return 0;
1018 }
1019
1020 /* Add other config variables here and to Documentation/config.txt. */
1021 return 0;
1022}
1023
1024static int git_default_mailmap_config(const char *var, const char *value)
1025{
1026 if (!strcmp(var, "mailmap.file"))
1027 return git_config_pathname(&git_mailmap_file, var, value);
1028 if (!strcmp(var, "mailmap.blob"))
1029 return git_config_string(&git_mailmap_blob, var, value);
1030
1031 /* Add other config variables here and to Documentation/config.txt. */
1032 return 0;
1033}
1034
1035int git_default_config(const char *var, const char *value, void *dummy)
1036{
1037 if (starts_with(var, "core."))
1038 return git_default_core_config(var, value);
1039
1040 if (starts_with(var, "user."))
1041 return git_ident_config(var, value, dummy);
1042
1043 if (starts_with(var, "i18n."))
1044 return git_default_i18n_config(var, value);
1045
1046 if (starts_with(var, "branch."))
1047 return git_default_branch_config(var, value);
1048
1049 if (starts_with(var, "push."))
1050 return git_default_push_config(var, value);
1051
1052 if (starts_with(var, "mailmap."))
1053 return git_default_mailmap_config(var, value);
1054
1055 if (starts_with(var, "advice."))
1056 return git_default_advice_config(var, value);
1057
1058 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
1059 pager_use_color = git_config_bool(var,value);
1060 return 0;
1061 }
1062
1063 if (!strcmp(var, "pack.packsizelimit")) {
1064 pack_size_limit_cfg = git_config_ulong(var, value);
1065 return 0;
1066 }
1067 /* Add other config variables here and to Documentation/config.txt. */
1068 return 0;
1069}
1070
1071/*
1072 * All source specific fields in the union, die_on_error, name and the callbacks
1073 * fgetc, ungetc, ftell of top need to be initialized before calling
1074 * this function.
1075 */
1076static int do_config_from(struct config_source *top, config_fn_t fn, void *data)
1077{
1078 int ret;
1079
1080 /* push config-file parsing state stack */
1081 top->prev = cf;
1082 top->linenr = 1;
1083 top->eof = 0;
1084 strbuf_init(&top->value, 1024);
1085 strbuf_init(&top->var, 1024);
1086 cf = top;
1087
1088 ret = git_parse_source(fn, data);
1089
1090 /* pop config-file parsing state stack */
1091 strbuf_release(&top->value);
1092 strbuf_release(&top->var);
1093 cf = top->prev;
1094
1095 return ret;
1096}
1097
1098static int do_config_from_file(config_fn_t fn,
1099 const char *origin_type, const char *name, const char *path, FILE *f,
1100 void *data)
1101{
1102 struct config_source top;
1103
1104 top.u.file = f;
1105 top.origin_type = origin_type;
1106 top.name = name;
1107 top.path = path;
1108 top.die_on_error = 1;
1109 top.do_fgetc = config_file_fgetc;
1110 top.do_ungetc = config_file_ungetc;
1111 top.do_ftell = config_file_ftell;
1112
1113 return do_config_from(&top, fn, data);
1114}
1115
1116static int git_config_from_stdin(config_fn_t fn, void *data)
1117{
1118 return do_config_from_file(fn, "standard input", "", NULL, stdin, data);
1119}
1120
1121int git_config_from_file(config_fn_t fn, const char *filename, void *data)
1122{
1123 int ret = -1;
1124 FILE *f;
1125
1126 f = fopen(filename, "r");
1127 if (f) {
1128 flockfile(f);
1129 ret = do_config_from_file(fn, "file", filename, filename, f, data);
1130 funlockfile(f);
1131 fclose(f);
1132 }
1133 return ret;
1134}
1135
1136int git_config_from_mem(config_fn_t fn, const char *origin_type,
1137 const char *name, const char *buf, size_t len, void *data)
1138{
1139 struct config_source top;
1140
1141 top.u.buf.buf = buf;
1142 top.u.buf.len = len;
1143 top.u.buf.pos = 0;
1144 top.origin_type = origin_type;
1145 top.name = name;
1146 top.path = NULL;
1147 top.die_on_error = 0;
1148 top.do_fgetc = config_buf_fgetc;
1149 top.do_ungetc = config_buf_ungetc;
1150 top.do_ftell = config_buf_ftell;
1151
1152 return do_config_from(&top, fn, data);
1153}
1154
1155static int git_config_from_blob_sha1(config_fn_t fn,
1156 const char *name,
1157 const unsigned char *sha1,
1158 void *data)
1159{
1160 enum object_type type;
1161 char *buf;
1162 unsigned long size;
1163 int ret;
1164
1165 buf = read_sha1_file(sha1, &type, &size);
1166 if (!buf)
1167 return error("unable to load config blob object '%s'", name);
1168 if (type != OBJ_BLOB) {
1169 free(buf);
1170 return error("reference '%s' does not point to a blob", name);
1171 }
1172
1173 ret = git_config_from_mem(fn, "blob", name, buf, size, data);
1174 free(buf);
1175
1176 return ret;
1177}
1178
1179static int git_config_from_blob_ref(config_fn_t fn,
1180 const char *name,
1181 void *data)
1182{
1183 unsigned char sha1[20];
1184
1185 if (get_sha1(name, sha1) < 0)
1186 return error("unable to resolve config blob '%s'", name);
1187 return git_config_from_blob_sha1(fn, name, sha1, data);
1188}
1189
1190const char *git_etc_gitconfig(void)
1191{
1192 static const char *system_wide;
1193 if (!system_wide)
1194 system_wide = system_path(ETC_GITCONFIG);
1195 return system_wide;
1196}
1197
1198/*
1199 * Parse environment variable 'k' as a boolean (in various
1200 * possible spellings); if missing, use the default value 'def'.
1201 */
1202int git_env_bool(const char *k, int def)
1203{
1204 const char *v = getenv(k);
1205 return v ? git_config_bool(k, v) : def;
1206}
1207
1208/*
1209 * Parse environment variable 'k' as ulong with possibly a unit
1210 * suffix; if missing, use the default value 'val'.
1211 */
1212unsigned long git_env_ulong(const char *k, unsigned long val)
1213{
1214 const char *v = getenv(k);
1215 if (v && !git_parse_ulong(v, &val))
1216 die("failed to parse %s", k);
1217 return val;
1218}
1219
1220int git_config_system(void)
1221{
1222 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
1223}
1224
1225static int do_git_config_sequence(config_fn_t fn, void *data)
1226{
1227 int ret = 0;
1228 char *xdg_config = xdg_config_home("config");
1229 char *user_config = expand_user_path("~/.gitconfig");
1230 char *repo_config = git_pathdup("config");
1231
1232 if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0))
1233 ret += git_config_from_file(fn, git_etc_gitconfig(),
1234 data);
1235
1236 if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
1237 ret += git_config_from_file(fn, xdg_config, data);
1238
1239 if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
1240 ret += git_config_from_file(fn, user_config, data);
1241
1242 if (repo_config && !access_or_die(repo_config, R_OK, 0))
1243 ret += git_config_from_file(fn, repo_config, data);
1244
1245 if (git_config_from_parameters(fn, data) < 0)
1246 die(_("unable to parse command-line config"));
1247
1248 free(xdg_config);
1249 free(user_config);
1250 free(repo_config);
1251 return ret;
1252}
1253
1254int git_config_with_options(config_fn_t fn, void *data,
1255 struct git_config_source *config_source,
1256 int respect_includes)
1257{
1258 struct config_include_data inc = CONFIG_INCLUDE_INIT;
1259
1260 if (respect_includes) {
1261 inc.fn = fn;
1262 inc.data = data;
1263 fn = git_config_include;
1264 data = &inc;
1265 }
1266
1267 /*
1268 * If we have a specific filename, use it. Otherwise, follow the
1269 * regular lookup sequence.
1270 */
1271 if (config_source && config_source->use_stdin)
1272 return git_config_from_stdin(fn, data);
1273 else if (config_source && config_source->file)
1274 return git_config_from_file(fn, config_source->file, data);
1275 else if (config_source && config_source->blob)
1276 return git_config_from_blob_ref(fn, config_source->blob, data);
1277
1278 return do_git_config_sequence(fn, data);
1279}
1280
1281static void git_config_raw(config_fn_t fn, void *data)
1282{
1283 if (git_config_with_options(fn, data, NULL, 1) < 0)
1284 /*
1285 * git_config_with_options() normally returns only
1286 * zero, as most errors are fatal, and
1287 * non-fatal potential errors are guarded by "if"
1288 * statements that are entered only when no error is
1289 * possible.
1290 *
1291 * If we ever encounter a non-fatal error, it means
1292 * something went really wrong and we should stop
1293 * immediately.
1294 */
1295 die(_("unknown error occured while reading the configuration files"));
1296}
1297
1298static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
1299{
1300 int i, value_index;
1301 struct string_list *values;
1302 struct config_set_element *entry;
1303 struct configset_list *list = &cs->list;
1304
1305 for (i = 0; i < list->nr; i++) {
1306 entry = list->items[i].e;
1307 value_index = list->items[i].value_index;
1308 values = &entry->value_list;
1309
1310 current_config_kvi = values->items[value_index].util;
1311
1312 if (fn(entry->key, values->items[value_index].string, data) < 0)
1313 git_die_config_linenr(entry->key,
1314 current_config_kvi->filename,
1315 current_config_kvi->linenr);
1316
1317 current_config_kvi = NULL;
1318 }
1319}
1320
1321static void git_config_check_init(void);
1322
1323void git_config(config_fn_t fn, void *data)
1324{
1325 git_config_check_init();
1326 configset_iter(&the_config_set, fn, data);
1327}
1328
1329static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
1330{
1331 struct config_set_element k;
1332 struct config_set_element *found_entry;
1333 char *normalized_key;
1334 /*
1335 * `key` may come from the user, so normalize it before using it
1336 * for querying entries from the hashmap.
1337 */
1338 if (git_config_parse_key(key, &normalized_key, NULL))
1339 return NULL;
1340
1341 hashmap_entry_init(&k, strhash(normalized_key));
1342 k.key = normalized_key;
1343 found_entry = hashmap_get(&cs->config_hash, &k, NULL);
1344 free(normalized_key);
1345 return found_entry;
1346}
1347
1348static int configset_add_value(struct config_set *cs, const char *key, const char *value)
1349{
1350 struct config_set_element *e;
1351 struct string_list_item *si;
1352 struct configset_list_item *l_item;
1353 struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
1354
1355 e = configset_find_element(cs, key);
1356 /*
1357 * Since the keys are being fed by git_config*() callback mechanism, they
1358 * are already normalized. So simply add them without any further munging.
1359 */
1360 if (!e) {
1361 e = xmalloc(sizeof(*e));
1362 hashmap_entry_init(e, strhash(key));
1363 e->key = xstrdup(key);
1364 string_list_init(&e->value_list, 1);
1365 hashmap_add(&cs->config_hash, e);
1366 }
1367 si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
1368
1369 ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc);
1370 l_item = &cs->list.items[cs->list.nr++];
1371 l_item->e = e;
1372 l_item->value_index = e->value_list.nr - 1;
1373
1374 if (!cf)
1375 die("BUG: configset_add_value has no source");
1376 if (cf->name) {
1377 kv_info->filename = strintern(cf->name);
1378 kv_info->linenr = cf->linenr;
1379 kv_info->origin_type = strintern(cf->origin_type);
1380 } else {
1381 /* for values read from `git_config_from_parameters()` */
1382 kv_info->filename = NULL;
1383 kv_info->linenr = -1;
1384 kv_info->origin_type = NULL;
1385 }
1386 si->util = kv_info;
1387
1388 return 0;
1389}
1390
1391static int config_set_element_cmp(const struct config_set_element *e1,
1392 const struct config_set_element *e2, const void *unused)
1393{
1394 return strcmp(e1->key, e2->key);
1395}
1396
1397void git_configset_init(struct config_set *cs)
1398{
1399 hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
1400 cs->hash_initialized = 1;
1401 cs->list.nr = 0;
1402 cs->list.alloc = 0;
1403 cs->list.items = NULL;
1404}
1405
1406void git_configset_clear(struct config_set *cs)
1407{
1408 struct config_set_element *entry;
1409 struct hashmap_iter iter;
1410 if (!cs->hash_initialized)
1411 return;
1412
1413 hashmap_iter_init(&cs->config_hash, &iter);
1414 while ((entry = hashmap_iter_next(&iter))) {
1415 free(entry->key);
1416 string_list_clear(&entry->value_list, 1);
1417 }
1418 hashmap_free(&cs->config_hash, 1);
1419 cs->hash_initialized = 0;
1420 free(cs->list.items);
1421 cs->list.nr = 0;
1422 cs->list.alloc = 0;
1423 cs->list.items = NULL;
1424}
1425
1426static int config_set_callback(const char *key, const char *value, void *cb)
1427{
1428 struct config_set *cs = cb;
1429 configset_add_value(cs, key, value);
1430 return 0;
1431}
1432
1433int git_configset_add_file(struct config_set *cs, const char *filename)
1434{
1435 return git_config_from_file(config_set_callback, filename, cs);
1436}
1437
1438int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
1439{
1440 const struct string_list *values = NULL;
1441 /*
1442 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
1443 * queried key in the files of the configset, the value returned will be the last
1444 * value in the value list for that key.
1445 */
1446 values = git_configset_get_value_multi(cs, key);
1447
1448 if (!values)
1449 return 1;
1450 assert(values->nr > 0);
1451 *value = values->items[values->nr - 1].string;
1452 return 0;
1453}
1454
1455const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
1456{
1457 struct config_set_element *e = configset_find_element(cs, key);
1458 return e ? &e->value_list : NULL;
1459}
1460
1461int git_configset_get_string_const(struct config_set *cs, const char *key, const char **dest)
1462{
1463 const char *value;
1464 if (!git_configset_get_value(cs, key, &value))
1465 return git_config_string(dest, key, value);
1466 else
1467 return 1;
1468}
1469
1470int git_configset_get_string(struct config_set *cs, const char *key, char **dest)
1471{
1472 return git_configset_get_string_const(cs, key, (const char **)dest);
1473}
1474
1475int git_configset_get_int(struct config_set *cs, const char *key, int *dest)
1476{
1477 const char *value;
1478 if (!git_configset_get_value(cs, key, &value)) {
1479 *dest = git_config_int(key, value);
1480 return 0;
1481 } else
1482 return 1;
1483}
1484
1485int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest)
1486{
1487 const char *value;
1488 if (!git_configset_get_value(cs, key, &value)) {
1489 *dest = git_config_ulong(key, value);
1490 return 0;
1491 } else
1492 return 1;
1493}
1494
1495int git_configset_get_bool(struct config_set *cs, const char *key, int *dest)
1496{
1497 const char *value;
1498 if (!git_configset_get_value(cs, key, &value)) {
1499 *dest = git_config_bool(key, value);
1500 return 0;
1501 } else
1502 return 1;
1503}
1504
1505int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
1506 int *is_bool, int *dest)
1507{
1508 const char *value;
1509 if (!git_configset_get_value(cs, key, &value)) {
1510 *dest = git_config_bool_or_int(key, value, is_bool);
1511 return 0;
1512 } else
1513 return 1;
1514}
1515
1516int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest)
1517{
1518 const char *value;
1519 if (!git_configset_get_value(cs, key, &value)) {
1520 *dest = git_config_maybe_bool(key, value);
1521 if (*dest == -1)
1522 return -1;
1523 return 0;
1524 } else
1525 return 1;
1526}
1527
1528int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest)
1529{
1530 const char *value;
1531 if (!git_configset_get_value(cs, key, &value))
1532 return git_config_pathname(dest, key, value);
1533 else
1534 return 1;
1535}
1536
1537static void git_config_check_init(void)
1538{
1539 if (the_config_set.hash_initialized)
1540 return;
1541 git_configset_init(&the_config_set);
1542 git_config_raw(config_set_callback, &the_config_set);
1543}
1544
1545void git_config_clear(void)
1546{
1547 if (!the_config_set.hash_initialized)
1548 return;
1549 git_configset_clear(&the_config_set);
1550}
1551
1552int git_config_get_value(const char *key, const char **value)
1553{
1554 git_config_check_init();
1555 return git_configset_get_value(&the_config_set, key, value);
1556}
1557
1558const struct string_list *git_config_get_value_multi(const char *key)
1559{
1560 git_config_check_init();
1561 return git_configset_get_value_multi(&the_config_set, key);
1562}
1563
1564int git_config_get_string_const(const char *key, const char **dest)
1565{
1566 int ret;
1567 git_config_check_init();
1568 ret = git_configset_get_string_const(&the_config_set, key, dest);
1569 if (ret < 0)
1570 git_die_config(key, NULL);
1571 return ret;
1572}
1573
1574int git_config_get_string(const char *key, char **dest)
1575{
1576 git_config_check_init();
1577 return git_config_get_string_const(key, (const char **)dest);
1578}
1579
1580int git_config_get_int(const char *key, int *dest)
1581{
1582 git_config_check_init();
1583 return git_configset_get_int(&the_config_set, key, dest);
1584}
1585
1586int git_config_get_ulong(const char *key, unsigned long *dest)
1587{
1588 git_config_check_init();
1589 return git_configset_get_ulong(&the_config_set, key, dest);
1590}
1591
1592int git_config_get_bool(const char *key, int *dest)
1593{
1594 git_config_check_init();
1595 return git_configset_get_bool(&the_config_set, key, dest);
1596}
1597
1598int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
1599{
1600 git_config_check_init();
1601 return git_configset_get_bool_or_int(&the_config_set, key, is_bool, dest);
1602}
1603
1604int git_config_get_maybe_bool(const char *key, int *dest)
1605{
1606 git_config_check_init();
1607 return git_configset_get_maybe_bool(&the_config_set, key, dest);
1608}
1609
1610int git_config_get_pathname(const char *key, const char **dest)
1611{
1612 int ret;
1613 git_config_check_init();
1614 ret = git_configset_get_pathname(&the_config_set, key, dest);
1615 if (ret < 0)
1616 git_die_config(key, NULL);
1617 return ret;
1618}
1619
1620int git_config_get_untracked_cache(void)
1621{
1622 int val = -1;
1623 const char *v;
1624
1625 /* Hack for test programs like test-dump-untracked-cache */
1626 if (ignore_untracked_cache_config)
1627 return -1;
1628
1629 if (!git_config_get_maybe_bool("core.untrackedcache", &val))
1630 return val;
1631
1632 if (!git_config_get_value("core.untrackedcache", &v)) {
1633 if (!strcasecmp(v, "keep"))
1634 return -1;
1635
1636 error("unknown core.untrackedCache value '%s'; "
1637 "using 'keep' default value", v);
1638 return -1;
1639 }
1640
1641 return -1; /* default value */
1642}
1643
1644NORETURN
1645void git_die_config_linenr(const char *key, const char *filename, int linenr)
1646{
1647 if (!filename)
1648 die(_("unable to parse '%s' from command-line config"), key);
1649 else
1650 die(_("bad config variable '%s' in file '%s' at line %d"),
1651 key, filename, linenr);
1652}
1653
1654NORETURN __attribute__((format(printf, 2, 3)))
1655void git_die_config(const char *key, const char *err, ...)
1656{
1657 const struct string_list *values;
1658 struct key_value_info *kv_info;
1659
1660 if (err) {
1661 va_list params;
1662 va_start(params, err);
1663 vreportf("error: ", err, params);
1664 va_end(params);
1665 }
1666 values = git_config_get_value_multi(key);
1667 kv_info = values->items[values->nr - 1].util;
1668 git_die_config_linenr(key, kv_info->filename, kv_info->linenr);
1669}
1670
1671/*
1672 * Find all the stuff for git_config_set() below.
1673 */
1674
1675static struct {
1676 int baselen;
1677 char *key;
1678 int do_not_match;
1679 regex_t *value_regex;
1680 int multi_replace;
1681 size_t *offset;
1682 unsigned int offset_alloc;
1683 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
1684 int seen;
1685} store;
1686
1687static int matches(const char *key, const char *value)
1688{
1689 if (strcmp(key, store.key))
1690 return 0; /* not ours */
1691 if (!store.value_regex)
1692 return 1; /* always matches */
1693 if (store.value_regex == CONFIG_REGEX_NONE)
1694 return 0; /* never matches */
1695
1696 return store.do_not_match ^
1697 (value && !regexec(store.value_regex, value, 0, NULL, 0));
1698}
1699
1700static int store_aux(const char *key, const char *value, void *cb)
1701{
1702 const char *ep;
1703 size_t section_len;
1704
1705 switch (store.state) {
1706 case KEY_SEEN:
1707 if (matches(key, value)) {
1708 if (store.seen == 1 && store.multi_replace == 0) {
1709 warning(_("%s has multiple values"), key);
1710 }
1711
1712 ALLOC_GROW(store.offset, store.seen + 1,
1713 store.offset_alloc);
1714
1715 store.offset[store.seen] = cf->do_ftell(cf);
1716 store.seen++;
1717 }
1718 break;
1719 case SECTION_SEEN:
1720 /*
1721 * What we are looking for is in store.key (both
1722 * section and var), and its section part is baselen
1723 * long. We found key (again, both section and var).
1724 * We would want to know if this key is in the same
1725 * section as what we are looking for. We already
1726 * know we are in the same section as what should
1727 * hold store.key.
1728 */
1729 ep = strrchr(key, '.');
1730 section_len = ep - key;
1731
1732 if ((section_len != store.baselen) ||
1733 memcmp(key, store.key, section_len+1)) {
1734 store.state = SECTION_END_SEEN;
1735 break;
1736 }
1737
1738 /*
1739 * Do not increment matches: this is no match, but we
1740 * just made sure we are in the desired section.
1741 */
1742 ALLOC_GROW(store.offset, store.seen + 1,
1743 store.offset_alloc);
1744 store.offset[store.seen] = cf->do_ftell(cf);
1745 /* fallthru */
1746 case SECTION_END_SEEN:
1747 case START:
1748 if (matches(key, value)) {
1749 ALLOC_GROW(store.offset, store.seen + 1,
1750 store.offset_alloc);
1751 store.offset[store.seen] = cf->do_ftell(cf);
1752 store.state = KEY_SEEN;
1753 store.seen++;
1754 } else {
1755 if (strrchr(key, '.') - key == store.baselen &&
1756 !strncmp(key, store.key, store.baselen)) {
1757 store.state = SECTION_SEEN;
1758 ALLOC_GROW(store.offset,
1759 store.seen + 1,
1760 store.offset_alloc);
1761 store.offset[store.seen] = cf->do_ftell(cf);
1762 }
1763 }
1764 }
1765 return 0;
1766}
1767
1768static int write_error(const char *filename)
1769{
1770 error("failed to write new configuration file %s", filename);
1771
1772 /* Same error code as "failed to rename". */
1773 return 4;
1774}
1775
1776static int store_write_section(int fd, const char *key)
1777{
1778 const char *dot;
1779 int i, success;
1780 struct strbuf sb = STRBUF_INIT;
1781
1782 dot = memchr(key, '.', store.baselen);
1783 if (dot) {
1784 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
1785 for (i = dot - key + 1; i < store.baselen; i++) {
1786 if (key[i] == '"' || key[i] == '\\')
1787 strbuf_addch(&sb, '\\');
1788 strbuf_addch(&sb, key[i]);
1789 }
1790 strbuf_addstr(&sb, "\"]\n");
1791 } else {
1792 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
1793 }
1794
1795 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
1796 strbuf_release(&sb);
1797
1798 return success;
1799}
1800
1801static int store_write_pair(int fd, const char *key, const char *value)
1802{
1803 int i, success;
1804 int length = strlen(key + store.baselen + 1);
1805 const char *quote = "";
1806 struct strbuf sb = STRBUF_INIT;
1807
1808 /*
1809 * Check to see if the value needs to be surrounded with a dq pair.
1810 * Note that problematic characters are always backslash-quoted; this
1811 * check is about not losing leading or trailing SP and strings that
1812 * follow beginning-of-comment characters (i.e. ';' and '#') by the
1813 * configuration parser.
1814 */
1815 if (value[0] == ' ')
1816 quote = "\"";
1817 for (i = 0; value[i]; i++)
1818 if (value[i] == ';' || value[i] == '#')
1819 quote = "\"";
1820 if (i && value[i - 1] == ' ')
1821 quote = "\"";
1822
1823 strbuf_addf(&sb, "\t%.*s = %s",
1824 length, key + store.baselen + 1, quote);
1825
1826 for (i = 0; value[i]; i++)
1827 switch (value[i]) {
1828 case '\n':
1829 strbuf_addstr(&sb, "\\n");
1830 break;
1831 case '\t':
1832 strbuf_addstr(&sb, "\\t");
1833 break;
1834 case '"':
1835 case '\\':
1836 strbuf_addch(&sb, '\\');
1837 default:
1838 strbuf_addch(&sb, value[i]);
1839 break;
1840 }
1841 strbuf_addf(&sb, "%s\n", quote);
1842
1843 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
1844 strbuf_release(&sb);
1845
1846 return success;
1847}
1848
1849static ssize_t find_beginning_of_line(const char *contents, size_t size,
1850 size_t offset_, int *found_bracket)
1851{
1852 size_t equal_offset = size, bracket_offset = size;
1853 ssize_t offset;
1854
1855contline:
1856 for (offset = offset_-2; offset > 0
1857 && contents[offset] != '\n'; offset--)
1858 switch (contents[offset]) {
1859 case '=': equal_offset = offset; break;
1860 case ']': bracket_offset = offset; break;
1861 }
1862 if (offset > 0 && contents[offset-1] == '\\') {
1863 offset_ = offset;
1864 goto contline;
1865 }
1866 if (bracket_offset < equal_offset) {
1867 *found_bracket = 1;
1868 offset = bracket_offset+1;
1869 } else
1870 offset++;
1871
1872 return offset;
1873}
1874
1875int git_config_set_in_file_gently(const char *config_filename,
1876 const char *key, const char *value)
1877{
1878 return git_config_set_multivar_in_file_gently(config_filename, key, value, NULL, 0);
1879}
1880
1881void git_config_set_in_file(const char *config_filename,
1882 const char *key, const char *value)
1883{
1884 git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
1885}
1886
1887int git_config_set_gently(const char *key, const char *value)
1888{
1889 return git_config_set_multivar_gently(key, value, NULL, 0);
1890}
1891
1892void git_config_set(const char *key, const char *value)
1893{
1894 git_config_set_multivar(key, value, NULL, 0);
1895}
1896
1897/*
1898 * Auxiliary function to sanity-check and split the key into the section
1899 * identifier and variable name.
1900 *
1901 * Returns 0 on success, -1 when there is an invalid character in the key and
1902 * -2 if there is no section name in the key.
1903 *
1904 * store_key - pointer to char* which will hold a copy of the key with
1905 * lowercase section and variable name
1906 * baselen - pointer to int which will hold the length of the
1907 * section + subsection part, can be NULL
1908 */
1909static int git_config_parse_key_1(const char *key, char **store_key, int *baselen_, int quiet)
1910{
1911 int i, dot, baselen;
1912 const char *last_dot = strrchr(key, '.');
1913
1914 /*
1915 * Since "key" actually contains the section name and the real
1916 * key name separated by a dot, we have to know where the dot is.
1917 */
1918
1919 if (last_dot == NULL || last_dot == key) {
1920 if (!quiet)
1921 error("key does not contain a section: %s", key);
1922 return -CONFIG_NO_SECTION_OR_NAME;
1923 }
1924
1925 if (!last_dot[1]) {
1926 if (!quiet)
1927 error("key does not contain variable name: %s", key);
1928 return -CONFIG_NO_SECTION_OR_NAME;
1929 }
1930
1931 baselen = last_dot - key;
1932 if (baselen_)
1933 *baselen_ = baselen;
1934
1935 /*
1936 * Validate the key and while at it, lower case it for matching.
1937 */
1938 if (store_key)
1939 *store_key = xmallocz(strlen(key));
1940
1941 dot = 0;
1942 for (i = 0; key[i]; i++) {
1943 unsigned char c = key[i];
1944 if (c == '.')
1945 dot = 1;
1946 /* Leave the extended basename untouched.. */
1947 if (!dot || i > baselen) {
1948 if (!iskeychar(c) ||
1949 (i == baselen + 1 && !isalpha(c))) {
1950 if (!quiet)
1951 error("invalid key: %s", key);
1952 goto out_free_ret_1;
1953 }
1954 c = tolower(c);
1955 } else if (c == '\n') {
1956 if (!quiet)
1957 error("invalid key (newline): %s", key);
1958 goto out_free_ret_1;
1959 }
1960 if (store_key)
1961 (*store_key)[i] = c;
1962 }
1963
1964 return 0;
1965
1966out_free_ret_1:
1967 if (store_key) {
1968 free(*store_key);
1969 *store_key = NULL;
1970 }
1971 return -CONFIG_INVALID_KEY;
1972}
1973
1974int git_config_parse_key(const char *key, char **store_key, int *baselen)
1975{
1976 return git_config_parse_key_1(key, store_key, baselen, 0);
1977}
1978
1979int git_config_key_is_valid(const char *key)
1980{
1981 return !git_config_parse_key_1(key, NULL, NULL, 1);
1982}
1983
1984/*
1985 * If value==NULL, unset in (remove from) config,
1986 * if value_regex!=NULL, disregard key/value pairs where value does not match.
1987 * if value_regex==CONFIG_REGEX_NONE, do not match any existing values
1988 * (only add a new one)
1989 * if multi_replace==0, nothing, or only one matching key/value is replaced,
1990 * else all matching key/values (regardless how many) are removed,
1991 * before the new pair is written.
1992 *
1993 * Returns 0 on success.
1994 *
1995 * This function does this:
1996 *
1997 * - it locks the config file by creating ".git/config.lock"
1998 *
1999 * - it then parses the config using store_aux() as validator to find
2000 * the position on the key/value pair to replace. If it is to be unset,
2001 * it must be found exactly once.
2002 *
2003 * - the config file is mmap()ed and the part before the match (if any) is
2004 * written to the lock file, then the changed part and the rest.
2005 *
2006 * - the config file is removed and the lock file rename()d to it.
2007 *
2008 */
2009int git_config_set_multivar_in_file_gently(const char *config_filename,
2010 const char *key, const char *value,
2011 const char *value_regex,
2012 int multi_replace)
2013{
2014 int fd = -1, in_fd = -1;
2015 int ret;
2016 struct lock_file *lock = NULL;
2017 char *filename_buf = NULL;
2018 char *contents = NULL;
2019 size_t contents_sz;
2020
2021 /* parse-key returns negative; flip the sign to feed exit(3) */
2022 ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
2023 if (ret)
2024 goto out_free;
2025
2026 store.multi_replace = multi_replace;
2027
2028 if (!config_filename)
2029 config_filename = filename_buf = git_pathdup("config");
2030
2031 /*
2032 * The lock serves a purpose in addition to locking: the new
2033 * contents of .git/config will be written into it.
2034 */
2035 lock = xcalloc(1, sizeof(struct lock_file));
2036 fd = hold_lock_file_for_update(lock, config_filename, 0);
2037 if (fd < 0) {
2038 error_errno("could not lock config file %s", config_filename);
2039 free(store.key);
2040 ret = CONFIG_NO_LOCK;
2041 goto out_free;
2042 }
2043
2044 /*
2045 * If .git/config does not exist yet, write a minimal version.
2046 */
2047 in_fd = open(config_filename, O_RDONLY);
2048 if ( in_fd < 0 ) {
2049 free(store.key);
2050
2051 if ( ENOENT != errno ) {
2052 error_errno("opening %s", config_filename);
2053 ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
2054 goto out_free;
2055 }
2056 /* if nothing to unset, error out */
2057 if (value == NULL) {
2058 ret = CONFIG_NOTHING_SET;
2059 goto out_free;
2060 }
2061
2062 store.key = (char *)key;
2063 if (!store_write_section(fd, key) ||
2064 !store_write_pair(fd, key, value))
2065 goto write_err_out;
2066 } else {
2067 struct stat st;
2068 size_t copy_begin, copy_end;
2069 int i, new_line = 0;
2070
2071 if (value_regex == NULL)
2072 store.value_regex = NULL;
2073 else if (value_regex == CONFIG_REGEX_NONE)
2074 store.value_regex = CONFIG_REGEX_NONE;
2075 else {
2076 if (value_regex[0] == '!') {
2077 store.do_not_match = 1;
2078 value_regex++;
2079 } else
2080 store.do_not_match = 0;
2081
2082 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
2083 if (regcomp(store.value_regex, value_regex,
2084 REG_EXTENDED)) {
2085 error("invalid pattern: %s", value_regex);
2086 free(store.value_regex);
2087 ret = CONFIG_INVALID_PATTERN;
2088 goto out_free;
2089 }
2090 }
2091
2092 ALLOC_GROW(store.offset, 1, store.offset_alloc);
2093 store.offset[0] = 0;
2094 store.state = START;
2095 store.seen = 0;
2096
2097 /*
2098 * After this, store.offset will contain the *end* offset
2099 * of the last match, or remain at 0 if no match was found.
2100 * As a side effect, we make sure to transform only a valid
2101 * existing config file.
2102 */
2103 if (git_config_from_file(store_aux, config_filename, NULL)) {
2104 error("invalid config file %s", config_filename);
2105 free(store.key);
2106 if (store.value_regex != NULL &&
2107 store.value_regex != CONFIG_REGEX_NONE) {
2108 regfree(store.value_regex);
2109 free(store.value_regex);
2110 }
2111 ret = CONFIG_INVALID_FILE;
2112 goto out_free;
2113 }
2114
2115 free(store.key);
2116 if (store.value_regex != NULL &&
2117 store.value_regex != CONFIG_REGEX_NONE) {
2118 regfree(store.value_regex);
2119 free(store.value_regex);
2120 }
2121
2122 /* if nothing to unset, or too many matches, error out */
2123 if ((store.seen == 0 && value == NULL) ||
2124 (store.seen > 1 && multi_replace == 0)) {
2125 ret = CONFIG_NOTHING_SET;
2126 goto out_free;
2127 }
2128
2129 fstat(in_fd, &st);
2130 contents_sz = xsize_t(st.st_size);
2131 contents = xmmap_gently(NULL, contents_sz, PROT_READ,
2132 MAP_PRIVATE, in_fd, 0);
2133 if (contents == MAP_FAILED) {
2134 if (errno == ENODEV && S_ISDIR(st.st_mode))
2135 errno = EISDIR;
2136 error_errno("unable to mmap '%s'", config_filename);
2137 ret = CONFIG_INVALID_FILE;
2138 contents = NULL;
2139 goto out_free;
2140 }
2141 close(in_fd);
2142 in_fd = -1;
2143
2144 if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
2145 error_errno("chmod on %s failed", get_lock_file_path(lock));
2146 ret = CONFIG_NO_WRITE;
2147 goto out_free;
2148 }
2149
2150 if (store.seen == 0)
2151 store.seen = 1;
2152
2153 for (i = 0, copy_begin = 0; i < store.seen; i++) {
2154 if (store.offset[i] == 0) {
2155 store.offset[i] = copy_end = contents_sz;
2156 } else if (store.state != KEY_SEEN) {
2157 copy_end = store.offset[i];
2158 } else
2159 copy_end = find_beginning_of_line(
2160 contents, contents_sz,
2161 store.offset[i]-2, &new_line);
2162
2163 if (copy_end > 0 && contents[copy_end-1] != '\n')
2164 new_line = 1;
2165
2166 /* write the first part of the config */
2167 if (copy_end > copy_begin) {
2168 if (write_in_full(fd, contents + copy_begin,
2169 copy_end - copy_begin) <
2170 copy_end - copy_begin)
2171 goto write_err_out;
2172 if (new_line &&
2173 write_str_in_full(fd, "\n") != 1)
2174 goto write_err_out;
2175 }
2176 copy_begin = store.offset[i];
2177 }
2178
2179 /* write the pair (value == NULL means unset) */
2180 if (value != NULL) {
2181 if (store.state == START) {
2182 if (!store_write_section(fd, key))
2183 goto write_err_out;
2184 }
2185 if (!store_write_pair(fd, key, value))
2186 goto write_err_out;
2187 }
2188
2189 /* write the rest of the config */
2190 if (copy_begin < contents_sz)
2191 if (write_in_full(fd, contents + copy_begin,
2192 contents_sz - copy_begin) <
2193 contents_sz - copy_begin)
2194 goto write_err_out;
2195
2196 munmap(contents, contents_sz);
2197 contents = NULL;
2198 }
2199
2200 if (commit_lock_file(lock) < 0) {
2201 error_errno("could not write config file %s", config_filename);
2202 ret = CONFIG_NO_WRITE;
2203 lock = NULL;
2204 goto out_free;
2205 }
2206
2207 /*
2208 * lock is committed, so don't try to roll it back below.
2209 * NOTE: Since lockfile.c keeps a linked list of all created
2210 * lock_file structures, it isn't safe to free(lock). It's
2211 * better to just leave it hanging around.
2212 */
2213 lock = NULL;
2214 ret = 0;
2215
2216 /* Invalidate the config cache */
2217 git_config_clear();
2218
2219out_free:
2220 if (lock)
2221 rollback_lock_file(lock);
2222 free(filename_buf);
2223 if (contents)
2224 munmap(contents, contents_sz);
2225 if (in_fd >= 0)
2226 close(in_fd);
2227 return ret;
2228
2229write_err_out:
2230 ret = write_error(get_lock_file_path(lock));
2231 goto out_free;
2232
2233}
2234
2235void git_config_set_multivar_in_file(const char *config_filename,
2236 const char *key, const char *value,
2237 const char *value_regex, int multi_replace)
2238{
2239 if (!git_config_set_multivar_in_file_gently(config_filename, key, value,
2240 value_regex, multi_replace))
2241 return;
2242 if (value)
2243 die(_("could not set '%s' to '%s'"), key, value);
2244 else
2245 die(_("could not unset '%s'"), key);
2246}
2247
2248int git_config_set_multivar_gently(const char *key, const char *value,
2249 const char *value_regex, int multi_replace)
2250{
2251 return git_config_set_multivar_in_file_gently(NULL, key, value, value_regex,
2252 multi_replace);
2253}
2254
2255void git_config_set_multivar(const char *key, const char *value,
2256 const char *value_regex, int multi_replace)
2257{
2258 git_config_set_multivar_in_file(NULL, key, value, value_regex,
2259 multi_replace);
2260}
2261
2262static int section_name_match (const char *buf, const char *name)
2263{
2264 int i = 0, j = 0, dot = 0;
2265 if (buf[i] != '[')
2266 return 0;
2267 for (i = 1; buf[i] && buf[i] != ']'; i++) {
2268 if (!dot && isspace(buf[i])) {
2269 dot = 1;
2270 if (name[j++] != '.')
2271 break;
2272 for (i++; isspace(buf[i]); i++)
2273 ; /* do nothing */
2274 if (buf[i] != '"')
2275 break;
2276 continue;
2277 }
2278 if (buf[i] == '\\' && dot)
2279 i++;
2280 else if (buf[i] == '"' && dot) {
2281 for (i++; isspace(buf[i]); i++)
2282 ; /* do_nothing */
2283 break;
2284 }
2285 if (buf[i] != name[j++])
2286 break;
2287 }
2288 if (buf[i] == ']' && name[j] == 0) {
2289 /*
2290 * We match, now just find the right length offset by
2291 * gobbling up any whitespace after it, as well
2292 */
2293 i++;
2294 for (; buf[i] && isspace(buf[i]); i++)
2295 ; /* do nothing */
2296 return i;
2297 }
2298 return 0;
2299}
2300
2301static int section_name_is_ok(const char *name)
2302{
2303 /* Empty section names are bogus. */
2304 if (!*name)
2305 return 0;
2306
2307 /*
2308 * Before a dot, we must be alphanumeric or dash. After the first dot,
2309 * anything goes, so we can stop checking.
2310 */
2311 for (; *name && *name != '.'; name++)
2312 if (*name != '-' && !isalnum(*name))
2313 return 0;
2314 return 1;
2315}
2316
2317/* if new_name == NULL, the section is removed instead */
2318int git_config_rename_section_in_file(const char *config_filename,
2319 const char *old_name, const char *new_name)
2320{
2321 int ret = 0, remove = 0;
2322 char *filename_buf = NULL;
2323 struct lock_file *lock;
2324 int out_fd;
2325 char buf[1024];
2326 FILE *config_file;
2327 struct stat st;
2328
2329 if (new_name && !section_name_is_ok(new_name)) {
2330 ret = error("invalid section name: %s", new_name);
2331 goto out;
2332 }
2333
2334 if (!config_filename)
2335 config_filename = filename_buf = git_pathdup("config");
2336
2337 lock = xcalloc(1, sizeof(struct lock_file));
2338 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
2339 if (out_fd < 0) {
2340 ret = error("could not lock config file %s", config_filename);
2341 goto out;
2342 }
2343
2344 if (!(config_file = fopen(config_filename, "rb"))) {
2345 /* no config file means nothing to rename, no error */
2346 goto unlock_and_out;
2347 }
2348
2349 fstat(fileno(config_file), &st);
2350
2351 if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
2352 ret = error_errno("chmod on %s failed",
2353 get_lock_file_path(lock));
2354 goto out;
2355 }
2356
2357 while (fgets(buf, sizeof(buf), config_file)) {
2358 int i;
2359 int length;
2360 char *output = buf;
2361 for (i = 0; buf[i] && isspace(buf[i]); i++)
2362 ; /* do nothing */
2363 if (buf[i] == '[') {
2364 /* it's a section */
2365 int offset = section_name_match(&buf[i], old_name);
2366 if (offset > 0) {
2367 ret++;
2368 if (new_name == NULL) {
2369 remove = 1;
2370 continue;
2371 }
2372 store.baselen = strlen(new_name);
2373 if (!store_write_section(out_fd, new_name)) {
2374 ret = write_error(get_lock_file_path(lock));
2375 goto out;
2376 }
2377 /*
2378 * We wrote out the new section, with
2379 * a newline, now skip the old
2380 * section's length
2381 */
2382 output += offset + i;
2383 if (strlen(output) > 0) {
2384 /*
2385 * More content means there's
2386 * a declaration to put on the
2387 * next line; indent with a
2388 * tab
2389 */
2390 output -= 1;
2391 output[0] = '\t';
2392 }
2393 }
2394 remove = 0;
2395 }
2396 if (remove)
2397 continue;
2398 length = strlen(output);
2399 if (write_in_full(out_fd, output, length) != length) {
2400 ret = write_error(get_lock_file_path(lock));
2401 goto out;
2402 }
2403 }
2404 fclose(config_file);
2405unlock_and_out:
2406 if (commit_lock_file(lock) < 0)
2407 ret = error_errno("could not write config file %s",
2408 config_filename);
2409out:
2410 free(filename_buf);
2411 return ret;
2412}
2413
2414int git_config_rename_section(const char *old_name, const char *new_name)
2415{
2416 return git_config_rename_section_in_file(NULL, old_name, new_name);
2417}
2418
2419/*
2420 * Call this to report error for your variable that should not
2421 * get a boolean value (i.e. "[my] var" means "true").
2422 */
2423#undef config_error_nonbool
2424int config_error_nonbool(const char *var)
2425{
2426 return error("missing value for '%s'", var);
2427}
2428
2429int parse_config_key(const char *var,
2430 const char *section,
2431 const char **subsection, int *subsection_len,
2432 const char **key)
2433{
2434 int section_len = strlen(section);
2435 const char *dot;
2436
2437 /* Does it start with "section." ? */
2438 if (!starts_with(var, section) || var[section_len] != '.')
2439 return -1;
2440
2441 /*
2442 * Find the key; we don't know yet if we have a subsection, but we must
2443 * parse backwards from the end, since the subsection may have dots in
2444 * it, too.
2445 */
2446 dot = strrchr(var, '.');
2447 *key = dot + 1;
2448
2449 /* Did we have a subsection at all? */
2450 if (dot == var + section_len) {
2451 *subsection = NULL;
2452 *subsection_len = 0;
2453 }
2454 else {
2455 *subsection = var + section_len + 1;
2456 *subsection_len = dot - *subsection;
2457 }
2458
2459 return 0;
2460}
2461
2462const char *current_config_origin_type(void)
2463{
2464 const char *type;
2465 if (current_config_kvi)
2466 type = current_config_kvi->origin_type;
2467 else if(cf)
2468 type = cf->origin_type;
2469 else
2470 die("BUG: current_config_origin_type called outside config callback");
2471 return type ? type : "command line";
2472}
2473
2474const char *current_config_name(void)
2475{
2476 const char *name;
2477 if (current_config_kvi)
2478 name = current_config_kvi->filename;
2479 else if (cf)
2480 name = cf->name;
2481 else
2482 die("BUG: current_config_name called outside config callback");
2483 return name ? name : "";
2484}