Fix install-doc-quick target
[git/mergetool.git] / config.c
blobdd2de6e38e8d68d8622ad542e5c73cbcffeceeac
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
7 */
8 #include "cache.h"
10 #define MAXNAME (256)
12 static FILE *config_file;
13 static const char *config_file_name;
14 static int config_linenr;
15 static int zlib_compression_seen;
17 static int get_next_char(void)
19 int c;
20 FILE *f;
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';
33 if (c == '\n')
34 config_linenr++;
35 if (c == EOF) {
36 config_file = NULL;
37 c = '\n';
40 return c;
43 static char *parse_value(void)
45 static char value[1024];
46 int quote = 0, comment = 0, len = 0, space = 0;
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;
58 if (comment)
59 continue;
60 if (isspace(c) && !quote) {
61 space = 1;
62 continue;
64 if (!quote) {
65 if (c == ';' || c == '#') {
66 comment = 1;
67 continue;
70 if (space) {
71 if (len)
72 value[len++] = ' ';
73 space = 0;
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;
96 value[len++] = c;
97 continue;
99 if (c == '"') {
100 quote = 1-quote;
101 continue;
103 value[len++] = c;
107 static inline int iskeychar(int c)
109 return isalnum(c) || c == '-';
112 static int get_value(config_fn_t fn, char *name, unsigned int len)
114 int c;
115 char *value;
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;
128 name[len] = 0;
129 while (c == ' ' || c == '\t')
130 c = get_next_char();
132 value = NULL;
133 if (c != '\n') {
134 if (c != '=')
135 return -1;
136 value = parse_value();
137 if (!value)
138 return -1;
140 return fn(name, value);
143 static int get_extended_base_var(char *name, int baselen, int c)
145 do {
146 if (c == '\n')
147 return -1;
148 c = get_next_char();
149 } while (isspace(c));
151 /* We require the format to be '[base "extension"]' */
152 if (c != '"')
153 return -1;
154 name[baselen++] = '.';
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;
167 name[baselen++] = c;
168 if (baselen > MAXNAME / 2)
169 return -1;
172 /* Final ']' */
173 if (get_next_char() != ']')
174 return -1;
175 return baselen;
178 static int get_base_var(char *name)
180 int baselen = 0;
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);
198 static int git_parse_file(config_fn_t fn)
200 int comment = 0;
201 int baselen = 0;
202 static char var[MAXNAME];
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;
213 if (comment || isspace(c))
214 continue;
215 if (c == '#' || c == ';') {
216 comment = 1;
217 continue;
219 if (c == '[') {
220 baselen = get_base_var(var);
221 if (baselen <= 0)
222 break;
223 var[baselen++] = '.';
224 var[baselen] = 0;
225 continue;
227 if (!isalpha(c))
228 break;
229 var[baselen] = tolower(c);
230 if (get_value(fn, var, baselen+1) < 0)
231 break;
233 die("bad config file line %d in %s", config_linenr, config_file_name);
236 static unsigned long get_unit_factor(const char *end)
238 if (!*end)
239 return 1;
240 else if (!strcasecmp(end, "k"))
241 return 1024;
242 else if (!strcasecmp(end, "m"))
243 return 1024 * 1024;
244 else if (!strcasecmp(end, "g"))
245 return 1024 * 1024 * 1024;
246 die("unknown unit: '%s'", end);
249 int git_parse_long(const char *value, long *ret)
251 if (value && *value) {
252 char *end;
253 long val = strtol(value, &end, 0);
254 *ret = val * get_unit_factor(end);
255 return 1;
257 return 0;
260 int git_parse_ulong(const char *value, unsigned long *ret)
262 if (value && *value) {
263 char *end;
264 unsigned long val = strtoul(value, &end, 0);
265 *ret = val * get_unit_factor(end);
266 return 1;
268 return 0;
271 int git_config_int(const char *name, const char *value)
273 long ret;
274 if (!git_parse_long(value, &ret))
275 die("bad config value for '%s' in %s", name, config_file_name);
276 return ret;
279 unsigned long git_config_ulong(const char *name, const char *value)
281 unsigned long ret;
282 if (!git_parse_ulong(value, &ret))
283 die("bad config value for '%s' in %s", name, config_file_name);
284 return ret;
287 int git_config_bool(const char *name, const char *value)
289 if (!value)
290 return 1;
291 if (!*value)
292 return 0;
293 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
294 return 1;
295 if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
296 return 0;
297 return git_config_int(name, value) != 0;
300 int git_default_config(const char *var, const char *value)
302 /* This needs a better name */
303 if (!strcmp(var, "core.filemode")) {
304 trust_executable_bit = git_config_bool(var, value);
305 return 0;
308 if (!strcmp(var, "core.quotepath")) {
309 quote_path_fully = git_config_bool(var, value);
310 return 0;
313 if (!strcmp(var, "core.symlinks")) {
314 has_symlinks = git_config_bool(var, value);
315 return 0;
318 if (!strcmp(var, "core.bare")) {
319 is_bare_repository_cfg = git_config_bool(var, value);
320 return 0;
323 if (!strcmp(var, "core.ignorestat")) {
324 assume_unchanged = git_config_bool(var, value);
325 return 0;
328 if (!strcmp(var, "core.prefersymlinkrefs")) {
329 prefer_symlink_refs = git_config_bool(var, value);
330 return 0;
333 if (!strcmp(var, "core.logallrefupdates")) {
334 log_all_ref_updates = git_config_bool(var, value);
335 return 0;
338 if (!strcmp(var, "core.warnambiguousrefs")) {
339 warn_ambiguous_refs = git_config_bool(var, value);
340 return 0;
343 if (!strcmp(var, "core.loosecompression")) {
344 int level = git_config_int(var, value);
345 if (level == -1)
346 level = Z_DEFAULT_COMPRESSION;
347 else if (level < 0 || level > Z_BEST_COMPRESSION)
348 die("bad zlib compression level %d", level);
349 zlib_compression_level = level;
350 zlib_compression_seen = 1;
351 return 0;
354 if (!strcmp(var, "core.compression")) {
355 int level = git_config_int(var, value);
356 if (level == -1)
357 level = Z_DEFAULT_COMPRESSION;
358 else if (level < 0 || level > Z_BEST_COMPRESSION)
359 die("bad zlib compression level %d", level);
360 core_compression_level = level;
361 core_compression_seen = 1;
362 if (!zlib_compression_seen)
363 zlib_compression_level = level;
364 return 0;
367 if (!strcmp(var, "core.packedgitwindowsize")) {
368 int pgsz_x2 = getpagesize() * 2;
369 packed_git_window_size = git_config_int(var, value);
371 /* This value must be multiple of (pagesize * 2) */
372 packed_git_window_size /= pgsz_x2;
373 if (packed_git_window_size < 1)
374 packed_git_window_size = 1;
375 packed_git_window_size *= pgsz_x2;
376 return 0;
379 if (!strcmp(var, "core.packedgitlimit")) {
380 packed_git_limit = git_config_int(var, value);
381 return 0;
384 if (!strcmp(var, "core.deltabasecachelimit")) {
385 delta_base_cache_limit = git_config_int(var, value);
386 return 0;
389 if (!strcmp(var, "core.autocrlf")) {
390 if (value && !strcasecmp(value, "input")) {
391 auto_crlf = -1;
392 return 0;
394 auto_crlf = git_config_bool(var, value);
395 return 0;
398 if (!strcmp(var, "user.name")) {
399 strlcpy(git_default_name, value, sizeof(git_default_name));
400 return 0;
403 if (!strcmp(var, "user.email")) {
404 strlcpy(git_default_email, value, sizeof(git_default_email));
405 return 0;
408 if (!strcmp(var, "i18n.commitencoding")) {
409 git_commit_encoding = xstrdup(value);
410 return 0;
413 if (!strcmp(var, "i18n.logoutputencoding")) {
414 git_log_output_encoding = xstrdup(value);
415 return 0;
419 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
420 pager_use_color = git_config_bool(var,value);
421 return 0;
424 if (!strcmp(var, "core.pager")) {
425 pager_program = xstrdup(value);
426 return 0;
429 /* Add other config variables here and to Documentation/config.txt. */
430 return 0;
433 int git_config_from_file(config_fn_t fn, const char *filename)
435 int ret;
436 FILE *f = fopen(filename, "r");
438 ret = -1;
439 if (f) {
440 config_file = f;
441 config_file_name = filename;
442 config_linenr = 1;
443 ret = git_parse_file(fn);
444 fclose(f);
445 config_file_name = NULL;
447 return ret;
450 int git_config(config_fn_t fn)
452 int ret = 0;
453 char *repo_config = NULL;
454 const char *home = NULL, *filename;
456 /* $GIT_CONFIG makes git read _only_ the given config file,
457 * $GIT_CONFIG_LOCAL will make it process it in addition to the
458 * global config file, the same way it would the per-repository
459 * config file otherwise. */
460 filename = getenv(CONFIG_ENVIRONMENT);
461 if (!filename) {
462 if (!access(ETC_GITCONFIG, R_OK))
463 ret += git_config_from_file(fn, ETC_GITCONFIG);
464 home = getenv("HOME");
465 filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
466 if (!filename)
467 filename = repo_config = xstrdup(git_path("config"));
470 if (home) {
471 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
472 if (!access(user_config, R_OK))
473 ret = git_config_from_file(fn, user_config);
474 free(user_config);
477 ret += git_config_from_file(fn, filename);
478 free(repo_config);
479 return ret;
483 * Find all the stuff for git_config_set() below.
486 #define MAX_MATCHES 512
488 static struct {
489 int baselen;
490 char* key;
491 int do_not_match;
492 regex_t* value_regex;
493 int multi_replace;
494 size_t offset[MAX_MATCHES];
495 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
496 int seen;
497 } store;
499 static int matches(const char* key, const char* value)
501 return !strcmp(key, store.key) &&
502 (store.value_regex == NULL ||
503 (store.do_not_match ^
504 !regexec(store.value_regex, value, 0, NULL, 0)));
507 static int store_aux(const char* key, const char* value)
509 const char *ep;
510 size_t section_len;
512 switch (store.state) {
513 case KEY_SEEN:
514 if (matches(key, value)) {
515 if (store.seen == 1 && store.multi_replace == 0) {
516 fprintf(stderr,
517 "Warning: %s has multiple values\n",
518 key);
519 } else if (store.seen >= MAX_MATCHES) {
520 fprintf(stderr, "Too many matches\n");
521 return 1;
524 store.offset[store.seen] = ftell(config_file);
525 store.seen++;
527 break;
528 case SECTION_SEEN:
530 * What we are looking for is in store.key (both
531 * section and var), and its section part is baselen
532 * long. We found key (again, both section and var).
533 * We would want to know if this key is in the same
534 * section as what we are looking for. We already
535 * know we are in the same section as what should
536 * hold store.key.
538 ep = strrchr(key, '.');
539 section_len = ep - key;
541 if ((section_len != store.baselen) ||
542 memcmp(key, store.key, section_len+1)) {
543 store.state = SECTION_END_SEEN;
544 break;
548 * Do not increment matches: this is no match, but we
549 * just made sure we are in the desired section.
551 store.offset[store.seen] = ftell(config_file);
552 /* fallthru */
553 case SECTION_END_SEEN:
554 case START:
555 if (matches(key, value)) {
556 store.offset[store.seen] = ftell(config_file);
557 store.state = KEY_SEEN;
558 store.seen++;
559 } else {
560 if (strrchr(key, '.') - key == store.baselen &&
561 !strncmp(key, store.key, store.baselen)) {
562 store.state = SECTION_SEEN;
563 store.offset[store.seen] = ftell(config_file);
567 return 0;
570 static int write_error(void)
572 fprintf(stderr, "Failed to write new configuration file\n");
574 /* Same error code as "failed to rename". */
575 return 4;
578 static int store_write_section(int fd, const char* key)
580 const char *dot = strchr(key, '.');
581 int len1 = store.baselen, len2 = -1;
583 dot = strchr(key, '.');
584 if (dot) {
585 int dotlen = dot - key;
586 if (dotlen < len1) {
587 len2 = len1 - dotlen - 1;
588 len1 = dotlen;
592 if (write_in_full(fd, "[", 1) != 1 ||
593 write_in_full(fd, key, len1) != len1)
594 return 0;
595 if (len2 >= 0) {
596 if (write_in_full(fd, " \"", 2) != 2)
597 return 0;
598 while (--len2 >= 0) {
599 unsigned char c = *++dot;
600 if (c == '"')
601 if (write_in_full(fd, "\\", 1) != 1)
602 return 0;
603 if (write_in_full(fd, &c, 1) != 1)
604 return 0;
606 if (write_in_full(fd, "\"", 1) != 1)
607 return 0;
609 if (write_in_full(fd, "]\n", 2) != 2)
610 return 0;
612 return 1;
615 static int store_write_pair(int fd, const char* key, const char* value)
617 int i;
618 int length = strlen(key+store.baselen+1);
619 int quote = 0;
621 /* Check to see if the value needs to be quoted. */
622 if (value[0] == ' ')
623 quote = 1;
624 for (i = 0; value[i]; i++)
625 if (value[i] == ';' || value[i] == '#')
626 quote = 1;
627 if (value[i-1] == ' ')
628 quote = 1;
630 if (write_in_full(fd, "\t", 1) != 1 ||
631 write_in_full(fd, key+store.baselen+1, length) != length ||
632 write_in_full(fd, " = ", 3) != 3)
633 return 0;
634 if (quote && write_in_full(fd, "\"", 1) != 1)
635 return 0;
636 for (i = 0; value[i]; i++)
637 switch (value[i]) {
638 case '\n':
639 if (write_in_full(fd, "\\n", 2) != 2)
640 return 0;
641 break;
642 case '\t':
643 if (write_in_full(fd, "\\t", 2) != 2)
644 return 0;
645 break;
646 case '"':
647 case '\\':
648 if (write_in_full(fd, "\\", 1) != 1)
649 return 0;
650 default:
651 if (write_in_full(fd, value+i, 1) != 1)
652 return 0;
653 break;
655 if (quote && write_in_full(fd, "\"", 1) != 1)
656 return 0;
657 if (write_in_full(fd, "\n", 1) != 1)
658 return 0;
659 return 1;
662 static ssize_t find_beginning_of_line(const char* contents, size_t size,
663 size_t offset_, int* found_bracket)
665 size_t equal_offset = size, bracket_offset = size;
666 ssize_t offset;
668 for (offset = offset_-2; offset > 0
669 && contents[offset] != '\n'; offset--)
670 switch (contents[offset]) {
671 case '=': equal_offset = offset; break;
672 case ']': bracket_offset = offset; break;
674 if (bracket_offset < equal_offset) {
675 *found_bracket = 1;
676 offset = bracket_offset+1;
677 } else
678 offset++;
680 return offset;
683 int git_config_set(const char* key, const char* value)
685 return git_config_set_multivar(key, value, NULL, 0);
689 * If value==NULL, unset in (remove from) config,
690 * if value_regex!=NULL, disregard key/value pairs where value does not match.
691 * if multi_replace==0, nothing, or only one matching key/value is replaced,
692 * else all matching key/values (regardless how many) are removed,
693 * before the new pair is written.
695 * Returns 0 on success.
697 * This function does this:
699 * - it locks the config file by creating ".git/config.lock"
701 * - it then parses the config using store_aux() as validator to find
702 * the position on the key/value pair to replace. If it is to be unset,
703 * it must be found exactly once.
705 * - the config file is mmap()ed and the part before the match (if any) is
706 * written to the lock file, then the changed part and the rest.
708 * - the config file is removed and the lock file rename()d to it.
711 int git_config_set_multivar(const char* key, const char* value,
712 const char* value_regex, int multi_replace)
714 int i, dot;
715 int fd = -1, in_fd;
716 int ret;
717 char* config_filename;
718 struct lock_file *lock = NULL;
719 const char* last_dot = strrchr(key, '.');
721 config_filename = getenv(CONFIG_ENVIRONMENT);
722 if (!config_filename) {
723 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
724 if (!config_filename)
725 config_filename = git_path("config");
727 config_filename = xstrdup(config_filename);
730 * Since "key" actually contains the section name and the real
731 * key name separated by a dot, we have to know where the dot is.
734 if (last_dot == NULL) {
735 fprintf(stderr, "key does not contain a section: %s\n", key);
736 ret = 2;
737 goto out_free;
739 store.baselen = last_dot - key;
741 store.multi_replace = multi_replace;
744 * Validate the key and while at it, lower case it for matching.
746 store.key = xmalloc(strlen(key) + 1);
747 dot = 0;
748 for (i = 0; key[i]; i++) {
749 unsigned char c = key[i];
750 if (c == '.')
751 dot = 1;
752 /* Leave the extended basename untouched.. */
753 if (!dot || i > store.baselen) {
754 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
755 fprintf(stderr, "invalid key: %s\n", key);
756 free(store.key);
757 ret = 1;
758 goto out_free;
760 c = tolower(c);
761 } else if (c == '\n') {
762 fprintf(stderr, "invalid key (newline): %s\n", key);
763 free(store.key);
764 ret = 1;
765 goto out_free;
767 store.key[i] = c;
769 store.key[i] = 0;
772 * The lock serves a purpose in addition to locking: the new
773 * contents of .git/config will be written into it.
775 lock = xcalloc(sizeof(struct lock_file), 1);
776 fd = hold_lock_file_for_update(lock, config_filename, 0);
777 if (fd < 0) {
778 fprintf(stderr, "could not lock config file\n");
779 free(store.key);
780 ret = -1;
781 goto out_free;
785 * If .git/config does not exist yet, write a minimal version.
787 in_fd = open(config_filename, O_RDONLY);
788 if ( in_fd < 0 ) {
789 free(store.key);
791 if ( ENOENT != errno ) {
792 error("opening %s: %s", config_filename,
793 strerror(errno));
794 ret = 3; /* same as "invalid config file" */
795 goto out_free;
797 /* if nothing to unset, error out */
798 if (value == NULL) {
799 ret = 5;
800 goto out_free;
803 store.key = (char*)key;
804 if (!store_write_section(fd, key) ||
805 !store_write_pair(fd, key, value))
806 goto write_err_out;
807 } else {
808 struct stat st;
809 char* contents;
810 size_t contents_sz, copy_begin, copy_end;
811 int i, new_line = 0;
813 if (value_regex == NULL)
814 store.value_regex = NULL;
815 else {
816 if (value_regex[0] == '!') {
817 store.do_not_match = 1;
818 value_regex++;
819 } else
820 store.do_not_match = 0;
822 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
823 if (regcomp(store.value_regex, value_regex,
824 REG_EXTENDED)) {
825 fprintf(stderr, "Invalid pattern: %s\n",
826 value_regex);
827 free(store.value_regex);
828 ret = 6;
829 goto out_free;
833 store.offset[0] = 0;
834 store.state = START;
835 store.seen = 0;
838 * After this, store.offset will contain the *end* offset
839 * of the last match, or remain at 0 if no match was found.
840 * As a side effect, we make sure to transform only a valid
841 * existing config file.
843 if (git_config_from_file(store_aux, config_filename)) {
844 fprintf(stderr, "invalid config file\n");
845 free(store.key);
846 if (store.value_regex != NULL) {
847 regfree(store.value_regex);
848 free(store.value_regex);
850 ret = 3;
851 goto out_free;
854 free(store.key);
855 if (store.value_regex != NULL) {
856 regfree(store.value_regex);
857 free(store.value_regex);
860 /* if nothing to unset, or too many matches, error out */
861 if ((store.seen == 0 && value == NULL) ||
862 (store.seen > 1 && multi_replace == 0)) {
863 ret = 5;
864 goto out_free;
867 fstat(in_fd, &st);
868 contents_sz = xsize_t(st.st_size);
869 contents = xmmap(NULL, contents_sz, PROT_READ,
870 MAP_PRIVATE, in_fd, 0);
871 close(in_fd);
873 if (store.seen == 0)
874 store.seen = 1;
876 for (i = 0, copy_begin = 0; i < store.seen; i++) {
877 if (store.offset[i] == 0) {
878 store.offset[i] = copy_end = contents_sz;
879 } else if (store.state != KEY_SEEN) {
880 copy_end = store.offset[i];
881 } else
882 copy_end = find_beginning_of_line(
883 contents, contents_sz,
884 store.offset[i]-2, &new_line);
886 /* write the first part of the config */
887 if (copy_end > copy_begin) {
888 if (write_in_full(fd, contents + copy_begin,
889 copy_end - copy_begin) <
890 copy_end - copy_begin)
891 goto write_err_out;
892 if (new_line &&
893 write_in_full(fd, "\n", 1) != 1)
894 goto write_err_out;
896 copy_begin = store.offset[i];
899 /* write the pair (value == NULL means unset) */
900 if (value != NULL) {
901 if (store.state == START) {
902 if (!store_write_section(fd, key))
903 goto write_err_out;
905 if (!store_write_pair(fd, key, value))
906 goto write_err_out;
909 /* write the rest of the config */
910 if (copy_begin < contents_sz)
911 if (write_in_full(fd, contents + copy_begin,
912 contents_sz - copy_begin) <
913 contents_sz - copy_begin)
914 goto write_err_out;
916 munmap(contents, contents_sz);
919 if (close(fd) || commit_lock_file(lock) < 0) {
920 fprintf(stderr, "Cannot commit config file!\n");
921 ret = 4;
922 goto out_free;
925 /* fd is closed, so don't try to close it below. */
926 fd = -1;
928 * lock is committed, so don't try to roll it back below.
929 * NOTE: Since lockfile.c keeps a linked list of all created
930 * lock_file structures, it isn't safe to free(lock). It's
931 * better to just leave it hanging around.
933 lock = NULL;
934 ret = 0;
936 out_free:
937 if (0 <= fd)
938 close(fd);
939 if (lock)
940 rollback_lock_file(lock);
941 free(config_filename);
942 return ret;
944 write_err_out:
945 ret = write_error();
946 goto out_free;
950 static int section_name_match (const char *buf, const char *name)
952 int i = 0, j = 0, dot = 0;
953 for (; buf[i] && buf[i] != ']'; i++) {
954 if (!dot && isspace(buf[i])) {
955 dot = 1;
956 if (name[j++] != '.')
957 break;
958 for (i++; isspace(buf[i]); i++)
959 ; /* do nothing */
960 if (buf[i] != '"')
961 break;
962 continue;
964 if (buf[i] == '\\' && dot)
965 i++;
966 else if (buf[i] == '"' && dot) {
967 for (i++; isspace(buf[i]); i++)
968 ; /* do_nothing */
969 break;
971 if (buf[i] != name[j++])
972 break;
974 return (buf[i] == ']' && name[j] == 0);
977 /* if new_name == NULL, the section is removed instead */
978 int git_config_rename_section(const char *old_name, const char *new_name)
980 int ret = 0, remove = 0;
981 char *config_filename;
982 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
983 int out_fd;
984 char buf[1024];
986 config_filename = getenv(CONFIG_ENVIRONMENT);
987 if (!config_filename) {
988 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
989 if (!config_filename)
990 config_filename = git_path("config");
992 config_filename = xstrdup(config_filename);
993 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
994 if (out_fd < 0) {
995 ret = error("Could not lock config file!");
996 goto out;
999 if (!(config_file = fopen(config_filename, "rb"))) {
1000 /* no config file means nothing to rename, no error */
1001 goto unlock_and_out;
1004 while (fgets(buf, sizeof(buf), config_file)) {
1005 int i;
1006 int length;
1007 for (i = 0; buf[i] && isspace(buf[i]); i++)
1008 ; /* do nothing */
1009 if (buf[i] == '[') {
1010 /* it's a section */
1011 if (section_name_match (&buf[i+1], old_name)) {
1012 ret++;
1013 if (new_name == NULL) {
1014 remove = 1;
1015 continue;
1017 store.baselen = strlen(new_name);
1018 if (!store_write_section(out_fd, new_name)) {
1019 ret = write_error();
1020 goto out;
1022 continue;
1024 remove = 0;
1026 if (remove)
1027 continue;
1028 length = strlen(buf);
1029 if (write_in_full(out_fd, buf, length) != length) {
1030 ret = write_error();
1031 goto out;
1034 fclose(config_file);
1035 unlock_and_out:
1036 if (close(out_fd) || commit_lock_file(lock) < 0)
1037 ret = error("Cannot commit config file!");
1038 out:
1039 free(config_filename);
1040 return ret;