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