]> git.ipfire.org Git - thirdparty/git.git/blame - config.c
avoid segfault when reading header of malformed commits
[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"
7f0e39fa 9#include "exec_cmd.h"
572e4f6a 10#include "strbuf.h"
2b64fc89 11#include "quote.h"
17712991
LT
12
13#define MAXNAME (256)
14
924aaf3e
RJ
15typedef struct config_file {
16 struct config_file *prev;
17 FILE *f;
18 const char *name;
19 int linenr;
20 int eof;
21 struct strbuf value;
22 char var[MAXNAME];
23} config_file;
24
25static config_file *cf;
26
960ccca6
DH
27static int zlib_compression_seen;
28
dc871831
DB
29const char *config_exclusive_filename = NULL;
30
8b1fa778
AR
31static void lowercase(char *p)
32{
33 for (; *p; p++)
34 *p = tolower(*p);
35}
8b1fa778 36
2b64fc89
JK
37void git_config_push_parameter(const char *text)
38{
39 struct strbuf env = STRBUF_INIT;
40 const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
41 if (old) {
42 strbuf_addstr(&env, old);
43 strbuf_addch(&env, ' ');
44 }
45 sq_quote_buf(&env, text);
46 setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
47 strbuf_release(&env);
48}
49
2496844b
JK
50int git_config_parse_parameter(const char *text,
51 config_fn_t fn, void *data)
8b1fa778 52{
572e4f6a 53 struct strbuf **pair;
f77bccae 54 pair = strbuf_split_str(text, '=', 2);
c5d6350b
JK
55 if (!pair[0])
56 return error("bogus config parameter: %s", text);
572e4f6a
AR
57 if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=')
58 strbuf_setlen(pair[0], pair[0]->len - 1);
59 strbuf_trim(pair[0]);
60 if (!pair[0]->len) {
61 strbuf_list_free(pair);
06eb708f 62 return error("bogus config parameter: %s", text);
8b1fa778 63 }
06eb708f
JK
64 lowercase(pair[0]->buf);
65 if (fn(pair[0]->buf, pair[1] ? pair[1]->buf : NULL, data) < 0) {
66 strbuf_list_free(pair);
67 return -1;
572e4f6a
AR
68 }
69 strbuf_list_free(pair);
8b1fa778
AR
70 return 0;
71}
72
06eb708f
JK
73int git_config_from_parameters(config_fn_t fn, void *data)
74{
2b64fc89
JK
75 const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
76 char *envw;
77 const char **argv = NULL;
78 int nr = 0, alloc = 0;
79 int i;
80
81 if (!env)
82 return 0;
83 /* sq_dequote will write over it */
84 envw = xstrdup(env);
85
86 if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
87 free(envw);
88 return error("bogus format in " CONFIG_DATA_ENVIRONMENT);
89 }
90
91 for (i = 0; i < nr; i++) {
06eb708f 92 if (git_config_parse_parameter(argv[i], fn, data) < 0) {
2b64fc89
JK
93 free(argv);
94 free(envw);
95 return -1;
96 }
97 }
98
99 free(argv);
100 free(envw);
06eb708f 101 return nr > 0;
2b64fc89
JK
102}
103
17712991
LT
104static int get_next_char(void)
105{
106 int c;
107 FILE *f;
108
109 c = '\n';
924aaf3e 110 if (cf && ((f = cf->f) != NULL)) {
17712991 111 c = fgetc(f);
db2c075d
JH
112 if (c == '\r') {
113 /* DOS like systems */
114 c = fgetc(f);
115 if (c != '\n') {
116 ungetc(c, f);
117 c = '\r';
118 }
119 }
17712991 120 if (c == '\n')
924aaf3e 121 cf->linenr++;
17712991 122 if (c == EOF) {
924aaf3e 123 cf->eof = 1;
17712991
LT
124 c = '\n';
125 }
126 }
127 return c;
128}
129
130static char *parse_value(void)
131{
e96c19c5 132 int quote = 0, comment = 0, space = 0;
17712991 133
924aaf3e 134 strbuf_reset(&cf->value);
17712991
LT
135 for (;;) {
136 int c = get_next_char();
17712991 137 if (c == '\n') {
4b340593
MS
138 if (quote) {
139 cf->linenr--;
17712991 140 return NULL;
4b340593 141 }
924aaf3e 142 return cf->value.buf;
17712991
LT
143 }
144 if (comment)
145 continue;
146 if (isspace(c) && !quote) {
924aaf3e 147 if (cf->value.len)
ebdaae37 148 space++;
17712991
LT
149 continue;
150 }
7ebdba61
JS
151 if (!quote) {
152 if (c == ';' || c == '#') {
153 comment = 1;
154 continue;
155 }
156 }
ebdaae37 157 for (; space; space--)
924aaf3e 158 strbuf_addch(&cf->value, ' ');
17712991
LT
159 if (c == '\\') {
160 c = get_next_char();
161 switch (c) {
162 case '\n':
163 continue;
164 case 't':
165 c = '\t';
166 break;
167 case 'b':
168 c = '\b';
169 break;
170 case 'n':
171 c = '\n';
172 break;
5cbb401d
LT
173 /* Some characters escape as themselves */
174 case '\\': case '"':
175 break;
176 /* Reject unknown escape sequences */
177 default:
178 return NULL;
17712991 179 }
924aaf3e 180 strbuf_addch(&cf->value, c);
17712991
LT
181 continue;
182 }
183 if (c == '"') {
184 quote = 1-quote;
185 continue;
186 }
924aaf3e 187 strbuf_addch(&cf->value, c);
17712991
LT
188 }
189}
190
38c5afa8
LT
191static inline int iskeychar(int c)
192{
193 return isalnum(c) || c == '-';
194}
195
ef90d6d4 196static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
17712991
LT
197{
198 int c;
199 char *value;
200
201 /* Get the full name */
202 for (;;) {
203 c = get_next_char();
924aaf3e 204 if (cf->eof)
17712991 205 break;
38c5afa8 206 if (!iskeychar(c))
17712991
LT
207 break;
208 name[len++] = tolower(c);
209 if (len >= MAXNAME)
210 return -1;
211 }
212 name[len] = 0;
213 while (c == ' ' || c == '\t')
214 c = get_next_char();
215
216 value = NULL;
217 if (c != '\n') {
218 if (c != '=')
219 return -1;
220 value = parse_value();
221 if (!value)
222 return -1;
223 }
ef90d6d4 224 return fn(name, value, data);
17712991
LT
225}
226
d14f7764
LT
227static int get_extended_base_var(char *name, int baselen, int c)
228{
229 do {
230 if (c == '\n')
4b340593 231 goto error_incomplete_line;
d14f7764
LT
232 c = get_next_char();
233 } while (isspace(c));
234
235 /* We require the format to be '[base "extension"]' */
236 if (c != '"')
237 return -1;
238 name[baselen++] = '.';
239
240 for (;;) {
241 int c = get_next_char();
242 if (c == '\n')
4b340593 243 goto error_incomplete_line;
d14f7764
LT
244 if (c == '"')
245 break;
246 if (c == '\\') {
247 c = get_next_char();
248 if (c == '\n')
4b340593 249 goto error_incomplete_line;
d14f7764
LT
250 }
251 name[baselen++] = c;
252 if (baselen > MAXNAME / 2)
253 return -1;
254 }
255
256 /* Final ']' */
257 if (get_next_char() != ']')
258 return -1;
259 return baselen;
4b340593
MS
260error_incomplete_line:
261 cf->linenr--;
262 return -1;
d14f7764
LT
263}
264
17712991
LT
265static int get_base_var(char *name)
266{
267 int baselen = 0;
268
269 for (;;) {
270 int c = get_next_char();
924aaf3e 271 if (cf->eof)
17712991
LT
272 return -1;
273 if (c == ']')
274 return baselen;
d14f7764
LT
275 if (isspace(c))
276 return get_extended_base_var(name, baselen, c);
38c5afa8 277 if (!iskeychar(c) && c != '.')
17712991
LT
278 return -1;
279 if (baselen > MAXNAME / 2)
280 return -1;
281 name[baselen++] = tolower(c);
282 }
283}
284
ef90d6d4 285static int git_parse_file(config_fn_t fn, void *data)
17712991
LT
286{
287 int comment = 0;
288 int baselen = 0;
924aaf3e 289 char *var = cf->var;
17712991 290
de056402
PB
291 /* U+FEFF Byte Order Mark in UTF8 */
292 static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
293 const unsigned char *bomptr = utf8_bom;
294
17712991
LT
295 for (;;) {
296 int c = get_next_char();
de056402
PB
297 if (bomptr && *bomptr) {
298 /* We are at the file beginning; skip UTF8-encoded BOM
299 * if present. Sane editors won't put this in on their
300 * own, but e.g. Windows Notepad will do it happily. */
301 if ((unsigned char) c == *bomptr) {
302 bomptr++;
303 continue;
304 } else {
305 /* Do not tolerate partial BOM. */
306 if (bomptr != utf8_bom)
307 break;
308 /* No BOM at file beginning. Cool. */
309 bomptr = NULL;
310 }
311 }
17712991 312 if (c == '\n') {
924aaf3e 313 if (cf->eof)
17712991
LT
314 return 0;
315 comment = 0;
316 continue;
317 }
318 if (comment || isspace(c))
319 continue;
320 if (c == '#' || c == ';') {
321 comment = 1;
322 continue;
323 }
324 if (c == '[') {
325 baselen = get_base_var(var);
326 if (baselen <= 0)
327 break;
328 var[baselen++] = '.';
329 var[baselen] = 0;
330 continue;
331 }
332 if (!isalpha(c))
333 break;
128af9d1 334 var[baselen] = tolower(c);
ef90d6d4 335 if (get_value(fn, data, var, baselen+1) < 0)
17712991
LT
336 break;
337 }
924aaf3e 338 die("bad config file line %d in %s", cf->linenr, cf->name);
17712991
LT
339}
340
ebaa1bd4 341static int parse_unit_factor(const char *end, uintmax_t *val)
0b87b6e0
BD
342{
343 if (!*end)
344 return 1;
c8deb5a1
SP
345 else if (!strcasecmp(end, "k")) {
346 *val *= 1024;
347 return 1;
348 }
349 else if (!strcasecmp(end, "m")) {
350 *val *= 1024 * 1024;
351 return 1;
352 }
353 else if (!strcasecmp(end, "g")) {
354 *val *= 1024 * 1024 * 1024;
355 return 1;
356 }
357 return 0;
0b87b6e0
BD
358}
359
0433bcd9 360static int git_parse_long(const char *value, long *ret)
0b87b6e0
BD
361{
362 if (value && *value) {
363 char *end;
ebaa1bd4
NA
364 intmax_t val;
365 uintmax_t uval;
366 uintmax_t factor = 1;
367
368 errno = 0;
369 val = strtoimax(value, &end, 0);
370 if (errno == ERANGE)
371 return 0;
c8deb5a1
SP
372 if (!parse_unit_factor(end, &factor))
373 return 0;
ebaa1bd4
NA
374 uval = abs(val);
375 uval *= factor;
376 if ((uval > maximum_signed_value_of_type(long)) ||
377 (abs(val) > uval))
378 return 0;
379 val *= factor;
380 *ret = val;
0b87b6e0
BD
381 return 1;
382 }
383 return 0;
384}
385
386int git_parse_ulong(const char *value, unsigned long *ret)
17712991
LT
387{
388 if (value && *value) {
389 char *end;
ebaa1bd4
NA
390 uintmax_t val;
391 uintmax_t oldval;
392
393 errno = 0;
394 val = strtoumax(value, &end, 0);
395 if (errno == ERANGE)
396 return 0;
397 oldval = val;
c8deb5a1
SP
398 if (!parse_unit_factor(end, &val))
399 return 0;
ebaa1bd4
NA
400 if ((val > maximum_unsigned_value_of_type(long)) ||
401 (oldval > val))
402 return 0;
c8deb5a1 403 *ret = val;
0b87b6e0 404 return 1;
17712991 405 }
0b87b6e0
BD
406 return 0;
407}
408
c1867cea
JK
409static void die_bad_config(const char *name)
410{
924aaf3e
RJ
411 if (cf && cf->name)
412 die("bad config value for '%s' in %s", name, cf->name);
c1867cea
JK
413 die("bad config value for '%s'", name);
414}
415
0b87b6e0
BD
416int git_config_int(const char *name, const char *value)
417{
0433bcd9 418 long ret = 0;
0b87b6e0 419 if (!git_parse_long(value, &ret))
c1867cea 420 die_bad_config(name);
0b87b6e0
BD
421 return ret;
422}
423
424unsigned long git_config_ulong(const char *name, const char *value)
425{
426 unsigned long ret;
427 if (!git_parse_ulong(value, &ret))
c1867cea 428 die_bad_config(name);
0b87b6e0 429 return ret;
17712991
LT
430}
431
b2be2f6a 432static int git_config_maybe_bool_text(const char *name, const char *value)
17712991
LT
433{
434 if (!value)
435 return 1;
436 if (!*value)
437 return 0;
8420ccd8
JH
438 if (!strcasecmp(value, "true")
439 || !strcasecmp(value, "yes")
440 || !strcasecmp(value, "on"))
17712991 441 return 1;
8420ccd8
JH
442 if (!strcasecmp(value, "false")
443 || !strcasecmp(value, "no")
444 || !strcasecmp(value, "off"))
17712991 445 return 0;
8420ccd8
JH
446 return -1;
447}
448
b2be2f6a
JK
449int git_config_maybe_bool(const char *name, const char *value)
450{
db6195ef 451 long v = git_config_maybe_bool_text(name, value);
b2be2f6a
JK
452 if (0 <= v)
453 return v;
db6195ef
JK
454 if (git_parse_long(value, &v))
455 return !!v;
b2be2f6a
JK
456 return -1;
457}
458
8420ccd8
JH
459int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
460{
b2be2f6a 461 int v = git_config_maybe_bool_text(name, value);
8420ccd8
JH
462 if (0 <= v) {
463 *is_bool = 1;
464 return v;
465 }
a53f2ec6 466 *is_bool = 0;
c35b0b58 467 return git_config_int(name, value);
17712991
LT
468}
469
a53f2ec6
JH
470int git_config_bool(const char *name, const char *value)
471{
472 int discard;
c35b0b58 473 return !!git_config_bool_or_int(name, value, &discard);
a53f2ec6
JH
474}
475
ea5105a5
CC
476int git_config_string(const char **dest, const char *var, const char *value)
477{
478 if (!value)
479 return config_error_nonbool(var);
480 *dest = xstrdup(value);
481 return 0;
482}
483
395de250
MM
484int git_config_pathname(const char **dest, const char *var, const char *value)
485{
486 if (!value)
487 return config_error_nonbool(var);
488 *dest = expand_user_path(value);
489 if (!*dest)
490 die("Failed to expand user dir in: '%s'", value);
491 return 0;
492}
493
806e2ad7 494static int git_default_core_config(const char *var, const char *value)
17712991
LT
495{
496 /* This needs a better name */
497 if (!strcmp(var, "core.filemode")) {
498 trust_executable_bit = git_config_bool(var, value);
499 return 0;
500 }
1ce4790b
AR
501 if (!strcmp(var, "core.trustctime")) {
502 trust_ctime = git_config_bool(var, value);
503 return 0;
504 }
17712991 505
9378c161
JH
506 if (!strcmp(var, "core.quotepath")) {
507 quote_path_fully = git_config_bool(var, value);
508 return 0;
509 }
510
78a8d641
JS
511 if (!strcmp(var, "core.symlinks")) {
512 has_symlinks = git_config_bool(var, value);
513 return 0;
514 }
515
0a9b88b7
LT
516 if (!strcmp(var, "core.ignorecase")) {
517 ignore_case = git_config_bool(var, value);
518 return 0;
519 }
520
64589a03
JH
521 if (!strcmp(var, "core.attributesfile"))
522 return git_config_pathname(&git_attributes_file, var, value);
523
7d1864ce
JH
524 if (!strcmp(var, "core.bare")) {
525 is_bare_repository_cfg = git_config_bool(var, value);
526 return 0;
527 }
528
5f73076c
JH
529 if (!strcmp(var, "core.ignorestat")) {
530 assume_unchanged = git_config_bool(var, value);
531 return 0;
532 }
533
e388c738
JH
534 if (!strcmp(var, "core.prefersymlinkrefs")) {
535 prefer_symlink_refs = git_config_bool(var, value);
f8348be3
JS
536 return 0;
537 }
538
6de08ae6
SP
539 if (!strcmp(var, "core.logallrefupdates")) {
540 log_all_ref_updates = git_config_bool(var, value);
541 return 0;
542 }
543
2f8acdb3
JH
544 if (!strcmp(var, "core.warnambiguousrefs")) {
545 warn_ambiguous_refs = git_config_bool(var, value);
546 return 0;
547 }
548
a71f09fe 549 if (!strcmp(var, "core.abbrev")) {
dce96489
LT
550 int abbrev = git_config_int(var, value);
551 if (abbrev < minimum_abbrev || abbrev > 40)
552 return -1;
553 default_abbrev = abbrev;
554 return 0;
555 }
556
960ccca6 557 if (!strcmp(var, "core.loosecompression")) {
12f6c308
JBH
558 int level = git_config_int(var, value);
559 if (level == -1)
560 level = Z_DEFAULT_COMPRESSION;
561 else if (level < 0 || level > Z_BEST_COMPRESSION)
562 die("bad zlib compression level %d", level);
563 zlib_compression_level = level;
960ccca6
DH
564 zlib_compression_seen = 1;
565 return 0;
566 }
567
568 if (!strcmp(var, "core.compression")) {
569 int level = git_config_int(var, value);
570 if (level == -1)
571 level = Z_DEFAULT_COMPRESSION;
572 else if (level < 0 || level > Z_BEST_COMPRESSION)
573 die("bad zlib compression level %d", level);
574 core_compression_level = level;
575 core_compression_seen = 1;
576 if (!zlib_compression_seen)
577 zlib_compression_level = level;
12f6c308
JBH
578 return 0;
579 }
580
60bb8b14 581 if (!strcmp(var, "core.packedgitwindowsize")) {
5faaf246 582 int pgsz_x2 = getpagesize() * 2;
ebaa1bd4 583 packed_git_window_size = git_config_ulong(var, value);
5faaf246
JH
584
585 /* This value must be multiple of (pagesize * 2) */
586 packed_git_window_size /= pgsz_x2;
587 if (packed_git_window_size < 1)
588 packed_git_window_size = 1;
589 packed_git_window_size *= pgsz_x2;
60bb8b14
SP
590 return 0;
591 }
592
15366280 593 if (!strcmp(var, "core.bigfilethreshold")) {
ebaa1bd4 594 big_file_threshold = git_config_ulong(var, value);
15366280
JH
595 return 0;
596 }
597
77ccc5bb 598 if (!strcmp(var, "core.packedgitlimit")) {
ebaa1bd4 599 packed_git_limit = git_config_ulong(var, value);
77ccc5bb
SP
600 return 0;
601 }
602
18bdec11 603 if (!strcmp(var, "core.deltabasecachelimit")) {
ebaa1bd4 604 delta_base_cache_limit = git_config_ulong(var, value);
18bdec11
SP
605 return 0;
606 }
607
5f44324d
JH
608 if (!strcmp(var, "core.logpackaccess"))
609 return git_config_string(&log_pack_access, var, value);
610
6c510bee 611 if (!strcmp(var, "core.autocrlf")) {
d7f46334 612 if (value && !strcasecmp(value, "input")) {
ec70f52f 613 if (core_eol == EOL_CRLF)
942e7747 614 return error("core.autocrlf=input conflicts with core.eol=crlf");
fd6cce9e 615 auto_crlf = AUTO_CRLF_INPUT;
d7f46334
LT
616 return 0;
617 }
6c510bee
LT
618 auto_crlf = git_config_bool(var, value);
619 return 0;
620 }
621
21e5ad50
SP
622 if (!strcmp(var, "core.safecrlf")) {
623 if (value && !strcasecmp(value, "warn")) {
624 safe_crlf = SAFE_CRLF_WARN;
625 return 0;
626 }
627 safe_crlf = git_config_bool(var, value);
628 return 0;
629 }
630
942e7747
EB
631 if (!strcmp(var, "core.eol")) {
632 if (value && !strcasecmp(value, "lf"))
ec70f52f 633 core_eol = EOL_LF;
942e7747 634 else if (value && !strcasecmp(value, "crlf"))
ec70f52f 635 core_eol = EOL_CRLF;
942e7747 636 else if (value && !strcasecmp(value, "native"))
ec70f52f 637 core_eol = EOL_NATIVE;
942e7747 638 else
ec70f52f
JH
639 core_eol = EOL_UNSET;
640 if (core_eol == EOL_CRLF && auto_crlf == AUTO_CRLF_INPUT)
942e7747
EB
641 return error("core.autocrlf=input conflicts with core.eol=crlf");
642 return 0;
643 }
644
a97a7468
JS
645 if (!strcmp(var, "core.notesref")) {
646 notes_ref_name = xstrdup(value);
647 return 0;
648 }
649
806e2ad7
LT
650 if (!strcmp(var, "core.pager"))
651 return git_config_string(&pager_program, var, value);
652
653 if (!strcmp(var, "core.editor"))
654 return git_config_string(&editor_program, var, value);
655
d3e7da89
AK
656 if (!strcmp(var, "core.askpass"))
657 return git_config_string(&askpass_program, var, value);
658
806e2ad7 659 if (!strcmp(var, "core.excludesfile"))
395de250 660 return git_config_pathname(&excludes_file, var, value);
806e2ad7
LT
661
662 if (!strcmp(var, "core.whitespace")) {
663 if (!value)
664 return config_error_nonbool(var);
665 whitespace_rule_cfg = parse_whitespace_rule(value);
666 return 0;
667 }
668
aafe9fba
LT
669 if (!strcmp(var, "core.fsyncobjectfiles")) {
670 fsync_object_files = git_config_bool(var, value);
671 return 0;
672 }
673
671c9b7e
LT
674 if (!strcmp(var, "core.preloadindex")) {
675 core_preload_index = git_config_bool(var, value);
676 return 0;
677 }
678
348df166
JS
679 if (!strcmp(var, "core.createobject")) {
680 if (!strcmp(value, "rename"))
681 object_creation_mode = OBJECT_CREATION_USES_RENAMES;
682 else if (!strcmp(value, "link"))
683 object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
684 else
685 die("Invalid mode for object creation: %s", value);
be66a6c4
JS
686 return 0;
687 }
688
08aefc9e
NTND
689 if (!strcmp(var, "core.sparsecheckout")) {
690 core_apply_sparse_checkout = git_config_bool(var, value);
691 return 0;
692 }
693
806e2ad7
LT
694 /* Add other config variables here and to Documentation/config.txt. */
695 return 0;
696}
697
d1364529 698static int git_default_user_config(const char *var, const char *value)
806e2ad7 699{
e1b10391 700 if (!strcmp(var, "user.name")) {
6c47d0e8
JH
701 if (!value)
702 return config_error_nonbool(var);
817151e6 703 strlcpy(git_default_name, value, sizeof(git_default_name));
91c38a21 704 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
e1b10391
LT
705 return 0;
706 }
707
708 if (!strcmp(var, "user.email")) {
6c47d0e8
JH
709 if (!value)
710 return config_error_nonbool(var);
817151e6 711 strlcpy(git_default_email, value, sizeof(git_default_email));
91c38a21 712 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
e1b10391
LT
713 return 0;
714 }
715
d1364529
LT
716 /* Add other config variables here and to Documentation/config.txt. */
717 return 0;
718}
719
1141f492 720static int git_default_i18n_config(const char *var, const char *value)
d1364529 721{
ea5105a5
CC
722 if (!strcmp(var, "i18n.commitencoding"))
723 return git_config_string(&git_commit_encoding, var, value);
d2c11a38 724
ea5105a5
CC
725 if (!strcmp(var, "i18n.logoutputencoding"))
726 return git_config_string(&git_log_output_encoding, var, value);
d2c11a38 727
1141f492
LT
728 /* Add other config variables here and to Documentation/config.txt. */
729 return 0;
730}
039bc64e 731
1141f492
LT
732static int git_default_branch_config(const char *var, const char *value)
733{
9ed36cfa
JS
734 if (!strcmp(var, "branch.autosetupmerge")) {
735 if (value && !strcasecmp(value, "always")) {
736 git_branch_track = BRANCH_TRACK_ALWAYS;
737 return 0;
738 }
739 git_branch_track = git_config_bool(var, value);
740 return 0;
741 }
c998ae9b
DS
742 if (!strcmp(var, "branch.autosetuprebase")) {
743 if (!value)
744 return config_error_nonbool(var);
745 else if (!strcmp(value, "never"))
746 autorebase = AUTOREBASE_NEVER;
747 else if (!strcmp(value, "local"))
748 autorebase = AUTOREBASE_LOCAL;
749 else if (!strcmp(value, "remote"))
750 autorebase = AUTOREBASE_REMOTE;
751 else if (!strcmp(value, "always"))
752 autorebase = AUTOREBASE_ALWAYS;
753 else
754 return error("Malformed value for %s", var);
755 return 0;
756 }
a9cc857a 757
1ab661dd 758 /* Add other config variables here and to Documentation/config.txt. */
17712991
LT
759 return 0;
760}
761
52153747
FAG
762static int git_default_push_config(const char *var, const char *value)
763{
764 if (!strcmp(var, "push.default")) {
765 if (!value)
766 return config_error_nonbool(var);
767 else if (!strcmp(value, "nothing"))
768 push_default = PUSH_DEFAULT_NOTHING;
769 else if (!strcmp(value, "matching"))
770 push_default = PUSH_DEFAULT_MATCHING;
53c40311
JH
771 else if (!strcmp(value, "upstream"))
772 push_default = PUSH_DEFAULT_UPSTREAM;
773 else if (!strcmp(value, "tracking")) /* deprecated */
774 push_default = PUSH_DEFAULT_UPSTREAM;
52153747
FAG
775 else if (!strcmp(value, "current"))
776 push_default = PUSH_DEFAULT_CURRENT;
777 else {
778 error("Malformed value for %s: %s", var, value);
779 return error("Must be one of nothing, matching, "
780 "tracking or current.");
781 }
782 return 0;
783 }
784
785 /* Add other config variables here and to Documentation/config.txt. */
786 return 0;
787}
788
d551a488
MSO
789static int git_default_mailmap_config(const char *var, const char *value)
790{
791 if (!strcmp(var, "mailmap.file"))
792 return git_config_string(&git_mailmap_file, var, value);
793
794 /* Add other config variables here and to Documentation/config.txt. */
795 return 0;
796}
797
1141f492
LT
798int git_default_config(const char *var, const char *value, void *dummy)
799{
800 if (!prefixcmp(var, "core."))
801 return git_default_core_config(var, value);
802
803 if (!prefixcmp(var, "user."))
804 return git_default_user_config(var, value);
805
806 if (!prefixcmp(var, "i18n."))
807 return git_default_i18n_config(var, value);
808
809 if (!prefixcmp(var, "branch."))
810 return git_default_branch_config(var, value);
811
52153747
FAG
812 if (!prefixcmp(var, "push."))
813 return git_default_push_config(var, value);
814
d551a488
MSO
815 if (!prefixcmp(var, "mailmap."))
816 return git_default_mailmap_config(var, value);
817
75194438
JK
818 if (!prefixcmp(var, "advice."))
819 return git_default_advice_config(var, value);
820
1141f492
LT
821 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
822 pager_use_color = git_config_bool(var,value);
823 return 0;
824 }
825
568508e7
JH
826 if (!strcmp(var, "pack.packsizelimit")) {
827 pack_size_limit_cfg = git_config_ulong(var, value);
828 return 0;
829 }
1141f492
LT
830 /* Add other config variables here and to Documentation/config.txt. */
831 return 0;
832}
833
ef90d6d4 834int git_config_from_file(config_fn_t fn, const char *filename, void *data)
17712991
LT
835{
836 int ret;
4f629539 837 FILE *f = fopen(filename, "r");
17712991
LT
838
839 ret = -1;
840 if (f) {
924aaf3e
RJ
841 config_file top;
842
843 /* push config-file parsing state stack */
844 top.prev = cf;
845 top.f = f;
846 top.name = filename;
847 top.linenr = 1;
848 top.eof = 0;
849 strbuf_init(&top.value, 1024);
850 cf = &top;
851
ef90d6d4 852 ret = git_parse_file(fn, data);
924aaf3e
RJ
853
854 /* pop config-file parsing state stack */
855 strbuf_release(&top.value);
856 cf = top.prev;
857
17712991
LT
858 fclose(f);
859 }
860 return ret;
861}
10bea152 862
506b17b1
JS
863const char *git_etc_gitconfig(void)
864{
7f0e39fa 865 static const char *system_wide;
2de9de5e
SP
866 if (!system_wide)
867 system_wide = system_path(ETC_GITCONFIG);
7f0e39fa 868 return system_wide;
506b17b1
JS
869}
870
0ef37164 871int git_env_bool(const char *k, int def)
ab88c363
JK
872{
873 const char *v = getenv(k);
874 return v ? git_config_bool(k, v) : def;
875}
876
877int git_config_system(void)
878{
879 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
880}
881
dbdf5854 882int git_config_early(config_fn_t fn, void *data, const char *repo_config)
4f629539 883{
aa387407 884 int ret = 0, found = 0;
dc871831 885 const char *home = NULL;
5f1a63e0 886
8befc50c 887 /* Setting $GIT_CONFIG makes git read _only_ the given config file. */
dc871831
DB
888 if (config_exclusive_filename)
889 return git_config_from_file(fn, config_exclusive_filename, data);
aa387407 890 if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) {
dc871831
DB
891 ret += git_config_from_file(fn, git_etc_gitconfig(),
892 data);
aa387407
FC
893 found += 1;
894 }
5f1a63e0 895
dc871831 896 home = getenv("HOME");
8f323c00 897 if (home) {
05bab3ea
RJ
898 char buf[PATH_MAX];
899 char *user_config = mksnpath(buf, sizeof(buf), "%s/.gitconfig", home);
aa387407 900 if (!access(user_config, R_OK)) {
dc871831 901 ret += git_config_from_file(fn, user_config, data);
aa387407
FC
902 found += 1;
903 }
5f1a63e0
JS
904 }
905
dbdf5854 906 if (repo_config && !access(repo_config, R_OK)) {
aa387407
FC
907 ret += git_config_from_file(fn, repo_config, data);
908 found += 1;
909 }
8b1fa778 910
5a0c9eeb
JK
911 switch (git_config_from_parameters(fn, data)) {
912 case -1: /* error */
1c2c9bee 913 die("unable to parse command-line config");
5a0c9eeb
JK
914 break;
915 case 0: /* found nothing */
916 break;
917 default: /* found at least one item */
918 found++;
919 break;
920 }
8b1fa778 921
1f2baa78 922 return ret == 0 ? found : ret;
4f629539
JH
923}
924
dbdf5854
NTND
925int git_config(config_fn_t fn, void *data)
926{
927 char *repo_config = NULL;
928 int ret;
929
930 repo_config = git_pathdup("config");
931 ret = git_config_early(fn, data, repo_config);
932 if (repo_config)
933 free(repo_config);
934 return ret;
935}
936
10bea152
JS
937/*
938 * Find all the stuff for git_config_set() below.
939 */
4ddba79d
JS
940
941#define MAX_MATCHES 512
942
10bea152
JS
943static struct {
944 int baselen;
4b25d091 945 char *key;
f98d863d 946 int do_not_match;
4b25d091 947 regex_t *value_regex;
4ddba79d 948 int multi_replace;
dc49cd76 949 size_t offset[MAX_MATCHES];
10bea152
JS
950 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
951 int seen;
952} store;
953
4b25d091 954static int matches(const char *key, const char *value)
f98d863d
JS
955{
956 return !strcmp(key, store.key) &&
957 (store.value_regex == NULL ||
958 (store.do_not_match ^
959 !regexec(store.value_regex, value, 0, NULL, 0)));
960}
961
4b25d091 962static int store_aux(const char *key, const char *value, void *cb)
10bea152 963{
ae9ee41d
JH
964 const char *ep;
965 size_t section_len;
924aaf3e 966 FILE *f = cf->f;
ae9ee41d 967
10bea152
JS
968 switch (store.state) {
969 case KEY_SEEN:
f98d863d 970 if (matches(key, value)) {
4ddba79d 971 if (store.seen == 1 && store.multi_replace == 0) {
64c0d71c 972 warning("%s has multiple values", key);
4ddba79d 973 } else if (store.seen >= MAX_MATCHES) {
64c0d71c 974 error("too many matches for %s", key);
4ddba79d 975 return 1;
10bea152 976 }
4ddba79d 977
924aaf3e 978 store.offset[store.seen] = ftell(f);
10bea152
JS
979 store.seen++;
980 }
981 break;
982 case SECTION_SEEN:
ae9ee41d
JH
983 /*
984 * What we are looking for is in store.key (both
985 * section and var), and its section part is baselen
986 * long. We found key (again, both section and var).
987 * We would want to know if this key is in the same
988 * section as what we are looking for. We already
989 * know we are in the same section as what should
990 * hold store.key.
991 */
992 ep = strrchr(key, '.');
993 section_len = ep - key;
994
995 if ((section_len != store.baselen) ||
996 memcmp(key, store.key, section_len+1)) {
10bea152
JS
997 store.state = SECTION_END_SEEN;
998 break;
ae9ee41d
JH
999 }
1000
1001 /*
1002 * Do not increment matches: this is no match, but we
1003 * just made sure we are in the desired section.
1004 */
924aaf3e 1005 store.offset[store.seen] = ftell(f);
10bea152
JS
1006 /* fallthru */
1007 case SECTION_END_SEEN:
1008 case START:
f98d863d 1009 if (matches(key, value)) {
924aaf3e 1010 store.offset[store.seen] = ftell(f);
10bea152
JS
1011 store.state = KEY_SEEN;
1012 store.seen++;
d14f7764
LT
1013 } else {
1014 if (strrchr(key, '.') - key == store.baselen &&
bdf0ef08 1015 !strncmp(key, store.key, store.baselen)) {
93ddef3e 1016 store.state = SECTION_SEEN;
924aaf3e 1017 store.offset[store.seen] = ftell(f);
d14f7764 1018 }
bdf0ef08 1019 }
10bea152
JS
1020 }
1021 return 0;
1022}
1023
64c0d71c 1024static int write_error(const char *filename)
480c9e52 1025{
64c0d71c 1026 error("failed to write new configuration file %s", filename);
480c9e52
AW
1027
1028 /* Same error code as "failed to rename". */
1029 return 4;
1030}
1031
4b25d091 1032static int store_write_section(int fd, const char *key)
10bea152 1033{
cb891a59
KH
1034 const char *dot;
1035 int i, success;
f285a2d7 1036 struct strbuf sb = STRBUF_INIT;
d14f7764 1037
cb891a59 1038 dot = memchr(key, '.', store.baselen);
d14f7764 1039 if (dot) {
cb891a59
KH
1040 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
1041 for (i = dot - key + 1; i < store.baselen; i++) {
e5c349ba 1042 if (key[i] == '"' || key[i] == '\\')
cb891a59
KH
1043 strbuf_addch(&sb, '\\');
1044 strbuf_addch(&sb, key[i]);
d14f7764 1045 }
cb891a59
KH
1046 strbuf_addstr(&sb, "\"]\n");
1047 } else {
1048 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
d14f7764
LT
1049 }
1050
cb891a59
KH
1051 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
1052 strbuf_release(&sb);
480c9e52 1053
cb891a59 1054 return success;
10bea152
JS
1055}
1056
4b25d091 1057static int store_write_pair(int fd, const char *key, const char *value)
10bea152 1058{
cb891a59
KH
1059 int i, success;
1060 int length = strlen(key + store.baselen + 1);
1061 const char *quote = "";
f285a2d7 1062 struct strbuf sb = STRBUF_INIT;
cdd4fb15 1063
6281f394
JM
1064 /*
1065 * Check to see if the value needs to be surrounded with a dq pair.
1066 * Note that problematic characters are always backslash-quoted; this
1067 * check is about not losing leading or trailing SP and strings that
1068 * follow beginning-of-comment characters (i.e. ';' and '#') by the
1069 * configuration parser.
1070 */
cdd4fb15 1071 if (value[0] == ' ')
cb891a59 1072 quote = "\"";
cdd4fb15
BG
1073 for (i = 0; value[i]; i++)
1074 if (value[i] == ';' || value[i] == '#')
cb891a59
KH
1075 quote = "\"";
1076 if (i && value[i - 1] == ' ')
1077 quote = "\"";
1078
cb891a59
KH
1079 strbuf_addf(&sb, "\t%.*s = %s",
1080 length, key + store.baselen + 1, quote);
10bea152 1081
10bea152
JS
1082 for (i = 0; value[i]; i++)
1083 switch (value[i]) {
480c9e52 1084 case '\n':
cb891a59 1085 strbuf_addstr(&sb, "\\n");
480c9e52
AW
1086 break;
1087 case '\t':
cb891a59 1088 strbuf_addstr(&sb, "\\t");
480c9e52
AW
1089 break;
1090 case '"':
1091 case '\\':
cb891a59 1092 strbuf_addch(&sb, '\\');
480c9e52 1093 default:
cb891a59 1094 strbuf_addch(&sb, value[i]);
480c9e52
AW
1095 break;
1096 }
cb891a59
KH
1097 strbuf_addf(&sb, "%s\n", quote);
1098
1099 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
1100 strbuf_release(&sb);
1101
1102 return success;
10bea152
JS
1103}
1104
4b25d091
FC
1105static ssize_t find_beginning_of_line(const char *contents, size_t size,
1106 size_t offset_, int *found_bracket)
4ddba79d 1107{
dc49cd76
SP
1108 size_t equal_offset = size, bracket_offset = size;
1109 ssize_t offset;
4ddba79d 1110
7a31cc0f 1111contline:
a6080a0a 1112 for (offset = offset_-2; offset > 0
4ddba79d
JS
1113 && contents[offset] != '\n'; offset--)
1114 switch (contents[offset]) {
1115 case '=': equal_offset = offset; break;
1116 case ']': bracket_offset = offset; break;
1117 }
7a31cc0f
FL
1118 if (offset > 0 && contents[offset-1] == '\\') {
1119 offset_ = offset;
1120 goto contline;
1121 }
4ddba79d
JS
1122 if (bracket_offset < equal_offset) {
1123 *found_bracket = 1;
1124 offset = bracket_offset+1;
1125 } else
1126 offset++;
1127
1128 return offset;
1129}
1130
5ec31182
RR
1131int git_config_set_in_file(const char *config_filename,
1132 const char *key, const char *value)
1133{
1134 return git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
1135}
1136
4b25d091 1137int git_config_set(const char *key, const char *value)
10bea152 1138{
4ddba79d 1139 return git_config_set_multivar(key, value, NULL, 0);
10bea152
JS
1140}
1141
b09c53a3
LP
1142/*
1143 * Auxiliary function to sanity-check and split the key into the section
1144 * identifier and variable name.
1145 *
1146 * Returns 0 on success, -1 when there is an invalid character in the key and
1147 * -2 if there is no section name in the key.
1148 *
1149 * store_key - pointer to char* which will hold a copy of the key with
1150 * lowercase section and variable name
1151 * baselen - pointer to int which will hold the length of the
1152 * section + subsection part, can be NULL
1153 */
1154int git_config_parse_key(const char *key, char **store_key, int *baselen_)
1155{
1156 int i, dot, baselen;
1157 const char *last_dot = strrchr(key, '.');
1158
1159 /*
1160 * Since "key" actually contains the section name and the real
1161 * key name separated by a dot, we have to know where the dot is.
1162 */
1163
2169ddc0 1164 if (last_dot == NULL || last_dot == key) {
b09c53a3 1165 error("key does not contain a section: %s", key);
7a397419 1166 return -CONFIG_NO_SECTION_OR_NAME;
b09c53a3
LP
1167 }
1168
2169ddc0
LP
1169 if (!last_dot[1]) {
1170 error("key does not contain variable name: %s", key);
7a397419 1171 return -CONFIG_NO_SECTION_OR_NAME;
2169ddc0
LP
1172 }
1173
b09c53a3
LP
1174 baselen = last_dot - key;
1175 if (baselen_)
1176 *baselen_ = baselen;
1177
1178 /*
1179 * Validate the key and while at it, lower case it for matching.
1180 */
1181 *store_key = xmalloc(strlen(key) + 1);
1182
1183 dot = 0;
1184 for (i = 0; key[i]; i++) {
1185 unsigned char c = key[i];
1186 if (c == '.')
1187 dot = 1;
1188 /* Leave the extended basename untouched.. */
1189 if (!dot || i > baselen) {
1190 if (!iskeychar(c) ||
1191 (i == baselen + 1 && !isalpha(c))) {
1192 error("invalid key: %s", key);
1193 goto out_free_ret_1;
1194 }
1195 c = tolower(c);
1196 } else if (c == '\n') {
1197 error("invalid key (newline): %s", key);
1198 goto out_free_ret_1;
1199 }
1200 (*store_key)[i] = c;
1201 }
1202 (*store_key)[i] = 0;
1203
1204 return 0;
1205
1206out_free_ret_1:
1207 free(*store_key);
7a397419 1208 return -CONFIG_INVALID_KEY;
b09c53a3
LP
1209}
1210
10bea152
JS
1211/*
1212 * If value==NULL, unset in (remove from) config,
1213 * if value_regex!=NULL, disregard key/value pairs where value does not match.
4ddba79d
JS
1214 * if multi_replace==0, nothing, or only one matching key/value is replaced,
1215 * else all matching key/values (regardless how many) are removed,
1216 * before the new pair is written.
10bea152
JS
1217 *
1218 * Returns 0 on success.
1219 *
1220 * This function does this:
1221 *
1222 * - it locks the config file by creating ".git/config.lock"
1223 *
1224 * - it then parses the config using store_aux() as validator to find
1225 * the position on the key/value pair to replace. If it is to be unset,
1226 * it must be found exactly once.
1227 *
1228 * - the config file is mmap()ed and the part before the match (if any) is
1229 * written to the lock file, then the changed part and the rest.
1230 *
1231 * - the config file is removed and the lock file rename()d to it.
1232 *
1233 */
5ec31182
RR
1234int git_config_set_multivar_in_file(const char *config_filename,
1235 const char *key, const char *value,
1236 const char *value_regex, int multi_replace)
10bea152 1237{
f8ba655e 1238 int fd = -1, in_fd;
dafc88b1 1239 int ret;
6cbf973c 1240 struct lock_file *lock = NULL;
4ddba79d 1241
b09c53a3
LP
1242 /* parse-key returns negative; flip the sign to feed exit(3) */
1243 ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
1244 if (ret)
dafc88b1 1245 goto out_free;
b17e659d
JS
1246
1247 store.multi_replace = multi_replace;
10bea152 1248
10bea152
JS
1249
1250 /*
6cbf973c 1251 * The lock serves a purpose in addition to locking: the new
10bea152
JS
1252 * contents of .git/config will be written into it.
1253 */
6cbf973c
BS
1254 lock = xcalloc(sizeof(struct lock_file), 1);
1255 fd = hold_lock_file_for_update(lock, config_filename, 0);
1256 if (fd < 0) {
6ffd567b 1257 error("could not lock config file %s: %s", config_filename, strerror(errno));
10bea152 1258 free(store.key);
7a397419 1259 ret = CONFIG_NO_LOCK;
dafc88b1 1260 goto out_free;
10bea152
JS
1261 }
1262
1263 /*
1264 * If .git/config does not exist yet, write a minimal version.
1265 */
88fb958b
AR
1266 in_fd = open(config_filename, O_RDONLY);
1267 if ( in_fd < 0 ) {
10bea152
JS
1268 free(store.key);
1269
88fb958b
AR
1270 if ( ENOENT != errno ) {
1271 error("opening %s: %s", config_filename,
1272 strerror(errno));
7a397419 1273 ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
dafc88b1 1274 goto out_free;
88fb958b 1275 }
10bea152
JS
1276 /* if nothing to unset, error out */
1277 if (value == NULL) {
7a397419 1278 ret = CONFIG_NOTHING_SET;
dafc88b1 1279 goto out_free;
10bea152
JS
1280 }
1281
4b25d091 1282 store.key = (char *)key;
480c9e52 1283 if (!store_write_section(fd, key) ||
93c1e079
JH
1284 !store_write_pair(fd, key, value))
1285 goto write_err_out;
1286 } else {
88fb958b 1287 struct stat st;
4b25d091 1288 char *contents;
dc49cd76
SP
1289 size_t contents_sz, copy_begin, copy_end;
1290 int i, new_line = 0;
10bea152
JS
1291
1292 if (value_regex == NULL)
1293 store.value_regex = NULL;
1294 else {
f98d863d
JS
1295 if (value_regex[0] == '!') {
1296 store.do_not_match = 1;
1297 value_regex++;
1298 } else
1299 store.do_not_match = 0;
1300
2d7320d0 1301 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
10bea152
JS
1302 if (regcomp(store.value_regex, value_regex,
1303 REG_EXTENDED)) {
64c0d71c 1304 error("invalid pattern: %s", value_regex);
10bea152 1305 free(store.value_regex);
7a397419 1306 ret = CONFIG_INVALID_PATTERN;
dafc88b1 1307 goto out_free;
10bea152
JS
1308 }
1309 }
1310
4ddba79d 1311 store.offset[0] = 0;
10bea152
JS
1312 store.state = START;
1313 store.seen = 0;
1314
1315 /*
1316 * After this, store.offset will contain the *end* offset
1317 * of the last match, or remain at 0 if no match was found.
1318 * As a side effect, we make sure to transform only a valid
1319 * existing config file.
1320 */
ef90d6d4 1321 if (git_config_from_file(store_aux, config_filename, NULL)) {
64c0d71c 1322 error("invalid config file %s", config_filename);
10bea152
JS
1323 free(store.key);
1324 if (store.value_regex != NULL) {
1325 regfree(store.value_regex);
1326 free(store.value_regex);
1327 }
7a397419 1328 ret = CONFIG_INVALID_FILE;
dafc88b1 1329 goto out_free;
10bea152
JS
1330 }
1331
1332 free(store.key);
1333 if (store.value_regex != NULL) {
1334 regfree(store.value_regex);
1335 free(store.value_regex);
1336 }
1337
4ddba79d
JS
1338 /* if nothing to unset, or too many matches, error out */
1339 if ((store.seen == 0 && value == NULL) ||
1340 (store.seen > 1 && multi_replace == 0)) {
7a397419 1341 ret = CONFIG_NOTHING_SET;
dafc88b1 1342 goto out_free;
10bea152
JS
1343 }
1344
88fb958b 1345 fstat(in_fd, &st);
dc49cd76
SP
1346 contents_sz = xsize_t(st.st_size);
1347 contents = xmmap(NULL, contents_sz, PROT_READ,
10bea152
JS
1348 MAP_PRIVATE, in_fd, 0);
1349 close(in_fd);
1350
4ddba79d
JS
1351 if (store.seen == 0)
1352 store.seen = 1;
1353
1354 for (i = 0, copy_begin = 0; i < store.seen; i++) {
1355 if (store.offset[i] == 0) {
dc49cd76 1356 store.offset[i] = copy_end = contents_sz;
4ddba79d
JS
1357 } else if (store.state != KEY_SEEN) {
1358 copy_end = store.offset[i];
10bea152 1359 } else
4ddba79d 1360 copy_end = find_beginning_of_line(
dc49cd76 1361 contents, contents_sz,
4ddba79d
JS
1362 store.offset[i]-2, &new_line);
1363
02e5ba4a
JK
1364 if (copy_end > 0 && contents[copy_end-1] != '\n')
1365 new_line = 1;
1366
4ddba79d
JS
1367 /* write the first part of the config */
1368 if (copy_end > copy_begin) {
93c1e079
JH
1369 if (write_in_full(fd, contents + copy_begin,
1370 copy_end - copy_begin) <
1371 copy_end - copy_begin)
1372 goto write_err_out;
1373 if (new_line &&
2b7ca830 1374 write_str_in_full(fd, "\n") != 1)
93c1e079 1375 goto write_err_out;
4ddba79d
JS
1376 }
1377 copy_begin = store.offset[i];
10bea152
JS
1378 }
1379
10bea152
JS
1380 /* write the pair (value == NULL means unset) */
1381 if (value != NULL) {
93c1e079
JH
1382 if (store.state == START) {
1383 if (!store_write_section(fd, key))
1384 goto write_err_out;
480c9e52 1385 }
93c1e079
JH
1386 if (!store_write_pair(fd, key, value))
1387 goto write_err_out;
10bea152
JS
1388 }
1389
1390 /* write the rest of the config */
dc49cd76 1391 if (copy_begin < contents_sz)
93c1e079 1392 if (write_in_full(fd, contents + copy_begin,
dc49cd76
SP
1393 contents_sz - copy_begin) <
1394 contents_sz - copy_begin)
93c1e079 1395 goto write_err_out;
10bea152 1396
dc49cd76 1397 munmap(contents, contents_sz);
10bea152
JS
1398 }
1399
4ed7cd3a 1400 if (commit_lock_file(lock) < 0) {
64c0d71c 1401 error("could not commit config file %s", config_filename);
7a397419 1402 ret = CONFIG_NO_WRITE;
dafc88b1 1403 goto out_free;
10bea152
JS
1404 }
1405
6cbf973c
BS
1406 /*
1407 * lock is committed, so don't try to roll it back below.
1408 * NOTE: Since lockfile.c keeps a linked list of all created
1409 * lock_file structures, it isn't safe to free(lock). It's
1410 * better to just leave it hanging around.
1411 */
1412 lock = NULL;
dafc88b1
SH
1413 ret = 0;
1414
1415out_free:
6cbf973c
BS
1416 if (lock)
1417 rollback_lock_file(lock);
dafc88b1 1418 return ret;
93c1e079
JH
1419
1420write_err_out:
64c0d71c 1421 ret = write_error(lock->filename);
93c1e079
JH
1422 goto out_free;
1423
10bea152
JS
1424}
1425
5ec31182
RR
1426int git_config_set_multivar(const char *key, const char *value,
1427 const char *value_regex, int multi_replace)
1428{
1429 const char *config_filename;
1430 char *buf = NULL;
1431 int ret;
1432
1433 if (config_exclusive_filename)
1434 config_filename = config_exclusive_filename;
1435 else
1436 config_filename = buf = git_pathdup("config");
1437
1438 ret = git_config_set_multivar_in_file(config_filename, key, value,
1439 value_regex, multi_replace);
1440 free(buf);
1441 return ret;
1442}
1443
118f8b24
PB
1444static int section_name_match (const char *buf, const char *name)
1445{
1446 int i = 0, j = 0, dot = 0;
a4c0d463
AV
1447 if (buf[i] != '[')
1448 return 0;
1449 for (i = 1; buf[i] && buf[i] != ']'; i++) {
118f8b24
PB
1450 if (!dot && isspace(buf[i])) {
1451 dot = 1;
1452 if (name[j++] != '.')
1453 break;
1454 for (i++; isspace(buf[i]); i++)
1455 ; /* do nothing */
1456 if (buf[i] != '"')
1457 break;
1458 continue;
1459 }
1460 if (buf[i] == '\\' && dot)
1461 i++;
1462 else if (buf[i] == '"' && dot) {
1463 for (i++; isspace(buf[i]); i++)
1464 ; /* do_nothing */
1465 break;
1466 }
1467 if (buf[i] != name[j++])
1468 break;
1469 }
a4c0d463
AV
1470 if (buf[i] == ']' && name[j] == 0) {
1471 /*
1472 * We match, now just find the right length offset by
1473 * gobbling up any whitespace after it, as well
1474 */
1475 i++;
1476 for (; buf[i] && isspace(buf[i]); i++)
1477 ; /* do nothing */
1478 return i;
1479 }
1480 return 0;
118f8b24
PB
1481}
1482
1483/* if new_name == NULL, the section is removed instead */
0667fcfb
JS
1484int git_config_rename_section(const char *old_name, const char *new_name)
1485{
118f8b24 1486 int ret = 0, remove = 0;
fc1905bb 1487 char *config_filename;
0667fcfb
JS
1488 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
1489 int out_fd;
1490 char buf[1024];
924aaf3e 1491 FILE *config_file;
0667fcfb 1492
dc871831
DB
1493 if (config_exclusive_filename)
1494 config_filename = xstrdup(config_exclusive_filename);
1495 else
a4f34cbb 1496 config_filename = git_pathdup("config");
0667fcfb 1497 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
fc1905bb 1498 if (out_fd < 0) {
64c0d71c 1499 ret = error("could not lock config file %s", config_filename);
fc1905bb
JH
1500 goto out;
1501 }
0667fcfb 1502
fc1905bb 1503 if (!(config_file = fopen(config_filename, "rb"))) {
01ebb9dc
GB
1504 /* no config file means nothing to rename, no error */
1505 goto unlock_and_out;
fc1905bb 1506 }
0667fcfb
JS
1507
1508 while (fgets(buf, sizeof(buf), config_file)) {
1509 int i;
480c9e52 1510 int length;
9a5abfc7 1511 char *output = buf;
0667fcfb
JS
1512 for (i = 0; buf[i] && isspace(buf[i]); i++)
1513 ; /* do nothing */
1514 if (buf[i] == '[') {
1515 /* it's a section */
a4c0d463
AV
1516 int offset = section_name_match(&buf[i], old_name);
1517 if (offset > 0) {
118f8b24
PB
1518 ret++;
1519 if (new_name == NULL) {
1520 remove = 1;
0667fcfb
JS
1521 continue;
1522 }
0667fcfb 1523 store.baselen = strlen(new_name);
480c9e52 1524 if (!store_write_section(out_fd, new_name)) {
64c0d71c 1525 ret = write_error(lock->filename);
480c9e52
AW
1526 goto out;
1527 }
9a5abfc7
AV
1528 /*
1529 * We wrote out the new section, with
1530 * a newline, now skip the old
1531 * section's length
1532 */
1533 output += offset + i;
1534 if (strlen(output) > 0) {
1535 /*
1536 * More content means there's
1537 * a declaration to put on the
1538 * next line; indent with a
1539 * tab
1540 */
1541 output -= 1;
1542 output[0] = '\t';
1543 }
0667fcfb 1544 }
118f8b24 1545 remove = 0;
0667fcfb 1546 }
118f8b24
PB
1547 if (remove)
1548 continue;
9a5abfc7
AV
1549 length = strlen(output);
1550 if (write_in_full(out_fd, output, length) != length) {
64c0d71c 1551 ret = write_error(lock->filename);
480c9e52
AW
1552 goto out;
1553 }
0667fcfb 1554 }
fc1905bb 1555 fclose(config_file);
8b590075 1556unlock_and_out:
4ed7cd3a 1557 if (commit_lock_file(lock) < 0)
64c0d71c 1558 ret = error("could not commit config file %s", config_filename);
8b590075 1559out:
fc1905bb 1560 free(config_filename);
0667fcfb
JS
1561 return ret;
1562}
40ea4ed9
JH
1563
1564/*
1565 * Call this to report error for your variable that should not
1566 * get a boolean value (i.e. "[my] var" means "true").
1567 */
1568int config_error_nonbool(const char *var)
1569{
1570 return error("Missing value for '%s'", var);
1571}