Undo /usr/bin/sort and /usr/bin/find workarounds.
[git/dscho.git] / config.c
blobb0c6a24eb70f4bbd7aa18d5b1143c6a1ae2569d2
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"
9 #include "exec_cmd.h"
11 #define MAXNAME (256)
13 static FILE *config_file;
14 static const char *config_file_name;
15 static int config_linenr;
16 static int zlib_compression_seen;
18 static int get_next_char(void)
20 int c;
21 FILE *f;
23 c = '\n';
24 if ((f = config_file) != NULL) {
25 c = fgetc(f);
26 if (c == '\r') {
27 /* DOS like systems */
28 c = fgetc(f);
29 if (c != '\n') {
30 ungetc(c, f);
31 c = '\r';
34 if (c == '\n')
35 config_linenr++;
36 if (c == EOF) {
37 config_file = NULL;
38 c = '\n';
41 return c;
44 static char *parse_value(void)
46 static char value[1024];
47 int quote = 0, comment = 0, len = 0, space = 0;
49 for (;;) {
50 int c = get_next_char();
51 if (len >= sizeof(value))
52 return NULL;
53 if (c == '\n') {
54 if (quote)
55 return NULL;
56 value[len] = 0;
57 return value;
59 if (comment)
60 continue;
61 if (isspace(c) && !quote) {
62 space = 1;
63 continue;
65 if (!quote) {
66 if (c == ';' || c == '#') {
67 comment = 1;
68 continue;
71 if (space) {
72 if (len)
73 value[len++] = ' ';
74 space = 0;
76 if (c == '\\') {
77 c = get_next_char();
78 switch (c) {
79 case '\n':
80 continue;
81 case 't':
82 c = '\t';
83 break;
84 case 'b':
85 c = '\b';
86 break;
87 case 'n':
88 c = '\n';
89 break;
90 /* Some characters escape as themselves */
91 case '\\': case '"':
92 break;
93 /* Reject unknown escape sequences */
94 default:
95 return NULL;
97 value[len++] = c;
98 continue;
100 if (c == '"') {
101 quote = 1-quote;
102 continue;
104 value[len++] = c;
108 static inline int iskeychar(int c)
110 return isalnum(c) || c == '-';
113 static int get_value(config_fn_t fn, char *name, unsigned int len)
115 int c;
116 char *value;
118 /* Get the full name */
119 for (;;) {
120 c = get_next_char();
121 if (c == EOF)
122 break;
123 if (!iskeychar(c))
124 break;
125 name[len++] = tolower(c);
126 if (len >= MAXNAME)
127 return -1;
129 name[len] = 0;
130 while (c == ' ' || c == '\t')
131 c = get_next_char();
133 value = NULL;
134 if (c != '\n') {
135 if (c != '=')
136 return -1;
137 value = parse_value();
138 if (!value)
139 return -1;
141 return fn(name, value);
144 static int get_extended_base_var(char *name, int baselen, int c)
146 do {
147 if (c == '\n')
148 return -1;
149 c = get_next_char();
150 } while (isspace(c));
152 /* We require the format to be '[base "extension"]' */
153 if (c != '"')
154 return -1;
155 name[baselen++] = '.';
157 for (;;) {
158 int c = get_next_char();
159 if (c == '\n')
160 return -1;
161 if (c == '"')
162 break;
163 if (c == '\\') {
164 c = get_next_char();
165 if (c == '\n')
166 return -1;
168 name[baselen++] = c;
169 if (baselen > MAXNAME / 2)
170 return -1;
173 /* Final ']' */
174 if (get_next_char() != ']')
175 return -1;
176 return baselen;
179 static int get_base_var(char *name)
181 int baselen = 0;
183 for (;;) {
184 int c = get_next_char();
185 if (c == EOF)
186 return -1;
187 if (c == ']')
188 return baselen;
189 if (isspace(c))
190 return get_extended_base_var(name, baselen, c);
191 if (!iskeychar(c) && c != '.')
192 return -1;
193 if (baselen > MAXNAME / 2)
194 return -1;
195 name[baselen++] = tolower(c);
199 static int git_parse_file(config_fn_t fn)
201 int comment = 0;
202 int baselen = 0;
203 static char var[MAXNAME];
205 for (;;) {
206 int c = get_next_char();
207 if (c == '\n') {
208 /* EOF? */
209 if (!config_file)
210 return 0;
211 comment = 0;
212 continue;
214 if (comment || isspace(c))
215 continue;
216 if (c == '#' || c == ';') {
217 comment = 1;
218 continue;
220 if (c == '[') {
221 baselen = get_base_var(var);
222 if (baselen <= 0)
223 break;
224 var[baselen++] = '.';
225 var[baselen] = 0;
226 continue;
228 if (!isalpha(c))
229 break;
230 var[baselen] = tolower(c);
231 if (get_value(fn, var, baselen+1) < 0)
232 break;
234 die("bad config file line %d in %s", config_linenr, config_file_name);
237 static unsigned long get_unit_factor(const char *end)
239 if (!*end)
240 return 1;
241 else if (!strcasecmp(end, "k"))
242 return 1024;
243 else if (!strcasecmp(end, "m"))
244 return 1024 * 1024;
245 else if (!strcasecmp(end, "g"))
246 return 1024 * 1024 * 1024;
247 die("unknown unit: '%s'", end);
250 int git_parse_long(const char *value, long *ret)
252 if (value && *value) {
253 char *end;
254 long val = strtol(value, &end, 0);
255 *ret = val * get_unit_factor(end);
256 return 1;
258 return 0;
261 int git_parse_ulong(const char *value, unsigned long *ret)
263 if (value && *value) {
264 char *end;
265 unsigned long val = strtoul(value, &end, 0);
266 *ret = val * get_unit_factor(end);
267 return 1;
269 return 0;
272 int git_config_int(const char *name, const char *value)
274 long ret;
275 if (!git_parse_long(value, &ret))
276 die("bad config value for '%s' in %s", name, config_file_name);
277 return ret;
280 unsigned long git_config_ulong(const char *name, const char *value)
282 unsigned long ret;
283 if (!git_parse_ulong(value, &ret))
284 die("bad config value for '%s' in %s", name, config_file_name);
285 return ret;
288 int git_config_bool(const char *name, const char *value)
290 if (!value)
291 return 1;
292 if (!*value)
293 return 0;
294 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
295 return 1;
296 if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
297 return 0;
298 return git_config_int(name, value) != 0;
301 int git_default_config(const char *var, const char *value)
303 /* This needs a better name */
304 if (!strcmp(var, "core.filemode")) {
305 trust_executable_bit = git_config_bool(var, value);
306 return 0;
309 if (!strcmp(var, "core.quotepath")) {
310 quote_path_fully = git_config_bool(var, value);
311 return 0;
314 if (!strcmp(var, "core.symlinks")) {
315 has_symlinks = git_config_bool(var, value);
316 return 0;
319 if (!strcmp(var, "core.bare")) {
320 is_bare_repository_cfg = git_config_bool(var, value);
321 return 0;
324 if (!strcmp(var, "core.ignorestat")) {
325 assume_unchanged = git_config_bool(var, value);
326 return 0;
329 if (!strcmp(var, "core.prefersymlinkrefs")) {
330 prefer_symlink_refs = git_config_bool(var, value);
331 return 0;
334 if (!strcmp(var, "core.logallrefupdates")) {
335 log_all_ref_updates = git_config_bool(var, value);
336 return 0;
339 if (!strcmp(var, "core.warnambiguousrefs")) {
340 warn_ambiguous_refs = git_config_bool(var, value);
341 return 0;
344 if (!strcmp(var, "core.loosecompression")) {
345 int level = git_config_int(var, value);
346 if (level == -1)
347 level = Z_DEFAULT_COMPRESSION;
348 else if (level < 0 || level > Z_BEST_COMPRESSION)
349 die("bad zlib compression level %d", level);
350 zlib_compression_level = level;
351 zlib_compression_seen = 1;
352 return 0;
355 if (!strcmp(var, "core.compression")) {
356 int level = git_config_int(var, value);
357 if (level == -1)
358 level = Z_DEFAULT_COMPRESSION;
359 else if (level < 0 || level > Z_BEST_COMPRESSION)
360 die("bad zlib compression level %d", level);
361 core_compression_level = level;
362 core_compression_seen = 1;
363 if (!zlib_compression_seen)
364 zlib_compression_level = level;
365 return 0;
368 if (!strcmp(var, "core.packedgitwindowsize")) {
369 int pgsz_x2 = getpagesize() * 2;
370 packed_git_window_size = git_config_int(var, value);
372 /* This value must be multiple of (pagesize * 2) */
373 packed_git_window_size /= pgsz_x2;
374 if (packed_git_window_size < 1)
375 packed_git_window_size = 1;
376 packed_git_window_size *= pgsz_x2;
377 return 0;
380 if (!strcmp(var, "core.packedgitlimit")) {
381 packed_git_limit = git_config_int(var, value);
382 return 0;
385 if (!strcmp(var, "core.deltabasecachelimit")) {
386 delta_base_cache_limit = git_config_int(var, value);
387 return 0;
390 if (!strcmp(var, "core.autocrlf")) {
391 if (value && !strcasecmp(value, "input")) {
392 auto_crlf = -1;
393 return 0;
395 auto_crlf = git_config_bool(var, value);
396 return 0;
399 if (!strcmp(var, "user.name")) {
400 strlcpy(git_default_name, value, sizeof(git_default_name));
401 return 0;
404 if (!strcmp(var, "user.email")) {
405 strlcpy(git_default_email, value, sizeof(git_default_email));
406 return 0;
409 if (!strcmp(var, "i18n.commitencoding")) {
410 git_commit_encoding = xstrdup(value);
411 return 0;
414 if (!strcmp(var, "i18n.logoutputencoding")) {
415 git_log_output_encoding = xstrdup(value);
416 return 0;
420 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
421 pager_use_color = git_config_bool(var,value);
422 return 0;
425 if (!strcmp(var, "core.pager")) {
426 pager_program = xstrdup(value);
427 return 0;
430 if (!strcmp(var, "core.editor")) {
431 editor_program = xstrdup(value);
432 return 0;
435 /* Add other config variables here and to Documentation/config.txt. */
436 return 0;
439 int git_config_from_file(config_fn_t fn, const char *filename)
441 int ret;
442 FILE *f = fopen(filename, "r");
444 ret = -1;
445 if (f) {
446 config_file = f;
447 config_file_name = filename;
448 config_linenr = 1;
449 ret = git_parse_file(fn);
450 fclose(f);
451 config_file_name = NULL;
453 return ret;
456 const char *git_etc_gitconfig(void)
458 static const char *system_wide;
459 if (!system_wide) {
460 system_wide = ETC_GITCONFIG;
461 /* interpret path relative to exec-dir */
462 if (!is_absolute_path(system_wide)) {
463 const char *exec_path = git_exec_path();
464 system_wide = prefix_path(exec_path, strlen(exec_path),
465 system_wide);
468 return system_wide;
471 int git_config(config_fn_t fn)
473 int ret = 0;
474 char *repo_config = NULL;
475 const char *home = NULL, *filename;
477 /* $GIT_CONFIG makes git read _only_ the given config file,
478 * $GIT_CONFIG_LOCAL will make it process it in addition to the
479 * global config file, the same way it would the per-repository
480 * config file otherwise. */
481 filename = getenv(CONFIG_ENVIRONMENT);
482 if (!filename) {
483 if (!access(git_etc_gitconfig(), R_OK))
484 ret += git_config_from_file(fn, git_etc_gitconfig());
485 home = getenv("HOME");
486 filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
487 if (!filename)
488 filename = repo_config = xstrdup(git_path("config"));
491 if (home) {
492 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
493 if (!access(user_config, R_OK))
494 ret = git_config_from_file(fn, user_config);
495 free(user_config);
498 ret += git_config_from_file(fn, filename);
499 free(repo_config);
500 return ret;
504 * Find all the stuff for git_config_set() below.
507 #define MAX_MATCHES 512
509 static struct {
510 int baselen;
511 char* key;
512 int do_not_match;
513 regex_t* value_regex;
514 int multi_replace;
515 size_t offset[MAX_MATCHES];
516 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
517 int seen;
518 } store;
520 static int matches(const char* key, const char* value)
522 return !strcmp(key, store.key) &&
523 (store.value_regex == NULL ||
524 (store.do_not_match ^
525 !regexec(store.value_regex, value, 0, NULL, 0)));
528 static int store_aux(const char* key, const char* value)
530 const char *ep;
531 size_t section_len;
533 switch (store.state) {
534 case KEY_SEEN:
535 if (matches(key, value)) {
536 if (store.seen == 1 && store.multi_replace == 0) {
537 fprintf(stderr,
538 "Warning: %s has multiple values\n",
539 key);
540 } else if (store.seen >= MAX_MATCHES) {
541 fprintf(stderr, "Too many matches\n");
542 return 1;
545 store.offset[store.seen] = ftell(config_file);
546 store.seen++;
548 break;
549 case SECTION_SEEN:
551 * What we are looking for is in store.key (both
552 * section and var), and its section part is baselen
553 * long. We found key (again, both section and var).
554 * We would want to know if this key is in the same
555 * section as what we are looking for. We already
556 * know we are in the same section as what should
557 * hold store.key.
559 ep = strrchr(key, '.');
560 section_len = ep - key;
562 if ((section_len != store.baselen) ||
563 memcmp(key, store.key, section_len+1)) {
564 store.state = SECTION_END_SEEN;
565 break;
569 * Do not increment matches: this is no match, but we
570 * just made sure we are in the desired section.
572 store.offset[store.seen] = ftell(config_file);
573 /* fallthru */
574 case SECTION_END_SEEN:
575 case START:
576 if (matches(key, value)) {
577 store.offset[store.seen] = ftell(config_file);
578 store.state = KEY_SEEN;
579 store.seen++;
580 } else {
581 if (strrchr(key, '.') - key == store.baselen &&
582 !strncmp(key, store.key, store.baselen)) {
583 store.state = SECTION_SEEN;
584 store.offset[store.seen] = ftell(config_file);
588 return 0;
591 static int write_error(void)
593 fprintf(stderr, "Failed to write new configuration file\n");
595 /* Same error code as "failed to rename". */
596 return 4;
599 static int store_write_section(int fd, const char* key)
601 const char *dot = strchr(key, '.');
602 int len1 = store.baselen, len2 = -1;
604 dot = strchr(key, '.');
605 if (dot) {
606 int dotlen = dot - key;
607 if (dotlen < len1) {
608 len2 = len1 - dotlen - 1;
609 len1 = dotlen;
613 if (write_in_full(fd, "[", 1) != 1 ||
614 write_in_full(fd, key, len1) != len1)
615 return 0;
616 if (len2 >= 0) {
617 if (write_in_full(fd, " \"", 2) != 2)
618 return 0;
619 while (--len2 >= 0) {
620 unsigned char c = *++dot;
621 if (c == '"')
622 if (write_in_full(fd, "\\", 1) != 1)
623 return 0;
624 if (write_in_full(fd, &c, 1) != 1)
625 return 0;
627 if (write_in_full(fd, "\"", 1) != 1)
628 return 0;
630 if (write_in_full(fd, "]\n", 2) != 2)
631 return 0;
633 return 1;
636 static int store_write_pair(int fd, const char* key, const char* value)
638 int i;
639 int length = strlen(key+store.baselen+1);
640 int quote = 0;
642 /* Check to see if the value needs to be quoted. */
643 if (value[0] == ' ')
644 quote = 1;
645 for (i = 0; value[i]; i++)
646 if (value[i] == ';' || value[i] == '#')
647 quote = 1;
648 if (value[i-1] == ' ')
649 quote = 1;
651 if (write_in_full(fd, "\t", 1) != 1 ||
652 write_in_full(fd, key+store.baselen+1, length) != length ||
653 write_in_full(fd, " = ", 3) != 3)
654 return 0;
655 if (quote && write_in_full(fd, "\"", 1) != 1)
656 return 0;
657 for (i = 0; value[i]; i++)
658 switch (value[i]) {
659 case '\n':
660 if (write_in_full(fd, "\\n", 2) != 2)
661 return 0;
662 break;
663 case '\t':
664 if (write_in_full(fd, "\\t", 2) != 2)
665 return 0;
666 break;
667 case '"':
668 case '\\':
669 if (write_in_full(fd, "\\", 1) != 1)
670 return 0;
671 default:
672 if (write_in_full(fd, value+i, 1) != 1)
673 return 0;
674 break;
676 if (quote && write_in_full(fd, "\"", 1) != 1)
677 return 0;
678 if (write_in_full(fd, "\n", 1) != 1)
679 return 0;
680 return 1;
683 static ssize_t find_beginning_of_line(const char* contents, size_t size,
684 size_t offset_, int* found_bracket)
686 size_t equal_offset = size, bracket_offset = size;
687 ssize_t offset;
689 for (offset = offset_-2; offset > 0
690 && contents[offset] != '\n'; offset--)
691 switch (contents[offset]) {
692 case '=': equal_offset = offset; break;
693 case ']': bracket_offset = offset; break;
695 if (bracket_offset < equal_offset) {
696 *found_bracket = 1;
697 offset = bracket_offset+1;
698 } else
699 offset++;
701 return offset;
704 int git_config_set(const char* key, const char* value)
706 return git_config_set_multivar(key, value, NULL, 0);
710 * If value==NULL, unset in (remove from) config,
711 * if value_regex!=NULL, disregard key/value pairs where value does not match.
712 * if multi_replace==0, nothing, or only one matching key/value is replaced,
713 * else all matching key/values (regardless how many) are removed,
714 * before the new pair is written.
716 * Returns 0 on success.
718 * This function does this:
720 * - it locks the config file by creating ".git/config.lock"
722 * - it then parses the config using store_aux() as validator to find
723 * the position on the key/value pair to replace. If it is to be unset,
724 * it must be found exactly once.
726 * - the config file is mmap()ed and the part before the match (if any) is
727 * written to the lock file, then the changed part and the rest.
729 * - the config file is removed and the lock file rename()d to it.
732 int git_config_set_multivar(const char* key, const char* value,
733 const char* value_regex, int multi_replace)
735 int i, dot;
736 int fd = -1, in_fd;
737 int ret;
738 char* config_filename;
739 struct lock_file *lock = NULL;
740 const char* last_dot = strrchr(key, '.');
742 config_filename = getenv(CONFIG_ENVIRONMENT);
743 if (!config_filename) {
744 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
745 if (!config_filename)
746 config_filename = git_path("config");
748 config_filename = xstrdup(config_filename);
751 * Since "key" actually contains the section name and the real
752 * key name separated by a dot, we have to know where the dot is.
755 if (last_dot == NULL) {
756 fprintf(stderr, "key does not contain a section: %s\n", key);
757 ret = 2;
758 goto out_free;
760 store.baselen = last_dot - key;
762 store.multi_replace = multi_replace;
765 * Validate the key and while at it, lower case it for matching.
767 store.key = xmalloc(strlen(key) + 1);
768 dot = 0;
769 for (i = 0; key[i]; i++) {
770 unsigned char c = key[i];
771 if (c == '.')
772 dot = 1;
773 /* Leave the extended basename untouched.. */
774 if (!dot || i > store.baselen) {
775 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
776 fprintf(stderr, "invalid key: %s\n", key);
777 free(store.key);
778 ret = 1;
779 goto out_free;
781 c = tolower(c);
782 } else if (c == '\n') {
783 fprintf(stderr, "invalid key (newline): %s\n", key);
784 free(store.key);
785 ret = 1;
786 goto out_free;
788 store.key[i] = c;
790 store.key[i] = 0;
793 * The lock serves a purpose in addition to locking: the new
794 * contents of .git/config will be written into it.
796 lock = xcalloc(sizeof(struct lock_file), 1);
797 fd = hold_lock_file_for_update(lock, config_filename, 0);
798 if (fd < 0) {
799 fprintf(stderr, "could not lock config file\n");
800 free(store.key);
801 ret = -1;
802 goto out_free;
806 * If .git/config does not exist yet, write a minimal version.
808 in_fd = open(config_filename, O_RDONLY);
809 if ( in_fd < 0 ) {
810 free(store.key);
812 if ( ENOENT != errno ) {
813 error("opening %s: %s", config_filename,
814 strerror(errno));
815 ret = 3; /* same as "invalid config file" */
816 goto out_free;
818 /* if nothing to unset, error out */
819 if (value == NULL) {
820 ret = 5;
821 goto out_free;
824 store.key = (char*)key;
825 if (!store_write_section(fd, key) ||
826 !store_write_pair(fd, key, value))
827 goto write_err_out;
828 } else {
829 struct stat st;
830 char* contents;
831 size_t contents_sz, copy_begin, copy_end;
832 int i, new_line = 0;
834 if (value_regex == NULL)
835 store.value_regex = NULL;
836 else {
837 if (value_regex[0] == '!') {
838 store.do_not_match = 1;
839 value_regex++;
840 } else
841 store.do_not_match = 0;
843 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
844 if (regcomp(store.value_regex, value_regex,
845 REG_EXTENDED)) {
846 fprintf(stderr, "Invalid pattern: %s\n",
847 value_regex);
848 free(store.value_regex);
849 ret = 6;
850 goto out_free;
854 store.offset[0] = 0;
855 store.state = START;
856 store.seen = 0;
859 * After this, store.offset will contain the *end* offset
860 * of the last match, or remain at 0 if no match was found.
861 * As a side effect, we make sure to transform only a valid
862 * existing config file.
864 if (git_config_from_file(store_aux, config_filename)) {
865 fprintf(stderr, "invalid config file\n");
866 free(store.key);
867 if (store.value_regex != NULL) {
868 regfree(store.value_regex);
869 free(store.value_regex);
871 ret = 3;
872 goto out_free;
875 free(store.key);
876 if (store.value_regex != NULL) {
877 regfree(store.value_regex);
878 free(store.value_regex);
881 /* if nothing to unset, or too many matches, error out */
882 if ((store.seen == 0 && value == NULL) ||
883 (store.seen > 1 && multi_replace == 0)) {
884 ret = 5;
885 goto out_free;
888 fstat(in_fd, &st);
889 contents_sz = xsize_t(st.st_size);
890 contents = xmmap(NULL, contents_sz, PROT_READ,
891 MAP_PRIVATE, in_fd, 0);
892 close(in_fd);
894 if (store.seen == 0)
895 store.seen = 1;
897 for (i = 0, copy_begin = 0; i < store.seen; i++) {
898 if (store.offset[i] == 0) {
899 store.offset[i] = copy_end = contents_sz;
900 } else if (store.state != KEY_SEEN) {
901 copy_end = store.offset[i];
902 } else
903 copy_end = find_beginning_of_line(
904 contents, contents_sz,
905 store.offset[i]-2, &new_line);
907 /* write the first part of the config */
908 if (copy_end > copy_begin) {
909 if (write_in_full(fd, contents + copy_begin,
910 copy_end - copy_begin) <
911 copy_end - copy_begin)
912 goto write_err_out;
913 if (new_line &&
914 write_in_full(fd, "\n", 1) != 1)
915 goto write_err_out;
917 copy_begin = store.offset[i];
920 /* write the pair (value == NULL means unset) */
921 if (value != NULL) {
922 if (store.state == START) {
923 if (!store_write_section(fd, key))
924 goto write_err_out;
926 if (!store_write_pair(fd, key, value))
927 goto write_err_out;
930 /* write the rest of the config */
931 if (copy_begin < contents_sz)
932 if (write_in_full(fd, contents + copy_begin,
933 contents_sz - copy_begin) <
934 contents_sz - copy_begin)
935 goto write_err_out;
937 munmap(contents, contents_sz);
940 if (close(fd) || commit_lock_file(lock) < 0) {
941 fprintf(stderr, "Cannot commit config file!\n");
942 ret = 4;
943 goto out_free;
946 /* fd is closed, so don't try to close it below. */
947 fd = -1;
949 * lock is committed, so don't try to roll it back below.
950 * NOTE: Since lockfile.c keeps a linked list of all created
951 * lock_file structures, it isn't safe to free(lock). It's
952 * better to just leave it hanging around.
954 lock = NULL;
955 ret = 0;
957 out_free:
958 if (0 <= fd)
959 close(fd);
960 if (lock)
961 rollback_lock_file(lock);
962 free(config_filename);
963 return ret;
965 write_err_out:
966 ret = write_error();
967 goto out_free;
971 static int section_name_match (const char *buf, const char *name)
973 int i = 0, j = 0, dot = 0;
974 for (; buf[i] && buf[i] != ']'; i++) {
975 if (!dot && isspace(buf[i])) {
976 dot = 1;
977 if (name[j++] != '.')
978 break;
979 for (i++; isspace(buf[i]); i++)
980 ; /* do nothing */
981 if (buf[i] != '"')
982 break;
983 continue;
985 if (buf[i] == '\\' && dot)
986 i++;
987 else if (buf[i] == '"' && dot) {
988 for (i++; isspace(buf[i]); i++)
989 ; /* do_nothing */
990 break;
992 if (buf[i] != name[j++])
993 break;
995 return (buf[i] == ']' && name[j] == 0);
998 /* if new_name == NULL, the section is removed instead */
999 int git_config_rename_section(const char *old_name, const char *new_name)
1001 int ret = 0, remove = 0;
1002 char *config_filename;
1003 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
1004 int out_fd;
1005 char buf[1024];
1007 config_filename = getenv(CONFIG_ENVIRONMENT);
1008 if (!config_filename) {
1009 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
1010 if (!config_filename)
1011 config_filename = git_path("config");
1013 config_filename = xstrdup(config_filename);
1014 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
1015 if (out_fd < 0) {
1016 ret = error("Could not lock config file!");
1017 goto out;
1020 if (!(config_file = fopen(config_filename, "rb"))) {
1021 /* no config file means nothing to rename, no error */
1022 goto unlock_and_out;
1025 while (fgets(buf, sizeof(buf), config_file)) {
1026 int i;
1027 int length;
1028 for (i = 0; buf[i] && isspace(buf[i]); i++)
1029 ; /* do nothing */
1030 if (buf[i] == '[') {
1031 /* it's a section */
1032 if (section_name_match (&buf[i+1], old_name)) {
1033 ret++;
1034 if (new_name == NULL) {
1035 remove = 1;
1036 continue;
1038 store.baselen = strlen(new_name);
1039 if (!store_write_section(out_fd, new_name)) {
1040 ret = write_error();
1041 goto out;
1043 continue;
1045 remove = 0;
1047 if (remove)
1048 continue;
1049 length = strlen(buf);
1050 if (write_in_full(out_fd, buf, length) != length) {
1051 ret = write_error();
1052 goto out;
1055 fclose(config_file);
1056 unlock_and_out:
1057 if (close(out_fd) || commit_lock_file(lock) < 0)
1058 ret = error("Cannot commit config file!");
1059 out:
1060 free(config_filename);
1061 return ret;