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_bool_or_int(const char *name
, const char *value
, int *is_bool
)
332 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") || !strcasecmp(value
, "on"))
334 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no") || !strcasecmp(value
, "off"))
337 return git_config_int(name
, value
);
340 int git_config_bool(const char *name
, const char *value
)
343 return !!git_config_bool_or_int(name
, value
, &discard
);
346 int git_config_string(const char **dest
, const char *var
, const char *value
)
349 return config_error_nonbool(var
);
350 *dest
= xstrdup(value
);
354 int git_config_pathname(const char **dest
, const char *var
, const char *value
)
357 return config_error_nonbool(var
);
358 *dest
= expand_user_path(value
);
360 die("Failed to expand user dir in: '%s'", value
);
364 static int git_default_core_config(const char *var
, const char *value
)
366 /* This needs a better name */
367 if (!strcmp(var
, "core.filemode")) {
368 trust_executable_bit
= git_config_bool(var
, value
);
371 if (!strcmp(var
, "core.trustctime")) {
372 trust_ctime
= git_config_bool(var
, value
);
376 if (!strcmp(var
, "core.quotepath")) {
377 quote_path_fully
= git_config_bool(var
, value
);
381 if (!strcmp(var
, "core.symlinks")) {
382 has_symlinks
= git_config_bool(var
, value
);
386 if (!strcmp(var
, "core.ignorecase")) {
387 ignore_case
= git_config_bool(var
, value
);
391 if (!strcmp(var
, "core.bare")) {
392 is_bare_repository_cfg
= git_config_bool(var
, value
);
396 if (!strcmp(var
, "core.ignorestat")) {
397 assume_unchanged
= git_config_bool(var
, value
);
401 if (!strcmp(var
, "core.prefersymlinkrefs")) {
402 prefer_symlink_refs
= git_config_bool(var
, value
);
406 if (!strcmp(var
, "core.logallrefupdates")) {
407 log_all_ref_updates
= git_config_bool(var
, value
);
411 if (!strcmp(var
, "core.warnambiguousrefs")) {
412 warn_ambiguous_refs
= git_config_bool(var
, value
);
416 if (!strcmp(var
, "core.loosecompression")) {
417 int level
= git_config_int(var
, value
);
419 level
= Z_DEFAULT_COMPRESSION
;
420 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
421 die("bad zlib compression level %d", level
);
422 zlib_compression_level
= level
;
423 zlib_compression_seen
= 1;
427 if (!strcmp(var
, "core.compression")) {
428 int level
= git_config_int(var
, value
);
430 level
= Z_DEFAULT_COMPRESSION
;
431 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
432 die("bad zlib compression level %d", level
);
433 core_compression_level
= level
;
434 core_compression_seen
= 1;
435 if (!zlib_compression_seen
)
436 zlib_compression_level
= level
;
440 if (!strcmp(var
, "core.packedgitwindowsize")) {
441 int pgsz_x2
= getpagesize() * 2;
442 packed_git_window_size
= git_config_int(var
, value
);
444 /* This value must be multiple of (pagesize * 2) */
445 packed_git_window_size
/= pgsz_x2
;
446 if (packed_git_window_size
< 1)
447 packed_git_window_size
= 1;
448 packed_git_window_size
*= pgsz_x2
;
452 if (!strcmp(var
, "core.packedgitlimit")) {
453 packed_git_limit
= git_config_int(var
, value
);
457 if (!strcmp(var
, "core.deltabasecachelimit")) {
458 delta_base_cache_limit
= git_config_int(var
, value
);
462 if (!strcmp(var
, "core.autocrlf")) {
463 if (value
&& !strcasecmp(value
, "input")) {
465 return error("core.autocrlf=input conflicts with core.eol=crlf");
466 auto_crlf
= AUTO_CRLF_INPUT
;
469 auto_crlf
= git_config_bool(var
, value
);
473 if (!strcmp(var
, "core.safecrlf")) {
474 if (value
&& !strcasecmp(value
, "warn")) {
475 safe_crlf
= SAFE_CRLF_WARN
;
478 safe_crlf
= git_config_bool(var
, value
);
482 if (!strcmp(var
, "core.eol")) {
483 if (value
&& !strcasecmp(value
, "lf"))
485 else if (value
&& !strcasecmp(value
, "crlf"))
487 else if (value
&& !strcasecmp(value
, "native"))
491 if (eol
== EOL_CRLF
&& auto_crlf
== AUTO_CRLF_INPUT
)
492 return error("core.autocrlf=input conflicts with core.eol=crlf");
496 if (!strcmp(var
, "core.notesref")) {
497 notes_ref_name
= xstrdup(value
);
501 if (!strcmp(var
, "core.pager"))
502 return git_config_string(&pager_program
, var
, value
);
504 if (!strcmp(var
, "core.editor"))
505 return git_config_string(&editor_program
, var
, value
);
507 if (!strcmp(var
, "core.excludesfile"))
508 return git_config_pathname(&excludes_file
, var
, value
);
510 if (!strcmp(var
, "core.whitespace")) {
512 return config_error_nonbool(var
);
513 whitespace_rule_cfg
= parse_whitespace_rule(value
);
517 if (!strcmp(var
, "core.fsyncobjectfiles")) {
518 fsync_object_files
= git_config_bool(var
, value
);
522 if (!strcmp(var
, "core.preloadindex")) {
523 core_preload_index
= git_config_bool(var
, value
);
527 if (!strcmp(var
, "core.createobject")) {
528 if (!strcmp(value
, "rename"))
529 object_creation_mode
= OBJECT_CREATION_USES_RENAMES
;
530 else if (!strcmp(value
, "link"))
531 object_creation_mode
= OBJECT_CREATION_USES_HARDLINKS
;
533 die("Invalid mode for object creation: %s", value
);
537 if (!strcmp(var
, "core.sparsecheckout")) {
538 core_apply_sparse_checkout
= git_config_bool(var
, value
);
542 /* Add other config variables here and to Documentation/config.txt. */
546 static int git_default_user_config(const char *var
, const char *value
)
548 if (!strcmp(var
, "user.name")) {
550 return config_error_nonbool(var
);
551 strlcpy(git_default_name
, value
, sizeof(git_default_name
));
552 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
556 if (!strcmp(var
, "user.email")) {
558 return config_error_nonbool(var
);
559 strlcpy(git_default_email
, value
, sizeof(git_default_email
));
560 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
564 /* Add other config variables here and to Documentation/config.txt. */
568 static int git_default_i18n_config(const char *var
, const char *value
)
570 if (!strcmp(var
, "i18n.commitencoding"))
571 return git_config_string(&git_commit_encoding
, var
, value
);
573 if (!strcmp(var
, "i18n.logoutputencoding"))
574 return git_config_string(&git_log_output_encoding
, var
, value
);
576 /* Add other config variables here and to Documentation/config.txt. */
580 static int git_default_branch_config(const char *var
, const char *value
)
582 if (!strcmp(var
, "branch.autosetupmerge")) {
583 if (value
&& !strcasecmp(value
, "always")) {
584 git_branch_track
= BRANCH_TRACK_ALWAYS
;
587 git_branch_track
= git_config_bool(var
, value
);
590 if (!strcmp(var
, "branch.autosetuprebase")) {
592 return config_error_nonbool(var
);
593 else if (!strcmp(value
, "never"))
594 autorebase
= AUTOREBASE_NEVER
;
595 else if (!strcmp(value
, "local"))
596 autorebase
= AUTOREBASE_LOCAL
;
597 else if (!strcmp(value
, "remote"))
598 autorebase
= AUTOREBASE_REMOTE
;
599 else if (!strcmp(value
, "always"))
600 autorebase
= AUTOREBASE_ALWAYS
;
602 return error("Malformed value for %s", var
);
606 /* Add other config variables here and to Documentation/config.txt. */
610 static int git_default_push_config(const char *var
, const char *value
)
612 if (!strcmp(var
, "push.default")) {
614 return config_error_nonbool(var
);
615 else if (!strcmp(value
, "nothing"))
616 push_default
= PUSH_DEFAULT_NOTHING
;
617 else if (!strcmp(value
, "matching"))
618 push_default
= PUSH_DEFAULT_MATCHING
;
619 else if (!strcmp(value
, "tracking"))
620 push_default
= PUSH_DEFAULT_TRACKING
;
621 else if (!strcmp(value
, "current"))
622 push_default
= PUSH_DEFAULT_CURRENT
;
624 error("Malformed value for %s: %s", var
, value
);
625 return error("Must be one of nothing, matching, "
626 "tracking or current.");
631 /* Add other config variables here and to Documentation/config.txt. */
635 static int git_default_mailmap_config(const char *var
, const char *value
)
637 if (!strcmp(var
, "mailmap.file"))
638 return git_config_string(&git_mailmap_file
, var
, value
);
640 /* Add other config variables here and to Documentation/config.txt. */
644 int git_default_config(const char *var
, const char *value
, void *dummy
)
646 if (!prefixcmp(var
, "core."))
647 return git_default_core_config(var
, value
);
649 if (!prefixcmp(var
, "user."))
650 return git_default_user_config(var
, value
);
652 if (!prefixcmp(var
, "i18n."))
653 return git_default_i18n_config(var
, value
);
655 if (!prefixcmp(var
, "branch."))
656 return git_default_branch_config(var
, value
);
658 if (!prefixcmp(var
, "push."))
659 return git_default_push_config(var
, value
);
661 if (!prefixcmp(var
, "mailmap."))
662 return git_default_mailmap_config(var
, value
);
664 if (!prefixcmp(var
, "advice."))
665 return git_default_advice_config(var
, value
);
667 if (!strcmp(var
, "pager.color") || !strcmp(var
, "color.pager")) {
668 pager_use_color
= git_config_bool(var
,value
);
672 /* Add other config variables here and to Documentation/config.txt. */
676 int git_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
679 FILE *f
= fopen(filename
, "r");
684 config_file_name
= filename
;
687 ret
= git_parse_file(fn
, data
);
689 config_file_name
= NULL
;
694 const char *git_etc_gitconfig(void)
696 static const char *system_wide
;
698 system_wide
= system_path(ETC_GITCONFIG
);
702 static int git_env_bool(const char *k
, int def
)
704 const char *v
= getenv(k
);
705 return v
? git_config_bool(k
, v
) : def
;
708 int git_config_system(void)
710 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
713 int git_config_global(void)
715 return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
718 int git_config(config_fn_t fn
, void *data
)
720 int ret
= 0, found
= 0;
721 char *repo_config
= NULL
;
722 const char *home
= NULL
;
724 /* Setting $GIT_CONFIG makes git read _only_ the given config file. */
725 if (config_exclusive_filename
)
726 return git_config_from_file(fn
, config_exclusive_filename
, data
);
727 if (git_config_system() && !access(git_etc_gitconfig(), R_OK
)) {
728 ret
+= git_config_from_file(fn
, git_etc_gitconfig(),
733 home
= getenv("HOME");
734 if (git_config_global() && home
) {
735 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
736 if (!access(user_config
, R_OK
)) {
737 ret
+= git_config_from_file(fn
, user_config
, data
);
743 repo_config
= git_pathdup("config");
744 if (!access(repo_config
, R_OK
)) {
745 ret
+= git_config_from_file(fn
, repo_config
, data
);
755 * Find all the stuff for git_config_set() below.
758 #define MAX_MATCHES 512
764 regex_t
*value_regex
;
766 size_t offset
[MAX_MATCHES
];
767 enum { START
, SECTION_SEEN
, SECTION_END_SEEN
, KEY_SEEN
} state
;
771 static int matches(const char *key
, const char *value
)
773 return !strcmp(key
, store
.key
) &&
774 (store
.value_regex
== NULL
||
775 (store
.do_not_match
^
776 !regexec(store
.value_regex
, value
, 0, NULL
, 0)));
779 static int store_aux(const char *key
, const char *value
, void *cb
)
784 switch (store
.state
) {
786 if (matches(key
, value
)) {
787 if (store
.seen
== 1 && store
.multi_replace
== 0) {
788 warning("%s has multiple values", key
);
789 } else if (store
.seen
>= MAX_MATCHES
) {
790 error("too many matches for %s", key
);
794 store
.offset
[store
.seen
] = ftell(config_file
);
800 * What we are looking for is in store.key (both
801 * section and var), and its section part is baselen
802 * long. We found key (again, both section and var).
803 * We would want to know if this key is in the same
804 * section as what we are looking for. We already
805 * know we are in the same section as what should
808 ep
= strrchr(key
, '.');
809 section_len
= ep
- key
;
811 if ((section_len
!= store
.baselen
) ||
812 memcmp(key
, store
.key
, section_len
+1)) {
813 store
.state
= SECTION_END_SEEN
;
818 * Do not increment matches: this is no match, but we
819 * just made sure we are in the desired section.
821 store
.offset
[store
.seen
] = ftell(config_file
);
823 case SECTION_END_SEEN
:
825 if (matches(key
, value
)) {
826 store
.offset
[store
.seen
] = ftell(config_file
);
827 store
.state
= KEY_SEEN
;
830 if (strrchr(key
, '.') - key
== store
.baselen
&&
831 !strncmp(key
, store
.key
, store
.baselen
)) {
832 store
.state
= SECTION_SEEN
;
833 store
.offset
[store
.seen
] = ftell(config_file
);
840 static int write_error(const char *filename
)
842 error("failed to write new configuration file %s", filename
);
844 /* Same error code as "failed to rename". */
848 static int store_write_section(int fd
, const char *key
)
852 struct strbuf sb
= STRBUF_INIT
;
854 dot
= memchr(key
, '.', store
.baselen
);
856 strbuf_addf(&sb
, "[%.*s \"", (int)(dot
- key
), key
);
857 for (i
= dot
- key
+ 1; i
< store
.baselen
; i
++) {
858 if (key
[i
] == '"' || key
[i
] == '\\')
859 strbuf_addch(&sb
, '\\');
860 strbuf_addch(&sb
, key
[i
]);
862 strbuf_addstr(&sb
, "\"]\n");
864 strbuf_addf(&sb
, "[%.*s]\n", store
.baselen
, key
);
867 success
= write_in_full(fd
, sb
.buf
, sb
.len
) == sb
.len
;
873 static int store_write_pair(int fd
, const char *key
, const char *value
)
876 int length
= strlen(key
+ store
.baselen
+ 1);
877 const char *quote
= "";
878 struct strbuf sb
= STRBUF_INIT
;
881 * Check to see if the value needs to be surrounded with a dq pair.
882 * Note that problematic characters are always backslash-quoted; this
883 * check is about not losing leading or trailing SP and strings that
884 * follow beginning-of-comment characters (i.e. ';' and '#') by the
885 * configuration parser.
889 for (i
= 0; value
[i
]; i
++)
890 if (value
[i
] == ';' || value
[i
] == '#')
892 if (i
&& value
[i
- 1] == ' ')
895 strbuf_addf(&sb
, "\t%.*s = %s",
896 length
, key
+ store
.baselen
+ 1, quote
);
898 for (i
= 0; value
[i
]; i
++)
901 strbuf_addstr(&sb
, "\\n");
904 strbuf_addstr(&sb
, "\\t");
908 strbuf_addch(&sb
, '\\');
910 strbuf_addch(&sb
, value
[i
]);
913 strbuf_addf(&sb
, "%s\n", quote
);
915 success
= write_in_full(fd
, sb
.buf
, sb
.len
) == sb
.len
;
921 static ssize_t
find_beginning_of_line(const char *contents
, size_t size
,
922 size_t offset_
, int *found_bracket
)
924 size_t equal_offset
= size
, bracket_offset
= size
;
928 for (offset
= offset_
-2; offset
> 0
929 && contents
[offset
] != '\n'; offset
--)
930 switch (contents
[offset
]) {
931 case '=': equal_offset
= offset
; break;
932 case ']': bracket_offset
= offset
; break;
934 if (offset
> 0 && contents
[offset
-1] == '\\') {
938 if (bracket_offset
< equal_offset
) {
940 offset
= bracket_offset
+1;
947 int git_config_set(const char *key
, const char *value
)
949 return git_config_set_multivar(key
, value
, NULL
, 0);
953 * If value==NULL, unset in (remove from) config,
954 * if value_regex!=NULL, disregard key/value pairs where value does not match.
955 * if multi_replace==0, nothing, or only one matching key/value is replaced,
956 * else all matching key/values (regardless how many) are removed,
957 * before the new pair is written.
959 * Returns 0 on success.
961 * This function does this:
963 * - it locks the config file by creating ".git/config.lock"
965 * - it then parses the config using store_aux() as validator to find
966 * the position on the key/value pair to replace. If it is to be unset,
967 * it must be found exactly once.
969 * - the config file is mmap()ed and the part before the match (if any) is
970 * written to the lock file, then the changed part and the rest.
972 * - the config file is removed and the lock file rename()d to it.
975 int git_config_set_multivar(const char *key
, const char *value
,
976 const char *value_regex
, int multi_replace
)
981 char *config_filename
;
982 struct lock_file
*lock
= NULL
;
983 const char *last_dot
= strrchr(key
, '.');
985 if (config_exclusive_filename
)
986 config_filename
= xstrdup(config_exclusive_filename
);
988 config_filename
= git_pathdup("config");
991 * Since "key" actually contains the section name and the real
992 * key name separated by a dot, we have to know where the dot is.
995 if (last_dot
== NULL
) {
996 error("key does not contain a section: %s", key
);
1000 store
.baselen
= last_dot
- key
;
1002 store
.multi_replace
= multi_replace
;
1005 * Validate the key and while at it, lower case it for matching.
1007 store
.key
= xmalloc(strlen(key
) + 1);
1009 for (i
= 0; key
[i
]; i
++) {
1010 unsigned char c
= key
[i
];
1013 /* Leave the extended basename untouched.. */
1014 if (!dot
|| i
> store
.baselen
) {
1015 if (!iskeychar(c
) || (i
== store
.baselen
+1 && !isalpha(c
))) {
1016 error("invalid key: %s", key
);
1022 } else if (c
== '\n') {
1023 error("invalid key (newline): %s", key
);
1033 * The lock serves a purpose in addition to locking: the new
1034 * contents of .git/config will be written into it.
1036 lock
= xcalloc(sizeof(struct lock_file
), 1);
1037 fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
1039 error("could not lock config file %s: %s", config_filename
, strerror(errno
));
1046 * If .git/config does not exist yet, write a minimal version.
1048 in_fd
= open(config_filename
, O_RDONLY
);
1052 if ( ENOENT
!= errno
) {
1053 error("opening %s: %s", config_filename
,
1055 ret
= 3; /* same as "invalid config file" */
1058 /* if nothing to unset, error out */
1059 if (value
== NULL
) {
1064 store
.key
= (char *)key
;
1065 if (!store_write_section(fd
, key
) ||
1066 !store_write_pair(fd
, key
, value
))
1071 size_t contents_sz
, copy_begin
, copy_end
;
1072 int i
, new_line
= 0;
1074 if (value_regex
== NULL
)
1075 store
.value_regex
= NULL
;
1077 if (value_regex
[0] == '!') {
1078 store
.do_not_match
= 1;
1081 store
.do_not_match
= 0;
1083 store
.value_regex
= (regex_t
*)xmalloc(sizeof(regex_t
));
1084 if (regcomp(store
.value_regex
, value_regex
,
1086 error("invalid pattern: %s", value_regex
);
1087 free(store
.value_regex
);
1093 store
.offset
[0] = 0;
1094 store
.state
= START
;
1098 * After this, store.offset will contain the *end* offset
1099 * of the last match, or remain at 0 if no match was found.
1100 * As a side effect, we make sure to transform only a valid
1101 * existing config file.
1103 if (git_config_from_file(store_aux
, config_filename
, NULL
)) {
1104 error("invalid config file %s", config_filename
);
1106 if (store
.value_regex
!= NULL
) {
1107 regfree(store
.value_regex
);
1108 free(store
.value_regex
);
1115 if (store
.value_regex
!= NULL
) {
1116 regfree(store
.value_regex
);
1117 free(store
.value_regex
);
1120 /* if nothing to unset, or too many matches, error out */
1121 if ((store
.seen
== 0 && value
== NULL
) ||
1122 (store
.seen
> 1 && multi_replace
== 0)) {
1128 contents_sz
= xsize_t(st
.st_size
);
1129 contents
= xmmap(NULL
, contents_sz
, PROT_READ
,
1130 MAP_PRIVATE
, in_fd
, 0);
1133 if (store
.seen
== 0)
1136 for (i
= 0, copy_begin
= 0; i
< store
.seen
; i
++) {
1137 if (store
.offset
[i
] == 0) {
1138 store
.offset
[i
] = copy_end
= contents_sz
;
1139 } else if (store
.state
!= KEY_SEEN
) {
1140 copy_end
= store
.offset
[i
];
1142 copy_end
= find_beginning_of_line(
1143 contents
, contents_sz
,
1144 store
.offset
[i
]-2, &new_line
);
1146 if (copy_end
> 0 && contents
[copy_end
-1] != '\n')
1149 /* write the first part of the config */
1150 if (copy_end
> copy_begin
) {
1151 if (write_in_full(fd
, contents
+ copy_begin
,
1152 copy_end
- copy_begin
) <
1153 copy_end
- copy_begin
)
1156 write_str_in_full(fd
, "\n") != 1)
1159 copy_begin
= store
.offset
[i
];
1162 /* write the pair (value == NULL means unset) */
1163 if (value
!= NULL
) {
1164 if (store
.state
== START
) {
1165 if (!store_write_section(fd
, key
))
1168 if (!store_write_pair(fd
, key
, value
))
1172 /* write the rest of the config */
1173 if (copy_begin
< contents_sz
)
1174 if (write_in_full(fd
, contents
+ copy_begin
,
1175 contents_sz
- copy_begin
) <
1176 contents_sz
- copy_begin
)
1179 munmap(contents
, contents_sz
);
1182 if (commit_lock_file(lock
) < 0) {
1183 error("could not commit config file %s", config_filename
);
1189 * lock is committed, so don't try to roll it back below.
1190 * NOTE: Since lockfile.c keeps a linked list of all created
1191 * lock_file structures, it isn't safe to free(lock). It's
1192 * better to just leave it hanging around.
1199 rollback_lock_file(lock
);
1200 free(config_filename
);
1204 ret
= write_error(lock
->filename
);
1209 static int section_name_match (const char *buf
, const char *name
)
1211 int i
= 0, j
= 0, dot
= 0;
1214 for (i
= 1; buf
[i
] && buf
[i
] != ']'; i
++) {
1215 if (!dot
&& isspace(buf
[i
])) {
1217 if (name
[j
++] != '.')
1219 for (i
++; isspace(buf
[i
]); i
++)
1225 if (buf
[i
] == '\\' && dot
)
1227 else if (buf
[i
] == '"' && dot
) {
1228 for (i
++; isspace(buf
[i
]); i
++)
1232 if (buf
[i
] != name
[j
++])
1235 if (buf
[i
] == ']' && name
[j
] == 0) {
1237 * We match, now just find the right length offset by
1238 * gobbling up any whitespace after it, as well
1241 for (; buf
[i
] && isspace(buf
[i
]); i
++)
1248 /* if new_name == NULL, the section is removed instead */
1249 int git_config_rename_section(const char *old_name
, const char *new_name
)
1251 int ret
= 0, remove
= 0;
1252 char *config_filename
;
1253 struct lock_file
*lock
= xcalloc(sizeof(struct lock_file
), 1);
1257 if (config_exclusive_filename
)
1258 config_filename
= xstrdup(config_exclusive_filename
);
1260 config_filename
= git_pathdup("config");
1261 out_fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
1263 ret
= error("could not lock config file %s", config_filename
);
1267 if (!(config_file
= fopen(config_filename
, "rb"))) {
1268 /* no config file means nothing to rename, no error */
1269 goto unlock_and_out
;
1272 while (fgets(buf
, sizeof(buf
), config_file
)) {
1276 for (i
= 0; buf
[i
] && isspace(buf
[i
]); i
++)
1278 if (buf
[i
] == '[') {
1279 /* it's a section */
1280 int offset
= section_name_match(&buf
[i
], old_name
);
1283 if (new_name
== NULL
) {
1287 store
.baselen
= strlen(new_name
);
1288 if (!store_write_section(out_fd
, new_name
)) {
1289 ret
= write_error(lock
->filename
);
1293 * We wrote out the new section, with
1294 * a newline, now skip the old
1297 output
+= offset
+ i
;
1298 if (strlen(output
) > 0) {
1300 * More content means there's
1301 * a declaration to put on the
1302 * next line; indent with a
1313 length
= strlen(output
);
1314 if (write_in_full(out_fd
, output
, length
) != length
) {
1315 ret
= write_error(lock
->filename
);
1319 fclose(config_file
);
1321 if (commit_lock_file(lock
) < 0)
1322 ret
= error("could not commit config file %s", config_filename
);
1324 free(config_filename
);
1329 * Call this to report error for your variable that should not
1330 * get a boolean value (i.e. "[my] var" means "true").
1332 int config_error_nonbool(const char *var
)
1334 return error("Missing value for '%s'", var
);