Custom compression levels for objects and packs
[git/git.git] / config.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
6 *
7 */
8 #include "cache.h"
9
10 #define MAXNAME (256)
11
12 static FILE *config_file;
13 static const char *config_file_name;
14 static int config_linenr;
15 static int zlib_compression_seen;
16
17 static int get_next_char(void)
18 {
19 int c;
20 FILE *f;
21
22 c = '\n';
23 if ((f = config_file) != NULL) {
24 c = fgetc(f);
25 if (c == '\r') {
26 /* DOS like systems */
27 c = fgetc(f);
28 if (c != '\n') {
29 ungetc(c, f);
30 c = '\r';
31 }
32 }
33 if (c == '\n')
34 config_linenr++;
35 if (c == EOF) {
36 config_file = NULL;
37 c = '\n';
38 }
39 }
40 return c;
41 }
42
43 static char *parse_value(void)
44 {
45 static char value[1024];
46 int quote = 0, comment = 0, len = 0, space = 0;
47
48 for (;;) {
49 int c = get_next_char();
50 if (len >= sizeof(value))
51 return NULL;
52 if (c == '\n') {
53 if (quote)
54 return NULL;
55 value[len] = 0;
56 return value;
57 }
58 if (comment)
59 continue;
60 if (isspace(c) && !quote) {
61 space = 1;
62 continue;
63 }
64 if (!quote) {
65 if (c == ';' || c == '#') {
66 comment = 1;
67 continue;
68 }
69 }
70 if (space) {
71 if (len)
72 value[len++] = ' ';
73 space = 0;
74 }
75 if (c == '\\') {
76 c = get_next_char();
77 switch (c) {
78 case '\n':
79 continue;
80 case 't':
81 c = '\t';
82 break;
83 case 'b':
84 c = '\b';
85 break;
86 case 'n':
87 c = '\n';
88 break;
89 /* Some characters escape as themselves */
90 case '\\': case '"':
91 break;
92 /* Reject unknown escape sequences */
93 default:
94 return NULL;
95 }
96 value[len++] = c;
97 continue;
98 }
99 if (c == '"') {
100 quote = 1-quote;
101 continue;
102 }
103 value[len++] = c;
104 }
105 }
106
107 static inline int iskeychar(int c)
108 {
109 return isalnum(c) || c == '-';
110 }
111
112 static int get_value(config_fn_t fn, char *name, unsigned int len)
113 {
114 int c;
115 char *value;
116
117 /* Get the full name */
118 for (;;) {
119 c = get_next_char();
120 if (c == EOF)
121 break;
122 if (!iskeychar(c))
123 break;
124 name[len++] = tolower(c);
125 if (len >= MAXNAME)
126 return -1;
127 }
128 name[len] = 0;
129 while (c == ' ' || c == '\t')
130 c = get_next_char();
131
132 value = NULL;
133 if (c != '\n') {
134 if (c != '=')
135 return -1;
136 value = parse_value();
137 if (!value)
138 return -1;
139 }
140 return fn(name, value);
141 }
142
143 static int get_extended_base_var(char *name, int baselen, int c)
144 {
145 do {
146 if (c == '\n')
147 return -1;
148 c = get_next_char();
149 } while (isspace(c));
150
151 /* We require the format to be '[base "extension"]' */
152 if (c != '"')
153 return -1;
154 name[baselen++] = '.';
155
156 for (;;) {
157 int c = get_next_char();
158 if (c == '\n')
159 return -1;
160 if (c == '"')
161 break;
162 if (c == '\\') {
163 c = get_next_char();
164 if (c == '\n')
165 return -1;
166 }
167 name[baselen++] = c;
168 if (baselen > MAXNAME / 2)
169 return -1;
170 }
171
172 /* Final ']' */
173 if (get_next_char() != ']')
174 return -1;
175 return baselen;
176 }
177
178 static int get_base_var(char *name)
179 {
180 int baselen = 0;
181
182 for (;;) {
183 int c = get_next_char();
184 if (c == EOF)
185 return -1;
186 if (c == ']')
187 return baselen;
188 if (isspace(c))
189 return get_extended_base_var(name, baselen, c);
190 if (!iskeychar(c) && c != '.')
191 return -1;
192 if (baselen > MAXNAME / 2)
193 return -1;
194 name[baselen++] = tolower(c);
195 }
196 }
197
198 static int git_parse_file(config_fn_t fn)
199 {
200 int comment = 0;
201 int baselen = 0;
202 static char var[MAXNAME];
203
204 for (;;) {
205 int c = get_next_char();
206 if (c == '\n') {
207 /* EOF? */
208 if (!config_file)
209 return 0;
210 comment = 0;
211 continue;
212 }
213 if (comment || isspace(c))
214 continue;
215 if (c == '#' || c == ';') {
216 comment = 1;
217 continue;
218 }
219 if (c == '[') {
220 baselen = get_base_var(var);
221 if (baselen <= 0)
222 break;
223 var[baselen++] = '.';
224 var[baselen] = 0;
225 continue;
226 }
227 if (!isalpha(c))
228 break;
229 var[baselen] = tolower(c);
230 if (get_value(fn, var, baselen+1) < 0)
231 break;
232 }
233 die("bad config file line %d in %s", config_linenr, config_file_name);
234 }
235
236 int git_config_int(const char *name, const char *value)
237 {
238 if (value && *value) {
239 char *end;
240 int val = strtol(value, &end, 0);
241 if (!*end)
242 return val;
243 if (!strcasecmp(end, "k"))
244 return val * 1024;
245 if (!strcasecmp(end, "m"))
246 return val * 1024 * 1024;
247 if (!strcasecmp(end, "g"))
248 return val * 1024 * 1024 * 1024;
249 }
250 die("bad config value for '%s' in %s", name, config_file_name);
251 }
252
253 int git_config_bool(const char *name, const char *value)
254 {
255 if (!value)
256 return 1;
257 if (!*value)
258 return 0;
259 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
260 return 1;
261 if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
262 return 0;
263 return git_config_int(name, value) != 0;
264 }
265
266 int git_default_config(const char *var, const char *value)
267 {
268 /* This needs a better name */
269 if (!strcmp(var, "core.filemode")) {
270 trust_executable_bit = git_config_bool(var, value);
271 return 0;
272 }
273
274 if (!strcmp(var, "core.symlinks")) {
275 has_symlinks = git_config_bool(var, value);
276 return 0;
277 }
278
279 if (!strcmp(var, "core.bare")) {
280 is_bare_repository_cfg = git_config_bool(var, value);
281 return 0;
282 }
283
284 if (!strcmp(var, "core.ignorestat")) {
285 assume_unchanged = git_config_bool(var, value);
286 return 0;
287 }
288
289 if (!strcmp(var, "core.prefersymlinkrefs")) {
290 prefer_symlink_refs = git_config_bool(var, value);
291 return 0;
292 }
293
294 if (!strcmp(var, "core.logallrefupdates")) {
295 log_all_ref_updates = git_config_bool(var, value);
296 return 0;
297 }
298
299 if (!strcmp(var, "core.warnambiguousrefs")) {
300 warn_ambiguous_refs = git_config_bool(var, value);
301 return 0;
302 }
303
304 if (!strcmp(var, "core.legacyheaders")) {
305 use_legacy_headers = git_config_bool(var, value);
306 return 0;
307 }
308
309 if (!strcmp(var, "core.loosecompression")) {
310 int level = git_config_int(var, value);
311 if (level == -1)
312 level = Z_DEFAULT_COMPRESSION;
313 else if (level < 0 || level > Z_BEST_COMPRESSION)
314 die("bad zlib compression level %d", level);
315 zlib_compression_level = level;
316 zlib_compression_seen = 1;
317 return 0;
318 }
319
320 if (!strcmp(var, "core.compression")) {
321 int level = git_config_int(var, value);
322 if (level == -1)
323 level = Z_DEFAULT_COMPRESSION;
324 else if (level < 0 || level > Z_BEST_COMPRESSION)
325 die("bad zlib compression level %d", level);
326 core_compression_level = level;
327 core_compression_seen = 1;
328 if (!zlib_compression_seen)
329 zlib_compression_level = level;
330 return 0;
331 }
332
333 if (!strcmp(var, "core.packedgitwindowsize")) {
334 int pgsz_x2 = getpagesize() * 2;
335 packed_git_window_size = git_config_int(var, value);
336
337 /* This value must be multiple of (pagesize * 2) */
338 packed_git_window_size /= pgsz_x2;
339 if (packed_git_window_size < 1)
340 packed_git_window_size = 1;
341 packed_git_window_size *= pgsz_x2;
342 return 0;
343 }
344
345 if (!strcmp(var, "core.packedgitlimit")) {
346 packed_git_limit = git_config_int(var, value);
347 return 0;
348 }
349
350 if (!strcmp(var, "core.deltabasecachelimit")) {
351 delta_base_cache_limit = git_config_int(var, value);
352 return 0;
353 }
354
355 if (!strcmp(var, "core.autocrlf")) {
356 if (value && !strcasecmp(value, "input")) {
357 auto_crlf = -1;
358 return 0;
359 }
360 auto_crlf = git_config_bool(var, value);
361 return 0;
362 }
363
364 if (!strcmp(var, "user.name")) {
365 strlcpy(git_default_name, value, sizeof(git_default_name));
366 return 0;
367 }
368
369 if (!strcmp(var, "user.email")) {
370 strlcpy(git_default_email, value, sizeof(git_default_email));
371 return 0;
372 }
373
374 if (!strcmp(var, "i18n.commitencoding")) {
375 git_commit_encoding = xstrdup(value);
376 return 0;
377 }
378
379 if (!strcmp(var, "i18n.logoutputencoding")) {
380 git_log_output_encoding = xstrdup(value);
381 return 0;
382 }
383
384
385 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
386 pager_use_color = git_config_bool(var,value);
387 return 0;
388 }
389
390 /* Add other config variables here and to Documentation/config.txt. */
391 return 0;
392 }
393
394 int git_config_from_file(config_fn_t fn, const char *filename)
395 {
396 int ret;
397 FILE *f = fopen(filename, "r");
398
399 ret = -1;
400 if (f) {
401 config_file = f;
402 config_file_name = filename;
403 config_linenr = 1;
404 ret = git_parse_file(fn);
405 fclose(f);
406 config_file_name = NULL;
407 }
408 return ret;
409 }
410
411 int git_config(config_fn_t fn)
412 {
413 int ret = 0;
414 char *repo_config = NULL;
415 const char *home = NULL, *filename;
416
417 /* $GIT_CONFIG makes git read _only_ the given config file,
418 * $GIT_CONFIG_LOCAL will make it process it in addition to the
419 * global config file, the same way it would the per-repository
420 * config file otherwise. */
421 filename = getenv(CONFIG_ENVIRONMENT);
422 if (!filename) {
423 if (!access(ETC_GITCONFIG, R_OK))
424 ret += git_config_from_file(fn, ETC_GITCONFIG);
425 home = getenv("HOME");
426 filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
427 if (!filename)
428 filename = repo_config = xstrdup(git_path("config"));
429 }
430
431 if (home) {
432 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
433 if (!access(user_config, R_OK))
434 ret = git_config_from_file(fn, user_config);
435 free(user_config);
436 }
437
438 ret += git_config_from_file(fn, filename);
439 free(repo_config);
440 return ret;
441 }
442
443 /*
444 * Find all the stuff for git_config_set() below.
445 */
446
447 #define MAX_MATCHES 512
448
449 static struct {
450 int baselen;
451 char* key;
452 int do_not_match;
453 regex_t* value_regex;
454 int multi_replace;
455 size_t offset[MAX_MATCHES];
456 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
457 int seen;
458 } store;
459
460 static int matches(const char* key, const char* value)
461 {
462 return !strcmp(key, store.key) &&
463 (store.value_regex == NULL ||
464 (store.do_not_match ^
465 !regexec(store.value_regex, value, 0, NULL, 0)));
466 }
467
468 static int store_aux(const char* key, const char* value)
469 {
470 switch (store.state) {
471 case KEY_SEEN:
472 if (matches(key, value)) {
473 if (store.seen == 1 && store.multi_replace == 0) {
474 fprintf(stderr,
475 "Warning: %s has multiple values\n",
476 key);
477 } else if (store.seen >= MAX_MATCHES) {
478 fprintf(stderr, "Too many matches\n");
479 return 1;
480 }
481
482 store.offset[store.seen] = ftell(config_file);
483 store.seen++;
484 }
485 break;
486 case SECTION_SEEN:
487 if (strncmp(key, store.key, store.baselen+1)) {
488 store.state = SECTION_END_SEEN;
489 break;
490 } else
491 /* do not increment matches: this is no match */
492 store.offset[store.seen] = ftell(config_file);
493 /* fallthru */
494 case SECTION_END_SEEN:
495 case START:
496 if (matches(key, value)) {
497 store.offset[store.seen] = ftell(config_file);
498 store.state = KEY_SEEN;
499 store.seen++;
500 } else {
501 if (strrchr(key, '.') - key == store.baselen &&
502 !strncmp(key, store.key, store.baselen)) {
503 store.state = SECTION_SEEN;
504 store.offset[store.seen] = ftell(config_file);
505 }
506 }
507 }
508 return 0;
509 }
510
511 static int write_error()
512 {
513 fprintf(stderr, "Failed to write new configuration file\n");
514
515 /* Same error code as "failed to rename". */
516 return 4;
517 }
518
519 static int store_write_section(int fd, const char* key)
520 {
521 const char *dot = strchr(key, '.');
522 int len1 = store.baselen, len2 = -1;
523
524 dot = strchr(key, '.');
525 if (dot) {
526 int dotlen = dot - key;
527 if (dotlen < len1) {
528 len2 = len1 - dotlen - 1;
529 len1 = dotlen;
530 }
531 }
532
533 if (write_in_full(fd, "[", 1) != 1 ||
534 write_in_full(fd, key, len1) != len1)
535 return 0;
536 if (len2 >= 0) {
537 if (write_in_full(fd, " \"", 2) != 2)
538 return 0;
539 while (--len2 >= 0) {
540 unsigned char c = *++dot;
541 if (c == '"')
542 if (write_in_full(fd, "\\", 1) != 1)
543 return 0;
544 if (write_in_full(fd, &c, 1) != 1)
545 return 0;
546 }
547 if (write_in_full(fd, "\"", 1) != 1)
548 return 0;
549 }
550 if (write_in_full(fd, "]\n", 2) != 2)
551 return 0;
552
553 return 1;
554 }
555
556 static int store_write_pair(int fd, const char* key, const char* value)
557 {
558 int i;
559 int length = strlen(key+store.baselen+1);
560 int quote = 0;
561
562 /* Check to see if the value needs to be quoted. */
563 if (value[0] == ' ')
564 quote = 1;
565 for (i = 0; value[i]; i++)
566 if (value[i] == ';' || value[i] == '#')
567 quote = 1;
568 if (value[i-1] == ' ')
569 quote = 1;
570
571 if (write_in_full(fd, "\t", 1) != 1 ||
572 write_in_full(fd, key+store.baselen+1, length) != length ||
573 write_in_full(fd, " = ", 3) != 3)
574 return 0;
575 if (quote && write_in_full(fd, "\"", 1) != 1)
576 return 0;
577 for (i = 0; value[i]; i++)
578 switch (value[i]) {
579 case '\n':
580 if (write_in_full(fd, "\\n", 2) != 2)
581 return 0;
582 break;
583 case '\t':
584 if (write_in_full(fd, "\\t", 2) != 2)
585 return 0;
586 break;
587 case '"':
588 case '\\':
589 if (write_in_full(fd, "\\", 1) != 1)
590 return 0;
591 default:
592 if (write_in_full(fd, value+i, 1) != 1)
593 return 0;
594 break;
595 }
596 if (quote && write_in_full(fd, "\"", 1) != 1)
597 return 0;
598 if (write_in_full(fd, "\n", 1) != 1)
599 return 0;
600 return 1;
601 }
602
603 static ssize_t find_beginning_of_line(const char* contents, size_t size,
604 size_t offset_, int* found_bracket)
605 {
606 size_t equal_offset = size, bracket_offset = size;
607 ssize_t offset;
608
609 for (offset = offset_-2; offset > 0
610 && contents[offset] != '\n'; offset--)
611 switch (contents[offset]) {
612 case '=': equal_offset = offset; break;
613 case ']': bracket_offset = offset; break;
614 }
615 if (bracket_offset < equal_offset) {
616 *found_bracket = 1;
617 offset = bracket_offset+1;
618 } else
619 offset++;
620
621 return offset;
622 }
623
624 int git_config_set(const char* key, const char* value)
625 {
626 return git_config_set_multivar(key, value, NULL, 0);
627 }
628
629 /*
630 * If value==NULL, unset in (remove from) config,
631 * if value_regex!=NULL, disregard key/value pairs where value does not match.
632 * if multi_replace==0, nothing, or only one matching key/value is replaced,
633 * else all matching key/values (regardless how many) are removed,
634 * before the new pair is written.
635 *
636 * Returns 0 on success.
637 *
638 * This function does this:
639 *
640 * - it locks the config file by creating ".git/config.lock"
641 *
642 * - it then parses the config using store_aux() as validator to find
643 * the position on the key/value pair to replace. If it is to be unset,
644 * it must be found exactly once.
645 *
646 * - the config file is mmap()ed and the part before the match (if any) is
647 * written to the lock file, then the changed part and the rest.
648 *
649 * - the config file is removed and the lock file rename()d to it.
650 *
651 */
652 int git_config_set_multivar(const char* key, const char* value,
653 const char* value_regex, int multi_replace)
654 {
655 int i, dot;
656 int fd = -1, in_fd;
657 int ret;
658 char* config_filename;
659 char* lock_file;
660 const char* last_dot = strrchr(key, '.');
661
662 config_filename = getenv(CONFIG_ENVIRONMENT);
663 if (!config_filename) {
664 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
665 if (!config_filename)
666 config_filename = git_path("config");
667 }
668 config_filename = xstrdup(config_filename);
669 lock_file = xstrdup(mkpath("%s.lock", config_filename));
670
671 /*
672 * Since "key" actually contains the section name and the real
673 * key name separated by a dot, we have to know where the dot is.
674 */
675
676 if (last_dot == NULL) {
677 fprintf(stderr, "key does not contain a section: %s\n", key);
678 ret = 2;
679 goto out_free;
680 }
681 store.baselen = last_dot - key;
682
683 store.multi_replace = multi_replace;
684
685 /*
686 * Validate the key and while at it, lower case it for matching.
687 */
688 store.key = xmalloc(strlen(key) + 1);
689 dot = 0;
690 for (i = 0; key[i]; i++) {
691 unsigned char c = key[i];
692 if (c == '.')
693 dot = 1;
694 /* Leave the extended basename untouched.. */
695 if (!dot || i > store.baselen) {
696 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
697 fprintf(stderr, "invalid key: %s\n", key);
698 free(store.key);
699 ret = 1;
700 goto out_free;
701 }
702 c = tolower(c);
703 } else if (c == '\n') {
704 fprintf(stderr, "invalid key (newline): %s\n", key);
705 free(store.key);
706 ret = 1;
707 goto out_free;
708 }
709 store.key[i] = c;
710 }
711 store.key[i] = 0;
712
713 /*
714 * The lock_file serves a purpose in addition to locking: the new
715 * contents of .git/config will be written into it.
716 */
717 fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
718 if (fd < 0 || adjust_shared_perm(lock_file)) {
719 fprintf(stderr, "could not lock config file\n");
720 free(store.key);
721 ret = -1;
722 goto out_free;
723 }
724
725 /*
726 * If .git/config does not exist yet, write a minimal version.
727 */
728 in_fd = open(config_filename, O_RDONLY);
729 if ( in_fd < 0 ) {
730 free(store.key);
731
732 if ( ENOENT != errno ) {
733 error("opening %s: %s", config_filename,
734 strerror(errno));
735 ret = 3; /* same as "invalid config file" */
736 goto out_free;
737 }
738 /* if nothing to unset, error out */
739 if (value == NULL) {
740 ret = 5;
741 goto out_free;
742 }
743
744 store.key = (char*)key;
745 if (!store_write_section(fd, key) ||
746 !store_write_pair(fd, key, value))
747 goto write_err_out;
748 } else {
749 struct stat st;
750 char* contents;
751 size_t contents_sz, copy_begin, copy_end;
752 int i, new_line = 0;
753
754 if (value_regex == NULL)
755 store.value_regex = NULL;
756 else {
757 if (value_regex[0] == '!') {
758 store.do_not_match = 1;
759 value_regex++;
760 } else
761 store.do_not_match = 0;
762
763 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
764 if (regcomp(store.value_regex, value_regex,
765 REG_EXTENDED)) {
766 fprintf(stderr, "Invalid pattern: %s\n",
767 value_regex);
768 free(store.value_regex);
769 ret = 6;
770 goto out_free;
771 }
772 }
773
774 store.offset[0] = 0;
775 store.state = START;
776 store.seen = 0;
777
778 /*
779 * After this, store.offset will contain the *end* offset
780 * of the last match, or remain at 0 if no match was found.
781 * As a side effect, we make sure to transform only a valid
782 * existing config file.
783 */
784 if (git_config_from_file(store_aux, config_filename)) {
785 fprintf(stderr, "invalid config file\n");
786 free(store.key);
787 if (store.value_regex != NULL) {
788 regfree(store.value_regex);
789 free(store.value_regex);
790 }
791 ret = 3;
792 goto out_free;
793 }
794
795 free(store.key);
796 if (store.value_regex != NULL) {
797 regfree(store.value_regex);
798 free(store.value_regex);
799 }
800
801 /* if nothing to unset, or too many matches, error out */
802 if ((store.seen == 0 && value == NULL) ||
803 (store.seen > 1 && multi_replace == 0)) {
804 ret = 5;
805 goto out_free;
806 }
807
808 fstat(in_fd, &st);
809 contents_sz = xsize_t(st.st_size);
810 contents = xmmap(NULL, contents_sz, PROT_READ,
811 MAP_PRIVATE, in_fd, 0);
812 close(in_fd);
813
814 if (store.seen == 0)
815 store.seen = 1;
816
817 for (i = 0, copy_begin = 0; i < store.seen; i++) {
818 if (store.offset[i] == 0) {
819 store.offset[i] = copy_end = contents_sz;
820 } else if (store.state != KEY_SEEN) {
821 copy_end = store.offset[i];
822 } else
823 copy_end = find_beginning_of_line(
824 contents, contents_sz,
825 store.offset[i]-2, &new_line);
826
827 /* write the first part of the config */
828 if (copy_end > copy_begin) {
829 if (write_in_full(fd, contents + copy_begin,
830 copy_end - copy_begin) <
831 copy_end - copy_begin)
832 goto write_err_out;
833 if (new_line &&
834 write_in_full(fd, "\n", 1) != 1)
835 goto write_err_out;
836 }
837 copy_begin = store.offset[i];
838 }
839
840 /* write the pair (value == NULL means unset) */
841 if (value != NULL) {
842 if (store.state == START) {
843 if (!store_write_section(fd, key))
844 goto write_err_out;
845 }
846 if (!store_write_pair(fd, key, value))
847 goto write_err_out;
848 }
849
850 /* write the rest of the config */
851 if (copy_begin < contents_sz)
852 if (write_in_full(fd, contents + copy_begin,
853 contents_sz - copy_begin) <
854 contents_sz - copy_begin)
855 goto write_err_out;
856
857 munmap(contents, contents_sz);
858 unlink(config_filename);
859 }
860
861 if (rename(lock_file, config_filename) < 0) {
862 fprintf(stderr, "Could not rename the lock file?\n");
863 ret = 4;
864 goto out_free;
865 }
866
867 ret = 0;
868
869 out_free:
870 if (0 <= fd)
871 close(fd);
872 free(config_filename);
873 if (lock_file) {
874 unlink(lock_file);
875 free(lock_file);
876 }
877 return ret;
878
879 write_err_out:
880 ret = write_error();
881 goto out_free;
882
883 }
884
885 static int section_name_match (const char *buf, const char *name)
886 {
887 int i = 0, j = 0, dot = 0;
888 for (; buf[i] && buf[i] != ']'; i++) {
889 if (!dot && isspace(buf[i])) {
890 dot = 1;
891 if (name[j++] != '.')
892 break;
893 for (i++; isspace(buf[i]); i++)
894 ; /* do nothing */
895 if (buf[i] != '"')
896 break;
897 continue;
898 }
899 if (buf[i] == '\\' && dot)
900 i++;
901 else if (buf[i] == '"' && dot) {
902 for (i++; isspace(buf[i]); i++)
903 ; /* do_nothing */
904 break;
905 }
906 if (buf[i] != name[j++])
907 break;
908 }
909 return (buf[i] == ']' && name[j] == 0);
910 }
911
912 /* if new_name == NULL, the section is removed instead */
913 int git_config_rename_section(const char *old_name, const char *new_name)
914 {
915 int ret = 0, remove = 0;
916 char *config_filename;
917 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
918 int out_fd;
919 char buf[1024];
920
921 config_filename = getenv(CONFIG_ENVIRONMENT);
922 if (!config_filename) {
923 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
924 if (!config_filename)
925 config_filename = git_path("config");
926 }
927 config_filename = xstrdup(config_filename);
928 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
929 if (out_fd < 0) {
930 ret = error("Could not lock config file!");
931 goto out;
932 }
933
934 if (!(config_file = fopen(config_filename, "rb"))) {
935 /* no config file means nothing to rename, no error */
936 goto unlock_and_out;
937 }
938
939 while (fgets(buf, sizeof(buf), config_file)) {
940 int i;
941 int length;
942 for (i = 0; buf[i] && isspace(buf[i]); i++)
943 ; /* do nothing */
944 if (buf[i] == '[') {
945 /* it's a section */
946 if (section_name_match (&buf[i+1], old_name)) {
947 ret++;
948 if (new_name == NULL) {
949 remove = 1;
950 continue;
951 }
952 store.baselen = strlen(new_name);
953 if (!store_write_section(out_fd, new_name)) {
954 ret = write_error();
955 goto out;
956 }
957 continue;
958 }
959 remove = 0;
960 }
961 if (remove)
962 continue;
963 length = strlen(buf);
964 if (write_in_full(out_fd, buf, length) != length) {
965 ret = write_error();
966 goto out;
967 }
968 }
969 fclose(config_file);
970 unlock_and_out:
971 if (close(out_fd) || commit_lock_file(lock) < 0)
972 ret = error("Cannot commit config file!");
973 out:
974 free(config_filename);
975 return ret;
976 }
977