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