]> git.ipfire.org Git - thirdparty/git.git/blame - config.c
config: return configset value for current_config_ functions
[thirdparty/git.git] / config.c
CommitLineData
10bea152
JS
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
6 *
7 */
17712991 8#include "cache.h"
697cc8ef 9#include "lockfile.h"
7f0e39fa 10#include "exec_cmd.h"
572e4f6a 11#include "strbuf.h"
2b64fc89 12#include "quote.h"
3c8687a7
TA
13#include "hashmap.h"
14#include "string-list.h"
599446dc 15#include "utf8.h"
17712991 16
4d8dd149
HV
17struct config_source {
18 struct config_source *prev;
19 union {
20 FILE *file;
1bc88819
HV
21 struct config_buf {
22 const char *buf;
23 size_t len;
24 size_t pos;
25 } buf;
4d8dd149 26 } u;
473166b9 27 const char *origin_type;
924aaf3e 28 const char *name;
d14d4244 29 const char *path;
b2dc0945 30 int die_on_error;
924aaf3e
RJ
31 int linenr;
32 int eof;
33 struct strbuf value;
0971e992 34 struct strbuf var;
924aaf3e 35
49d6cfa5
JK
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);
4d8dd149
HV
39};
40
0d44a2da
JK
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 */
4d8dd149 57static struct config_source *cf;
0d44a2da 58static struct key_value_info *current_config_kvi;
924aaf3e 59
960ccca6
DH
60static int zlib_compression_seen;
61
3c8687a7
TA
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
4d8dd149
HV
69static int config_file_fgetc(struct config_source *conf)
70{
260d408e 71 return getc_unlocked(conf->u.file);
4d8dd149
HV
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
1bc88819
HV
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{
1d0655c1
JK
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 }
1bc88819
HV
101
102 return EOF;
103}
104
105static long config_buf_ftell(struct config_source *conf)
106{
107 return conf->u.buf.pos;
108}
109
9b25a0b5
JK
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;
67beb600 121 char *expanded;
4c0a89fc 122
67beb600
JK
123 if (!path)
124 return config_error_nonbool("include.path");
125
126 expanded = expand_user_path(path);
4c0a89fc 127 if (!expanded)
8c3ca351 128 return error("could not expand include path '%s'", path);
4c0a89fc 129 path = expanded;
9b25a0b5
JK
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
d14d4244 138 if (!cf || !cf->path)
9b25a0b5
JK
139 return error("relative config includes must come from files");
140
d14d4244 141 slash = find_last_dir_sep(cf->path);
9b25a0b5 142 if (slash)
d14d4244 143 strbuf_add(&buf, cf->path, slash - cf->path + 1);
9b25a0b5
JK
144 strbuf_addstr(&buf, path);
145 path = buf.buf;
146 }
147
4698c8fe 148 if (!access_or_die(path, R_OK, 0)) {
9b25a0b5
JK
149 if (++inc->depth > MAX_INCLUDE_DEPTH)
150 die(include_depth_advice, MAX_INCLUDE_DEPTH, path,
3258258f
JK
151 !cf ? "<unknown>" :
152 cf->name ? cf->name :
153 "the command line");
9b25a0b5
JK
154 ret = git_config_from_file(git_config_include, path, inc);
155 inc->depth--;
156 }
157 strbuf_release(&buf);
4c0a89fc 158 free(expanded);
9b25a0b5
JK
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;
9b25a0b5
JK
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
37007c3a 175 if (!strcmp(var, "include.path"))
9b25a0b5
JK
176 ret = handle_path_include(value, inc);
177 return ret;
178}
179
2b64fc89
JK
180void git_config_push_parameter(const char *text)
181{
182 struct strbuf env = STRBUF_INIT;
183 const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
d1f88498 184 if (old && *old) {
2b64fc89
JK
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
2496844b
JK
193int git_config_parse_parameter(const char *text,
194 config_fn_t fn, void *data)
8b1fa778 195{
a789ca70 196 const char *value;
572e4f6a 197 struct strbuf **pair;
a789ca70 198
f77bccae 199 pair = strbuf_split_str(text, '=', 2);
c5d6350b
JK
200 if (!pair[0])
201 return error("bogus config parameter: %s", text);
a789ca70
JH
202
203 if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') {
572e4f6a 204 strbuf_setlen(pair[0], pair[0]->len - 1);
a789ca70
JH
205 value = pair[1] ? pair[1]->buf : "";
206 } else {
207 value = NULL;
208 }
209
572e4f6a
AR
210 strbuf_trim(pair[0]);
211 if (!pair[0]->len) {
212 strbuf_list_free(pair);
06eb708f 213 return error("bogus config parameter: %s", text);
8b1fa778 214 }
ffb20ce1 215 strbuf_tolower(pair[0]);
a789ca70 216 if (fn(pair[0]->buf, value, data) < 0) {
06eb708f
JK
217 strbuf_list_free(pair);
218 return -1;
572e4f6a
AR
219 }
220 strbuf_list_free(pair);
8b1fa778
AR
221 return 0;
222}
223
06eb708f
JK
224int git_config_from_parameters(config_fn_t fn, void *data)
225{
2b64fc89 226 const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
a77d6db6 227 int ret = 0;
2b64fc89
JK
228 char *envw;
229 const char **argv = NULL;
230 int nr = 0, alloc = 0;
231 int i;
3258258f 232 struct config_source source;
2b64fc89
JK
233
234 if (!env)
235 return 0;
3258258f
JK
236
237 memset(&source, 0, sizeof(source));
238 source.prev = cf;
239 cf = &source;
240
2b64fc89
JK
241 /* sq_dequote will write over it */
242 envw = xstrdup(env);
243
244 if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
a77d6db6
JK
245 ret = error("bogus format in " CONFIG_DATA_ENVIRONMENT);
246 goto out;
2b64fc89
JK
247 }
248
249 for (i = 0; i < nr; i++) {
06eb708f 250 if (git_config_parse_parameter(argv[i], fn, data) < 0) {
a77d6db6
JK
251 ret = -1;
252 goto out;
2b64fc89
JK
253 }
254 }
255
a77d6db6 256out:
2b64fc89
JK
257 free(argv);
258 free(envw);
3258258f 259 cf = source.prev;
a77d6db6 260 return ret;
2b64fc89
JK
261}
262
17712991
LT
263static int get_next_char(void)
264{
49d6cfa5 265 int c = cf->do_fgetc(cf);
17712991 266
dbb9a812
HV
267 if (c == '\r') {
268 /* DOS like systems */
49d6cfa5 269 c = cf->do_fgetc(cf);
dbb9a812 270 if (c != '\n') {
5e0be134
JK
271 if (c != EOF)
272 cf->do_ungetc(c, cf);
dbb9a812 273 c = '\r';
17712991
LT
274 }
275 }
dbb9a812
HV
276 if (c == '\n')
277 cf->linenr++;
278 if (c == EOF) {
279 cf->eof = 1;
b3b3f60b 280 cf->linenr++;
dbb9a812
HV
281 c = '\n';
282 }
17712991
LT
283 return c;
284}
285
286static char *parse_value(void)
287{
e96c19c5 288 int quote = 0, comment = 0, space = 0;
17712991 289
924aaf3e 290 strbuf_reset(&cf->value);
17712991
LT
291 for (;;) {
292 int c = get_next_char();
17712991 293 if (c == '\n') {
4b340593
MS
294 if (quote) {
295 cf->linenr--;
17712991 296 return NULL;
4b340593 297 }
924aaf3e 298 return cf->value.buf;
17712991
LT
299 }
300 if (comment)
301 continue;
302 if (isspace(c) && !quote) {
924aaf3e 303 if (cf->value.len)
ebdaae37 304 space++;
17712991
LT
305 continue;
306 }
7ebdba61
JS
307 if (!quote) {
308 if (c == ';' || c == '#') {
309 comment = 1;
310 continue;
311 }
312 }
ebdaae37 313 for (; space; space--)
924aaf3e 314 strbuf_addch(&cf->value, ' ');
17712991
LT
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;
5cbb401d
LT
329 /* Some characters escape as themselves */
330 case '\\': case '"':
331 break;
332 /* Reject unknown escape sequences */
333 default:
334 return NULL;
17712991 335 }
924aaf3e 336 strbuf_addch(&cf->value, c);
17712991
LT
337 continue;
338 }
339 if (c == '"') {
340 quote = 1-quote;
341 continue;
342 }
924aaf3e 343 strbuf_addch(&cf->value, c);
17712991
LT
344 }
345}
346
38c5afa8
LT
347static inline int iskeychar(int c)
348{
349 return isalnum(c) || c == '-';
350}
351
0971e992 352static int get_value(config_fn_t fn, void *data, struct strbuf *name)
17712991
LT
353{
354 int c;
355 char *value;
b3b3f60b 356 int ret;
17712991
LT
357
358 /* Get the full name */
359 for (;;) {
360 c = get_next_char();
924aaf3e 361 if (cf->eof)
17712991 362 break;
38c5afa8 363 if (!iskeychar(c))
17712991 364 break;
0971e992 365 strbuf_addch(name, tolower(c));
17712991 366 }
0971e992 367
17712991
LT
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 }
b3b3f60b
MM
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;
17712991
LT
388}
389
0971e992 390static int get_extended_base_var(struct strbuf *name, int c)
d14f7764
LT
391{
392 do {
393 if (c == '\n')
4b340593 394 goto error_incomplete_line;
d14f7764
LT
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;
0971e992 401 strbuf_addch(name, '.');
d14f7764
LT
402
403 for (;;) {
404 int c = get_next_char();
405 if (c == '\n')
4b340593 406 goto error_incomplete_line;
d14f7764
LT
407 if (c == '"')
408 break;
409 if (c == '\\') {
410 c = get_next_char();
411 if (c == '\n')
4b340593 412 goto error_incomplete_line;
d14f7764 413 }
0971e992 414 strbuf_addch(name, c);
d14f7764
LT
415 }
416
417 /* Final ']' */
418 if (get_next_char() != ']')
419 return -1;
0971e992 420 return 0;
4b340593
MS
421error_incomplete_line:
422 cf->linenr--;
423 return -1;
d14f7764
LT
424}
425
0971e992 426static int get_base_var(struct strbuf *name)
17712991 427{
17712991
LT
428 for (;;) {
429 int c = get_next_char();
924aaf3e 430 if (cf->eof)
17712991
LT
431 return -1;
432 if (c == ']')
0971e992 433 return 0;
d14f7764 434 if (isspace(c))
0971e992 435 return get_extended_base_var(name, c);
38c5afa8 436 if (!iskeychar(c) && c != '.')
17712991 437 return -1;
0971e992 438 strbuf_addch(name, tolower(c));
17712991
LT
439 }
440}
441
4d8dd149 442static int git_parse_source(config_fn_t fn, void *data)
17712991
LT
443{
444 int comment = 0;
445 int baselen = 0;
0971e992 446 struct strbuf *var = &cf->var;
17712991 447
de056402 448 /* U+FEFF Byte Order Mark in UTF8 */
599446dc 449 const char *bomptr = utf8_bom;
de056402 450
17712991
LT
451 for (;;) {
452 int c = get_next_char();
de056402
PB
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. */
599446dc 457 if (c == (*bomptr & 0377)) {
de056402
PB
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 }
17712991 468 if (c == '\n') {
924aaf3e 469 if (cf->eof)
17712991
LT
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 == '[') {
0971e992
BW
481 /* Reset prior to determining a new stem */
482 strbuf_reset(var);
483 if (get_base_var(var) < 0 || var->len < 1)
17712991 484 break;
0971e992
BW
485 strbuf_addch(var, '.');
486 baselen = var->len;
17712991
LT
487 continue;
488 }
489 if (!isalpha(c))
490 break;
0971e992
BW
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)
17712991
LT
499 break;
500 }
b2dc0945 501 if (cf->die_on_error)
473166b9 502 die(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
b2dc0945 503 else
473166b9 504 return error(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
17712991
LT
505}
506
ebaa1bd4 507static int parse_unit_factor(const char *end, uintmax_t *val)
0b87b6e0
BD
508{
509 if (!*end)
510 return 1;
c8deb5a1
SP
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;
0b87b6e0
BD
524}
525
7192777d 526static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
0b87b6e0
BD
527{
528 if (value && *value) {
529 char *end;
ebaa1bd4
NA
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;
33fdd77e
JK
538 if (!parse_unit_factor(end, &factor)) {
539 errno = EINVAL;
c8deb5a1 540 return 0;
33fdd77e 541 }
83915ba5 542 uval = labs(val);
ebaa1bd4 543 uval *= factor;
83915ba5 544 if (uval > max || labs(val) > uval) {
33fdd77e 545 errno = ERANGE;
ebaa1bd4 546 return 0;
33fdd77e 547 }
ebaa1bd4
NA
548 val *= factor;
549 *ret = val;
0b87b6e0
BD
550 return 1;
551 }
33fdd77e 552 errno = EINVAL;
0b87b6e0
BD
553 return 0;
554}
555
0b4dc661 556static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
17712991
LT
557{
558 if (value && *value) {
559 char *end;
ebaa1bd4
NA
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;
33fdd77e
JK
568 if (!parse_unit_factor(end, &val)) {
569 errno = EINVAL;
c8deb5a1 570 return 0;
33fdd77e
JK
571 }
572 if (val > max || oldval > val) {
573 errno = ERANGE;
ebaa1bd4 574 return 0;
33fdd77e 575 }
c8deb5a1 576 *ret = val;
0b87b6e0 577 return 1;
17712991 578 }
33fdd77e 579 errno = EINVAL;
0b87b6e0
BD
580 return 0;
581}
582
42d194e9 583static int git_parse_int(const char *value, int *ret)
c1867cea 584{
7192777d 585 intmax_t tmp;
42d194e9 586 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
7192777d
JK
587 return 0;
588 *ret = tmp;
589 return 1;
590}
591
00160242
JK
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
7192777d
JK
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
06bdc23b 610NORETURN
2f666581 611static void die_bad_number(const char *name, const char *value)
c1867cea 612{
2f666581
JK
613 const char *reason = errno == ERANGE ?
614 "out of range" :
615 "invalid unit";
616 if (!value)
617 value = "";
618
473166b9
LS
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);
8262aaa2 622 die(_("bad numeric config value '%s' for '%s': %s"), value, name, reason);
c1867cea
JK
623}
624
0b87b6e0
BD
625int git_config_int(const char *name, const char *value)
626{
42d194e9
JK
627 int ret;
628 if (!git_parse_int(value, &ret))
2f666581 629 die_bad_number(name, value);
0b87b6e0
BD
630 return ret;
631}
632
00160242
JK
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);
0b87b6e0
BD
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))
2f666581 645 die_bad_number(name, value);
0b87b6e0 646 return ret;
17712991
LT
647}
648
9a549d43 649int git_parse_maybe_bool(const char *value)
17712991
LT
650{
651 if (!value)
652 return 1;
653 if (!*value)
654 return 0;
8420ccd8
JH
655 if (!strcasecmp(value, "true")
656 || !strcasecmp(value, "yes")
657 || !strcasecmp(value, "on"))
17712991 658 return 1;
8420ccd8
JH
659 if (!strcasecmp(value, "false")
660 || !strcasecmp(value, "no")
661 || !strcasecmp(value, "off"))
17712991 662 return 0;
8420ccd8
JH
663 return -1;
664}
665
b2be2f6a
JK
666int git_config_maybe_bool(const char *name, const char *value)
667{
9a549d43 668 int v = git_parse_maybe_bool(value);
b2be2f6a
JK
669 if (0 <= v)
670 return v;
42d194e9 671 if (git_parse_int(value, &v))
db6195ef 672 return !!v;
b2be2f6a
JK
673 return -1;
674}
675
8420ccd8
JH
676int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
677{
9a549d43 678 int v = git_parse_maybe_bool(value);
8420ccd8
JH
679 if (0 <= v) {
680 *is_bool = 1;
681 return v;
682 }
a53f2ec6 683 *is_bool = 0;
c35b0b58 684 return git_config_int(name, value);
17712991
LT
685}
686
a53f2ec6
JH
687int git_config_bool(const char *name, const char *value)
688{
689 int discard;
c35b0b58 690 return !!git_config_bool_or_int(name, value, &discard);
a53f2ec6
JH
691}
692
ea5105a5
CC
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
395de250
MM
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)
8262aaa2 707 die(_("failed to expand user dir in: '%s'"), value);
395de250
MM
708 return 0;
709}
710
806e2ad7 711static int git_default_core_config(const char *var, const char *value)
17712991
LT
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 }
1ce4790b
AR
718 if (!strcmp(var, "core.trustctime")) {
719 trust_ctime = git_config_bool(var, value);
720 return 0;
721 }
c1b5d738 722 if (!strcmp(var, "core.checkstat")) {
c08e4d5b
RR
723 if (!strcasecmp(value, "default"))
724 check_stat = 1;
725 else if (!strcasecmp(value, "minimal"))
726 check_stat = 0;
727 }
17712991 728
9378c161
JH
729 if (!strcmp(var, "core.quotepath")) {
730 quote_path_fully = git_config_bool(var, value);
731 return 0;
732 }
733
78a8d641
JS
734 if (!strcmp(var, "core.symlinks")) {
735 has_symlinks = git_config_bool(var, value);
736 return 0;
737 }
738
0a9b88b7
LT
739 if (!strcmp(var, "core.ignorecase")) {
740 ignore_case = git_config_bool(var, value);
741 return 0;
742 }
743
64589a03
JH
744 if (!strcmp(var, "core.attributesfile"))
745 return git_config_pathname(&git_attributes_file, var, value);
746
867ad08a
ÆAB
747 if (!strcmp(var, "core.hookspath"))
748 return git_config_pathname(&git_hooks_path, var, value);
749
7d1864ce
JH
750 if (!strcmp(var, "core.bare")) {
751 is_bare_repository_cfg = git_config_bool(var, value);
752 return 0;
753 }
754
5f73076c
JH
755 if (!strcmp(var, "core.ignorestat")) {
756 assume_unchanged = git_config_bool(var, value);
757 return 0;
758 }
759
e388c738
JH
760 if (!strcmp(var, "core.prefersymlinkrefs")) {
761 prefer_symlink_refs = git_config_bool(var, value);
f8348be3
JS
762 return 0;
763 }
764
6de08ae6
SP
765 if (!strcmp(var, "core.logallrefupdates")) {
766 log_all_ref_updates = git_config_bool(var, value);
767 return 0;
768 }
769
2f8acdb3
JH
770 if (!strcmp(var, "core.warnambiguousrefs")) {
771 warn_ambiguous_refs = git_config_bool(var, value);
772 return 0;
773 }
774
a71f09fe 775 if (!strcmp(var, "core.abbrev")) {
dce96489
LT
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
960ccca6 783 if (!strcmp(var, "core.loosecompression")) {
12f6c308
JBH
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)
8262aaa2 788 die(_("bad zlib compression level %d"), level);
12f6c308 789 zlib_compression_level = level;
960ccca6
DH
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)
8262aaa2 799 die(_("bad zlib compression level %d"), level);
960ccca6
DH
800 core_compression_level = level;
801 core_compression_seen = 1;
802 if (!zlib_compression_seen)
803 zlib_compression_level = level;
12f6c308
JBH
804 return 0;
805 }
806
60bb8b14 807 if (!strcmp(var, "core.packedgitwindowsize")) {
5faaf246 808 int pgsz_x2 = getpagesize() * 2;
ebaa1bd4 809 packed_git_window_size = git_config_ulong(var, value);
5faaf246
JH
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;
60bb8b14
SP
816 return 0;
817 }
818
15366280 819 if (!strcmp(var, "core.bigfilethreshold")) {
ebaa1bd4 820 big_file_threshold = git_config_ulong(var, value);
15366280
JH
821 return 0;
822 }
823
77ccc5bb 824 if (!strcmp(var, "core.packedgitlimit")) {
ebaa1bd4 825 packed_git_limit = git_config_ulong(var, value);
77ccc5bb
SP
826 return 0;
827 }
828
18bdec11 829 if (!strcmp(var, "core.deltabasecachelimit")) {
ebaa1bd4 830 delta_base_cache_limit = git_config_ulong(var, value);
18bdec11
SP
831 return 0;
832 }
833
6c510bee 834 if (!strcmp(var, "core.autocrlf")) {
d7f46334 835 if (value && !strcasecmp(value, "input")) {
fd6cce9e 836 auto_crlf = AUTO_CRLF_INPUT;
d7f46334
LT
837 return 0;
838 }
6c510bee
LT
839 auto_crlf = git_config_bool(var, value);
840 return 0;
841 }
842
21e5ad50
SP
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
942e7747
EB
852 if (!strcmp(var, "core.eol")) {
853 if (value && !strcasecmp(value, "lf"))
ec70f52f 854 core_eol = EOL_LF;
942e7747 855 else if (value && !strcasecmp(value, "crlf"))
ec70f52f 856 core_eol = EOL_CRLF;
942e7747 857 else if (value && !strcasecmp(value, "native"))
ec70f52f 858 core_eol = EOL_NATIVE;
942e7747 859 else
ec70f52f 860 core_eol = EOL_UNSET;
942e7747
EB
861 return 0;
862 }
863
a97a7468
JS
864 if (!strcmp(var, "core.notesref")) {
865 notes_ref_name = xstrdup(value);
866 return 0;
867 }
868
806e2ad7
LT
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
eff80a9f 875 if (!strcmp(var, "core.commentchar")) {
649409b7
JK
876 if (!value)
877 return config_error_nonbool(var);
ad524f83 878 else if (!strcasecmp(value, "auto"))
84c9dc2c 879 auto_comment_line_char = 1;
ad524f83 880 else if (value[0] && !value[1]) {
649409b7 881 comment_line_char = value[0];
84c9dc2c 882 auto_comment_line_char = 0;
50b54fd7
NTND
883 } else
884 return error("core.commentChar should only be one character");
885 return 0;
eff80a9f
JH
886 }
887
d3e7da89
AK
888 if (!strcmp(var, "core.askpass"))
889 return git_config_string(&askpass_program, var, value);
890
806e2ad7 891 if (!strcmp(var, "core.excludesfile"))
395de250 892 return git_config_pathname(&excludes_file, var, value);
806e2ad7
LT
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
aafe9fba
LT
901 if (!strcmp(var, "core.fsyncobjectfiles")) {
902 fsync_object_files = git_config_bool(var, value);
903 return 0;
904 }
905
671c9b7e
LT
906 if (!strcmp(var, "core.preloadindex")) {
907 core_preload_index = git_config_bool(var, value);
908 return 0;
909 }
910
348df166
JS
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
8262aaa2 917 die(_("invalid mode for object creation: %s"), value);
be66a6c4
JS
918 return 0;
919 }
920
08aefc9e
NTND
921 if (!strcmp(var, "core.sparsecheckout")) {
922 core_apply_sparse_checkout = git_config_bool(var, value);
923 return 0;
924 }
925
76759c7d
TB
926 if (!strcmp(var, "core.precomposeunicode")) {
927 precomposed_unicode = git_config_bool(var, value);
928 return 0;
929 }
930
a42643aa
JK
931 if (!strcmp(var, "core.protecthfs")) {
932 protect_hfs = git_config_bool(var, value);
933 return 0;
934 }
935
2b4c6efc
JS
936 if (!strcmp(var, "core.protectntfs")) {
937 protect_ntfs = git_config_bool(var, value);
938 return 0;
76759c7d
TB
939 }
940
f30afdab
JS
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
806e2ad7
LT
949 /* Add other config variables here and to Documentation/config.txt. */
950 return 0;
951}
952
1141f492 953static int git_default_i18n_config(const char *var, const char *value)
d1364529 954{
ea5105a5
CC
955 if (!strcmp(var, "i18n.commitencoding"))
956 return git_config_string(&git_commit_encoding, var, value);
d2c11a38 957
ea5105a5
CC
958 if (!strcmp(var, "i18n.logoutputencoding"))
959 return git_config_string(&git_log_output_encoding, var, value);
d2c11a38 960
1141f492
LT
961 /* Add other config variables here and to Documentation/config.txt. */
962 return 0;
963}
039bc64e 964
1141f492
LT
965static int git_default_branch_config(const char *var, const char *value)
966{
9ed36cfa
JS
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 }
c998ae9b
DS
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
8c3ca351 987 return error("malformed value for %s", var);
c998ae9b
DS
988 return 0;
989 }
a9cc857a 990
1ab661dd 991 /* Add other config variables here and to Documentation/config.txt. */
17712991
LT
992 return 0;
993}
994
52153747
FAG
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;
b55e6775
MM
1004 else if (!strcmp(value, "simple"))
1005 push_default = PUSH_DEFAULT_SIMPLE;
53c40311
JH
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;
52153747
FAG
1010 else if (!strcmp(value, "current"))
1011 push_default = PUSH_DEFAULT_CURRENT;
1012 else {
8c3ca351 1013 error("malformed value for %s: %s", var, value);
b55e6775
MM
1014 return error("Must be one of nothing, matching, simple, "
1015 "upstream or current.");
52153747
FAG
1016 }
1017 return 0;
1018 }
1019
1020 /* Add other config variables here and to Documentation/config.txt. */
1021 return 0;
1022}
1023
d551a488
MSO
1024static int git_default_mailmap_config(const char *var, const char *value)
1025{
1026 if (!strcmp(var, "mailmap.file"))
9352fd57 1027 return git_config_pathname(&git_mailmap_file, var, value);
08610900
JK
1028 if (!strcmp(var, "mailmap.blob"))
1029 return git_config_string(&git_mailmap_blob, var, value);
d551a488
MSO
1030
1031 /* Add other config variables here and to Documentation/config.txt. */
1032 return 0;
1033}
1034
1141f492
LT
1035int git_default_config(const char *var, const char *value, void *dummy)
1036{
59556548 1037 if (starts_with(var, "core."))
1141f492
LT
1038 return git_default_core_config(var, value);
1039
59556548 1040 if (starts_with(var, "user."))
9597921b 1041 return git_ident_config(var, value, dummy);
1141f492 1042
59556548 1043 if (starts_with(var, "i18n."))
1141f492
LT
1044 return git_default_i18n_config(var, value);
1045
59556548 1046 if (starts_with(var, "branch."))
1141f492
LT
1047 return git_default_branch_config(var, value);
1048
59556548 1049 if (starts_with(var, "push."))
52153747
FAG
1050 return git_default_push_config(var, value);
1051
59556548 1052 if (starts_with(var, "mailmap."))
d551a488
MSO
1053 return git_default_mailmap_config(var, value);
1054
59556548 1055 if (starts_with(var, "advice."))
75194438
JK
1056 return git_default_advice_config(var, value);
1057
1141f492
LT
1058 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
1059 pager_use_color = git_config_bool(var,value);
1060 return 0;
1061 }
1062
568508e7
JH
1063 if (!strcmp(var, "pack.packsizelimit")) {
1064 pack_size_limit_cfg = git_config_ulong(var, value);
1065 return 0;
1066 }
1141f492
LT
1067 /* Add other config variables here and to Documentation/config.txt. */
1068 return 0;
1069}
1070
ca4b5de2 1071/*
b2dc0945 1072 * All source specific fields in the union, die_on_error, name and the callbacks
4d8dd149 1073 * fgetc, ungetc, ftell of top need to be initialized before calling
ca4b5de2
HV
1074 * this function.
1075 */
4d8dd149 1076static int do_config_from(struct config_source *top, config_fn_t fn, void *data)
ca4b5de2
HV
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
4d8dd149 1088 ret = git_parse_source(fn, data);
ca4b5de2
HV
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
3caec73b 1098static int do_config_from_file(config_fn_t fn,
473166b9
LS
1099 const char *origin_type, const char *name, const char *path, FILE *f,
1100 void *data)
17712991 1101{
3caec73b 1102 struct config_source top;
17712991 1103
3caec73b 1104 top.u.file = f;
473166b9 1105 top.origin_type = origin_type;
3caec73b
KS
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;
924aaf3e 1112
3caec73b
KS
1113 return do_config_from(&top, fn, data);
1114}
924aaf3e 1115
3caec73b
KS
1116static int git_config_from_stdin(config_fn_t fn, void *data)
1117{
473166b9 1118 return do_config_from_file(fn, "standard input", "", NULL, stdin, data);
3caec73b
KS
1119}
1120
1121int git_config_from_file(config_fn_t fn, const char *filename, void *data)
1122{
1123 int ret = -1;
1124 FILE *f;
924aaf3e 1125
3caec73b
KS
1126 f = fopen(filename, "r");
1127 if (f) {
260d408e 1128 flockfile(f);
473166b9 1129 ret = do_config_from_file(fn, "file", filename, filename, f, data);
260d408e 1130 funlockfile(f);
17712991
LT
1131 fclose(f);
1132 }
1133 return ret;
1134}
10bea152 1135
473166b9
LS
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)
1bc88819
HV
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;
473166b9 1144 top.origin_type = origin_type;
1bc88819 1145 top.name = name;
d14d4244 1146 top.path = NULL;
b2dc0945 1147 top.die_on_error = 0;
49d6cfa5
JK
1148 top.do_fgetc = config_buf_fgetc;
1149 top.do_ungetc = config_buf_ungetc;
1150 top.do_ftell = config_buf_ftell;
1bc88819
HV
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
473166b9 1173 ret = git_config_from_mem(fn, "blob", name, buf, size, data);
1bc88819
HV
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
506b17b1
JS
1190const char *git_etc_gitconfig(void)
1191{
7f0e39fa 1192 static const char *system_wide;
2de9de5e
SP
1193 if (!system_wide)
1194 system_wide = system_path(ETC_GITCONFIG);
7f0e39fa 1195 return system_wide;
506b17b1
JS
1196}
1197
23b0c478
SP
1198/*
1199 * Parse environment variable 'k' as a boolean (in various
1200 * possible spellings); if missing, use the default value 'def'.
1201 */
0ef37164 1202int git_env_bool(const char *k, int def)
ab88c363
JK
1203{
1204 const char *v = getenv(k);
1205 return v ? git_config_bool(k, v) : def;
1206}
1207
23b0c478
SP
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
ab88c363
JK
1220int git_config_system(void)
1221{
1222 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
1223}
1224
80181868 1225static int do_git_config_sequence(config_fn_t fn, void *data)
4f629539 1226{
c72ee44b 1227 int ret = 0;
509adc33
PT
1228 char *xdg_config = xdg_config_home("config");
1229 char *user_config = expand_user_path("~/.gitconfig");
80181868 1230 char *repo_config = git_pathdup("config");
5f1a63e0 1231
c72ee44b 1232 if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0))
dc871831
DB
1233 ret += git_config_from_file(fn, git_etc_gitconfig(),
1234 data);
5f1a63e0 1235
c72ee44b 1236 if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
21cf3227 1237 ret += git_config_from_file(fn, xdg_config, data);
21cf3227 1238
c72ee44b 1239 if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
21cf3227 1240 ret += git_config_from_file(fn, user_config, data);
5f1a63e0 1241
c72ee44b 1242 if (repo_config && !access_or_die(repo_config, R_OK, 0))
aa387407 1243 ret += git_config_from_file(fn, repo_config, data);
8b1fa778 1244
c72ee44b 1245 if (git_config_from_parameters(fn, data) < 0)
8262aaa2 1246 die(_("unable to parse command-line config"));
8b1fa778 1247
21cf3227
HKNN
1248 free(xdg_config);
1249 free(user_config);
80181868 1250 free(repo_config);
c72ee44b 1251 return ret;
4f629539
JH
1252}
1253
c9b5e2a5 1254int git_config_with_options(config_fn_t fn, void *data,
c8985ce0 1255 struct git_config_source *config_source,
1bc88819 1256 int respect_includes)
dbdf5854 1257{
9b25a0b5
JK
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 }
dbdf5854 1266
c9b5e2a5
JK
1267 /*
1268 * If we have a specific filename, use it. Otherwise, follow the
1269 * regular lookup sequence.
1270 */
3caec73b
KS
1271 if (config_source && config_source->use_stdin)
1272 return git_config_from_stdin(fn, data);
1273 else if (config_source && config_source->file)
c8985ce0
KS
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);
c9b5e2a5 1277
80181868 1278 return do_git_config_sequence(fn, data);
dbdf5854
NTND
1279}
1280
155ef25f 1281static void git_config_raw(config_fn_t fn, void *data)
c9b5e2a5 1282{
aace4385
TA
1283 if (git_config_with_options(fn, data, NULL, 1) < 0)
1284 /*
1285 * git_config_with_options() normally returns only
c72ee44b 1286 * zero, as most errors are fatal, and
aace4385
TA
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"));
c9b5e2a5
JK
1296}
1297
155ef25f 1298static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
c9b5e2a5 1299{
155ef25f
TA
1300 int i, value_index;
1301 struct string_list *values;
1302 struct config_set_element *entry;
1303 struct configset_list *list = &cs->list;
155ef25f
TA
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;
0d44a2da
JK
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;
155ef25f
TA
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);
c9b5e2a5
JK
1327}
1328
3c8687a7
TA
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;
3c8687a7
TA
1334 /*
1335 * `key` may come from the user, so normalize it before using it
1336 * for querying entries from the hashmap.
1337 */
270cd9ea 1338 if (git_config_parse_key(key, &normalized_key, NULL))
3c8687a7
TA
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;
3df8fd62 1351 struct string_list_item *si;
155ef25f 1352 struct configset_list_item *l_item;
3df8fd62
TA
1353 struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
1354
3c8687a7
TA
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 }
8c53f071 1367 si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
155ef25f
TA
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
3258258f
JK
1374 if (!cf)
1375 die("BUG: configset_add_value has no source");
1376 if (cf->name) {
3df8fd62
TA
1377 kv_info->filename = strintern(cf->name);
1378 kv_info->linenr = cf->linenr;
0d44a2da 1379 kv_info->origin_type = strintern(cf->origin_type);
3df8fd62
TA
1380 } else {
1381 /* for values read from `git_config_from_parameters()` */
1382 kv_info->filename = NULL;
1383 kv_info->linenr = -1;
0d44a2da 1384 kv_info->origin_type = NULL;
3df8fd62
TA
1385 }
1386 si->util = kv_info;
3c8687a7
TA
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;
155ef25f
TA
1401 cs->list.nr = 0;
1402 cs->list.alloc = 0;
1403 cs->list.items = NULL;
3c8687a7
TA
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);
3df8fd62 1416 string_list_clear(&entry->value_list, 1);
3c8687a7
TA
1417 }
1418 hashmap_free(&cs->config_hash, 1);
1419 cs->hash_initialized = 0;
155ef25f
TA
1420 free(cs->list.items);
1421 cs->list.nr = 0;
1422 cs->list.alloc = 0;
1423 cs->list.items = NULL;
3c8687a7
TA
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);
155ef25f 1542 git_config_raw(config_set_callback, &the_config_set);
3c8687a7
TA
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{
5a80e97c 1566 int ret;
3c8687a7 1567 git_config_check_init();
5a80e97c
TA
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;
3c8687a7
TA
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{
5a80e97c 1612 int ret;
3c8687a7 1613 git_config_check_init();
5a80e97c
TA
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
435ec090
CC
1620int git_config_get_untracked_cache(void)
1621{
1622 int val = -1;
1623 const char *v;
1624
dae6c322
CC
1625 /* Hack for test programs like test-dump-untracked-cache */
1626 if (ignore_untracked_cache_config)
1627 return -1;
1628
435ec090
CC
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
5a80e97c
TA
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);
3c8687a7
TA
1669}
1670
10bea152
JS
1671/*
1672 * Find all the stuff for git_config_set() below.
1673 */
4ddba79d 1674
10bea152
JS
1675static struct {
1676 int baselen;
4b25d091 1677 char *key;
f98d863d 1678 int do_not_match;
4b25d091 1679 regex_t *value_regex;
4ddba79d 1680 int multi_replace;
83786fa4
TR
1681 size_t *offset;
1682 unsigned int offset_alloc;
10bea152
JS
1683 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
1684 int seen;
1685} store;
1686
4b25d091 1687static int matches(const char *key, const char *value)
f98d863d 1688{
c1063be2
JK
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));
f98d863d
JS
1698}
1699
4b25d091 1700static int store_aux(const char *key, const char *value, void *cb)
10bea152 1701{
ae9ee41d
JH
1702 const char *ep;
1703 size_t section_len;
1704
10bea152
JS
1705 switch (store.state) {
1706 case KEY_SEEN:
f98d863d 1707 if (matches(key, value)) {
4ddba79d 1708 if (store.seen == 1 && store.multi_replace == 0) {
8262aaa2 1709 warning(_("%s has multiple values"), key);
10bea152 1710 }
4ddba79d 1711
83786fa4
TR
1712 ALLOC_GROW(store.offset, store.seen + 1,
1713 store.offset_alloc);
1714
49d6cfa5 1715 store.offset[store.seen] = cf->do_ftell(cf);
10bea152
JS
1716 store.seen++;
1717 }
1718 break;
1719 case SECTION_SEEN:
ae9ee41d
JH
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)) {
10bea152
JS
1734 store.state = SECTION_END_SEEN;
1735 break;
ae9ee41d
JH
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 */
83786fa4
TR
1742 ALLOC_GROW(store.offset, store.seen + 1,
1743 store.offset_alloc);
49d6cfa5 1744 store.offset[store.seen] = cf->do_ftell(cf);
10bea152
JS
1745 /* fallthru */
1746 case SECTION_END_SEEN:
1747 case START:
f98d863d 1748 if (matches(key, value)) {
83786fa4
TR
1749 ALLOC_GROW(store.offset, store.seen + 1,
1750 store.offset_alloc);
49d6cfa5 1751 store.offset[store.seen] = cf->do_ftell(cf);
10bea152
JS
1752 store.state = KEY_SEEN;
1753 store.seen++;
d14f7764
LT
1754 } else {
1755 if (strrchr(key, '.') - key == store.baselen &&
bdf0ef08 1756 !strncmp(key, store.key, store.baselen)) {
93ddef3e 1757 store.state = SECTION_SEEN;
83786fa4
TR
1758 ALLOC_GROW(store.offset,
1759 store.seen + 1,
1760 store.offset_alloc);
49d6cfa5 1761 store.offset[store.seen] = cf->do_ftell(cf);
d14f7764 1762 }
bdf0ef08 1763 }
10bea152
JS
1764 }
1765 return 0;
1766}
1767
64c0d71c 1768static int write_error(const char *filename)
480c9e52 1769{
64c0d71c 1770 error("failed to write new configuration file %s", filename);
480c9e52
AW
1771
1772 /* Same error code as "failed to rename". */
1773 return 4;
1774}
1775
4b25d091 1776static int store_write_section(int fd, const char *key)
10bea152 1777{
cb891a59
KH
1778 const char *dot;
1779 int i, success;
f285a2d7 1780 struct strbuf sb = STRBUF_INIT;
d14f7764 1781
cb891a59 1782 dot = memchr(key, '.', store.baselen);
d14f7764 1783 if (dot) {
cb891a59
KH
1784 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
1785 for (i = dot - key + 1; i < store.baselen; i++) {
e5c349ba 1786 if (key[i] == '"' || key[i] == '\\')
cb891a59
KH
1787 strbuf_addch(&sb, '\\');
1788 strbuf_addch(&sb, key[i]);
d14f7764 1789 }
cb891a59
KH
1790 strbuf_addstr(&sb, "\"]\n");
1791 } else {
1792 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
d14f7764
LT
1793 }
1794
cb891a59
KH
1795 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
1796 strbuf_release(&sb);
480c9e52 1797
cb891a59 1798 return success;
10bea152
JS
1799}
1800
4b25d091 1801static int store_write_pair(int fd, const char *key, const char *value)
10bea152 1802{
cb891a59
KH
1803 int i, success;
1804 int length = strlen(key + store.baselen + 1);
1805 const char *quote = "";
f285a2d7 1806 struct strbuf sb = STRBUF_INIT;
cdd4fb15 1807
6281f394
JM
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 */
cdd4fb15 1815 if (value[0] == ' ')
cb891a59 1816 quote = "\"";
cdd4fb15
BG
1817 for (i = 0; value[i]; i++)
1818 if (value[i] == ';' || value[i] == '#')
cb891a59
KH
1819 quote = "\"";
1820 if (i && value[i - 1] == ' ')
1821 quote = "\"";
1822
cb891a59
KH
1823 strbuf_addf(&sb, "\t%.*s = %s",
1824 length, key + store.baselen + 1, quote);
10bea152 1825
10bea152
JS
1826 for (i = 0; value[i]; i++)
1827 switch (value[i]) {
480c9e52 1828 case '\n':
cb891a59 1829 strbuf_addstr(&sb, "\\n");
480c9e52
AW
1830 break;
1831 case '\t':
cb891a59 1832 strbuf_addstr(&sb, "\\t");
480c9e52
AW
1833 break;
1834 case '"':
1835 case '\\':
cb891a59 1836 strbuf_addch(&sb, '\\');
480c9e52 1837 default:
cb891a59 1838 strbuf_addch(&sb, value[i]);
480c9e52
AW
1839 break;
1840 }
cb891a59
KH
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;
10bea152
JS
1847}
1848
4b25d091
FC
1849static ssize_t find_beginning_of_line(const char *contents, size_t size,
1850 size_t offset_, int *found_bracket)
4ddba79d 1851{
dc49cd76
SP
1852 size_t equal_offset = size, bracket_offset = size;
1853 ssize_t offset;
4ddba79d 1854
7a31cc0f 1855contline:
a6080a0a 1856 for (offset = offset_-2; offset > 0
4ddba79d
JS
1857 && contents[offset] != '\n'; offset--)
1858 switch (contents[offset]) {
1859 case '=': equal_offset = offset; break;
1860 case ']': bracket_offset = offset; break;
1861 }
7a31cc0f
FL
1862 if (offset > 0 && contents[offset-1] == '\\') {
1863 offset_ = offset;
1864 goto contline;
1865 }
4ddba79d
JS
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
30598ad0
PS
1875int git_config_set_in_file_gently(const char *config_filename,
1876 const char *key, const char *value)
5ec31182 1877{
30598ad0 1878 return git_config_set_multivar_in_file_gently(config_filename, key, value, NULL, 0);
5ec31182
RR
1879}
1880
3d180648
PS
1881void git_config_set_in_file(const char *config_filename,
1882 const char *key, const char *value)
10bea152 1883{
3d180648 1884 git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
b4c8aba6
PS
1885}
1886
30598ad0 1887int git_config_set_gently(const char *key, const char *value)
10bea152 1888{
30598ad0 1889 return git_config_set_multivar_gently(key, value, NULL, 0);
10bea152
JS
1890}
1891
3d180648 1892void git_config_set(const char *key, const char *value)
b4c8aba6 1893{
3d180648 1894 git_config_set_multivar(key, value, NULL, 0);
10bea152
JS
1895}
1896
b09c53a3
LP
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 */
9e9de18f 1909static int git_config_parse_key_1(const char *key, char **store_key, int *baselen_, int quiet)
b09c53a3
LP
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
2169ddc0 1919 if (last_dot == NULL || last_dot == key) {
9e9de18f
JK
1920 if (!quiet)
1921 error("key does not contain a section: %s", key);
7a397419 1922 return -CONFIG_NO_SECTION_OR_NAME;
b09c53a3
LP
1923 }
1924
2169ddc0 1925 if (!last_dot[1]) {
9e9de18f
JK
1926 if (!quiet)
1927 error("key does not contain variable name: %s", key);
7a397419 1928 return -CONFIG_NO_SECTION_OR_NAME;
2169ddc0
LP
1929 }
1930
b09c53a3
LP
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 */
9e9de18f 1938 if (store_key)
3733e694 1939 *store_key = xmallocz(strlen(key));
b09c53a3
LP
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))) {
9e9de18f
JK
1950 if (!quiet)
1951 error("invalid key: %s", key);
b09c53a3
LP
1952 goto out_free_ret_1;
1953 }
1954 c = tolower(c);
1955 } else if (c == '\n') {
9e9de18f
JK
1956 if (!quiet)
1957 error("invalid key (newline): %s", key);
b09c53a3
LP
1958 goto out_free_ret_1;
1959 }
9e9de18f
JK
1960 if (store_key)
1961 (*store_key)[i] = c;
b09c53a3 1962 }
b09c53a3
LP
1963
1964 return 0;
1965
1966out_free_ret_1:
9e9de18f
JK
1967 if (store_key) {
1968 free(*store_key);
1969 *store_key = NULL;
1970 }
7a397419 1971 return -CONFIG_INVALID_KEY;
b09c53a3
LP
1972}
1973
9e9de18f
JK
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
10bea152
JS
1984/*
1985 * If value==NULL, unset in (remove from) config,
1986 * if value_regex!=NULL, disregard key/value pairs where value does not match.
c1063be2
JK
1987 * if value_regex==CONFIG_REGEX_NONE, do not match any existing values
1988 * (only add a new one)
4ddba79d
JS
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.
10bea152
JS
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 */
30598ad0
PS
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)
10bea152 2013{
54d160ec 2014 int fd = -1, in_fd = -1;
dafc88b1 2015 int ret;
6cbf973c 2016 struct lock_file *lock = NULL;
0a5f5759 2017 char *filename_buf = NULL;
3a1b3126
JK
2018 char *contents = NULL;
2019 size_t contents_sz;
4ddba79d 2020
b09c53a3
LP
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)
dafc88b1 2024 goto out_free;
b17e659d
JS
2025
2026 store.multi_replace = multi_replace;
10bea152 2027
0a5f5759
JK
2028 if (!config_filename)
2029 config_filename = filename_buf = git_pathdup("config");
10bea152
JS
2030
2031 /*
6cbf973c 2032 * The lock serves a purpose in addition to locking: the new
10bea152
JS
2033 * contents of .git/config will be written into it.
2034 */
f1064f6b 2035 lock = xcalloc(1, sizeof(struct lock_file));
6cbf973c
BS
2036 fd = hold_lock_file_for_update(lock, config_filename, 0);
2037 if (fd < 0) {
f0658ec9 2038 error_errno("could not lock config file %s", config_filename);
10bea152 2039 free(store.key);
7a397419 2040 ret = CONFIG_NO_LOCK;
dafc88b1 2041 goto out_free;
10bea152
JS
2042 }
2043
2044 /*
2045 * If .git/config does not exist yet, write a minimal version.
2046 */
88fb958b
AR
2047 in_fd = open(config_filename, O_RDONLY);
2048 if ( in_fd < 0 ) {
10bea152
JS
2049 free(store.key);
2050
88fb958b 2051 if ( ENOENT != errno ) {
f0658ec9 2052 error_errno("opening %s", config_filename);
7a397419 2053 ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
dafc88b1 2054 goto out_free;
88fb958b 2055 }
10bea152
JS
2056 /* if nothing to unset, error out */
2057 if (value == NULL) {
7a397419 2058 ret = CONFIG_NOTHING_SET;
dafc88b1 2059 goto out_free;
10bea152
JS
2060 }
2061
4b25d091 2062 store.key = (char *)key;
480c9e52 2063 if (!store_write_section(fd, key) ||
93c1e079
JH
2064 !store_write_pair(fd, key, value))
2065 goto write_err_out;
2066 } else {
88fb958b 2067 struct stat st;
3a1b3126 2068 size_t copy_begin, copy_end;
dc49cd76 2069 int i, new_line = 0;
10bea152
JS
2070
2071 if (value_regex == NULL)
2072 store.value_regex = NULL;
c1063be2
JK
2073 else if (value_regex == CONFIG_REGEX_NONE)
2074 store.value_regex = CONFIG_REGEX_NONE;
10bea152 2075 else {
f98d863d
JS
2076 if (value_regex[0] == '!') {
2077 store.do_not_match = 1;
2078 value_regex++;
2079 } else
2080 store.do_not_match = 0;
2081
2d7320d0 2082 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
10bea152
JS
2083 if (regcomp(store.value_regex, value_regex,
2084 REG_EXTENDED)) {
64c0d71c 2085 error("invalid pattern: %s", value_regex);
10bea152 2086 free(store.value_regex);
7a397419 2087 ret = CONFIG_INVALID_PATTERN;
dafc88b1 2088 goto out_free;
10bea152
JS
2089 }
2090 }
2091
83786fa4 2092 ALLOC_GROW(store.offset, 1, store.offset_alloc);
4ddba79d 2093 store.offset[0] = 0;
10bea152
JS
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 */
ef90d6d4 2103 if (git_config_from_file(store_aux, config_filename, NULL)) {
64c0d71c 2104 error("invalid config file %s", config_filename);
10bea152 2105 free(store.key);
c1063be2
JK
2106 if (store.value_regex != NULL &&
2107 store.value_regex != CONFIG_REGEX_NONE) {
10bea152
JS
2108 regfree(store.value_regex);
2109 free(store.value_regex);
2110 }
7a397419 2111 ret = CONFIG_INVALID_FILE;
dafc88b1 2112 goto out_free;
10bea152
JS
2113 }
2114
2115 free(store.key);
c1063be2
JK
2116 if (store.value_regex != NULL &&
2117 store.value_regex != CONFIG_REGEX_NONE) {
10bea152
JS
2118 regfree(store.value_regex);
2119 free(store.value_regex);
2120 }
2121
4ddba79d
JS
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)) {
7a397419 2125 ret = CONFIG_NOTHING_SET;
dafc88b1 2126 goto out_free;
10bea152
JS
2127 }
2128
88fb958b 2129 fstat(in_fd, &st);
dc49cd76 2130 contents_sz = xsize_t(st.st_size);
1570856b
JK
2131 contents = xmmap_gently(NULL, contents_sz, PROT_READ,
2132 MAP_PRIVATE, in_fd, 0);
2133 if (contents == MAP_FAILED) {
0e8771f1
JK
2134 if (errno == ENODEV && S_ISDIR(st.st_mode))
2135 errno = EISDIR;
f0658ec9 2136 error_errno("unable to mmap '%s'", config_filename);
1570856b
JK
2137 ret = CONFIG_INVALID_FILE;
2138 contents = NULL;
2139 goto out_free;
2140 }
10bea152 2141 close(in_fd);
54d160ec 2142 in_fd = -1;
10bea152 2143
b4fb09e4 2144 if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
f0658ec9 2145 error_errno("chmod on %s failed", get_lock_file_path(lock));
daa22c6f
EW
2146 ret = CONFIG_NO_WRITE;
2147 goto out_free;
2148 }
2149
4ddba79d
JS
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) {
dc49cd76 2155 store.offset[i] = copy_end = contents_sz;
4ddba79d
JS
2156 } else if (store.state != KEY_SEEN) {
2157 copy_end = store.offset[i];
10bea152 2158 } else
4ddba79d 2159 copy_end = find_beginning_of_line(
dc49cd76 2160 contents, contents_sz,
4ddba79d
JS
2161 store.offset[i]-2, &new_line);
2162
02e5ba4a
JK
2163 if (copy_end > 0 && contents[copy_end-1] != '\n')
2164 new_line = 1;
2165
4ddba79d
JS
2166 /* write the first part of the config */
2167 if (copy_end > copy_begin) {
93c1e079
JH
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 &&
2b7ca830 2173 write_str_in_full(fd, "\n") != 1)
93c1e079 2174 goto write_err_out;
4ddba79d
JS
2175 }
2176 copy_begin = store.offset[i];
10bea152
JS
2177 }
2178
10bea152
JS
2179 /* write the pair (value == NULL means unset) */
2180 if (value != NULL) {
93c1e079
JH
2181 if (store.state == START) {
2182 if (!store_write_section(fd, key))
2183 goto write_err_out;
480c9e52 2184 }
93c1e079
JH
2185 if (!store_write_pair(fd, key, value))
2186 goto write_err_out;
10bea152
JS
2187 }
2188
2189 /* write the rest of the config */
dc49cd76 2190 if (copy_begin < contents_sz)
93c1e079 2191 if (write_in_full(fd, contents + copy_begin,
dc49cd76
SP
2192 contents_sz - copy_begin) <
2193 contents_sz - copy_begin)
93c1e079 2194 goto write_err_out;
7a64592c
KB
2195
2196 munmap(contents, contents_sz);
2197 contents = NULL;
10bea152
JS
2198 }
2199
4ed7cd3a 2200 if (commit_lock_file(lock) < 0) {
f0658ec9 2201 error_errno("could not write config file %s", config_filename);
7a397419 2202 ret = CONFIG_NO_WRITE;
e831855e 2203 lock = NULL;
dafc88b1 2204 goto out_free;
10bea152
JS
2205 }
2206
6cbf973c
BS
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;
dafc88b1
SH
2214 ret = 0;
2215
3c8687a7
TA
2216 /* Invalidate the config cache */
2217 git_config_clear();
2218
dafc88b1 2219out_free:
6cbf973c
BS
2220 if (lock)
2221 rollback_lock_file(lock);
0a5f5759 2222 free(filename_buf);
3a1b3126
JK
2223 if (contents)
2224 munmap(contents, contents_sz);
54d160ec
SS
2225 if (in_fd >= 0)
2226 close(in_fd);
dafc88b1 2227 return ret;
93c1e079
JH
2228
2229write_err_out:
b4fb09e4 2230 ret = write_error(get_lock_file_path(lock));
93c1e079
JH
2231 goto out_free;
2232
10bea152
JS
2233}
2234
3d180648
PS
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)
b4c8aba6 2238{
1cae428e
JK
2239 if (!git_config_set_multivar_in_file_gently(config_filename, key, value,
2240 value_regex, multi_replace))
2241 return;
2242 if (value)
8c3ca351 2243 die(_("could not set '%s' to '%s'"), key, value);
1cae428e
JK
2244 else
2245 die(_("could not unset '%s'"), key);
b4c8aba6
PS
2246}
2247
30598ad0
PS
2248int git_config_set_multivar_gently(const char *key, const char *value,
2249 const char *value_regex, int multi_replace)
5ec31182 2250{
30598ad0
PS
2251 return git_config_set_multivar_in_file_gently(NULL, key, value, value_regex,
2252 multi_replace);
5ec31182
RR
2253}
2254
3d180648
PS
2255void git_config_set_multivar(const char *key, const char *value,
2256 const char *value_regex, int multi_replace)
5ec31182 2257{
3d180648
PS
2258 git_config_set_multivar_in_file(NULL, key, value, value_regex,
2259 multi_replace);
5ec31182
RR
2260}
2261
118f8b24
PB
2262static int section_name_match (const char *buf, const char *name)
2263{
2264 int i = 0, j = 0, dot = 0;
a4c0d463
AV
2265 if (buf[i] != '[')
2266 return 0;
2267 for (i = 1; buf[i] && buf[i] != ']'; i++) {
118f8b24
PB
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 }
a4c0d463
AV
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;
118f8b24
PB
2299}
2300
94a35b1a
JK
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
118f8b24 2317/* if new_name == NULL, the section is removed instead */
42bd39b5
JK
2318int git_config_rename_section_in_file(const char *config_filename,
2319 const char *old_name, const char *new_name)
0667fcfb 2320{
118f8b24 2321 int ret = 0, remove = 0;
42bd39b5 2322 char *filename_buf = NULL;
94a35b1a 2323 struct lock_file *lock;
0667fcfb
JS
2324 int out_fd;
2325 char buf[1024];
924aaf3e 2326 FILE *config_file;
daa22c6f 2327 struct stat st;
0667fcfb 2328
94a35b1a
JK
2329 if (new_name && !section_name_is_ok(new_name)) {
2330 ret = error("invalid section name: %s", new_name);
2331 goto out;
2332 }
2333
42bd39b5
JK
2334 if (!config_filename)
2335 config_filename = filename_buf = git_pathdup("config");
2336
f1064f6b 2337 lock = xcalloc(1, sizeof(struct lock_file));
0667fcfb 2338 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
fc1905bb 2339 if (out_fd < 0) {
64c0d71c 2340 ret = error("could not lock config file %s", config_filename);
fc1905bb
JH
2341 goto out;
2342 }
0667fcfb 2343
fc1905bb 2344 if (!(config_file = fopen(config_filename, "rb"))) {
01ebb9dc
GB
2345 /* no config file means nothing to rename, no error */
2346 goto unlock_and_out;
fc1905bb 2347 }
0667fcfb 2348
daa22c6f
EW
2349 fstat(fileno(config_file), &st);
2350
b4fb09e4 2351 if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
f0658ec9
NTND
2352 ret = error_errno("chmod on %s failed",
2353 get_lock_file_path(lock));
daa22c6f
EW
2354 goto out;
2355 }
2356
0667fcfb
JS
2357 while (fgets(buf, sizeof(buf), config_file)) {
2358 int i;
480c9e52 2359 int length;
9a5abfc7 2360 char *output = buf;
0667fcfb
JS
2361 for (i = 0; buf[i] && isspace(buf[i]); i++)
2362 ; /* do nothing */
2363 if (buf[i] == '[') {
2364 /* it's a section */
a4c0d463
AV
2365 int offset = section_name_match(&buf[i], old_name);
2366 if (offset > 0) {
118f8b24
PB
2367 ret++;
2368 if (new_name == NULL) {
2369 remove = 1;
0667fcfb
JS
2370 continue;
2371 }
0667fcfb 2372 store.baselen = strlen(new_name);
480c9e52 2373 if (!store_write_section(out_fd, new_name)) {
b4fb09e4 2374 ret = write_error(get_lock_file_path(lock));
480c9e52
AW
2375 goto out;
2376 }
9a5abfc7
AV
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 }
0667fcfb 2393 }
118f8b24 2394 remove = 0;
0667fcfb 2395 }
118f8b24
PB
2396 if (remove)
2397 continue;
9a5abfc7
AV
2398 length = strlen(output);
2399 if (write_in_full(out_fd, output, length) != length) {
b4fb09e4 2400 ret = write_error(get_lock_file_path(lock));
480c9e52
AW
2401 goto out;
2402 }
0667fcfb 2403 }
fc1905bb 2404 fclose(config_file);
8b590075 2405unlock_and_out:
4ed7cd3a 2406 if (commit_lock_file(lock) < 0)
f0658ec9
NTND
2407 ret = error_errno("could not write config file %s",
2408 config_filename);
8b590075 2409out:
42bd39b5 2410 free(filename_buf);
0667fcfb
JS
2411 return ret;
2412}
40ea4ed9 2413
42bd39b5
JK
2414int git_config_rename_section(const char *old_name, const char *new_name)
2415{
4a7bb5ba 2416 return git_config_rename_section_in_file(NULL, old_name, new_name);
42bd39b5
JK
2417}
2418
40ea4ed9
JH
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 */
a469a101 2423#undef config_error_nonbool
40ea4ed9
JH
2424int config_error_nonbool(const char *var)
2425{
8c3ca351 2426 return error("missing value for '%s'", var);
40ea4ed9 2427}
1b86bbb0
JK
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." ? */
59556548 2438 if (!starts_with(var, section) || var[section_len] != '.')
1b86bbb0
JK
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}
473166b9
LS
2461
2462const char *current_config_origin_type(void)
2463{
0d44a2da
JK
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
3258258f 2470 die("BUG: current_config_origin_type called outside config callback");
0d44a2da 2471 return type ? type : "command line";
473166b9
LS
2472}
2473
2474const char *current_config_name(void)
2475{
0d44a2da
JK
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
3258258f 2482 die("BUG: current_config_name called outside config callback");
0d44a2da 2483 return name ? name : "";
473166b9 2484}