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
) {
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
];
208 /* U+FEFF Byte Order Mark in UTF8 */
209 static const unsigned char *utf8_bom
= (unsigned char *) "\xef\xbb\xbf";
210 const unsigned char *bomptr
= utf8_bom
;
213 int c
= get_next_char();
214 if (bomptr
&& *bomptr
) {
215 /* We are at the file beginning; skip UTF8-encoded BOM
216 * if present. Sane editors won't put this in on their
217 * own, but e.g. Windows Notepad will do it happily. */
218 if ((unsigned char) c
== *bomptr
) {
222 /* Do not tolerate partial BOM. */
223 if (bomptr
!= utf8_bom
)
225 /* No BOM at file beginning. Cool. */
235 if (comment
|| isspace(c
))
237 if (c
== '#' || c
== ';') {
242 baselen
= get_base_var(var
);
245 var
[baselen
++] = '.';
251 var
[baselen
] = tolower(c
);
252 if (get_value(fn
, data
, var
, baselen
+1) < 0)
255 die("bad config file line %d in %s", config_linenr
, config_file_name
);
258 static int parse_unit_factor(const char *end
, unsigned long *val
)
262 else if (!strcasecmp(end
, "k")) {
266 else if (!strcasecmp(end
, "m")) {
270 else if (!strcasecmp(end
, "g")) {
271 *val
*= 1024 * 1024 * 1024;
277 static int git_parse_long(const char *value
, long *ret
)
279 if (value
&& *value
) {
281 long val
= strtol(value
, &end
, 0);
282 unsigned long factor
= 1;
283 if (!parse_unit_factor(end
, &factor
))
291 int git_parse_ulong(const char *value
, unsigned long *ret
)
293 if (value
&& *value
) {
295 unsigned long val
= strtoul(value
, &end
, 0);
296 if (!parse_unit_factor(end
, &val
))
304 static void die_bad_config(const char *name
)
306 if (config_file_name
)
307 die("bad config value for '%s' in %s", name
, config_file_name
);
308 die("bad config value for '%s'", name
);
311 int git_config_int(const char *name
, const char *value
)
314 if (!git_parse_long(value
, &ret
))
315 die_bad_config(name
);
319 unsigned long git_config_ulong(const char *name
, const char *value
)
322 if (!git_parse_ulong(value
, &ret
))
323 die_bad_config(name
);
327 int git_config_bool_or_int(const char *name
, const char *value
, int *is_bool
)
334 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") || !strcasecmp(value
, "on"))
336 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no") || !strcasecmp(value
, "off"))
339 return git_config_int(name
, value
);
342 int git_config_bool(const char *name
, const char *value
)
345 return !!git_config_bool_or_int(name
, value
, &discard
);
348 int git_config_string(const char **dest
, const char *var
, const char *value
)
351 return config_error_nonbool(var
);
352 *dest
= xstrdup(value
);
356 static int git_default_core_config(const char *var
, const char *value
)
358 /* This needs a better name */
359 if (!strcmp(var
, "core.filemode")) {
360 trust_executable_bit
= git_config_bool(var
, value
);
363 if (!strcmp(var
, "core.trustctime")) {
364 trust_ctime
= git_config_bool(var
, value
);
368 if (!strcmp(var
, "core.quotepath")) {
369 quote_path_fully
= git_config_bool(var
, value
);
373 if (!strcmp(var
, "core.symlinks")) {
374 has_symlinks
= git_config_bool(var
, value
);
378 if (!strcmp(var
, "core.ignorecase")) {
379 ignore_case
= git_config_bool(var
, value
);
383 if (!strcmp(var
, "core.bare")) {
384 is_bare_repository_cfg
= git_config_bool(var
, value
);
388 if (!strcmp(var
, "core.ignorestat")) {
389 assume_unchanged
= git_config_bool(var
, value
);
393 if (!strcmp(var
, "core.prefersymlinkrefs")) {
394 prefer_symlink_refs
= git_config_bool(var
, value
);
398 if (!strcmp(var
, "core.logallrefupdates")) {
399 log_all_ref_updates
= git_config_bool(var
, value
);
403 if (!strcmp(var
, "core.warnambiguousrefs")) {
404 warn_ambiguous_refs
= git_config_bool(var
, value
);
408 if (!strcmp(var
, "core.loosecompression")) {
409 int level
= git_config_int(var
, value
);
411 level
= Z_DEFAULT_COMPRESSION
;
412 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
413 die("bad zlib compression level %d", level
);
414 zlib_compression_level
= level
;
415 zlib_compression_seen
= 1;
419 if (!strcmp(var
, "core.compression")) {
420 int level
= git_config_int(var
, value
);
422 level
= Z_DEFAULT_COMPRESSION
;
423 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
424 die("bad zlib compression level %d", level
);
425 core_compression_level
= level
;
426 core_compression_seen
= 1;
427 if (!zlib_compression_seen
)
428 zlib_compression_level
= level
;
432 if (!strcmp(var
, "core.packedgitwindowsize")) {
433 int pgsz_x2
= getpagesize() * 2;
434 packed_git_window_size
= git_config_int(var
, value
);
436 /* This value must be multiple of (pagesize * 2) */
437 packed_git_window_size
/= pgsz_x2
;
438 if (packed_git_window_size
< 1)
439 packed_git_window_size
= 1;
440 packed_git_window_size
*= pgsz_x2
;
444 if (!strcmp(var
, "core.packedgitlimit")) {
445 packed_git_limit
= git_config_int(var
, value
);
449 if (!strcmp(var
, "core.deltabasecachelimit")) {
450 delta_base_cache_limit
= git_config_int(var
, value
);
454 if (!strcmp(var
, "core.autocrlf")) {
455 if (value
&& !strcasecmp(value
, "input")) {
459 auto_crlf
= git_config_bool(var
, value
);
463 if (!strcmp(var
, "core.safecrlf")) {
464 if (value
&& !strcasecmp(value
, "warn")) {
465 safe_crlf
= SAFE_CRLF_WARN
;
468 safe_crlf
= git_config_bool(var
, value
);
472 if (!strcmp(var
, "core.pager"))
473 return git_config_string(&pager_program
, var
, value
);
475 if (!strcmp(var
, "core.editor"))
476 return git_config_string(&editor_program
, var
, value
);
478 if (!strcmp(var
, "core.excludesfile"))
479 return git_config_string(&excludes_file
, var
, value
);
481 if (!strcmp(var
, "core.whitespace")) {
483 return config_error_nonbool(var
);
484 whitespace_rule_cfg
= parse_whitespace_rule(value
);
488 if (!strcmp(var
, "core.fsyncobjectfiles")) {
489 fsync_object_files
= git_config_bool(var
, value
);
493 if (!strcmp(var
, "core.preloadindex")) {
494 core_preload_index
= git_config_bool(var
, value
);
498 if (!strcmp(var
, "core.createobject")) {
499 if (!strcmp(value
, "rename"))
500 object_creation_mode
= OBJECT_CREATION_USES_RENAMES
;
501 else if (!strcmp(value
, "link"))
502 object_creation_mode
= OBJECT_CREATION_USES_HARDLINKS
;
504 die("Invalid mode for object creation: %s", value
);
508 if (!strcmp(var
, "core.hidedotfiles")) {
509 hide_dotfiles
= git_config_bool(var
, value
);
513 /* Add other config variables here and to Documentation/config.txt. */
517 static int git_default_user_config(const char *var
, const char *value
)
519 if (!strcmp(var
, "user.name")) {
521 return config_error_nonbool(var
);
522 strlcpy(git_default_name
, value
, sizeof(git_default_name
));
523 if (git_default_email
[0])
524 user_ident_explicitly_given
= 1;
528 if (!strcmp(var
, "user.email")) {
530 return config_error_nonbool(var
);
531 strlcpy(git_default_email
, value
, sizeof(git_default_email
));
532 if (git_default_name
[0])
533 user_ident_explicitly_given
= 1;
537 /* Add other config variables here and to Documentation/config.txt. */
541 static int git_default_i18n_config(const char *var
, const char *value
)
543 if (!strcmp(var
, "i18n.commitencoding"))
544 return git_config_string(&git_commit_encoding
, var
, value
);
546 if (!strcmp(var
, "i18n.logoutputencoding"))
547 return git_config_string(&git_log_output_encoding
, var
, value
);
549 /* Add other config variables here and to Documentation/config.txt. */
553 static int git_default_branch_config(const char *var
, const char *value
)
555 if (!strcmp(var
, "branch.autosetupmerge")) {
556 if (value
&& !strcasecmp(value
, "always")) {
557 git_branch_track
= BRANCH_TRACK_ALWAYS
;
560 git_branch_track
= git_config_bool(var
, value
);
563 if (!strcmp(var
, "branch.autosetuprebase")) {
565 return config_error_nonbool(var
);
566 else if (!strcmp(value
, "never"))
567 autorebase
= AUTOREBASE_NEVER
;
568 else if (!strcmp(value
, "local"))
569 autorebase
= AUTOREBASE_LOCAL
;
570 else if (!strcmp(value
, "remote"))
571 autorebase
= AUTOREBASE_REMOTE
;
572 else if (!strcmp(value
, "always"))
573 autorebase
= AUTOREBASE_ALWAYS
;
575 return error("Malformed value for %s", var
);
579 /* Add other config variables here and to Documentation/config.txt. */
583 static int git_default_push_config(const char *var
, const char *value
)
585 if (!strcmp(var
, "push.default")) {
587 return config_error_nonbool(var
);
588 else if (!strcmp(value
, "nothing"))
589 push_default
= PUSH_DEFAULT_NOTHING
;
590 else if (!strcmp(value
, "matching"))
591 push_default
= PUSH_DEFAULT_MATCHING
;
592 else if (!strcmp(value
, "tracking"))
593 push_default
= PUSH_DEFAULT_TRACKING
;
594 else if (!strcmp(value
, "current"))
595 push_default
= PUSH_DEFAULT_CURRENT
;
597 error("Malformed value for %s: %s", var
, value
);
598 return error("Must be one of nothing, matching, "
599 "tracking or current.");
604 /* Add other config variables here and to Documentation/config.txt. */
608 static int git_default_mailmap_config(const char *var
, const char *value
)
610 if (!strcmp(var
, "mailmap.file"))
611 return git_config_string(&git_mailmap_file
, var
, value
);
613 /* Add other config variables here and to Documentation/config.txt. */
617 int git_default_config(const char *var
, const char *value
, void *dummy
)
619 if (!prefixcmp(var
, "core."))
620 return git_default_core_config(var
, value
);
622 if (!prefixcmp(var
, "user."))
623 return git_default_user_config(var
, value
);
625 if (!prefixcmp(var
, "i18n."))
626 return git_default_i18n_config(var
, value
);
628 if (!prefixcmp(var
, "branch."))
629 return git_default_branch_config(var
, value
);
631 if (!prefixcmp(var
, "push."))
632 return git_default_push_config(var
, value
);
634 if (!prefixcmp(var
, "mailmap."))
635 return git_default_mailmap_config(var
, value
);
637 if (!strcmp(var
, "pager.color") || !strcmp(var
, "color.pager")) {
638 pager_use_color
= git_config_bool(var
,value
);
642 /* Add other config variables here and to Documentation/config.txt. */
646 int git_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
649 FILE *f
= fopen(filename
, "r");
654 config_file_name
= filename
;
657 ret
= git_parse_file(fn
, data
);
659 config_file_name
= NULL
;
664 const char *git_etc_gitconfig(void)
666 static const char *system_wide
;
668 system_wide
= system_path(ETC_GITCONFIG
);
672 static int git_env_bool(const char *k
, int def
)
674 const char *v
= getenv(k
);
675 return v
? git_config_bool(k
, v
) : def
;
678 int git_config_system(void)
680 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
683 int git_config_global(void)
685 return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
688 int git_config(config_fn_t fn
, void *data
)
690 int ret
= 0, found
= 0;
691 char *repo_config
= NULL
;
692 const char *home
= NULL
;
694 /* Setting $GIT_CONFIG makes git read _only_ the given config file. */
695 if (config_exclusive_filename
)
696 return git_config_from_file(fn
, config_exclusive_filename
, data
);
697 if (git_config_system() && !access(git_etc_gitconfig(), R_OK
)) {
698 ret
+= git_config_from_file(fn
, git_etc_gitconfig(),
703 home
= getenv("HOME");
704 if (git_config_global() && home
) {
705 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
706 if (!access(user_config
, R_OK
)) {
707 ret
+= git_config_from_file(fn
, user_config
, data
);
713 repo_config
= git_pathdup("config");
714 if (!access(repo_config
, R_OK
)) {
715 ret
+= git_config_from_file(fn
, repo_config
, data
);
725 * Find all the stuff for git_config_set() below.
728 #define MAX_MATCHES 512
734 regex_t
*value_regex
;
736 size_t offset
[MAX_MATCHES
];
737 enum { START
, SECTION_SEEN
, SECTION_END_SEEN
, KEY_SEEN
} state
;
741 static int matches(const char *key
, const char *value
)
743 return !strcmp(key
, store
.key
) &&
744 (store
.value_regex
== NULL
||
745 (store
.do_not_match
^
746 !regexec(store
.value_regex
, value
, 0, NULL
, 0)));
749 static int store_aux(const char *key
, const char *value
, void *cb
)
754 switch (store
.state
) {
756 if (matches(key
, value
)) {
757 if (store
.seen
== 1 && store
.multi_replace
== 0) {
758 warning("%s has multiple values", key
);
759 } else if (store
.seen
>= MAX_MATCHES
) {
760 error("too many matches for %s", key
);
764 store
.offset
[store
.seen
] = ftell(config_file
);
770 * What we are looking for is in store.key (both
771 * section and var), and its section part is baselen
772 * long. We found key (again, both section and var).
773 * We would want to know if this key is in the same
774 * section as what we are looking for. We already
775 * know we are in the same section as what should
778 ep
= strrchr(key
, '.');
779 section_len
= ep
- key
;
781 if ((section_len
!= store
.baselen
) ||
782 memcmp(key
, store
.key
, section_len
+1)) {
783 store
.state
= SECTION_END_SEEN
;
788 * Do not increment matches: this is no match, but we
789 * just made sure we are in the desired section.
791 store
.offset
[store
.seen
] = ftell(config_file
);
793 case SECTION_END_SEEN
:
795 if (matches(key
, value
)) {
796 store
.offset
[store
.seen
] = ftell(config_file
);
797 store
.state
= KEY_SEEN
;
800 if (strrchr(key
, '.') - key
== store
.baselen
&&
801 !strncmp(key
, store
.key
, store
.baselen
)) {
802 store
.state
= SECTION_SEEN
;
803 store
.offset
[store
.seen
] = ftell(config_file
);
810 static int write_error(const char *filename
)
812 error("failed to write new configuration file %s", filename
);
814 /* Same error code as "failed to rename". */
818 static int store_write_section(int fd
, const char *key
)
822 struct strbuf sb
= STRBUF_INIT
;
824 dot
= memchr(key
, '.', store
.baselen
);
826 strbuf_addf(&sb
, "[%.*s \"", (int)(dot
- key
), key
);
827 for (i
= dot
- key
+ 1; i
< store
.baselen
; i
++) {
828 if (key
[i
] == '"' || key
[i
] == '\\')
829 strbuf_addch(&sb
, '\\');
830 strbuf_addch(&sb
, key
[i
]);
832 strbuf_addstr(&sb
, "\"]\n");
834 strbuf_addf(&sb
, "[%.*s]\n", store
.baselen
, key
);
837 success
= write_in_full(fd
, sb
.buf
, sb
.len
) == sb
.len
;
843 static int store_write_pair(int fd
, const char *key
, const char *value
)
846 int length
= strlen(key
+ store
.baselen
+ 1);
847 const char *quote
= "";
848 struct strbuf sb
= STRBUF_INIT
;
851 * Check to see if the value needs to be surrounded with a dq pair.
852 * Note that problematic characters are always backslash-quoted; this
853 * check is about not losing leading or trailing SP and strings that
854 * follow beginning-of-comment characters (i.e. ';' and '#') by the
855 * configuration parser.
859 for (i
= 0; value
[i
]; i
++)
860 if (value
[i
] == ';' || value
[i
] == '#')
862 if (i
&& value
[i
- 1] == ' ')
865 strbuf_addf(&sb
, "\t%.*s = %s",
866 length
, key
+ store
.baselen
+ 1, quote
);
868 for (i
= 0; value
[i
]; i
++)
871 strbuf_addstr(&sb
, "\\n");
874 strbuf_addstr(&sb
, "\\t");
878 strbuf_addch(&sb
, '\\');
880 strbuf_addch(&sb
, value
[i
]);
883 strbuf_addf(&sb
, "%s\n", quote
);
885 success
= write_in_full(fd
, sb
.buf
, sb
.len
) == sb
.len
;
891 static ssize_t
find_beginning_of_line(const char *contents
, size_t size
,
892 size_t offset_
, int *found_bracket
)
894 size_t equal_offset
= size
, bracket_offset
= size
;
898 for (offset
= offset_
-2; offset
> 0
899 && contents
[offset
] != '\n'; offset
--)
900 switch (contents
[offset
]) {
901 case '=': equal_offset
= offset
; break;
902 case ']': bracket_offset
= offset
; break;
904 if (offset
> 0 && contents
[offset
-1] == '\\') {
908 if (bracket_offset
< equal_offset
) {
910 offset
= bracket_offset
+1;
917 int git_config_set(const char *key
, const char *value
)
919 return git_config_set_multivar(key
, value
, NULL
, 0);
923 * If value==NULL, unset in (remove from) config,
924 * if value_regex!=NULL, disregard key/value pairs where value does not match.
925 * if multi_replace==0, nothing, or only one matching key/value is replaced,
926 * else all matching key/values (regardless how many) are removed,
927 * before the new pair is written.
929 * Returns 0 on success.
931 * This function does this:
933 * - it locks the config file by creating ".git/config.lock"
935 * - it then parses the config using store_aux() as validator to find
936 * the position on the key/value pair to replace. If it is to be unset,
937 * it must be found exactly once.
939 * - the config file is mmap()ed and the part before the match (if any) is
940 * written to the lock file, then the changed part and the rest.
942 * - the config file is removed and the lock file rename()d to it.
945 int git_config_set_multivar(const char *key
, const char *value
,
946 const char *value_regex
, int multi_replace
)
951 char *config_filename
;
952 struct lock_file
*lock
= NULL
;
953 const char *last_dot
= strrchr(key
, '.');
955 if (config_exclusive_filename
)
956 config_filename
= xstrdup(config_exclusive_filename
);
958 config_filename
= git_pathdup("config");
961 * Since "key" actually contains the section name and the real
962 * key name separated by a dot, we have to know where the dot is.
965 if (last_dot
== NULL
) {
966 error("key does not contain a section: %s", key
);
970 store
.baselen
= last_dot
- key
;
972 store
.multi_replace
= multi_replace
;
975 * Validate the key and while at it, lower case it for matching.
977 store
.key
= xmalloc(strlen(key
) + 1);
979 for (i
= 0; key
[i
]; i
++) {
980 unsigned char c
= key
[i
];
983 /* Leave the extended basename untouched.. */
984 if (!dot
|| i
> store
.baselen
) {
985 if (!iskeychar(c
) || (i
== store
.baselen
+1 && !isalpha(c
))) {
986 error("invalid key: %s", key
);
992 } else if (c
== '\n') {
993 error("invalid key (newline): %s", key
);
1003 * The lock serves a purpose in addition to locking: the new
1004 * contents of .git/config will be written into it.
1006 lock
= xcalloc(sizeof(struct lock_file
), 1);
1007 fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
1009 error("could not lock config file %s: %s", config_filename
, strerror(errno
));
1016 * If .git/config does not exist yet, write a minimal version.
1018 in_fd
= open(config_filename
, O_RDONLY
);
1022 if ( ENOENT
!= errno
) {
1023 error("opening %s: %s", config_filename
,
1025 ret
= 3; /* same as "invalid config file" */
1028 /* if nothing to unset, error out */
1029 if (value
== NULL
) {
1034 store
.key
= (char *)key
;
1035 if (!store_write_section(fd
, key
) ||
1036 !store_write_pair(fd
, key
, value
))
1041 size_t contents_sz
, copy_begin
, copy_end
;
1042 int i
, new_line
= 0;
1044 if (value_regex
== NULL
)
1045 store
.value_regex
= NULL
;
1047 if (value_regex
[0] == '!') {
1048 store
.do_not_match
= 1;
1051 store
.do_not_match
= 0;
1053 store
.value_regex
= (regex_t
*)xmalloc(sizeof(regex_t
));
1054 if (regcomp(store
.value_regex
, value_regex
,
1056 error("invalid pattern: %s", value_regex
);
1057 free(store
.value_regex
);
1063 store
.offset
[0] = 0;
1064 store
.state
= START
;
1068 * After this, store.offset will contain the *end* offset
1069 * of the last match, or remain at 0 if no match was found.
1070 * As a side effect, we make sure to transform only a valid
1071 * existing config file.
1073 if (git_config_from_file(store_aux
, config_filename
, NULL
)) {
1074 error("invalid config file %s", config_filename
);
1076 if (store
.value_regex
!= NULL
) {
1077 regfree(store
.value_regex
);
1078 free(store
.value_regex
);
1085 if (store
.value_regex
!= NULL
) {
1086 regfree(store
.value_regex
);
1087 free(store
.value_regex
);
1090 /* if nothing to unset, or too many matches, error out */
1091 if ((store
.seen
== 0 && value
== NULL
) ||
1092 (store
.seen
> 1 && multi_replace
== 0)) {
1098 contents_sz
= xsize_t(st
.st_size
);
1099 contents
= xmmap(NULL
, contents_sz
, PROT_READ
,
1100 MAP_PRIVATE
, in_fd
, 0);
1103 if (store
.seen
== 0)
1106 for (i
= 0, copy_begin
= 0; i
< store
.seen
; i
++) {
1107 if (store
.offset
[i
] == 0) {
1108 store
.offset
[i
] = copy_end
= contents_sz
;
1109 } else if (store
.state
!= KEY_SEEN
) {
1110 copy_end
= store
.offset
[i
];
1112 copy_end
= find_beginning_of_line(
1113 contents
, contents_sz
,
1114 store
.offset
[i
]-2, &new_line
);
1116 if (copy_end
> 0 && contents
[copy_end
-1] != '\n')
1119 /* write the first part of the config */
1120 if (copy_end
> copy_begin
) {
1121 if (write_in_full(fd
, contents
+ copy_begin
,
1122 copy_end
- copy_begin
) <
1123 copy_end
- copy_begin
)
1126 write_in_full(fd
, "\n", 1) != 1)
1129 copy_begin
= store
.offset
[i
];
1132 /* write the pair (value == NULL means unset) */
1133 if (value
!= NULL
) {
1134 if (store
.state
== START
) {
1135 if (!store_write_section(fd
, key
))
1138 if (!store_write_pair(fd
, key
, value
))
1142 /* write the rest of the config */
1143 if (copy_begin
< contents_sz
)
1144 if (write_in_full(fd
, contents
+ copy_begin
,
1145 contents_sz
- copy_begin
) <
1146 contents_sz
- copy_begin
)
1149 munmap(contents
, contents_sz
);
1152 if (commit_lock_file(lock
) < 0) {
1153 error("could not commit config file %s", config_filename
);
1159 * lock is committed, so don't try to roll it back below.
1160 * NOTE: Since lockfile.c keeps a linked list of all created
1161 * lock_file structures, it isn't safe to free(lock). It's
1162 * better to just leave it hanging around.
1169 rollback_lock_file(lock
);
1170 free(config_filename
);
1174 ret
= write_error(lock
->filename
);
1179 static int section_name_match (const char *buf
, const char *name
)
1181 int i
= 0, j
= 0, dot
= 0;
1184 for (i
= 1; buf
[i
] && buf
[i
] != ']'; i
++) {
1185 if (!dot
&& isspace(buf
[i
])) {
1187 if (name
[j
++] != '.')
1189 for (i
++; isspace(buf
[i
]); i
++)
1195 if (buf
[i
] == '\\' && dot
)
1197 else if (buf
[i
] == '"' && dot
) {
1198 for (i
++; isspace(buf
[i
]); i
++)
1202 if (buf
[i
] != name
[j
++])
1205 if (buf
[i
] == ']' && name
[j
] == 0) {
1207 * We match, now just find the right length offset by
1208 * gobbling up any whitespace after it, as well
1211 for (; buf
[i
] && isspace(buf
[i
]); i
++)
1218 /* if new_name == NULL, the section is removed instead */
1219 int git_config_rename_section(const char *old_name
, const char *new_name
)
1221 int ret
= 0, remove
= 0;
1222 char *config_filename
;
1223 struct lock_file
*lock
= xcalloc(sizeof(struct lock_file
), 1);
1227 if (config_exclusive_filename
)
1228 config_filename
= xstrdup(config_exclusive_filename
);
1230 config_filename
= git_pathdup("config");
1231 out_fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
1233 ret
= error("could not lock config file %s", config_filename
);
1237 if (!(config_file
= fopen(config_filename
, "rb"))) {
1238 /* no config file means nothing to rename, no error */
1239 goto unlock_and_out
;
1242 while (fgets(buf
, sizeof(buf
), config_file
)) {
1246 for (i
= 0; buf
[i
] && isspace(buf
[i
]); i
++)
1248 if (buf
[i
] == '[') {
1249 /* it's a section */
1250 int offset
= section_name_match(&buf
[i
], old_name
);
1253 if (new_name
== NULL
) {
1257 store
.baselen
= strlen(new_name
);
1258 if (!store_write_section(out_fd
, new_name
)) {
1259 ret
= write_error(lock
->filename
);
1263 * We wrote out the new section, with
1264 * a newline, now skip the old
1267 output
+= offset
+ i
;
1268 if (strlen(output
) > 0) {
1270 * More content means there's
1271 * a declaration to put on the
1272 * next line; indent with a
1283 length
= strlen(output
);
1284 if (write_in_full(out_fd
, output
, length
) != length
) {
1285 ret
= write_error(lock
->filename
);
1289 fclose(config_file
);
1291 if (commit_lock_file(lock
) < 0)
1292 ret
= error("could not commit config file %s", config_filename
);
1294 free(config_filename
);
1299 * Call this to report error for your variable that should not
1300 * get a boolean value (i.e. "[my] var" means "true").
1302 int config_error_nonbool(const char *var
)
1304 return error("Missing value for '%s'", var
);