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