2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
14 static FILE *config_file
;
15 static const char *config_file_name
;
16 static int config_linenr
;
17 static int config_file_eof
;
18 static int zlib_compression_seen
;
20 const char *config_exclusive_filename
= NULL
;
24 struct config_item
*next
;
28 static struct config_item
*config_parameters
;
29 static struct config_item
**config_parameters_tail
= &config_parameters
;
31 static void lowercase(char *p
)
37 int git_config_parse_parameter(const char *text
)
39 struct config_item
*ct
;
40 struct strbuf tmp
= STRBUF_INIT
;
42 strbuf_addstr(&tmp
, text
);
43 pair
= strbuf_split(&tmp
, '=');
44 if (pair
[0]->len
&& pair
[0]->buf
[pair
[0]->len
- 1] == '=')
45 strbuf_setlen(pair
[0], pair
[0]->len
- 1);
48 strbuf_list_free(pair
);
51 ct
= xcalloc(1, sizeof(struct config_item
));
52 ct
->name
= strbuf_detach(pair
[0], NULL
);
55 ct
->value
= strbuf_detach(pair
[1], NULL
);
57 strbuf_list_free(pair
);
59 *config_parameters_tail
= ct
;
60 config_parameters_tail
= &ct
->next
;
64 static int get_next_char(void)
70 if ((f
= config_file
) != NULL
) {
73 /* DOS like systems */
90 static char *parse_value(void)
92 static char value
[1024];
93 int quote
= 0, comment
= 0, len
= 0, space
= 0;
96 int c
= get_next_char();
97 if (len
>= sizeof(value
) - 1)
107 if (isspace(c
) && !quote
) {
113 if (c
== ';' || c
== '#') {
118 for (; space
; space
--)
134 /* Some characters escape as themselves */
137 /* Reject unknown escape sequences */
152 static inline int iskeychar(int c
)
154 return isalnum(c
) || c
== '-';
157 static int get_value(config_fn_t fn
, void *data
, char *name
, unsigned int len
)
162 /* Get the full name */
169 name
[len
++] = tolower(c
);
174 while (c
== ' ' || c
== '\t')
181 value
= parse_value();
185 return fn(name
, value
, data
);
188 static int get_extended_base_var(char *name
, int baselen
, int c
)
194 } while (isspace(c
));
196 /* We require the format to be '[base "extension"]' */
199 name
[baselen
++] = '.';
202 int c
= get_next_char();
213 if (baselen
> MAXNAME
/ 2)
218 if (get_next_char() != ']')
223 static int get_base_var(char *name
)
228 int c
= get_next_char();
234 return get_extended_base_var(name
, baselen
, c
);
235 if (!iskeychar(c
) && c
!= '.')
237 if (baselen
> MAXNAME
/ 2)
239 name
[baselen
++] = tolower(c
);
243 static int git_parse_file(config_fn_t fn
, void *data
)
247 static char var
[MAXNAME
];
249 /* U+FEFF Byte Order Mark in UTF8 */
250 static const unsigned char *utf8_bom
= (unsigned char *) "\xef\xbb\xbf";
251 const unsigned char *bomptr
= utf8_bom
;
254 int c
= get_next_char();
255 if (bomptr
&& *bomptr
) {
256 /* We are at the file beginning; skip UTF8-encoded BOM
257 * if present. Sane editors won't put this in on their
258 * own, but e.g. Windows Notepad will do it happily. */
259 if ((unsigned char) c
== *bomptr
) {
263 /* Do not tolerate partial BOM. */
264 if (bomptr
!= utf8_bom
)
266 /* No BOM at file beginning. Cool. */
276 if (comment
|| isspace(c
))
278 if (c
== '#' || c
== ';') {
283 baselen
= get_base_var(var
);
286 var
[baselen
++] = '.';
292 var
[baselen
] = tolower(c
);
293 if (get_value(fn
, data
, var
, baselen
+1) < 0)
296 die("bad config file line %d in %s", config_linenr
, config_file_name
);
299 static int parse_unit_factor(const char *end
, unsigned long *val
)
303 else if (!strcasecmp(end
, "k")) {
307 else if (!strcasecmp(end
, "m")) {
311 else if (!strcasecmp(end
, "g")) {
312 *val
*= 1024 * 1024 * 1024;
318 static int git_parse_long(const char *value
, long *ret
)
320 if (value
&& *value
) {
322 long val
= strtol(value
, &end
, 0);
323 unsigned long factor
= 1;
324 if (!parse_unit_factor(end
, &factor
))
332 int git_parse_ulong(const char *value
, unsigned long *ret
)
334 if (value
&& *value
) {
336 unsigned long val
= strtoul(value
, &end
, 0);
337 if (!parse_unit_factor(end
, &val
))
345 static void die_bad_config(const char *name
)
347 if (config_file_name
)
348 die("bad config value for '%s' in %s", name
, config_file_name
);
349 die("bad config value for '%s'", name
);
352 int git_config_int(const char *name
, const char *value
)
355 if (!git_parse_long(value
, &ret
))
356 die_bad_config(name
);
360 unsigned long git_config_ulong(const char *name
, const char *value
)
363 if (!git_parse_ulong(value
, &ret
))
364 die_bad_config(name
);
368 int git_config_bool_or_int(const char *name
, const char *value
, int *is_bool
)
375 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") || !strcasecmp(value
, "on"))
377 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no") || !strcasecmp(value
, "off"))
380 return git_config_int(name
, value
);
383 int git_config_bool(const char *name
, const char *value
)
386 return !!git_config_bool_or_int(name
, value
, &discard
);
389 int git_config_string(const char **dest
, const char *var
, const char *value
)
392 return config_error_nonbool(var
);
393 *dest
= xstrdup(value
);
397 int git_config_pathname(const char **dest
, const char *var
, const char *value
)
400 return config_error_nonbool(var
);
401 *dest
= expand_user_path(value
);
403 die("Failed to expand user dir in: '%s'", value
);
407 static int git_default_core_config(const char *var
, const char *value
)
409 /* This needs a better name */
410 if (!strcmp(var
, "core.filemode")) {
411 trust_executable_bit
= git_config_bool(var
, value
);
414 if (!strcmp(var
, "core.trustctime")) {
415 trust_ctime
= git_config_bool(var
, value
);
419 if (!strcmp(var
, "core.quotepath")) {
420 quote_path_fully
= git_config_bool(var
, value
);
424 if (!strcmp(var
, "core.symlinks")) {
425 has_symlinks
= git_config_bool(var
, value
);
429 if (!strcmp(var
, "core.ignorecase")) {
430 ignore_case
= git_config_bool(var
, value
);
434 if (!strcmp(var
, "core.bare")) {
435 is_bare_repository_cfg
= git_config_bool(var
, value
);
439 if (!strcmp(var
, "core.ignorestat")) {
440 assume_unchanged
= git_config_bool(var
, value
);
444 if (!strcmp(var
, "core.prefersymlinkrefs")) {
445 prefer_symlink_refs
= git_config_bool(var
, value
);
449 if (!strcmp(var
, "core.logallrefupdates")) {
450 log_all_ref_updates
= git_config_bool(var
, value
);
454 if (!strcmp(var
, "core.warnambiguousrefs")) {
455 warn_ambiguous_refs
= git_config_bool(var
, value
);
459 if (!strcmp(var
, "core.loosecompression")) {
460 int level
= git_config_int(var
, value
);
462 level
= Z_DEFAULT_COMPRESSION
;
463 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
464 die("bad zlib compression level %d", level
);
465 zlib_compression_level
= level
;
466 zlib_compression_seen
= 1;
470 if (!strcmp(var
, "core.compression")) {
471 int level
= git_config_int(var
, value
);
473 level
= Z_DEFAULT_COMPRESSION
;
474 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
475 die("bad zlib compression level %d", level
);
476 core_compression_level
= level
;
477 core_compression_seen
= 1;
478 if (!zlib_compression_seen
)
479 zlib_compression_level
= level
;
483 if (!strcmp(var
, "core.packedgitwindowsize")) {
484 int pgsz_x2
= getpagesize() * 2;
485 packed_git_window_size
= git_config_int(var
, value
);
487 /* This value must be multiple of (pagesize * 2) */
488 packed_git_window_size
/= pgsz_x2
;
489 if (packed_git_window_size
< 1)
490 packed_git_window_size
= 1;
491 packed_git_window_size
*= pgsz_x2
;
495 if (!strcmp(var
, "core.packedgitlimit")) {
496 packed_git_limit
= git_config_int(var
, value
);
500 if (!strcmp(var
, "core.deltabasecachelimit")) {
501 delta_base_cache_limit
= git_config_int(var
, value
);
505 if (!strcmp(var
, "core.autocrlf")) {
506 if (value
&& !strcasecmp(value
, "input")) {
510 auto_crlf
= git_config_bool(var
, value
);
514 if (!strcmp(var
, "core.safecrlf")) {
515 if (value
&& !strcasecmp(value
, "warn")) {
516 safe_crlf
= SAFE_CRLF_WARN
;
519 safe_crlf
= git_config_bool(var
, value
);
523 if (!strcmp(var
, "core.notesref")) {
524 notes_ref_name
= xstrdup(value
);
528 if (!strcmp(var
, "core.pager"))
529 return git_config_string(&pager_program
, var
, value
);
531 if (!strcmp(var
, "core.editor"))
532 return git_config_string(&editor_program
, var
, value
);
534 if (!strcmp(var
, "core.excludesfile"))
535 return git_config_pathname(&excludes_file
, var
, value
);
537 if (!strcmp(var
, "core.whitespace")) {
539 return config_error_nonbool(var
);
540 whitespace_rule_cfg
= parse_whitespace_rule(value
);
544 if (!strcmp(var
, "core.fsyncobjectfiles")) {
545 fsync_object_files
= git_config_bool(var
, value
);
549 if (!strcmp(var
, "core.preloadindex")) {
550 core_preload_index
= git_config_bool(var
, value
);
554 if (!strcmp(var
, "core.createobject")) {
555 if (!strcmp(value
, "rename"))
556 object_creation_mode
= OBJECT_CREATION_USES_RENAMES
;
557 else if (!strcmp(value
, "link"))
558 object_creation_mode
= OBJECT_CREATION_USES_HARDLINKS
;
560 die("Invalid mode for object creation: %s", value
);
564 if (!strcmp(var
, "core.sparsecheckout")) {
565 core_apply_sparse_checkout
= git_config_bool(var
, value
);
569 /* Add other config variables here and to Documentation/config.txt. */
573 static int git_default_user_config(const char *var
, const char *value
)
575 if (!strcmp(var
, "user.name")) {
577 return config_error_nonbool(var
);
578 strlcpy(git_default_name
, value
, sizeof(git_default_name
));
579 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
583 if (!strcmp(var
, "user.email")) {
585 return config_error_nonbool(var
);
586 strlcpy(git_default_email
, value
, sizeof(git_default_email
));
587 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
591 /* Add other config variables here and to Documentation/config.txt. */
595 static int git_default_i18n_config(const char *var
, const char *value
)
597 if (!strcmp(var
, "i18n.commitencoding"))
598 return git_config_string(&git_commit_encoding
, var
, value
);
600 if (!strcmp(var
, "i18n.logoutputencoding"))
601 return git_config_string(&git_log_output_encoding
, var
, value
);
603 /* Add other config variables here and to Documentation/config.txt. */
607 static int git_default_branch_config(const char *var
, const char *value
)
609 if (!strcmp(var
, "branch.autosetupmerge")) {
610 if (value
&& !strcasecmp(value
, "always")) {
611 git_branch_track
= BRANCH_TRACK_ALWAYS
;
614 git_branch_track
= git_config_bool(var
, value
);
617 if (!strcmp(var
, "branch.autosetuprebase")) {
619 return config_error_nonbool(var
);
620 else if (!strcmp(value
, "never"))
621 autorebase
= AUTOREBASE_NEVER
;
622 else if (!strcmp(value
, "local"))
623 autorebase
= AUTOREBASE_LOCAL
;
624 else if (!strcmp(value
, "remote"))
625 autorebase
= AUTOREBASE_REMOTE
;
626 else if (!strcmp(value
, "always"))
627 autorebase
= AUTOREBASE_ALWAYS
;
629 return error("Malformed value for %s", var
);
633 /* Add other config variables here and to Documentation/config.txt. */
637 static int git_default_push_config(const char *var
, const char *value
)
639 if (!strcmp(var
, "push.default")) {
641 return config_error_nonbool(var
);
642 else if (!strcmp(value
, "nothing"))
643 push_default
= PUSH_DEFAULT_NOTHING
;
644 else if (!strcmp(value
, "matching"))
645 push_default
= PUSH_DEFAULT_MATCHING
;
646 else if (!strcmp(value
, "tracking"))
647 push_default
= PUSH_DEFAULT_TRACKING
;
648 else if (!strcmp(value
, "current"))
649 push_default
= PUSH_DEFAULT_CURRENT
;
651 error("Malformed value for %s: %s", var
, value
);
652 return error("Must be one of nothing, matching, "
653 "tracking or current.");
658 /* Add other config variables here and to Documentation/config.txt. */
662 static int git_default_mailmap_config(const char *var
, const char *value
)
664 if (!strcmp(var
, "mailmap.file"))
665 return git_config_string(&git_mailmap_file
, var
, value
);
667 /* Add other config variables here and to Documentation/config.txt. */
671 int git_default_config(const char *var
, const char *value
, void *dummy
)
673 if (!prefixcmp(var
, "core."))
674 return git_default_core_config(var
, value
);
676 if (!prefixcmp(var
, "user."))
677 return git_default_user_config(var
, value
);
679 if (!prefixcmp(var
, "i18n."))
680 return git_default_i18n_config(var
, value
);
682 if (!prefixcmp(var
, "branch."))
683 return git_default_branch_config(var
, value
);
685 if (!prefixcmp(var
, "push."))
686 return git_default_push_config(var
, value
);
688 if (!prefixcmp(var
, "mailmap."))
689 return git_default_mailmap_config(var
, value
);
691 if (!prefixcmp(var
, "advice."))
692 return git_default_advice_config(var
, value
);
694 if (!strcmp(var
, "pager.color") || !strcmp(var
, "color.pager")) {
695 pager_use_color
= git_config_bool(var
,value
);
699 /* Add other config variables here and to Documentation/config.txt. */
703 int git_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
706 FILE *f
= fopen(filename
, "r");
711 config_file_name
= filename
;
714 ret
= git_parse_file(fn
, data
);
716 config_file_name
= NULL
;
721 const char *git_etc_gitconfig(void)
723 static const char *system_wide
;
725 system_wide
= system_path(ETC_GITCONFIG
);
729 static int git_env_bool(const char *k
, int def
)
731 const char *v
= getenv(k
);
732 return v
? git_config_bool(k
, v
) : def
;
735 int git_config_system(void)
737 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
740 int git_config_global(void)
742 return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
745 int git_config_from_parameters(config_fn_t fn
, void *data
)
747 const struct config_item
*ct
;
748 for (ct
= config_parameters
; ct
; ct
= ct
->next
)
749 if (fn(ct
->name
, ct
->value
, data
) < 0)
754 int git_config(config_fn_t fn
, void *data
)
756 int ret
= 0, found
= 0;
757 char *repo_config
= NULL
;
758 const char *home
= NULL
;
760 /* Setting $GIT_CONFIG makes git read _only_ the given config file. */
761 if (config_exclusive_filename
)
762 return git_config_from_file(fn
, config_exclusive_filename
, data
);
763 if (git_config_system() && !access(git_etc_gitconfig(), R_OK
)) {
764 ret
+= git_config_from_file(fn
, git_etc_gitconfig(),
769 home
= getenv("HOME");
770 if (git_config_global() && home
) {
771 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
772 if (!access(user_config
, R_OK
)) {
773 ret
+= git_config_from_file(fn
, user_config
, data
);
779 repo_config
= git_pathdup("config");
780 if (!access(repo_config
, R_OK
)) {
781 ret
+= git_config_from_file(fn
, repo_config
, data
);
786 if (config_parameters
) {
787 ret
+= git_config_from_parameters(fn
, data
);
797 * Find all the stuff for git_config_set() below.
800 #define MAX_MATCHES 512
806 regex_t
*value_regex
;
808 size_t offset
[MAX_MATCHES
];
809 enum { START
, SECTION_SEEN
, SECTION_END_SEEN
, KEY_SEEN
} state
;
813 static int matches(const char *key
, const char *value
)
815 return !strcmp(key
, store
.key
) &&
816 (store
.value_regex
== NULL
||
817 (store
.do_not_match
^
818 !regexec(store
.value_regex
, value
, 0, NULL
, 0)));
821 static int store_aux(const char *key
, const char *value
, void *cb
)
826 switch (store
.state
) {
828 if (matches(key
, value
)) {
829 if (store
.seen
== 1 && store
.multi_replace
== 0) {
830 warning("%s has multiple values", key
);
831 } else if (store
.seen
>= MAX_MATCHES
) {
832 error("too many matches for %s", key
);
836 store
.offset
[store
.seen
] = ftell(config_file
);
842 * What we are looking for is in store.key (both
843 * section and var), and its section part is baselen
844 * long. We found key (again, both section and var).
845 * We would want to know if this key is in the same
846 * section as what we are looking for. We already
847 * know we are in the same section as what should
850 ep
= strrchr(key
, '.');
851 section_len
= ep
- key
;
853 if ((section_len
!= store
.baselen
) ||
854 memcmp(key
, store
.key
, section_len
+1)) {
855 store
.state
= SECTION_END_SEEN
;
860 * Do not increment matches: this is no match, but we
861 * just made sure we are in the desired section.
863 store
.offset
[store
.seen
] = ftell(config_file
);
865 case SECTION_END_SEEN
:
867 if (matches(key
, value
)) {
868 store
.offset
[store
.seen
] = ftell(config_file
);
869 store
.state
= KEY_SEEN
;
872 if (strrchr(key
, '.') - key
== store
.baselen
&&
873 !strncmp(key
, store
.key
, store
.baselen
)) {
874 store
.state
= SECTION_SEEN
;
875 store
.offset
[store
.seen
] = ftell(config_file
);
882 static int write_error(const char *filename
)
884 error("failed to write new configuration file %s", filename
);
886 /* Same error code as "failed to rename". */
890 static int store_write_section(int fd
, const char *key
)
894 struct strbuf sb
= STRBUF_INIT
;
896 dot
= memchr(key
, '.', store
.baselen
);
898 strbuf_addf(&sb
, "[%.*s \"", (int)(dot
- key
), key
);
899 for (i
= dot
- key
+ 1; i
< store
.baselen
; i
++) {
900 if (key
[i
] == '"' || key
[i
] == '\\')
901 strbuf_addch(&sb
, '\\');
902 strbuf_addch(&sb
, key
[i
]);
904 strbuf_addstr(&sb
, "\"]\n");
906 strbuf_addf(&sb
, "[%.*s]\n", store
.baselen
, key
);
909 success
= write_in_full(fd
, sb
.buf
, sb
.len
) == sb
.len
;
915 static int store_write_pair(int fd
, const char *key
, const char *value
)
918 int length
= strlen(key
+ store
.baselen
+ 1);
919 const char *quote
= "";
920 struct strbuf sb
= STRBUF_INIT
;
923 * Check to see if the value needs to be surrounded with a dq pair.
924 * Note that problematic characters are always backslash-quoted; this
925 * check is about not losing leading or trailing SP and strings that
926 * follow beginning-of-comment characters (i.e. ';' and '#') by the
927 * configuration parser.
931 for (i
= 0; value
[i
]; i
++)
932 if (value
[i
] == ';' || value
[i
] == '#')
934 if (i
&& value
[i
- 1] == ' ')
937 strbuf_addf(&sb
, "\t%.*s = %s",
938 length
, key
+ store
.baselen
+ 1, quote
);
940 for (i
= 0; value
[i
]; i
++)
943 strbuf_addstr(&sb
, "\\n");
946 strbuf_addstr(&sb
, "\\t");
950 strbuf_addch(&sb
, '\\');
952 strbuf_addch(&sb
, value
[i
]);
955 strbuf_addf(&sb
, "%s\n", quote
);
957 success
= write_in_full(fd
, sb
.buf
, sb
.len
) == sb
.len
;
963 static ssize_t
find_beginning_of_line(const char *contents
, size_t size
,
964 size_t offset_
, int *found_bracket
)
966 size_t equal_offset
= size
, bracket_offset
= size
;
970 for (offset
= offset_
-2; offset
> 0
971 && contents
[offset
] != '\n'; offset
--)
972 switch (contents
[offset
]) {
973 case '=': equal_offset
= offset
; break;
974 case ']': bracket_offset
= offset
; break;
976 if (offset
> 0 && contents
[offset
-1] == '\\') {
980 if (bracket_offset
< equal_offset
) {
982 offset
= bracket_offset
+1;
989 int git_config_set(const char *key
, const char *value
)
991 return git_config_set_multivar(key
, value
, NULL
, 0);
995 * If value==NULL, unset in (remove from) config,
996 * if value_regex!=NULL, disregard key/value pairs where value does not match.
997 * if multi_replace==0, nothing, or only one matching key/value is replaced,
998 * else all matching key/values (regardless how many) are removed,
999 * before the new pair is written.
1001 * Returns 0 on success.
1003 * This function does this:
1005 * - it locks the config file by creating ".git/config.lock"
1007 * - it then parses the config using store_aux() as validator to find
1008 * the position on the key/value pair to replace. If it is to be unset,
1009 * it must be found exactly once.
1011 * - the config file is mmap()ed and the part before the match (if any) is
1012 * written to the lock file, then the changed part and the rest.
1014 * - the config file is removed and the lock file rename()d to it.
1017 int git_config_set_multivar(const char *key
, const char *value
,
1018 const char *value_regex
, int multi_replace
)
1023 char *config_filename
;
1024 struct lock_file
*lock
= NULL
;
1025 const char *last_dot
= strrchr(key
, '.');
1027 if (config_exclusive_filename
)
1028 config_filename
= xstrdup(config_exclusive_filename
);
1030 config_filename
= git_pathdup("config");
1033 * Since "key" actually contains the section name and the real
1034 * key name separated by a dot, we have to know where the dot is.
1037 if (last_dot
== NULL
) {
1038 error("key does not contain a section: %s", key
);
1042 store
.baselen
= last_dot
- key
;
1044 store
.multi_replace
= multi_replace
;
1047 * Validate the key and while at it, lower case it for matching.
1049 store
.key
= xmalloc(strlen(key
) + 1);
1051 for (i
= 0; key
[i
]; i
++) {
1052 unsigned char c
= key
[i
];
1055 /* Leave the extended basename untouched.. */
1056 if (!dot
|| i
> store
.baselen
) {
1057 if (!iskeychar(c
) || (i
== store
.baselen
+1 && !isalpha(c
))) {
1058 error("invalid key: %s", key
);
1064 } else if (c
== '\n') {
1065 error("invalid key (newline): %s", key
);
1075 * The lock serves a purpose in addition to locking: the new
1076 * contents of .git/config will be written into it.
1078 lock
= xcalloc(sizeof(struct lock_file
), 1);
1079 fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
1081 error("could not lock config file %s: %s", config_filename
, strerror(errno
));
1088 * If .git/config does not exist yet, write a minimal version.
1090 in_fd
= open(config_filename
, O_RDONLY
);
1094 if ( ENOENT
!= errno
) {
1095 error("opening %s: %s", config_filename
,
1097 ret
= 3; /* same as "invalid config file" */
1100 /* if nothing to unset, error out */
1101 if (value
== NULL
) {
1106 store
.key
= (char *)key
;
1107 if (!store_write_section(fd
, key
) ||
1108 !store_write_pair(fd
, key
, value
))
1113 size_t contents_sz
, copy_begin
, copy_end
;
1114 int i
, new_line
= 0;
1116 if (value_regex
== NULL
)
1117 store
.value_regex
= NULL
;
1119 if (value_regex
[0] == '!') {
1120 store
.do_not_match
= 1;
1123 store
.do_not_match
= 0;
1125 store
.value_regex
= (regex_t
*)xmalloc(sizeof(regex_t
));
1126 if (regcomp(store
.value_regex
, value_regex
,
1128 error("invalid pattern: %s", value_regex
);
1129 free(store
.value_regex
);
1135 store
.offset
[0] = 0;
1136 store
.state
= START
;
1140 * After this, store.offset will contain the *end* offset
1141 * of the last match, or remain at 0 if no match was found.
1142 * As a side effect, we make sure to transform only a valid
1143 * existing config file.
1145 if (git_config_from_file(store_aux
, config_filename
, NULL
)) {
1146 error("invalid config file %s", config_filename
);
1148 if (store
.value_regex
!= NULL
) {
1149 regfree(store
.value_regex
);
1150 free(store
.value_regex
);
1157 if (store
.value_regex
!= NULL
) {
1158 regfree(store
.value_regex
);
1159 free(store
.value_regex
);
1162 /* if nothing to unset, or too many matches, error out */
1163 if ((store
.seen
== 0 && value
== NULL
) ||
1164 (store
.seen
> 1 && multi_replace
== 0)) {
1170 contents_sz
= xsize_t(st
.st_size
);
1171 contents
= xmmap(NULL
, contents_sz
, PROT_READ
,
1172 MAP_PRIVATE
, in_fd
, 0);
1175 if (store
.seen
== 0)
1178 for (i
= 0, copy_begin
= 0; i
< store
.seen
; i
++) {
1179 if (store
.offset
[i
] == 0) {
1180 store
.offset
[i
] = copy_end
= contents_sz
;
1181 } else if (store
.state
!= KEY_SEEN
) {
1182 copy_end
= store
.offset
[i
];
1184 copy_end
= find_beginning_of_line(
1185 contents
, contents_sz
,
1186 store
.offset
[i
]-2, &new_line
);
1188 if (copy_end
> 0 && contents
[copy_end
-1] != '\n')
1191 /* write the first part of the config */
1192 if (copy_end
> copy_begin
) {
1193 if (write_in_full(fd
, contents
+ copy_begin
,
1194 copy_end
- copy_begin
) <
1195 copy_end
- copy_begin
)
1198 write_str_in_full(fd
, "\n") != 1)
1201 copy_begin
= store
.offset
[i
];
1204 /* write the pair (value == NULL means unset) */
1205 if (value
!= NULL
) {
1206 if (store
.state
== START
) {
1207 if (!store_write_section(fd
, key
))
1210 if (!store_write_pair(fd
, key
, value
))
1214 /* write the rest of the config */
1215 if (copy_begin
< contents_sz
)
1216 if (write_in_full(fd
, contents
+ copy_begin
,
1217 contents_sz
- copy_begin
) <
1218 contents_sz
- copy_begin
)
1221 munmap(contents
, contents_sz
);
1224 if (commit_lock_file(lock
) < 0) {
1225 error("could not commit config file %s", config_filename
);
1231 * lock is committed, so don't try to roll it back below.
1232 * NOTE: Since lockfile.c keeps a linked list of all created
1233 * lock_file structures, it isn't safe to free(lock). It's
1234 * better to just leave it hanging around.
1241 rollback_lock_file(lock
);
1242 free(config_filename
);
1246 ret
= write_error(lock
->filename
);
1251 static int section_name_match (const char *buf
, const char *name
)
1253 int i
= 0, j
= 0, dot
= 0;
1256 for (i
= 1; buf
[i
] && buf
[i
] != ']'; i
++) {
1257 if (!dot
&& isspace(buf
[i
])) {
1259 if (name
[j
++] != '.')
1261 for (i
++; isspace(buf
[i
]); i
++)
1267 if (buf
[i
] == '\\' && dot
)
1269 else if (buf
[i
] == '"' && dot
) {
1270 for (i
++; isspace(buf
[i
]); i
++)
1274 if (buf
[i
] != name
[j
++])
1277 if (buf
[i
] == ']' && name
[j
] == 0) {
1279 * We match, now just find the right length offset by
1280 * gobbling up any whitespace after it, as well
1283 for (; buf
[i
] && isspace(buf
[i
]); i
++)
1290 /* if new_name == NULL, the section is removed instead */
1291 int git_config_rename_section(const char *old_name
, const char *new_name
)
1293 int ret
= 0, remove
= 0;
1294 char *config_filename
;
1295 struct lock_file
*lock
= xcalloc(sizeof(struct lock_file
), 1);
1299 if (config_exclusive_filename
)
1300 config_filename
= xstrdup(config_exclusive_filename
);
1302 config_filename
= git_pathdup("config");
1303 out_fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
1305 ret
= error("could not lock config file %s", config_filename
);
1309 if (!(config_file
= fopen(config_filename
, "rb"))) {
1310 /* no config file means nothing to rename, no error */
1311 goto unlock_and_out
;
1314 while (fgets(buf
, sizeof(buf
), config_file
)) {
1318 for (i
= 0; buf
[i
] && isspace(buf
[i
]); i
++)
1320 if (buf
[i
] == '[') {
1321 /* it's a section */
1322 int offset
= section_name_match(&buf
[i
], old_name
);
1325 if (new_name
== NULL
) {
1329 store
.baselen
= strlen(new_name
);
1330 if (!store_write_section(out_fd
, new_name
)) {
1331 ret
= write_error(lock
->filename
);
1335 * We wrote out the new section, with
1336 * a newline, now skip the old
1339 output
+= offset
+ i
;
1340 if (strlen(output
) > 0) {
1342 * More content means there's
1343 * a declaration to put on the
1344 * next line; indent with a
1355 length
= strlen(output
);
1356 if (write_in_full(out_fd
, output
, length
) != length
) {
1357 ret
= write_error(lock
->filename
);
1361 fclose(config_file
);
1363 if (commit_lock_file(lock
) < 0)
1364 ret
= error("could not commit config file %s", config_filename
);
1366 free(config_filename
);
1371 * Call this to report error for your variable that should not
1372 * get a boolean value (i.e. "[my] var" means "true").
1374 int config_error_nonbool(const char *var
)
1376 return error("Missing value for '%s'", var
);