user-manual: more detailed merge discussion
[git/mingw/4msysgit/kblees.git] / config.c
blobc938aa0b15513265866b88dbb9b8217958cac376
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 get_next_char(void)
17 int c;
18 FILE *f;
20 c = '\n';
21 if ((f = config_file) != NULL) {
22 c = fgetc(f);
23 if (c == '\r') {
24 /* DOS like systems */
25 c = fgetc(f);
26 if (c != '\n') {
27 ungetc(c, f);
28 c = '\r';
31 if (c == '\n')
32 config_linenr++;
33 if (c == EOF) {
34 config_file = NULL;
35 c = '\n';
38 return c;
41 static char *parse_value(void)
43 static char value[1024];
44 int quote = 0, comment = 0, len = 0, space = 0;
46 for (;;) {
47 int c = get_next_char();
48 if (len >= sizeof(value))
49 return NULL;
50 if (c == '\n') {
51 if (quote)
52 return NULL;
53 value[len] = 0;
54 return value;
56 if (comment)
57 continue;
58 if (isspace(c) && !quote) {
59 space = 1;
60 continue;
62 if (!quote) {
63 if (c == ';' || c == '#') {
64 comment = 1;
65 continue;
68 if (space) {
69 if (len)
70 value[len++] = ' ';
71 space = 0;
73 if (c == '\\') {
74 c = get_next_char();
75 switch (c) {
76 case '\n':
77 continue;
78 case 't':
79 c = '\t';
80 break;
81 case 'b':
82 c = '\b';
83 break;
84 case 'n':
85 c = '\n';
86 break;
87 /* Some characters escape as themselves */
88 case '\\': case '"':
89 break;
90 /* Reject unknown escape sequences */
91 default:
92 return NULL;
94 value[len++] = c;
95 continue;
97 if (c == '"') {
98 quote = 1-quote;
99 continue;
101 value[len++] = c;
105 static inline int iskeychar(int c)
107 return isalnum(c) || c == '-';
110 static int get_value(config_fn_t fn, char *name, unsigned int len)
112 int c;
113 char *value;
115 /* Get the full name */
116 for (;;) {
117 c = get_next_char();
118 if (c == EOF)
119 break;
120 if (!iskeychar(c))
121 break;
122 name[len++] = tolower(c);
123 if (len >= MAXNAME)
124 return -1;
126 name[len] = 0;
127 while (c == ' ' || c == '\t')
128 c = get_next_char();
130 value = NULL;
131 if (c != '\n') {
132 if (c != '=')
133 return -1;
134 value = parse_value();
135 if (!value)
136 return -1;
138 return fn(name, value);
141 static int get_extended_base_var(char *name, int baselen, int c)
143 do {
144 if (c == '\n')
145 return -1;
146 c = get_next_char();
147 } while (isspace(c));
149 /* We require the format to be '[base "extension"]' */
150 if (c != '"')
151 return -1;
152 name[baselen++] = '.';
154 for (;;) {
155 int c = get_next_char();
156 if (c == '\n')
157 return -1;
158 if (c == '"')
159 break;
160 if (c == '\\') {
161 c = get_next_char();
162 if (c == '\n')
163 return -1;
165 name[baselen++] = c;
166 if (baselen > MAXNAME / 2)
167 return -1;
170 /* Final ']' */
171 if (get_next_char() != ']')
172 return -1;
173 return baselen;
176 static int get_base_var(char *name)
178 int baselen = 0;
180 for (;;) {
181 int c = get_next_char();
182 if (c == EOF)
183 return -1;
184 if (c == ']')
185 return baselen;
186 if (isspace(c))
187 return get_extended_base_var(name, baselen, c);
188 if (!iskeychar(c) && c != '.')
189 return -1;
190 if (baselen > MAXNAME / 2)
191 return -1;
192 name[baselen++] = tolower(c);
196 static int git_parse_file(config_fn_t fn)
198 int comment = 0;
199 int baselen = 0;
200 static char var[MAXNAME];
202 for (;;) {
203 int c = get_next_char();
204 if (c == '\n') {
205 /* EOF? */
206 if (!config_file)
207 return 0;
208 comment = 0;
209 continue;
211 if (comment || isspace(c))
212 continue;
213 if (c == '#' || c == ';') {
214 comment = 1;
215 continue;
217 if (c == '[') {
218 baselen = get_base_var(var);
219 if (baselen <= 0)
220 break;
221 var[baselen++] = '.';
222 var[baselen] = 0;
223 continue;
225 if (!isalpha(c))
226 break;
227 var[baselen] = tolower(c);
228 if (get_value(fn, var, baselen+1) < 0)
229 break;
231 die("bad config file line %d in %s", config_linenr, config_file_name);
234 int git_config_int(const char *name, const char *value)
236 if (value && *value) {
237 char *end;
238 int val = strtol(value, &end, 0);
239 if (!*end)
240 return val;
241 if (!strcasecmp(end, "k"))
242 return val * 1024;
243 if (!strcasecmp(end, "m"))
244 return val * 1024 * 1024;
245 if (!strcasecmp(end, "g"))
246 return val * 1024 * 1024 * 1024;
248 die("bad config value for '%s' in %s", name, config_file_name);
251 int git_config_bool(const char *name, const char *value)
253 if (!value)
254 return 1;
255 if (!*value)
256 return 0;
257 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
258 return 1;
259 if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
260 return 0;
261 return git_config_int(name, value) != 0;
264 int git_default_config(const char *var, const char *value)
266 /* This needs a better name */
267 if (!strcmp(var, "core.filemode")) {
268 trust_executable_bit = git_config_bool(var, value);
269 return 0;
272 if (!strcmp(var, "core.bare")) {
273 is_bare_repository_cfg = git_config_bool(var, value);
274 return 0;
277 if (!strcmp(var, "core.ignorestat")) {
278 assume_unchanged = git_config_bool(var, value);
279 return 0;
282 if (!strcmp(var, "core.prefersymlinkrefs")) {
283 prefer_symlink_refs = git_config_bool(var, value);
284 return 0;
287 if (!strcmp(var, "core.logallrefupdates")) {
288 log_all_ref_updates = git_config_bool(var, value);
289 return 0;
292 if (!strcmp(var, "core.warnambiguousrefs")) {
293 warn_ambiguous_refs = git_config_bool(var, value);
294 return 0;
297 if (!strcmp(var, "core.legacyheaders")) {
298 use_legacy_headers = git_config_bool(var, value);
299 return 0;
302 if (!strcmp(var, "core.compression")) {
303 int level = git_config_int(var, value);
304 if (level == -1)
305 level = Z_DEFAULT_COMPRESSION;
306 else if (level < 0 || level > Z_BEST_COMPRESSION)
307 die("bad zlib compression level %d", level);
308 zlib_compression_level = level;
309 return 0;
312 if (!strcmp(var, "core.packedgitwindowsize")) {
313 int pgsz_x2 = getpagesize() * 2;
314 packed_git_window_size = git_config_int(var, value);
316 /* This value must be multiple of (pagesize * 2) */
317 packed_git_window_size /= pgsz_x2;
318 if (packed_git_window_size < 1)
319 packed_git_window_size = 1;
320 packed_git_window_size *= pgsz_x2;
321 return 0;
324 if (!strcmp(var, "core.packedgitlimit")) {
325 packed_git_limit = git_config_int(var, value);
326 return 0;
329 if (!strcmp(var, "user.name")) {
330 strlcpy(git_default_name, value, sizeof(git_default_name));
331 return 0;
334 if (!strcmp(var, "user.email")) {
335 strlcpy(git_default_email, value, sizeof(git_default_email));
336 return 0;
339 if (!strcmp(var, "i18n.commitencoding")) {
340 git_commit_encoding = strdup(value);
341 return 0;
344 if (!strcmp(var, "i18n.logoutputencoding")) {
345 git_log_output_encoding = strdup(value);
346 return 0;
350 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
351 pager_use_color = git_config_bool(var,value);
352 return 0;
355 /* Add other config variables here and to Documentation/config.txt. */
356 return 0;
359 int git_config_from_file(config_fn_t fn, const char *filename)
361 int ret;
362 FILE *f = fopen(filename, "r");
364 ret = -1;
365 if (f) {
366 config_file = f;
367 config_file_name = filename;
368 config_linenr = 1;
369 ret = git_parse_file(fn);
370 fclose(f);
371 config_file_name = NULL;
373 return ret;
376 int git_config(config_fn_t fn)
378 int ret = 0;
379 char *repo_config = NULL;
380 const char *home = NULL, *filename;
382 /* $GIT_CONFIG makes git read _only_ the given config file,
383 * $GIT_CONFIG_LOCAL will make it process it in addition to the
384 * global config file, the same way it would the per-repository
385 * config file otherwise. */
386 filename = getenv(CONFIG_ENVIRONMENT);
387 if (!filename) {
388 home = getenv("HOME");
389 filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
390 if (!filename)
391 filename = repo_config = xstrdup(git_path("config"));
394 if (home) {
395 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
396 if (!access(user_config, R_OK))
397 ret = git_config_from_file(fn, user_config);
398 free(user_config);
401 ret += git_config_from_file(fn, filename);
402 free(repo_config);
403 return ret;
407 * Find all the stuff for git_config_set() below.
410 #define MAX_MATCHES 512
412 static struct {
413 int baselen;
414 char* key;
415 int do_not_match;
416 regex_t* value_regex;
417 int multi_replace;
418 off_t offset[MAX_MATCHES];
419 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
420 int seen;
421 } store;
423 static int matches(const char* key, const char* value)
425 return !strcmp(key, store.key) &&
426 (store.value_regex == NULL ||
427 (store.do_not_match ^
428 !regexec(store.value_regex, value, 0, NULL, 0)));
431 static int store_aux(const char* key, const char* value)
433 switch (store.state) {
434 case KEY_SEEN:
435 if (matches(key, value)) {
436 if (store.seen == 1 && store.multi_replace == 0) {
437 fprintf(stderr,
438 "Warning: %s has multiple values\n",
439 key);
440 } else if (store.seen >= MAX_MATCHES) {
441 fprintf(stderr, "Too many matches\n");
442 return 1;
445 store.offset[store.seen] = ftell(config_file);
446 store.seen++;
448 break;
449 case SECTION_SEEN:
450 if (strncmp(key, store.key, store.baselen+1)) {
451 store.state = SECTION_END_SEEN;
452 break;
453 } else
454 /* do not increment matches: this is no match */
455 store.offset[store.seen] = ftell(config_file);
456 /* fallthru */
457 case SECTION_END_SEEN:
458 case START:
459 if (matches(key, value)) {
460 store.offset[store.seen] = ftell(config_file);
461 store.state = KEY_SEEN;
462 store.seen++;
463 } else {
464 if (strrchr(key, '.') - key == store.baselen &&
465 !strncmp(key, store.key, store.baselen)) {
466 store.state = SECTION_SEEN;
467 store.offset[store.seen] = ftell(config_file);
471 return 0;
474 static int write_error()
476 fprintf(stderr, "Failed to write new configuration file\n");
478 /* Same error code as "failed to rename". */
479 return 4;
482 static int store_write_section(int fd, const char* key)
484 const char *dot = strchr(key, '.');
485 int len1 = store.baselen, len2 = -1;
487 dot = strchr(key, '.');
488 if (dot) {
489 int dotlen = dot - key;
490 if (dotlen < len1) {
491 len2 = len1 - dotlen - 1;
492 len1 = dotlen;
496 if (write_in_full(fd, "[", 1) != 1 ||
497 write_in_full(fd, key, len1) != len1)
498 return 0;
499 if (len2 >= 0) {
500 if (write_in_full(fd, " \"", 2) != 2)
501 return 0;
502 while (--len2 >= 0) {
503 unsigned char c = *++dot;
504 if (c == '"')
505 if (write_in_full(fd, "\\", 1) != 1)
506 return 0;
507 if (write_in_full(fd, &c, 1) != 1)
508 return 0;
510 if (write_in_full(fd, "\"", 1) != 1)
511 return 0;
513 if (write_in_full(fd, "]\n", 2) != 2)
514 return 0;
516 return 1;
519 static int store_write_pair(int fd, const char* key, const char* value)
521 int i;
522 int length = strlen(key+store.baselen+1);
523 int quote = 0;
525 /* Check to see if the value needs to be quoted. */
526 if (value[0] == ' ')
527 quote = 1;
528 for (i = 0; value[i]; i++)
529 if (value[i] == ';' || value[i] == '#')
530 quote = 1;
531 if (value[i-1] == ' ')
532 quote = 1;
534 if (write_in_full(fd, "\t", 1) != 1 ||
535 write_in_full(fd, key+store.baselen+1, length) != length ||
536 write_in_full(fd, " = ", 3) != 3)
537 return 0;
538 if (quote && write_in_full(fd, "\"", 1) != 1)
539 return 0;
540 for (i = 0; value[i]; i++)
541 switch (value[i]) {
542 case '\n':
543 if (write_in_full(fd, "\\n", 2) != 2)
544 return 0;
545 break;
546 case '\t':
547 if (write_in_full(fd, "\\t", 2) != 2)
548 return 0;
549 break;
550 case '"':
551 case '\\':
552 if (write_in_full(fd, "\\", 1) != 1)
553 return 0;
554 default:
555 if (write_in_full(fd, value+i, 1) != 1)
556 return 0;
557 break;
559 if (quote && write_in_full(fd, "\"", 1) != 1)
560 return 0;
561 if (write_in_full(fd, "\n", 1) != 1)
562 return 0;
563 return 1;
566 static int find_beginning_of_line(const char* contents, int size,
567 int offset_, int* found_bracket)
569 int equal_offset = size, bracket_offset = size;
570 int offset;
572 for (offset = offset_-2; offset > 0
573 && contents[offset] != '\n'; offset--)
574 switch (contents[offset]) {
575 case '=': equal_offset = offset; break;
576 case ']': bracket_offset = offset; break;
578 if (bracket_offset < equal_offset) {
579 *found_bracket = 1;
580 offset = bracket_offset+1;
581 } else
582 offset++;
584 return offset;
587 int git_config_set(const char* key, const char* value)
589 return git_config_set_multivar(key, value, NULL, 0);
593 * If value==NULL, unset in (remove from) config,
594 * if value_regex!=NULL, disregard key/value pairs where value does not match.
595 * if multi_replace==0, nothing, or only one matching key/value is replaced,
596 * else all matching key/values (regardless how many) are removed,
597 * before the new pair is written.
599 * Returns 0 on success.
601 * This function does this:
603 * - it locks the config file by creating ".git/config.lock"
605 * - it then parses the config using store_aux() as validator to find
606 * the position on the key/value pair to replace. If it is to be unset,
607 * it must be found exactly once.
609 * - the config file is mmap()ed and the part before the match (if any) is
610 * written to the lock file, then the changed part and the rest.
612 * - the config file is removed and the lock file rename()d to it.
615 int git_config_set_multivar(const char* key, const char* value,
616 const char* value_regex, int multi_replace)
618 int i, dot;
619 int fd = -1, in_fd;
620 int ret;
621 char* config_filename;
622 char* lock_file;
623 const char* last_dot = strrchr(key, '.');
625 config_filename = getenv(CONFIG_ENVIRONMENT);
626 if (!config_filename) {
627 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
628 if (!config_filename)
629 config_filename = git_path("config");
631 config_filename = xstrdup(config_filename);
632 lock_file = xstrdup(mkpath("%s.lock", config_filename));
635 * Since "key" actually contains the section name and the real
636 * key name separated by a dot, we have to know where the dot is.
639 if (last_dot == NULL) {
640 fprintf(stderr, "key does not contain a section: %s\n", key);
641 ret = 2;
642 goto out_free;
644 store.baselen = last_dot - key;
646 store.multi_replace = multi_replace;
649 * Validate the key and while at it, lower case it for matching.
651 store.key = xmalloc(strlen(key) + 1);
652 dot = 0;
653 for (i = 0; key[i]; i++) {
654 unsigned char c = key[i];
655 if (c == '.')
656 dot = 1;
657 /* Leave the extended basename untouched.. */
658 if (!dot || i > store.baselen) {
659 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
660 fprintf(stderr, "invalid key: %s\n", key);
661 free(store.key);
662 ret = 1;
663 goto out_free;
665 c = tolower(c);
666 } else if (c == '\n') {
667 fprintf(stderr, "invalid key (newline): %s\n", key);
668 free(store.key);
669 ret = 1;
670 goto out_free;
672 store.key[i] = c;
674 store.key[i] = 0;
677 * The lock_file serves a purpose in addition to locking: the new
678 * contents of .git/config will be written into it.
680 fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
681 if (fd < 0 || adjust_shared_perm(lock_file)) {
682 fprintf(stderr, "could not lock config file\n");
683 free(store.key);
684 ret = -1;
685 goto out_free;
689 * If .git/config does not exist yet, write a minimal version.
691 in_fd = open(config_filename, O_RDONLY);
692 if ( in_fd < 0 ) {
693 free(store.key);
695 if ( ENOENT != errno ) {
696 error("opening %s: %s", config_filename,
697 strerror(errno));
698 ret = 3; /* same as "invalid config file" */
699 goto out_free;
701 /* if nothing to unset, error out */
702 if (value == NULL) {
703 ret = 5;
704 goto out_free;
707 store.key = (char*)key;
708 if (!store_write_section(fd, key) ||
709 !store_write_pair(fd, key, value))
710 goto write_err_out;
711 } else {
712 struct stat st;
713 char* contents;
714 int i, copy_begin, copy_end, new_line = 0;
716 if (value_regex == NULL)
717 store.value_regex = NULL;
718 else {
719 if (value_regex[0] == '!') {
720 store.do_not_match = 1;
721 value_regex++;
722 } else
723 store.do_not_match = 0;
725 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
726 if (regcomp(store.value_regex, value_regex,
727 REG_EXTENDED)) {
728 fprintf(stderr, "Invalid pattern: %s\n",
729 value_regex);
730 free(store.value_regex);
731 ret = 6;
732 goto out_free;
736 store.offset[0] = 0;
737 store.state = START;
738 store.seen = 0;
741 * After this, store.offset will contain the *end* offset
742 * of the last match, or remain at 0 if no match was found.
743 * As a side effect, we make sure to transform only a valid
744 * existing config file.
746 if (git_config_from_file(store_aux, config_filename)) {
747 fprintf(stderr, "invalid config file\n");
748 free(store.key);
749 if (store.value_regex != NULL) {
750 regfree(store.value_regex);
751 free(store.value_regex);
753 ret = 3;
754 goto out_free;
757 free(store.key);
758 if (store.value_regex != NULL) {
759 regfree(store.value_regex);
760 free(store.value_regex);
763 /* if nothing to unset, or too many matches, error out */
764 if ((store.seen == 0 && value == NULL) ||
765 (store.seen > 1 && multi_replace == 0)) {
766 ret = 5;
767 goto out_free;
770 fstat(in_fd, &st);
771 contents = xmmap(NULL, st.st_size, PROT_READ,
772 MAP_PRIVATE, in_fd, 0);
773 close(in_fd);
775 if (store.seen == 0)
776 store.seen = 1;
778 for (i = 0, copy_begin = 0; i < store.seen; i++) {
779 if (store.offset[i] == 0) {
780 store.offset[i] = copy_end = st.st_size;
781 } else if (store.state != KEY_SEEN) {
782 copy_end = store.offset[i];
783 } else
784 copy_end = find_beginning_of_line(
785 contents, st.st_size,
786 store.offset[i]-2, &new_line);
788 /* write the first part of the config */
789 if (copy_end > copy_begin) {
790 if (write_in_full(fd, contents + copy_begin,
791 copy_end - copy_begin) <
792 copy_end - copy_begin)
793 goto write_err_out;
794 if (new_line &&
795 write_in_full(fd, "\n", 1) != 1)
796 goto write_err_out;
798 copy_begin = store.offset[i];
801 /* write the pair (value == NULL means unset) */
802 if (value != NULL) {
803 if (store.state == START) {
804 if (!store_write_section(fd, key))
805 goto write_err_out;
807 if (!store_write_pair(fd, key, value))
808 goto write_err_out;
811 /* write the rest of the config */
812 if (copy_begin < st.st_size)
813 if (write_in_full(fd, contents + copy_begin,
814 st.st_size - copy_begin) <
815 st.st_size - copy_begin)
816 goto write_err_out;
818 munmap(contents, st.st_size);
819 unlink(config_filename);
822 if (rename(lock_file, config_filename) < 0) {
823 fprintf(stderr, "Could not rename the lock file?\n");
824 ret = 4;
825 goto out_free;
828 ret = 0;
830 out_free:
831 if (0 <= fd)
832 close(fd);
833 free(config_filename);
834 if (lock_file) {
835 unlink(lock_file);
836 free(lock_file);
838 return ret;
840 write_err_out:
841 ret = write_error();
842 goto out_free;
846 int git_config_rename_section(const char *old_name, const char *new_name)
848 int ret = 0;
849 char *config_filename;
850 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
851 int out_fd;
852 char buf[1024];
854 config_filename = getenv(CONFIG_ENVIRONMENT);
855 if (!config_filename) {
856 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
857 if (!config_filename)
858 config_filename = git_path("config");
860 config_filename = xstrdup(config_filename);
861 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
862 if (out_fd < 0) {
863 ret = error("Could not lock config file!");
864 goto out;
867 if (!(config_file = fopen(config_filename, "rb"))) {
868 ret = error("Could not open config file!");
869 goto out;
872 while (fgets(buf, sizeof(buf), config_file)) {
873 int i;
874 int length;
875 for (i = 0; buf[i] && isspace(buf[i]); i++)
876 ; /* do nothing */
877 if (buf[i] == '[') {
878 /* it's a section */
879 int j = 0, dot = 0;
880 for (i++; buf[i] && buf[i] != ']'; i++) {
881 if (!dot && isspace(buf[i])) {
882 dot = 1;
883 if (old_name[j++] != '.')
884 break;
885 for (i++; isspace(buf[i]); i++)
886 ; /* do nothing */
887 if (buf[i] != '"')
888 break;
889 continue;
891 if (buf[i] == '\\' && dot)
892 i++;
893 else if (buf[i] == '"' && dot) {
894 for (i++; isspace(buf[i]); i++)
895 ; /* do_nothing */
896 break;
898 if (buf[i] != old_name[j++])
899 break;
901 if (buf[i] == ']' && old_name[j] == 0) {
902 /* old_name matches */
903 ret++;
904 store.baselen = strlen(new_name);
905 if (!store_write_section(out_fd, new_name)) {
906 ret = write_error();
907 goto out;
909 continue;
912 length = strlen(buf);
913 if (write_in_full(out_fd, buf, length) != length) {
914 ret = write_error();
915 goto out;
918 fclose(config_file);
919 if (close(out_fd) || commit_lock_file(lock) < 0)
920 ret = error("Cannot commit config file!");
921 out:
922 free(config_filename);
923 return ret;