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) - 1)
64 if (isspace(c) && !quote) {
70 if (c == ';' || c == '#') {
75 for (; space; space--)
91 /* Some characters escape as themselves */
94 /* Reject unknown escape sequences */
109 static inline int iskeychar(int c)
111 return isalnum(c) || c == '-';
114 static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
119 /* Get the full name */
126 name[len++] = tolower(c);
131 while (c == ' ' || c == '\t')
138 value = parse_value();
142 return fn(name, value, data);
145 static int get_extended_base_var(char *name, int baselen, int c)
151 } while (isspace(c));
153 /* We require the format to be '[base "extension"]' */
156 name[baselen++] = '.';
159 int c = get_next_char();
170 if (baselen > MAXNAME / 2)
175 if (get_next_char() != ']')
180 static int get_base_var(char *name)
185 int c = get_next_char();
191 return get_extended_base_var(name, baselen, c);
192 if (!iskeychar(c) && c != '.')
194 if (baselen > MAXNAME / 2)
196 name[baselen++] = tolower(c);
200 static int git_parse_file(config_fn_t fn, void *data)
204 static char var[MAXNAME];
206 /* U+FEFF Byte Order Mark in UTF8 */
207 static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
208 const unsigned char *bomptr = utf8_bom;
211 int c = get_next_char();
212 if (bomptr && *bomptr) {
213 /* We are at the file beginning; skip UTF8-encoded BOM
214 * if present. Sane editors won't put this in on their
215 * own, but e.g. Windows Notepad will do it happily. */
216 if ((unsigned char) c == *bomptr) {
220 /* Do not tolerate partial BOM. */
221 if (bomptr != utf8_bom)
223 /* No BOM at file beginning. Cool. */
233 if (comment || isspace(c))
235 if (c == '#' || c == ';') {
240 baselen = get_base_var(var);
243 var[baselen++] = '.';
249 var[baselen] = tolower(c);
250 if (get_value(fn, data, var, baselen+1) < 0)
253 die("bad config file line %d in %s", config_linenr, config_file_name);
256 static int parse_unit_factor(const char *end, unsigned long *val)
260 else if (!strcasecmp(end, "k")) {
264 else if (!strcasecmp(end, "m")) {
268 else if (!strcasecmp(end, "g")) {
269 *val *= 1024 * 1024 * 1024;
275 static int git_parse_long(const char *value, long *ret)
277 if (value && *value) {
279 long val = strtol(value, &end, 0);
280 unsigned long factor = 1;
281 if (!parse_unit_factor(end, &factor))
289 int git_parse_ulong(const char *value, unsigned long *ret)
291 if (value && *value) {
293 unsigned long val = strtoul(value, &end, 0);
294 if (!parse_unit_factor(end, &val))
302 static void die_bad_config(const char *name)
304 if (config_file_name)
305 die("bad config value for '%s' in %s", name, config_file_name);
306 die("bad config value for '%s'", name);
309 int git_config_int(const char *name, const char *value)
312 if (!git_parse_long(value, &ret))
313 die_bad_config(name);
317 unsigned long git_config_ulong(const char *name, const char *value)
320 if (!git_parse_ulong(value, &ret))
321 die_bad_config(name);
325 int git_config_maybe_bool(const char *name, const char *value)
331 if (!strcasecmp(value, "true")
332 || !strcasecmp(value, "yes")
333 || !strcasecmp(value, "on"))
335 if (!strcasecmp(value, "false")
336 || !strcasecmp(value, "no")
337 || !strcasecmp(value, "off"))
342 int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
344 int v = git_config_maybe_bool(name, value);
350 return git_config_int(name, value);
353 int git_config_bool(const char *name, const char *value)
356 return !!git_config_bool_or_int(name, value, &discard);
359 int git_config_string(const char **dest, const char *var, const char *value)
362 return config_error_nonbool(var);
363 *dest = xstrdup(value);
367 int git_config_pathname(const char **dest, const char *var, const char *value)
370 return config_error_nonbool(var);
371 *dest = expand_user_path(value);
373 die("Failed to expand user dir in: '%s'", value);
377 static int git_default_core_config(const char *var, const char *value)
379 /* This needs a better name */
380 if (!strcmp(var, "core.filemode")) {
381 trust_executable_bit = git_config_bool(var, value);
384 if (!strcmp(var, "core.trustctime")) {
385 trust_ctime = git_config_bool(var, value);
389 if (!strcmp(var, "core.quotepath")) {
390 quote_path_fully = git_config_bool(var, value);
394 if (!strcmp(var, "core.symlinks")) {
395 has_symlinks = git_config_bool(var, value);
399 if (!strcmp(var, "core.ignorecase")) {
400 ignore_case = git_config_bool(var, value);
404 if (!strcmp(var, "core.bare")) {
405 is_bare_repository_cfg = git_config_bool(var, value);
409 if (!strcmp(var, "core.ignorestat")) {
410 assume_unchanged = git_config_bool(var, value);
414 if (!strcmp(var, "core.prefersymlinkrefs")) {
415 prefer_symlink_refs = git_config_bool(var, value);
419 if (!strcmp(var, "core.logallrefupdates")) {
420 log_all_ref_updates = git_config_bool(var, value);
424 if (!strcmp(var, "core.warnambiguousrefs")) {
425 warn_ambiguous_refs = git_config_bool(var, value);
429 if (!strcmp(var, "core.loosecompression")) {
430 int level = git_config_int(var, value);
432 level = Z_DEFAULT_COMPRESSION;
433 else if (level < 0 || level > Z_BEST_COMPRESSION)
434 die("bad zlib compression level %d", level);
435 zlib_compression_level = level;
436 zlib_compression_seen = 1;
440 if (!strcmp(var, "core.compression")) {
441 int level = git_config_int(var, value);
443 level = Z_DEFAULT_COMPRESSION;
444 else if (level < 0 || level > Z_BEST_COMPRESSION)
445 die("bad zlib compression level %d", level);
446 core_compression_level = level;
447 core_compression_seen = 1;
448 if (!zlib_compression_seen)
449 zlib_compression_level = level;
453 if (!strcmp(var, "core.packedgitwindowsize")) {
454 int pgsz_x2 = getpagesize() * 2;
455 packed_git_window_size = git_config_int(var, value);
457 /* This value must be multiple of (pagesize * 2) */
458 packed_git_window_size /= pgsz_x2;
459 if (packed_git_window_size < 1)
460 packed_git_window_size = 1;
461 packed_git_window_size *= pgsz_x2;
465 if (!strcmp(var, "core.packedgitlimit")) {
466 packed_git_limit = git_config_int(var, value);
470 if (!strcmp(var, "core.deltabasecachelimit")) {
471 delta_base_cache_limit = git_config_int(var, value);
475 if (!strcmp(var, "core.autocrlf")) {
476 if (value && !strcasecmp(value, "input")) {
480 auto_crlf = git_config_bool(var, value);
484 if (!strcmp(var, "core.safecrlf")) {
485 if (value && !strcasecmp(value, "warn")) {
486 safe_crlf = SAFE_CRLF_WARN;
489 safe_crlf = git_config_bool(var, value);
493 if (!strcmp(var, "core.notesref")) {
494 notes_ref_name = xstrdup(value);
498 if (!strcmp(var, "core.pager"))
499 return git_config_string(&pager_program, var, value);
501 if (!strcmp(var, "core.editor"))
502 return git_config_string(&editor_program, var, value);
504 if (!strcmp(var, "core.excludesfile"))
505 return git_config_pathname(&excludes_file, var, value);
507 if (!strcmp(var, "core.whitespace")) {
509 return config_error_nonbool(var);
510 whitespace_rule_cfg = parse_whitespace_rule(value);
514 if (!strcmp(var, "core.fsyncobjectfiles")) {
515 fsync_object_files = git_config_bool(var, value);
519 if (!strcmp(var, "core.preloadindex")) {
520 core_preload_index = git_config_bool(var, value);
524 if (!strcmp(var, "core.createobject")) {
525 if (!strcmp(value, "rename"))
526 object_creation_mode = OBJECT_CREATION_USES_RENAMES;
527 else if (!strcmp(value, "link"))
528 object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
530 die("Invalid mode for object creation: %s", value);
534 if (!strcmp(var, "core.sparsecheckout")) {
535 core_apply_sparse_checkout = git_config_bool(var, value);
539 /* Add other config variables here and to Documentation/config.txt. */
543 static int git_default_user_config(const char *var, const char *value)
545 if (!strcmp(var, "user.name")) {
547 return config_error_nonbool(var);
548 strlcpy(git_default_name, value, sizeof(git_default_name));
549 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
553 if (!strcmp(var, "user.email")) {
555 return config_error_nonbool(var);
556 strlcpy(git_default_email, value, sizeof(git_default_email));
557 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
561 /* Add other config variables here and to Documentation/config.txt. */
565 static int git_default_i18n_config(const char *var, const char *value)
567 if (!strcmp(var, "i18n.commitencoding"))
568 return git_config_string(&git_commit_encoding, var, value);
570 if (!strcmp(var, "i18n.logoutputencoding"))
571 return git_config_string(&git_log_output_encoding, var, value);
573 /* Add other config variables here and to Documentation/config.txt. */
577 static int git_default_branch_config(const char *var, const char *value)
579 if (!strcmp(var, "branch.autosetupmerge")) {
580 if (value && !strcasecmp(value, "always")) {
581 git_branch_track = BRANCH_TRACK_ALWAYS;
584 git_branch_track = git_config_bool(var, value);
587 if (!strcmp(var, "branch.autosetuprebase")) {
589 return config_error_nonbool(var);
590 else if (!strcmp(value, "never"))
591 autorebase = AUTOREBASE_NEVER;
592 else if (!strcmp(value, "local"))
593 autorebase = AUTOREBASE_LOCAL;
594 else if (!strcmp(value, "remote"))
595 autorebase = AUTOREBASE_REMOTE;
596 else if (!strcmp(value, "always"))
597 autorebase = AUTOREBASE_ALWAYS;
599 return error("Malformed value for %s", var);
603 /* Add other config variables here and to Documentation/config.txt. */
607 static int git_default_push_config(const char *var, const char *value)
609 if (!strcmp(var, "push.default")) {
611 return config_error_nonbool(var);
612 else if (!strcmp(value, "nothing"))
613 push_default = PUSH_DEFAULT_NOTHING;
614 else if (!strcmp(value, "matching"))
615 push_default = PUSH_DEFAULT_MATCHING;
616 else if (!strcmp(value, "tracking"))
617 push_default = PUSH_DEFAULT_TRACKING;
618 else if (!strcmp(value, "current"))
619 push_default = PUSH_DEFAULT_CURRENT;
621 error("Malformed value for %s: %s", var, value);
622 return error("Must be one of nothing, matching, "
623 "tracking or current.");
628 /* Add other config variables here and to Documentation/config.txt. */
632 static int git_default_mailmap_config(const char *var, const char *value)
634 if (!strcmp(var, "mailmap.file"))
635 return git_config_string(&git_mailmap_file, var, value);
637 /* Add other config variables here and to Documentation/config.txt. */
641 int git_default_config(const char *var, const char *value, void *dummy)
643 if (!prefixcmp(var, "core."))
644 return git_default_core_config(var, value);
646 if (!prefixcmp(var, "user."))
647 return git_default_user_config(var, value);
649 if (!prefixcmp(var, "i18n."))
650 return git_default_i18n_config(var, value);
652 if (!prefixcmp(var, "branch."))
653 return git_default_branch_config(var, value);
655 if (!prefixcmp(var, "push."))
656 return git_default_push_config(var, value);
658 if (!prefixcmp(var, "mailmap."))
659 return git_default_mailmap_config(var, value);
661 if (!prefixcmp(var, "advice."))
662 return git_default_advice_config(var, value);
664 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
665 pager_use_color = git_config_bool(var,value);
669 /* Add other config variables here and to Documentation/config.txt. */
673 int git_config_from_file(config_fn_t fn, const char *filename, void *data)
676 FILE *f = fopen(filename, "r");
681 config_file_name = filename;
684 ret = git_parse_file(fn, data);
686 config_file_name = NULL;
691 const char *git_etc_gitconfig(void)
693 static const char *system_wide;
695 system_wide = system_path(ETC_GITCONFIG);
699 static int git_env_bool(const char *k, int def)
701 const char *v = getenv(k);
702 return v ? git_config_bool(k, v) : def;
705 int git_config_system(void)
707 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
710 int git_config_global(void)
712 return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
715 int git_config(config_fn_t fn, void *data)
717 int ret = 0, found = 0;
718 char *repo_config = NULL;
719 const char *home = NULL;
721 /* Setting $GIT_CONFIG makes git read _only_ the given config file. */
722 if (config_exclusive_filename)
723 return git_config_from_file(fn, config_exclusive_filename, data);
724 if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) {
725 ret += git_config_from_file(fn, git_etc_gitconfig(),
730 home = getenv("HOME");
731 if (git_config_global() && home) {
732 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
733 if (!access(user_config, R_OK)) {
734 ret += git_config_from_file(fn, user_config, data);
740 repo_config = git_pathdup("config");
741 if (!access(repo_config, R_OK)) {
742 ret += git_config_from_file(fn, repo_config, data);
752 * Find all the stuff for git_config_set() below.
755 #define MAX_MATCHES 512
761 regex_t *value_regex;
763 size_t offset[MAX_MATCHES];
764 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
768 static int matches(const char *key, const char *value)
770 return !strcmp(key, store.key) &&
771 (store.value_regex == NULL ||
772 (store.do_not_match ^
773 !regexec(store.value_regex, value, 0, NULL, 0)));
776 static int store_aux(const char *key, const char *value, void *cb)
781 switch (store.state) {
783 if (matches(key, value)) {
784 if (store.seen == 1 && store.multi_replace == 0) {
785 warning("%s has multiple values", key);
786 } else if (store.seen >= MAX_MATCHES) {
787 error("too many matches for %s", key);
791 store.offset[store.seen] = ftell(config_file);
797 * What we are looking for is in store.key (both
798 * section and var), and its section part is baselen
799 * long. We found key (again, both section and var).
800 * We would want to know if this key is in the same
801 * section as what we are looking for. We already
802 * know we are in the same section as what should
805 ep = strrchr(key, '.');
806 section_len = ep - key;
808 if ((section_len != store.baselen) ||
809 memcmp(key, store.key, section_len+1)) {
810 store.state = SECTION_END_SEEN;
815 * Do not increment matches: this is no match, but we
816 * just made sure we are in the desired section.
818 store.offset[store.seen] = ftell(config_file);
820 case SECTION_END_SEEN:
822 if (matches(key, value)) {
823 store.offset[store.seen] = ftell(config_file);
824 store.state = KEY_SEEN;
827 if (strrchr(key, '.') - key == store.baselen &&
828 !strncmp(key, store.key, store.baselen)) {
829 store.state = SECTION_SEEN;
830 store.offset[store.seen] = ftell(config_file);
837 static int write_error(const char *filename)
839 error("failed to write new configuration file %s", filename);
841 /* Same error code as "failed to rename". */
845 static int store_write_section(int fd, const char *key)
849 struct strbuf sb = STRBUF_INIT;
851 dot = memchr(key, '.', store.baselen);
853 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
854 for (i = dot - key + 1; i < store.baselen; i++) {
855 if (key[i] == '"' || key[i] == '\\')
856 strbuf_addch(&sb, '\\');
857 strbuf_addch(&sb, key[i]);
859 strbuf_addstr(&sb, "\"]\n");
861 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
864 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
870 static int store_write_pair(int fd, const char *key, const char *value)
873 int length = strlen(key + store.baselen + 1);
874 const char *quote = "";
875 struct strbuf sb = STRBUF_INIT;
878 * Check to see if the value needs to be surrounded with a dq pair.
879 * Note that problematic characters are always backslash-quoted; this
880 * check is about not losing leading or trailing SP and strings that
881 * follow beginning-of-comment characters (i.e. ';' and '#') by the
882 * configuration parser.
886 for (i = 0; value[i]; i++)
887 if (value[i] == ';' || value[i] == '#')
889 if (i && value[i - 1] == ' ')
892 strbuf_addf(&sb, "\t%.*s = %s",
893 length, key + store.baselen + 1, quote);
895 for (i = 0; value[i]; i++)
898 strbuf_addstr(&sb, "\\n");
901 strbuf_addstr(&sb, "\\t");
905 strbuf_addch(&sb, '\\');
907 strbuf_addch(&sb, value[i]);
910 strbuf_addf(&sb, "%s\n", quote);
912 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
918 static ssize_t find_beginning_of_line(const char *contents, size_t size,
919 size_t offset_, int *found_bracket)
921 size_t equal_offset = size, bracket_offset = size;
925 for (offset = offset_-2; offset > 0
926 && contents[offset] != '\n'; offset--)
927 switch (contents[offset]) {
928 case '=': equal_offset = offset; break;
929 case ']': bracket_offset = offset; break;
931 if (offset > 0 && contents[offset-1] == '\\') {
935 if (bracket_offset < equal_offset) {
937 offset = bracket_offset+1;
944 int git_config_set(const char *key, const char *value)
946 return git_config_set_multivar(key, value, NULL, 0);
950 * If value==NULL, unset in (remove from) config,
951 * if value_regex!=NULL, disregard key/value pairs where value does not match.
952 * if multi_replace==0, nothing, or only one matching key/value is replaced,
953 * else all matching key/values (regardless how many) are removed,
954 * before the new pair is written.
956 * Returns 0 on success.
958 * This function does this:
960 * - it locks the config file by creating ".git/config.lock"
962 * - it then parses the config using store_aux() as validator to find
963 * the position on the key/value pair to replace. If it is to be unset,
964 * it must be found exactly once.
966 * - the config file is mmap()ed and the part before the match (if any) is
967 * written to the lock file, then the changed part and the rest.
969 * - the config file is removed and the lock file rename()d to it.
972 int git_config_set_multivar(const char *key, const char *value,
973 const char *value_regex, int multi_replace)
978 char *config_filename;
979 struct lock_file *lock = NULL;
980 const char *last_dot = strrchr(key, '.');
982 if (config_exclusive_filename)
983 config_filename = xstrdup(config_exclusive_filename);
985 config_filename = git_pathdup("config");
988 * Since "key" actually contains the section name and the real
989 * key name separated by a dot, we have to know where the dot is.
992 if (last_dot == NULL) {
993 error("key does not contain a section: %s", key);
997 store.baselen = last_dot - key;
999 store.multi_replace = multi_replace;
1002 * Validate the key and while at it, lower case it for matching.
1004 store.key = xmalloc(strlen(key) + 1);
1006 for (i = 0; key[i]; i++) {
1007 unsigned char c = key[i];
1010 /* Leave the extended basename untouched.. */
1011 if (!dot || i > store.baselen) {
1012 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
1013 error("invalid key: %s", key);
1019 } else if (c == '\n') {
1020 error("invalid key (newline): %s", key);
1030 * The lock serves a purpose in addition to locking: the new
1031 * contents of .git/config will be written into it.
1033 lock = xcalloc(sizeof(struct lock_file), 1);
1034 fd = hold_lock_file_for_update(lock, config_filename, 0);
1036 error("could not lock config file %s: %s", config_filename, strerror(errno));
1043 * If .git/config does not exist yet, write a minimal version.
1045 in_fd = open(config_filename, O_RDONLY);
1049 if ( ENOENT != errno ) {
1050 error("opening %s: %s", config_filename,
1052 ret = 3; /* same as "invalid config file" */
1055 /* if nothing to unset, error out */
1056 if (value == NULL) {
1061 store.key = (char *)key;
1062 if (!store_write_section(fd, key) ||
1063 !store_write_pair(fd, key, value))
1068 size_t contents_sz, copy_begin, copy_end;
1069 int i, new_line = 0;
1071 if (value_regex == NULL)
1072 store.value_regex = NULL;
1074 if (value_regex[0] == '!') {
1075 store.do_not_match = 1;
1078 store.do_not_match = 0;
1080 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
1081 if (regcomp(store.value_regex, value_regex,
1083 error("invalid pattern: %s", value_regex);
1084 free(store.value_regex);
1090 store.offset[0] = 0;
1091 store.state = START;
1095 * After this, store.offset will contain the *end* offset
1096 * of the last match, or remain at 0 if no match was found.
1097 * As a side effect, we make sure to transform only a valid
1098 * existing config file.
1100 if (git_config_from_file(store_aux, config_filename, NULL)) {
1101 error("invalid config file %s", config_filename);
1103 if (store.value_regex != NULL) {
1104 regfree(store.value_regex);
1105 free(store.value_regex);
1112 if (store.value_regex != NULL) {
1113 regfree(store.value_regex);
1114 free(store.value_regex);
1117 /* if nothing to unset, or too many matches, error out */
1118 if ((store.seen == 0 && value == NULL) ||
1119 (store.seen > 1 && multi_replace == 0)) {
1125 contents_sz = xsize_t(st.st_size);
1126 contents = xmmap(NULL, contents_sz, PROT_READ,
1127 MAP_PRIVATE, in_fd, 0);
1130 if (store.seen == 0)
1133 for (i = 0, copy_begin = 0; i < store.seen; i++) {
1134 if (store.offset[i] == 0) {
1135 store.offset[i] = copy_end = contents_sz;
1136 } else if (store.state != KEY_SEEN) {
1137 copy_end = store.offset[i];
1139 copy_end = find_beginning_of_line(
1140 contents, contents_sz,
1141 store.offset[i]-2, &new_line);
1143 if (copy_end > 0 && contents[copy_end-1] != '\n')
1146 /* write the first part of the config */
1147 if (copy_end > copy_begin) {
1148 if (write_in_full(fd, contents + copy_begin,
1149 copy_end - copy_begin) <
1150 copy_end - copy_begin)
1153 write_str_in_full(fd, "\n") != 1)
1156 copy_begin = store.offset[i];
1159 /* write the pair (value == NULL means unset) */
1160 if (value != NULL) {
1161 if (store.state == START) {
1162 if (!store_write_section(fd, key))
1165 if (!store_write_pair(fd, key, value))
1169 /* write the rest of the config */
1170 if (copy_begin < contents_sz)
1171 if (write_in_full(fd, contents + copy_begin,
1172 contents_sz - copy_begin) <
1173 contents_sz - copy_begin)
1176 munmap(contents, contents_sz);
1179 if (commit_lock_file(lock) < 0) {
1180 error("could not commit config file %s", config_filename);
1186 * lock is committed, so don't try to roll it back below.
1187 * NOTE: Since lockfile.c keeps a linked list of all created
1188 * lock_file structures, it isn't safe to free(lock). It's
1189 * better to just leave it hanging around.
1196 rollback_lock_file(lock);
1197 free(config_filename);
1201 ret = write_error(lock->filename);
1206 static int section_name_match (const char *buf, const char *name)
1208 int i = 0, j = 0, dot = 0;
1211 for (i = 1; buf[i] && buf[i] != ']'; i++) {
1212 if (!dot && isspace(buf[i])) {
1214 if (name[j++] != '.')
1216 for (i++; isspace(buf[i]); i++)
1222 if (buf[i] == '\\' && dot)
1224 else if (buf[i] == '"' && dot) {
1225 for (i++; isspace(buf[i]); i++)
1229 if (buf[i] != name[j++])
1232 if (buf[i] == ']' && name[j] == 0) {
1234 * We match, now just find the right length offset by
1235 * gobbling up any whitespace after it, as well
1238 for (; buf[i] && isspace(buf[i]); i++)
1245 /* if new_name == NULL, the section is removed instead */
1246 int git_config_rename_section(const char *old_name, const char *new_name)
1248 int ret = 0, remove = 0;
1249 char *config_filename;
1250 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
1254 if (config_exclusive_filename)
1255 config_filename = xstrdup(config_exclusive_filename);
1257 config_filename = git_pathdup("config");
1258 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
1260 ret = error("could not lock config file %s", config_filename);
1264 if (!(config_file = fopen(config_filename, "rb"))) {
1265 /* no config file means nothing to rename, no error */
1266 goto unlock_and_out;
1269 while (fgets(buf, sizeof(buf), config_file)) {
1273 for (i = 0; buf[i] && isspace(buf[i]); i++)
1275 if (buf[i] == '[') {
1276 /* it's a section */
1277 int offset = section_name_match(&buf[i], old_name);
1280 if (new_name == NULL) {
1284 store.baselen = strlen(new_name);
1285 if (!store_write_section(out_fd, new_name)) {
1286 ret = write_error(lock->filename);
1290 * We wrote out the new section, with
1291 * a newline, now skip the old
1294 output += offset + i;
1295 if (strlen(output) > 0) {
1297 * More content means there's
1298 * a declaration to put on the
1299 * next line; indent with a
1310 length = strlen(output);
1311 if (write_in_full(out_fd, output, length) != length) {
1312 ret = write_error(lock->filename);
1316 fclose(config_file);
1318 if (commit_lock_file(lock) < 0)
1319 ret = error("could not commit config file %s", config_filename);
1321 free(config_filename);
1326 * Call this to report error for your variable that should not
1327 * get a boolean value (i.e. "[my] var" means "true").
1329 int config_error_nonbool(const char *var)
1331 return error("Missing value for '%s'", var);