2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
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)
23 if ((f = config_file) != NULL) {
26 /* DOS like systems */
43 static char *parse_value(void)
45 static char value[1024];
46 int quote = 0, comment = 0, len = 0, space = 0;
49 int c = get_next_char();
50 if (len >= sizeof(value))
60 if (isspace(c) && !quote) {
65 if (c == ';' || c == '#') {
89 /* Some characters escape as themselves */
92 /* Reject unknown escape sequences */
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)
117 /* Get the full name */
124 name[len++] = tolower(c);
129 while (c == ' ' || c == '\t')
136 value = parse_value();
140 return fn(name, value);
143 static int get_extended_base_var(char *name, int baselen, int c)
149 } while (isspace(c));
151 /* We require the format to be '[base "extension"]' */
154 name[baselen++] = '.';
157 int c = get_next_char();
168 if (baselen > MAXNAME / 2)
173 if (get_next_char() != ']')
178 static int get_base_var(char *name)
183 int c = get_next_char();
189 return get_extended_base_var(name, baselen, c);
190 if (!iskeychar(c) && c != '.')
192 if (baselen > MAXNAME / 2)
194 name[baselen++] = tolower(c);
198 static int git_parse_file(config_fn_t fn)
202 static char var[MAXNAME];
205 int c = get_next_char();
213 if (comment || isspace(c))
215 if (c == '#' || c == ';') {
220 baselen = get_base_var(var);
223 var[baselen++] = '.';
229 var[baselen] = tolower(c);
230 if (get_value(fn, var, baselen+1) < 0)
233 die("bad config file line %d in %s", config_linenr, config_file_name);
236 static unsigned long get_unit_factor(const char *end)
240 else if (!strcasecmp(end, "k"))
242 else if (!strcasecmp(end, "m"))
244 else if (!strcasecmp(end, "g"))
245 return 1024 * 1024 * 1024;
246 die("unknown unit: '%s'", end);
249 static struct whitespace_rule {
250 const char *rule_name;
252 } whitespace_rule_names[] = {
253 { "trailing-space", WS_TRAILING_SPACE },
254 { "space-before-tab", WS_SPACE_BEFORE_TAB },
257 static unsigned parse_whitespace_rule(const char *string)
259 unsigned rule = WS_DEFAULT_RULE;
267 string = string + strspn(string, ", \t\n\r");
268 ep = strchr(string, ',');
270 len = strlen(string);
274 if (*string == '-') {
281 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
282 if (strncmp(whitespace_rule_names[i].rule_name,
286 rule &= ~whitespace_rule_names[i].rule_bits;
288 rule |= whitespace_rule_names[i].rule_bits;
296 int git_parse_long(const char *value, long *ret)
298 if (value && *value) {
300 long val = strtol(value, &end, 0);
301 *ret = val * get_unit_factor(end);
307 int git_parse_ulong(const char *value, unsigned long *ret)
309 if (value && *value) {
311 unsigned long val = strtoul(value, &end, 0);
312 *ret = val * get_unit_factor(end);
318 int git_config_int(const char *name, const char *value)
321 if (!git_parse_long(value, &ret))
322 die("bad config value for '%s' in %s", name, config_file_name);
326 unsigned long git_config_ulong(const char *name, const char *value)
329 if (!git_parse_ulong(value, &ret))
330 die("bad config value for '%s' in %s", name, config_file_name);
334 int git_config_bool(const char *name, const char *value)
340 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
342 if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
344 return git_config_int(name, value) != 0;
347 int git_default_config(const char *var, const char *value)
349 /* This needs a better name */
350 if (!strcmp(var, "core.filemode")) {
351 trust_executable_bit = git_config_bool(var, value);
355 if (!strcmp(var, "core.quotepath")) {
356 quote_path_fully = git_config_bool(var, value);
360 if (!strcmp(var, "core.symlinks")) {
361 has_symlinks = git_config_bool(var, value);
365 if (!strcmp(var, "core.bare")) {
366 is_bare_repository_cfg = git_config_bool(var, value);
370 if (!strcmp(var, "core.ignorestat")) {
371 assume_unchanged = git_config_bool(var, value);
375 if (!strcmp(var, "core.prefersymlinkrefs")) {
376 prefer_symlink_refs = git_config_bool(var, value);
380 if (!strcmp(var, "core.logallrefupdates")) {
381 log_all_ref_updates = git_config_bool(var, value);
385 if (!strcmp(var, "core.warnambiguousrefs")) {
386 warn_ambiguous_refs = git_config_bool(var, value);
390 if (!strcmp(var, "core.loosecompression")) {
391 int level = git_config_int(var, value);
393 level = Z_DEFAULT_COMPRESSION;
394 else if (level < 0 || level > Z_BEST_COMPRESSION)
395 die("bad zlib compression level %d", level);
396 zlib_compression_level = level;
397 zlib_compression_seen = 1;
401 if (!strcmp(var, "core.compression")) {
402 int level = git_config_int(var, value);
404 level = Z_DEFAULT_COMPRESSION;
405 else if (level < 0 || level > Z_BEST_COMPRESSION)
406 die("bad zlib compression level %d", level);
407 core_compression_level = level;
408 core_compression_seen = 1;
409 if (!zlib_compression_seen)
410 zlib_compression_level = level;
414 if (!strcmp(var, "core.packedgitwindowsize")) {
415 int pgsz_x2 = getpagesize() * 2;
416 packed_git_window_size = git_config_int(var, value);
418 /* This value must be multiple of (pagesize * 2) */
419 packed_git_window_size /= pgsz_x2;
420 if (packed_git_window_size < 1)
421 packed_git_window_size = 1;
422 packed_git_window_size *= pgsz_x2;
426 if (!strcmp(var, "core.packedgitlimit")) {
427 packed_git_limit = git_config_int(var, value);
431 if (!strcmp(var, "core.deltabasecachelimit")) {
432 delta_base_cache_limit = git_config_int(var, value);
436 if (!strcmp(var, "core.autocrlf")) {
437 if (value && !strcasecmp(value, "input")) {
441 auto_crlf = git_config_bool(var, value);
445 if (!strcmp(var, "user.name")) {
446 strlcpy(git_default_name, value, sizeof(git_default_name));
450 if (!strcmp(var, "user.email")) {
451 strlcpy(git_default_email, value, sizeof(git_default_email));
455 if (!strcmp(var, "i18n.commitencoding")) {
456 git_commit_encoding = xstrdup(value);
460 if (!strcmp(var, "i18n.logoutputencoding")) {
461 git_log_output_encoding = xstrdup(value);
466 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
467 pager_use_color = git_config_bool(var,value);
471 if (!strcmp(var, "core.pager")) {
472 pager_program = xstrdup(value);
476 if (!strcmp(var, "core.editor")) {
477 editor_program = xstrdup(value);
481 if (!strcmp(var, "core.whitespace")) {
482 whitespace_rule = parse_whitespace_rule(value);
486 /* Add other config variables here and to Documentation/config.txt. */
490 int git_config_from_file(config_fn_t fn, const char *filename)
493 FILE *f = fopen(filename, "r");
498 config_file_name = filename;
500 ret = git_parse_file(fn);
502 config_file_name = NULL;
507 int git_config(config_fn_t fn)
510 char *repo_config = NULL;
511 const char *home = NULL, *filename;
513 /* $GIT_CONFIG makes git read _only_ the given config file,
514 * $GIT_CONFIG_LOCAL will make it process it in addition to the
515 * global config file, the same way it would the per-repository
516 * config file otherwise. */
517 filename = getenv(CONFIG_ENVIRONMENT);
519 if (!access(ETC_GITCONFIG, R_OK))
520 ret += git_config_from_file(fn, ETC_GITCONFIG);
521 home = getenv("HOME");
522 filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
524 filename = repo_config = xstrdup(git_path("config"));
528 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
529 if (!access(user_config, R_OK))
530 ret = git_config_from_file(fn, user_config);
534 ret += git_config_from_file(fn, filename);
540 * Find all the stuff for git_config_set() below.
543 #define MAX_MATCHES 512
549 regex_t* value_regex;
551 size_t offset[MAX_MATCHES];
552 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
556 static int matches(const char* key, const char* value)
558 return !strcmp(key, store.key) &&
559 (store.value_regex == NULL ||
560 (store.do_not_match ^
561 !regexec(store.value_regex, value, 0, NULL, 0)));
564 static int store_aux(const char* key, const char* value)
569 switch (store.state) {
571 if (matches(key, value)) {
572 if (store.seen == 1 && store.multi_replace == 0) {
574 "Warning: %s has multiple values\n",
576 } else if (store.seen >= MAX_MATCHES) {
577 fprintf(stderr, "Too many matches\n");
581 store.offset[store.seen] = ftell(config_file);
587 * What we are looking for is in store.key (both
588 * section and var), and its section part is baselen
589 * long. We found key (again, both section and var).
590 * We would want to know if this key is in the same
591 * section as what we are looking for. We already
592 * know we are in the same section as what should
595 ep = strrchr(key, '.');
596 section_len = ep - key;
598 if ((section_len != store.baselen) ||
599 memcmp(key, store.key, section_len+1)) {
600 store.state = SECTION_END_SEEN;
605 * Do not increment matches: this is no match, but we
606 * just made sure we are in the desired section.
608 store.offset[store.seen] = ftell(config_file);
610 case SECTION_END_SEEN:
612 if (matches(key, value)) {
613 store.offset[store.seen] = ftell(config_file);
614 store.state = KEY_SEEN;
617 if (strrchr(key, '.') - key == store.baselen &&
618 !strncmp(key, store.key, store.baselen)) {
619 store.state = SECTION_SEEN;
620 store.offset[store.seen] = ftell(config_file);
627 static int write_error(void)
629 fprintf(stderr, "Failed to write new configuration file\n");
631 /* Same error code as "failed to rename". */
635 static int store_write_section(int fd, const char* key)
637 const char *dot = strchr(key, '.');
638 int len1 = store.baselen, len2 = -1;
640 dot = strchr(key, '.');
642 int dotlen = dot - key;
644 len2 = len1 - dotlen - 1;
649 if (write_in_full(fd, "[", 1) != 1 ||
650 write_in_full(fd, key, len1) != len1)
653 if (write_in_full(fd, " \"", 2) != 2)
655 while (--len2 >= 0) {
656 unsigned char c = *++dot;
658 if (write_in_full(fd, "\\", 1) != 1)
660 if (write_in_full(fd, &c, 1) != 1)
663 if (write_in_full(fd, "\"", 1) != 1)
666 if (write_in_full(fd, "]\n", 2) != 2)
672 static int store_write_pair(int fd, const char* key, const char* value)
675 int length = strlen(key+store.baselen+1);
678 /* Check to see if the value needs to be quoted. */
681 for (i = 0; value[i]; i++)
682 if (value[i] == ';' || value[i] == '#')
684 if (value[i-1] == ' ')
687 if (write_in_full(fd, "\t", 1) != 1 ||
688 write_in_full(fd, key+store.baselen+1, length) != length ||
689 write_in_full(fd, " = ", 3) != 3)
691 if (quote && write_in_full(fd, "\"", 1) != 1)
693 for (i = 0; value[i]; i++)
696 if (write_in_full(fd, "\\n", 2) != 2)
700 if (write_in_full(fd, "\\t", 2) != 2)
705 if (write_in_full(fd, "\\", 1) != 1)
708 if (write_in_full(fd, value+i, 1) != 1)
712 if (quote && write_in_full(fd, "\"", 1) != 1)
714 if (write_in_full(fd, "\n", 1) != 1)
719 static ssize_t find_beginning_of_line(const char* contents, size_t size,
720 size_t offset_, int* found_bracket)
722 size_t equal_offset = size, bracket_offset = size;
725 for (offset = offset_-2; offset > 0
726 && contents[offset] != '\n'; offset--)
727 switch (contents[offset]) {
728 case '=': equal_offset = offset; break;
729 case ']': bracket_offset = offset; break;
731 if (bracket_offset < equal_offset) {
733 offset = bracket_offset+1;
740 int git_config_set(const char* key, const char* value)
742 return git_config_set_multivar(key, value, NULL, 0);
746 * If value==NULL, unset in (remove from) config,
747 * if value_regex!=NULL, disregard key/value pairs where value does not match.
748 * if multi_replace==0, nothing, or only one matching key/value is replaced,
749 * else all matching key/values (regardless how many) are removed,
750 * before the new pair is written.
752 * Returns 0 on success.
754 * This function does this:
756 * - it locks the config file by creating ".git/config.lock"
758 * - it then parses the config using store_aux() as validator to find
759 * the position on the key/value pair to replace. If it is to be unset,
760 * it must be found exactly once.
762 * - the config file is mmap()ed and the part before the match (if any) is
763 * written to the lock file, then the changed part and the rest.
765 * - the config file is removed and the lock file rename()d to it.
768 int git_config_set_multivar(const char* key, const char* value,
769 const char* value_regex, int multi_replace)
774 char* config_filename;
775 struct lock_file *lock = NULL;
776 const char* last_dot = strrchr(key, '.');
778 config_filename = getenv(CONFIG_ENVIRONMENT);
779 if (!config_filename) {
780 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
781 if (!config_filename)
782 config_filename = git_path("config");
784 config_filename = xstrdup(config_filename);
787 * Since "key" actually contains the section name and the real
788 * key name separated by a dot, we have to know where the dot is.
791 if (last_dot == NULL) {
792 fprintf(stderr, "key does not contain a section: %s\n", key);
796 store.baselen = last_dot - key;
798 store.multi_replace = multi_replace;
801 * Validate the key and while at it, lower case it for matching.
803 store.key = xmalloc(strlen(key) + 1);
805 for (i = 0; key[i]; i++) {
806 unsigned char c = key[i];
809 /* Leave the extended basename untouched.. */
810 if (!dot || i > store.baselen) {
811 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
812 fprintf(stderr, "invalid key: %s\n", key);
818 } else if (c == '\n') {
819 fprintf(stderr, "invalid key (newline): %s\n", key);
829 * The lock serves a purpose in addition to locking: the new
830 * contents of .git/config will be written into it.
832 lock = xcalloc(sizeof(struct lock_file), 1);
833 fd = hold_lock_file_for_update(lock, config_filename, 0);
835 fprintf(stderr, "could not lock config file\n");
842 * If .git/config does not exist yet, write a minimal version.
844 in_fd = open(config_filename, O_RDONLY);
848 if ( ENOENT != errno ) {
849 error("opening %s: %s", config_filename,
851 ret = 3; /* same as "invalid config file" */
854 /* if nothing to unset, error out */
860 store.key = (char*)key;
861 if (!store_write_section(fd, key) ||
862 !store_write_pair(fd, key, value))
867 size_t contents_sz, copy_begin, copy_end;
870 if (value_regex == NULL)
871 store.value_regex = NULL;
873 if (value_regex[0] == '!') {
874 store.do_not_match = 1;
877 store.do_not_match = 0;
879 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
880 if (regcomp(store.value_regex, value_regex,
882 fprintf(stderr, "Invalid pattern: %s\n",
884 free(store.value_regex);
895 * After this, store.offset will contain the *end* offset
896 * of the last match, or remain at 0 if no match was found.
897 * As a side effect, we make sure to transform only a valid
898 * existing config file.
900 if (git_config_from_file(store_aux, config_filename)) {
901 fprintf(stderr, "invalid config file\n");
903 if (store.value_regex != NULL) {
904 regfree(store.value_regex);
905 free(store.value_regex);
912 if (store.value_regex != NULL) {
913 regfree(store.value_regex);
914 free(store.value_regex);
917 /* if nothing to unset, or too many matches, error out */
918 if ((store.seen == 0 && value == NULL) ||
919 (store.seen > 1 && multi_replace == 0)) {
925 contents_sz = xsize_t(st.st_size);
926 contents = xmmap(NULL, contents_sz, PROT_READ,
927 MAP_PRIVATE, in_fd, 0);
933 for (i = 0, copy_begin = 0; i < store.seen; i++) {
934 if (store.offset[i] == 0) {
935 store.offset[i] = copy_end = contents_sz;
936 } else if (store.state != KEY_SEEN) {
937 copy_end = store.offset[i];
939 copy_end = find_beginning_of_line(
940 contents, contents_sz,
941 store.offset[i]-2, &new_line);
943 /* write the first part of the config */
944 if (copy_end > copy_begin) {
945 if (write_in_full(fd, contents + copy_begin,
946 copy_end - copy_begin) <
947 copy_end - copy_begin)
950 write_in_full(fd, "\n", 1) != 1)
953 copy_begin = store.offset[i];
956 /* write the pair (value == NULL means unset) */
958 if (store.state == START) {
959 if (!store_write_section(fd, key))
962 if (!store_write_pair(fd, key, value))
966 /* write the rest of the config */
967 if (copy_begin < contents_sz)
968 if (write_in_full(fd, contents + copy_begin,
969 contents_sz - copy_begin) <
970 contents_sz - copy_begin)
973 munmap(contents, contents_sz);
976 if (close(fd) || commit_lock_file(lock) < 0) {
977 fprintf(stderr, "Cannot commit config file!\n");
982 /* fd is closed, so don't try to close it below. */
985 * lock is committed, so don't try to roll it back below.
986 * NOTE: Since lockfile.c keeps a linked list of all created
987 * lock_file structures, it isn't safe to free(lock). It's
988 * better to just leave it hanging around.
997 rollback_lock_file(lock);
998 free(config_filename);
1002 ret = write_error();
1007 static int section_name_match (const char *buf, const char *name)
1009 int i = 0, j = 0, dot = 0;
1010 for (; buf[i] && buf[i] != ']'; i++) {
1011 if (!dot && isspace(buf[i])) {
1013 if (name[j++] != '.')
1015 for (i++; isspace(buf[i]); i++)
1021 if (buf[i] == '\\' && dot)
1023 else if (buf[i] == '"' && dot) {
1024 for (i++; isspace(buf[i]); i++)
1028 if (buf[i] != name[j++])
1031 return (buf[i] == ']' && name[j] == 0);
1034 /* if new_name == NULL, the section is removed instead */
1035 int git_config_rename_section(const char *old_name, const char *new_name)
1037 int ret = 0, remove = 0;
1038 char *config_filename;
1039 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
1043 config_filename = getenv(CONFIG_ENVIRONMENT);
1044 if (!config_filename) {
1045 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
1046 if (!config_filename)
1047 config_filename = git_path("config");
1049 config_filename = xstrdup(config_filename);
1050 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
1052 ret = error("Could not lock config file!");
1056 if (!(config_file = fopen(config_filename, "rb"))) {
1057 /* no config file means nothing to rename, no error */
1058 goto unlock_and_out;
1061 while (fgets(buf, sizeof(buf), config_file)) {
1064 for (i = 0; buf[i] && isspace(buf[i]); i++)
1066 if (buf[i] == '[') {
1067 /* it's a section */
1068 if (section_name_match (&buf[i+1], old_name)) {
1070 if (new_name == NULL) {
1074 store.baselen = strlen(new_name);
1075 if (!store_write_section(out_fd, new_name)) {
1076 ret = write_error();
1085 length = strlen(buf);
1086 if (write_in_full(out_fd, buf, length) != length) {
1087 ret = write_error();
1091 fclose(config_file);
1093 if (close(out_fd) || commit_lock_file(lock) < 0)
1094 ret = error("Cannot commit config file!");
1096 free(config_filename);