2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
13 static FILE *config_file;
14 static const char *config_file_name;
15 static int config_linenr;
16 static int config_file_eof;
17 static int zlib_compression_seen;
19 const char *config_exclusive_filename = NULL;
21 static int get_next_char(void)
27 if ((f = config_file) != NULL) {
30 /* DOS like systems */
47 static char *parse_value(void)
49 static char value[1024];
50 int quote = 0, comment = 0, len = 0, space = 0;
53 int c = get_next_char();
54 if (len >= sizeof(value))
64 if (isspace(c) && !quote) {
69 if (c == ';' || c == '#') {
93 /* Some characters escape as themselves */
96 /* Reject unknown escape sequences */
111 static inline int iskeychar(int c)
113 return isalnum(c) || c == '-';
116 static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
121 /* Get the full name */
128 name[len++] = tolower(c);
133 while (c == ' ' || c == '\t')
140 value = parse_value();
144 return fn(name, value, data);
147 static int get_extended_base_var(char *name, int baselen, int c)
153 } while (isspace(c));
155 /* We require the format to be '[base "extension"]' */
158 name[baselen++] = '.';
161 int c = get_next_char();
172 if (baselen > MAXNAME / 2)
177 if (get_next_char() != ']')
182 static int get_base_var(char *name)
187 int c = get_next_char();
193 return get_extended_base_var(name, baselen, c);
194 if (!iskeychar(c) && c != '.')
196 if (baselen > MAXNAME / 2)
198 name[baselen++] = tolower(c);
202 static int git_parse_file(config_fn_t fn, void *data)
206 static char var[MAXNAME];
209 int c = get_next_char();
216 if (comment || isspace(c))
218 if (c == '#' || c == ';') {
223 baselen = get_base_var(var);
226 var[baselen++] = '.';
232 var[baselen] = tolower(c);
233 if (get_value(fn, data, var, baselen+1) < 0)
236 die("bad config file line %d in %s", config_linenr, config_file_name);
239 static int parse_unit_factor(const char *end, unsigned long *val)
243 else if (!strcasecmp(end, "k")) {
247 else if (!strcasecmp(end, "m")) {
251 else if (!strcasecmp(end, "g")) {
252 *val *= 1024 * 1024 * 1024;
258 int git_parse_long(const char *value, long *ret)
260 if (value && *value) {
262 long val = strtol(value, &end, 0);
263 unsigned long factor = 1;
264 if (!parse_unit_factor(end, &factor))
272 int git_parse_ulong(const char *value, unsigned long *ret)
274 if (value && *value) {
276 unsigned long val = strtoul(value, &end, 0);
277 if (!parse_unit_factor(end, &val))
285 static void die_bad_config(const char *name)
287 if (config_file_name)
288 die("bad config value for '%s' in %s", name, config_file_name);
289 die("bad config value for '%s'", name);
292 int git_config_int(const char *name, const char *value)
295 if (!git_parse_long(value, &ret))
296 die_bad_config(name);
300 unsigned long git_config_ulong(const char *name, const char *value)
303 if (!git_parse_ulong(value, &ret))
304 die_bad_config(name);
308 int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
315 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
317 if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
320 return git_config_int(name, value);
323 int git_config_bool(const char *name, const char *value)
326 return !!git_config_bool_or_int(name, value, &discard);
329 int git_config_string(const char **dest, const char *var, const char *value)
332 return config_error_nonbool(var);
333 *dest = xstrdup(value);
337 static int git_default_core_config(const char *var, const char *value)
339 /* This needs a better name */
340 if (!strcmp(var, "core.filemode")) {
341 trust_executable_bit = git_config_bool(var, value);
345 if (!strcmp(var, "core.quotepath")) {
346 quote_path_fully = git_config_bool(var, value);
350 if (!strcmp(var, "core.symlinks")) {
351 has_symlinks = git_config_bool(var, value);
355 if (!strcmp(var, "core.ignorecase")) {
356 ignore_case = git_config_bool(var, value);
360 if (!strcmp(var, "core.bare")) {
361 is_bare_repository_cfg = git_config_bool(var, value);
365 if (!strcmp(var, "core.ignorestat")) {
366 assume_unchanged = git_config_bool(var, value);
370 if (!strcmp(var, "core.prefersymlinkrefs")) {
371 prefer_symlink_refs = git_config_bool(var, value);
375 if (!strcmp(var, "core.logallrefupdates")) {
376 log_all_ref_updates = git_config_bool(var, value);
380 if (!strcmp(var, "core.warnambiguousrefs")) {
381 warn_ambiguous_refs = git_config_bool(var, value);
385 if (!strcmp(var, "core.loosecompression")) {
386 int level = git_config_int(var, value);
388 level = Z_DEFAULT_COMPRESSION;
389 else if (level < 0 || level > Z_BEST_COMPRESSION)
390 die("bad zlib compression level %d", level);
391 zlib_compression_level = level;
392 zlib_compression_seen = 1;
396 if (!strcmp(var, "core.compression")) {
397 int level = git_config_int(var, value);
399 level = Z_DEFAULT_COMPRESSION;
400 else if (level < 0 || level > Z_BEST_COMPRESSION)
401 die("bad zlib compression level %d", level);
402 core_compression_level = level;
403 core_compression_seen = 1;
404 if (!zlib_compression_seen)
405 zlib_compression_level = level;
409 if (!strcmp(var, "core.packedgitwindowsize")) {
410 int pgsz_x2 = getpagesize() * 2;
411 packed_git_window_size = git_config_int(var, value);
413 /* This value must be multiple of (pagesize * 2) */
414 packed_git_window_size /= pgsz_x2;
415 if (packed_git_window_size < 1)
416 packed_git_window_size = 1;
417 packed_git_window_size *= pgsz_x2;
421 if (!strcmp(var, "core.packedgitlimit")) {
422 packed_git_limit = git_config_int(var, value);
426 if (!strcmp(var, "core.deltabasecachelimit")) {
427 delta_base_cache_limit = git_config_int(var, value);
431 if (!strcmp(var, "core.autocrlf")) {
432 if (value && !strcasecmp(value, "input")) {
436 auto_crlf = git_config_bool(var, value);
440 if (!strcmp(var, "core.safecrlf")) {
441 if (value && !strcasecmp(value, "warn")) {
442 safe_crlf = SAFE_CRLF_WARN;
445 safe_crlf = git_config_bool(var, value);
449 if (!strcmp(var, "core.pager"))
450 return git_config_string(&pager_program, var, value);
452 if (!strcmp(var, "core.editor"))
453 return git_config_string(&editor_program, var, value);
455 if (!strcmp(var, "core.excludesfile"))
456 return git_config_string(&excludes_file, var, value);
458 if (!strcmp(var, "core.whitespace")) {
460 return config_error_nonbool(var);
461 whitespace_rule_cfg = parse_whitespace_rule(value);
465 if (!strcmp(var, "core.fsyncobjectfiles")) {
466 fsync_object_files = git_config_bool(var, value);
470 /* Add other config variables here and to Documentation/config.txt. */
474 static int git_default_user_config(const char *var, const char *value)
476 if (!strcmp(var, "user.name")) {
478 return config_error_nonbool(var);
479 strlcpy(git_default_name, value, sizeof(git_default_name));
480 if (git_default_email[0])
481 user_ident_explicitly_given = 1;
485 if (!strcmp(var, "user.email")) {
487 return config_error_nonbool(var);
488 strlcpy(git_default_email, value, sizeof(git_default_email));
489 if (git_default_name[0])
490 user_ident_explicitly_given = 1;
494 /* Add other config variables here and to Documentation/config.txt. */
498 static int git_default_i18n_config(const char *var, const char *value)
500 if (!strcmp(var, "i18n.commitencoding"))
501 return git_config_string(&git_commit_encoding, var, value);
503 if (!strcmp(var, "i18n.logoutputencoding"))
504 return git_config_string(&git_log_output_encoding, var, value);
506 /* Add other config variables here and to Documentation/config.txt. */
510 static int git_default_branch_config(const char *var, const char *value)
512 if (!strcmp(var, "branch.autosetupmerge")) {
513 if (value && !strcasecmp(value, "always")) {
514 git_branch_track = BRANCH_TRACK_ALWAYS;
517 git_branch_track = git_config_bool(var, value);
520 if (!strcmp(var, "branch.autosetuprebase")) {
522 return config_error_nonbool(var);
523 else if (!strcmp(value, "never"))
524 autorebase = AUTOREBASE_NEVER;
525 else if (!strcmp(value, "local"))
526 autorebase = AUTOREBASE_LOCAL;
527 else if (!strcmp(value, "remote"))
528 autorebase = AUTOREBASE_REMOTE;
529 else if (!strcmp(value, "always"))
530 autorebase = AUTOREBASE_ALWAYS;
532 return error("Malformed value for %s", var);
536 /* Add other config variables here and to Documentation/config.txt. */
540 int git_default_config(const char *var, const char *value, void *dummy)
542 if (!prefixcmp(var, "core."))
543 return git_default_core_config(var, value);
545 if (!prefixcmp(var, "user."))
546 return git_default_user_config(var, value);
548 if (!prefixcmp(var, "i18n."))
549 return git_default_i18n_config(var, value);
551 if (!prefixcmp(var, "branch."))
552 return git_default_branch_config(var, value);
554 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
555 pager_use_color = git_config_bool(var,value);
559 /* Add other config variables here and to Documentation/config.txt. */
563 int git_config_from_file(config_fn_t fn, const char *filename, void *data)
566 FILE *f = fopen(filename, "r");
571 config_file_name = filename;
574 ret = git_parse_file(fn, data);
576 config_file_name = NULL;
581 const char *git_etc_gitconfig(void)
583 static const char *system_wide;
585 system_wide = ETC_GITCONFIG;
586 if (!is_absolute_path(system_wide)) {
587 /* interpret path relative to exec-dir */
588 struct strbuf d = STRBUF_INIT;
589 strbuf_addf(&d, "%s/%s", git_exec_path(), system_wide);
590 system_wide = strbuf_detach(&d, NULL);
596 static int git_env_bool(const char *k, int def)
598 const char *v = getenv(k);
599 return v ? git_config_bool(k, v) : def;
602 int git_config_system(void)
604 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
607 int git_config_global(void)
609 return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
612 int git_config(config_fn_t fn, void *data)
615 char *repo_config = NULL;
616 const char *home = NULL;
618 /* $GIT_CONFIG makes git read _only_ the given config file,
619 * $GIT_CONFIG_LOCAL will make it process it in addition to the
620 * global config file, the same way it would the per-repository
621 * config file otherwise. */
622 if (config_exclusive_filename)
623 return git_config_from_file(fn, config_exclusive_filename, data);
624 if (git_config_system() && !access(git_etc_gitconfig(), R_OK))
625 ret += git_config_from_file(fn, git_etc_gitconfig(),
628 home = getenv("HOME");
629 if (git_config_global() && home) {
630 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
631 if (!access(user_config, R_OK))
632 ret += git_config_from_file(fn, user_config, data);
636 repo_config = xstrdup(git_path("config"));
637 ret += git_config_from_file(fn, repo_config, data);
643 * Find all the stuff for git_config_set() below.
646 #define MAX_MATCHES 512
652 regex_t* value_regex;
654 size_t offset[MAX_MATCHES];
655 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
659 static int matches(const char* key, const char* value)
661 return !strcmp(key, store.key) &&
662 (store.value_regex == NULL ||
663 (store.do_not_match ^
664 !regexec(store.value_regex, value, 0, NULL, 0)));
667 static int store_aux(const char* key, const char* value, void *cb)
672 switch (store.state) {
674 if (matches(key, value)) {
675 if (store.seen == 1 && store.multi_replace == 0) {
676 warning("%s has multiple values", key);
677 } else if (store.seen >= MAX_MATCHES) {
678 error("too many matches for %s", key);
682 store.offset[store.seen] = ftell(config_file);
688 * What we are looking for is in store.key (both
689 * section and var), and its section part is baselen
690 * long. We found key (again, both section and var).
691 * We would want to know if this key is in the same
692 * section as what we are looking for. We already
693 * know we are in the same section as what should
696 ep = strrchr(key, '.');
697 section_len = ep - key;
699 if ((section_len != store.baselen) ||
700 memcmp(key, store.key, section_len+1)) {
701 store.state = SECTION_END_SEEN;
706 * Do not increment matches: this is no match, but we
707 * just made sure we are in the desired section.
709 store.offset[store.seen] = ftell(config_file);
711 case SECTION_END_SEEN:
713 if (matches(key, value)) {
714 store.offset[store.seen] = ftell(config_file);
715 store.state = KEY_SEEN;
718 if (strrchr(key, '.') - key == store.baselen &&
719 !strncmp(key, store.key, store.baselen)) {
720 store.state = SECTION_SEEN;
721 store.offset[store.seen] = ftell(config_file);
728 static int write_error(const char *filename)
730 error("failed to write new configuration file %s", filename);
732 /* Same error code as "failed to rename". */
736 static int store_write_section(int fd, const char* key)
743 dot = memchr(key, '.', store.baselen);
745 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
746 for (i = dot - key + 1; i < store.baselen; i++) {
747 if (key[i] == '"' || key[i] == '\\')
748 strbuf_addch(&sb, '\\');
749 strbuf_addch(&sb, key[i]);
751 strbuf_addstr(&sb, "\"]\n");
753 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
756 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
762 static int store_write_pair(int fd, const char* key, const char* value)
765 int length = strlen(key + store.baselen + 1);
766 const char *quote = "";
770 * Check to see if the value needs to be surrounded with a dq pair.
771 * Note that problematic characters are always backslash-quoted; this
772 * check is about not losing leading or trailing SP and strings that
773 * follow beginning-of-comment characters (i.e. ';' and '#') by the
774 * configuration parser.
778 for (i = 0; value[i]; i++)
779 if (value[i] == ';' || value[i] == '#')
781 if (i && value[i - 1] == ' ')
785 strbuf_addf(&sb, "\t%.*s = %s",
786 length, key + store.baselen + 1, quote);
788 for (i = 0; value[i]; i++)
791 strbuf_addstr(&sb, "\\n");
794 strbuf_addstr(&sb, "\\t");
798 strbuf_addch(&sb, '\\');
800 strbuf_addch(&sb, value[i]);
803 strbuf_addf(&sb, "%s\n", quote);
805 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
811 static ssize_t find_beginning_of_line(const char* contents, size_t size,
812 size_t offset_, int* found_bracket)
814 size_t equal_offset = size, bracket_offset = size;
818 for (offset = offset_-2; offset > 0
819 && contents[offset] != '\n'; offset--)
820 switch (contents[offset]) {
821 case '=': equal_offset = offset; break;
822 case ']': bracket_offset = offset; break;
824 if (offset > 0 && contents[offset-1] == '\\') {
828 if (bracket_offset < equal_offset) {
830 offset = bracket_offset+1;
837 int git_config_set(const char* key, const char* value)
839 return git_config_set_multivar(key, value, NULL, 0);
843 * If value==NULL, unset in (remove from) config,
844 * if value_regex!=NULL, disregard key/value pairs where value does not match.
845 * if multi_replace==0, nothing, or only one matching key/value is replaced,
846 * else all matching key/values (regardless how many) are removed,
847 * before the new pair is written.
849 * Returns 0 on success.
851 * This function does this:
853 * - it locks the config file by creating ".git/config.lock"
855 * - it then parses the config using store_aux() as validator to find
856 * the position on the key/value pair to replace. If it is to be unset,
857 * it must be found exactly once.
859 * - the config file is mmap()ed and the part before the match (if any) is
860 * written to the lock file, then the changed part and the rest.
862 * - the config file is removed and the lock file rename()d to it.
865 int git_config_set_multivar(const char* key, const char* value,
866 const char* value_regex, int multi_replace)
871 char* config_filename;
872 struct lock_file *lock = NULL;
873 const char* last_dot = strrchr(key, '.');
875 if (config_exclusive_filename)
876 config_filename = xstrdup(config_exclusive_filename);
878 config_filename = xstrdup(git_path("config"));
881 * Since "key" actually contains the section name and the real
882 * key name separated by a dot, we have to know where the dot is.
885 if (last_dot == NULL) {
886 error("key does not contain a section: %s", key);
890 store.baselen = last_dot - key;
892 store.multi_replace = multi_replace;
895 * Validate the key and while at it, lower case it for matching.
897 store.key = xmalloc(strlen(key) + 1);
899 for (i = 0; key[i]; i++) {
900 unsigned char c = key[i];
903 /* Leave the extended basename untouched.. */
904 if (!dot || i > store.baselen) {
905 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
906 error("invalid key: %s", key);
912 } else if (c == '\n') {
913 error("invalid key (newline): %s", key);
923 * The lock serves a purpose in addition to locking: the new
924 * contents of .git/config will be written into it.
926 lock = xcalloc(sizeof(struct lock_file), 1);
927 fd = hold_lock_file_for_update(lock, config_filename, 0);
929 error("could not lock config file %s", config_filename);
936 * If .git/config does not exist yet, write a minimal version.
938 in_fd = open(config_filename, O_RDONLY);
942 if ( ENOENT != errno ) {
943 error("opening %s: %s", config_filename,
945 ret = 3; /* same as "invalid config file" */
948 /* if nothing to unset, error out */
954 store.key = (char*)key;
955 if (!store_write_section(fd, key) ||
956 !store_write_pair(fd, key, value))
961 size_t contents_sz, copy_begin, copy_end;
964 if (value_regex == NULL)
965 store.value_regex = NULL;
967 if (value_regex[0] == '!') {
968 store.do_not_match = 1;
971 store.do_not_match = 0;
973 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
974 if (regcomp(store.value_regex, value_regex,
976 error("invalid pattern: %s", value_regex);
977 free(store.value_regex);
988 * After this, store.offset will contain the *end* offset
989 * of the last match, or remain at 0 if no match was found.
990 * As a side effect, we make sure to transform only a valid
991 * existing config file.
993 if (git_config_from_file(store_aux, config_filename, NULL)) {
994 error("invalid config file %s", config_filename);
996 if (store.value_regex != NULL) {
997 regfree(store.value_regex);
998 free(store.value_regex);
1005 if (store.value_regex != NULL) {
1006 regfree(store.value_regex);
1007 free(store.value_regex);
1010 /* if nothing to unset, or too many matches, error out */
1011 if ((store.seen == 0 && value == NULL) ||
1012 (store.seen > 1 && multi_replace == 0)) {
1018 contents_sz = xsize_t(st.st_size);
1019 contents = xmmap(NULL, contents_sz, PROT_READ,
1020 MAP_PRIVATE, in_fd, 0);
1023 if (store.seen == 0)
1026 for (i = 0, copy_begin = 0; i < store.seen; i++) {
1027 if (store.offset[i] == 0) {
1028 store.offset[i] = copy_end = contents_sz;
1029 } else if (store.state != KEY_SEEN) {
1030 copy_end = store.offset[i];
1032 copy_end = find_beginning_of_line(
1033 contents, contents_sz,
1034 store.offset[i]-2, &new_line);
1036 if (copy_end > 0 && contents[copy_end-1] != '\n')
1039 /* write the first part of the config */
1040 if (copy_end > copy_begin) {
1041 if (write_in_full(fd, contents + copy_begin,
1042 copy_end - copy_begin) <
1043 copy_end - copy_begin)
1046 write_in_full(fd, "\n", 1) != 1)
1049 copy_begin = store.offset[i];
1052 /* write the pair (value == NULL means unset) */
1053 if (value != NULL) {
1054 if (store.state == START) {
1055 if (!store_write_section(fd, key))
1058 if (!store_write_pair(fd, key, value))
1062 /* write the rest of the config */
1063 if (copy_begin < contents_sz)
1064 if (write_in_full(fd, contents + copy_begin,
1065 contents_sz - copy_begin) <
1066 contents_sz - copy_begin)
1069 munmap(contents, contents_sz);
1072 if (commit_lock_file(lock) < 0) {
1073 error("could not commit config file %s", config_filename);
1079 * lock is committed, so don't try to roll it back below.
1080 * NOTE: Since lockfile.c keeps a linked list of all created
1081 * lock_file structures, it isn't safe to free(lock). It's
1082 * better to just leave it hanging around.
1089 rollback_lock_file(lock);
1090 free(config_filename);
1094 ret = write_error(lock->filename);
1099 static int section_name_match (const char *buf, const char *name)
1101 int i = 0, j = 0, dot = 0;
1102 for (; buf[i] && buf[i] != ']'; i++) {
1103 if (!dot && isspace(buf[i])) {
1105 if (name[j++] != '.')
1107 for (i++; isspace(buf[i]); i++)
1113 if (buf[i] == '\\' && dot)
1115 else if (buf[i] == '"' && dot) {
1116 for (i++; isspace(buf[i]); i++)
1120 if (buf[i] != name[j++])
1123 return (buf[i] == ']' && name[j] == 0);
1126 /* if new_name == NULL, the section is removed instead */
1127 int git_config_rename_section(const char *old_name, const char *new_name)
1129 int ret = 0, remove = 0;
1130 char *config_filename;
1131 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
1135 if (config_exclusive_filename)
1136 config_filename = xstrdup(config_exclusive_filename);
1138 config_filename = xstrdup(git_path("config"));
1139 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
1141 ret = error("could not lock config file %s", config_filename);
1145 if (!(config_file = fopen(config_filename, "rb"))) {
1146 /* no config file means nothing to rename, no error */
1147 goto unlock_and_out;
1150 while (fgets(buf, sizeof(buf), config_file)) {
1153 for (i = 0; buf[i] && isspace(buf[i]); i++)
1155 if (buf[i] == '[') {
1156 /* it's a section */
1157 if (section_name_match (&buf[i+1], old_name)) {
1159 if (new_name == NULL) {
1163 store.baselen = strlen(new_name);
1164 if (!store_write_section(out_fd, new_name)) {
1165 ret = write_error(lock->filename);
1174 length = strlen(buf);
1175 if (write_in_full(out_fd, buf, length) != length) {
1176 ret = write_error(lock->filename);
1180 fclose(config_file);
1182 if (commit_lock_file(lock) < 0)
1183 ret = error("could not commit config file %s", config_filename);
1185 free(config_filename);
1190 * Call this to report error for your variable that should not
1191 * get a boolean value (i.e. "[my] var" means "true").
1193 int config_error_nonbool(const char *var)
1195 return error("Missing value for '%s'", var);