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_maybe_bool(const char *name
, const char *value
)
374 if (!strcasecmp(value
, "true")
375 || !strcasecmp(value
, "yes")
376 || !strcasecmp(value
, "on"))
378 if (!strcasecmp(value
, "false")
379 || !strcasecmp(value
, "no")
380 || !strcasecmp(value
, "off"))
385 int git_config_bool_or_int(const char *name
, const char *value
, int *is_bool
)
387 int v
= git_config_maybe_bool(name
, value
);
393 return git_config_int(name
, value
);
396 int git_config_bool(const char *name
, const char *value
)
399 return !!git_config_bool_or_int(name
, value
, &discard
);
402 int git_config_string(const char **dest
, const char *var
, const char *value
)
405 return config_error_nonbool(var
);
406 *dest
= xstrdup(value
);
410 int git_config_pathname(const char **dest
, const char *var
, const char *value
)
413 return config_error_nonbool(var
);
414 *dest
= expand_user_path(value
);
416 die("Failed to expand user dir in: '%s'", value
);
420 static int git_default_core_config(const char *var
, const char *value
)
422 /* This needs a better name */
423 if (!strcmp(var
, "core.filemode")) {
424 trust_executable_bit
= git_config_bool(var
, value
);
427 if (!strcmp(var
, "core.trustctime")) {
428 trust_ctime
= git_config_bool(var
, value
);
432 if (!strcmp(var
, "core.quotepath")) {
433 quote_path_fully
= git_config_bool(var
, value
);
437 if (!strcmp(var
, "core.symlinks")) {
438 has_symlinks
= git_config_bool(var
, value
);
442 if (!strcmp(var
, "core.ignorecase")) {
443 ignore_case
= git_config_bool(var
, value
);
447 if (!strcmp(var
, "core.bare")) {
448 is_bare_repository_cfg
= git_config_bool(var
, value
);
452 if (!strcmp(var
, "core.ignorestat")) {
453 assume_unchanged
= git_config_bool(var
, value
);
457 if (!strcmp(var
, "core.prefersymlinkrefs")) {
458 prefer_symlink_refs
= git_config_bool(var
, value
);
462 if (!strcmp(var
, "core.logallrefupdates")) {
463 log_all_ref_updates
= git_config_bool(var
, value
);
467 if (!strcmp(var
, "core.warnambiguousrefs")) {
468 warn_ambiguous_refs
= git_config_bool(var
, value
);
472 if (!strcmp(var
, "core.loosecompression")) {
473 int level
= git_config_int(var
, value
);
475 level
= Z_DEFAULT_COMPRESSION
;
476 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
477 die("bad zlib compression level %d", level
);
478 zlib_compression_level
= level
;
479 zlib_compression_seen
= 1;
483 if (!strcmp(var
, "core.compression")) {
484 int level
= git_config_int(var
, value
);
486 level
= Z_DEFAULT_COMPRESSION
;
487 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
488 die("bad zlib compression level %d", level
);
489 core_compression_level
= level
;
490 core_compression_seen
= 1;
491 if (!zlib_compression_seen
)
492 zlib_compression_level
= level
;
496 if (!strcmp(var
, "core.packedgitwindowsize")) {
497 int pgsz_x2
= getpagesize() * 2;
498 packed_git_window_size
= git_config_int(var
, value
);
500 /* This value must be multiple of (pagesize * 2) */
501 packed_git_window_size
/= pgsz_x2
;
502 if (packed_git_window_size
< 1)
503 packed_git_window_size
= 1;
504 packed_git_window_size
*= pgsz_x2
;
508 if (!strcmp(var
, "core.packedgitlimit")) {
509 packed_git_limit
= git_config_int(var
, value
);
513 if (!strcmp(var
, "core.deltabasecachelimit")) {
514 delta_base_cache_limit
= git_config_int(var
, value
);
518 if (!strcmp(var
, "core.autocrlf")) {
519 if (value
&& !strcasecmp(value
, "input")) {
523 auto_crlf
= git_config_bool(var
, value
);
527 if (!strcmp(var
, "core.safecrlf")) {
528 if (value
&& !strcasecmp(value
, "warn")) {
529 safe_crlf
= SAFE_CRLF_WARN
;
532 safe_crlf
= git_config_bool(var
, value
);
536 if (!strcmp(var
, "core.notesref")) {
537 notes_ref_name
= xstrdup(value
);
541 if (!strcmp(var
, "core.pager"))
542 return git_config_string(&pager_program
, var
, value
);
544 if (!strcmp(var
, "core.editor"))
545 return git_config_string(&editor_program
, var
, value
);
547 if (!strcmp(var
, "core.excludesfile"))
548 return git_config_pathname(&excludes_file
, var
, value
);
550 if (!strcmp(var
, "core.whitespace")) {
552 return config_error_nonbool(var
);
553 whitespace_rule_cfg
= parse_whitespace_rule(value
);
557 if (!strcmp(var
, "core.fsyncobjectfiles")) {
558 fsync_object_files
= git_config_bool(var
, value
);
562 if (!strcmp(var
, "core.preloadindex")) {
563 core_preload_index
= git_config_bool(var
, value
);
567 if (!strcmp(var
, "core.createobject")) {
568 if (!strcmp(value
, "rename"))
569 object_creation_mode
= OBJECT_CREATION_USES_RENAMES
;
570 else if (!strcmp(value
, "link"))
571 object_creation_mode
= OBJECT_CREATION_USES_HARDLINKS
;
573 die("Invalid mode for object creation: %s", value
);
577 if (!strcmp(var
, "core.sparsecheckout")) {
578 core_apply_sparse_checkout
= git_config_bool(var
, value
);
582 if (!strcmp(var
, "core.hidedotfiles")) {
583 if (value
&& !strcasecmp(value
, "dotgitonly")) {
584 hide_dotfiles
= HIDE_DOTFILES_DOTGITONLY
;
587 hide_dotfiles
= git_config_bool(var
, value
);
591 /* Add other config variables here and to Documentation/config.txt. */
595 static int git_default_user_config(const char *var
, const char *value
)
597 if (!strcmp(var
, "user.name")) {
599 return config_error_nonbool(var
);
600 strlcpy(git_default_name
, value
, sizeof(git_default_name
));
601 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
605 if (!strcmp(var
, "user.email")) {
607 return config_error_nonbool(var
);
608 strlcpy(git_default_email
, value
, sizeof(git_default_email
));
609 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
613 /* Add other config variables here and to Documentation/config.txt. */
617 static int git_default_i18n_config(const char *var
, const char *value
)
619 if (!strcmp(var
, "i18n.commitencoding"))
620 return git_config_string(&git_commit_encoding
, var
, value
);
622 if (!strcmp(var
, "i18n.logoutputencoding"))
623 return git_config_string(&git_log_output_encoding
, var
, value
);
625 /* Add other config variables here and to Documentation/config.txt. */
629 static int git_default_branch_config(const char *var
, const char *value
)
631 if (!strcmp(var
, "branch.autosetupmerge")) {
632 if (value
&& !strcasecmp(value
, "always")) {
633 git_branch_track
= BRANCH_TRACK_ALWAYS
;
636 git_branch_track
= git_config_bool(var
, value
);
639 if (!strcmp(var
, "branch.autosetuprebase")) {
641 return config_error_nonbool(var
);
642 else if (!strcmp(value
, "never"))
643 autorebase
= AUTOREBASE_NEVER
;
644 else if (!strcmp(value
, "local"))
645 autorebase
= AUTOREBASE_LOCAL
;
646 else if (!strcmp(value
, "remote"))
647 autorebase
= AUTOREBASE_REMOTE
;
648 else if (!strcmp(value
, "always"))
649 autorebase
= AUTOREBASE_ALWAYS
;
651 return error("Malformed value for %s", var
);
655 /* Add other config variables here and to Documentation/config.txt. */
659 static int git_default_push_config(const char *var
, const char *value
)
661 if (!strcmp(var
, "push.default")) {
663 return config_error_nonbool(var
);
664 else if (!strcmp(value
, "nothing"))
665 push_default
= PUSH_DEFAULT_NOTHING
;
666 else if (!strcmp(value
, "matching"))
667 push_default
= PUSH_DEFAULT_MATCHING
;
668 else if (!strcmp(value
, "tracking"))
669 push_default
= PUSH_DEFAULT_TRACKING
;
670 else if (!strcmp(value
, "current"))
671 push_default
= PUSH_DEFAULT_CURRENT
;
673 error("Malformed value for %s: %s", var
, value
);
674 return error("Must be one of nothing, matching, "
675 "tracking or current.");
680 /* Add other config variables here and to Documentation/config.txt. */
684 static int git_default_mailmap_config(const char *var
, const char *value
)
686 if (!strcmp(var
, "mailmap.file"))
687 return git_config_string(&git_mailmap_file
, var
, value
);
689 /* Add other config variables here and to Documentation/config.txt. */
693 int git_default_config(const char *var
, const char *value
, void *dummy
)
695 if (!prefixcmp(var
, "core."))
696 return git_default_core_config(var
, value
);
698 if (!prefixcmp(var
, "user."))
699 return git_default_user_config(var
, value
);
701 if (!prefixcmp(var
, "i18n."))
702 return git_default_i18n_config(var
, value
);
704 if (!prefixcmp(var
, "branch."))
705 return git_default_branch_config(var
, value
);
707 if (!prefixcmp(var
, "push."))
708 return git_default_push_config(var
, value
);
710 if (!prefixcmp(var
, "mailmap."))
711 return git_default_mailmap_config(var
, value
);
713 if (!prefixcmp(var
, "advice."))
714 return git_default_advice_config(var
, value
);
716 if (!strcmp(var
, "pager.color") || !strcmp(var
, "color.pager")) {
717 pager_use_color
= git_config_bool(var
,value
);
721 /* Add other config variables here and to Documentation/config.txt. */
725 int git_config_from_file(config_fn_t fn
, const char *filename
, void *data
)
728 FILE *f
= fopen(filename
, "r");
733 config_file_name
= filename
;
736 ret
= git_parse_file(fn
, data
);
738 config_file_name
= NULL
;
743 const char *git_etc_gitconfig(void)
745 static const char *system_wide
;
747 system_wide
= system_path(ETC_GITCONFIG
);
751 int git_env_bool(const char *k
, int def
)
753 const char *v
= getenv(k
);
754 return v
? git_config_bool(k
, v
) : def
;
757 int git_config_system(void)
759 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
762 int git_config_global(void)
764 return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
767 int git_config_from_parameters(config_fn_t fn
, void *data
)
769 const struct config_item
*ct
;
770 for (ct
= config_parameters
; ct
; ct
= ct
->next
)
771 if (fn(ct
->name
, ct
->value
, data
) < 0)
776 int git_config(config_fn_t fn
, void *data
)
778 int ret
= 0, found
= 0;
779 char *repo_config
= NULL
;
780 const char *home
= NULL
;
782 /* Setting $GIT_CONFIG makes git read _only_ the given config file. */
783 if (config_exclusive_filename
)
784 return git_config_from_file(fn
, config_exclusive_filename
, data
);
785 if (git_config_system() && !access(git_etc_gitconfig(), R_OK
)) {
786 ret
+= git_config_from_file(fn
, git_etc_gitconfig(),
791 home
= get_home_directory();
792 if (git_config_global() && home
) {
793 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
794 if (!access(user_config
, R_OK
)) {
795 ret
+= git_config_from_file(fn
, user_config
, data
);
801 repo_config
= git_pathdup("config");
802 if (!access(repo_config
, R_OK
)) {
803 ret
+= git_config_from_file(fn
, repo_config
, data
);
808 if (config_parameters
) {
809 ret
+= git_config_from_parameters(fn
, data
);
819 * Find all the stuff for git_config_set() below.
822 #define MAX_MATCHES 512
828 regex_t
*value_regex
;
830 size_t offset
[MAX_MATCHES
];
831 enum { START
, SECTION_SEEN
, SECTION_END_SEEN
, KEY_SEEN
} state
;
835 static int matches(const char *key
, const char *value
)
837 return !strcmp(key
, store
.key
) &&
838 (store
.value_regex
== NULL
||
839 (store
.do_not_match
^
840 !regexec(store
.value_regex
, value
, 0, NULL
, 0)));
843 static int store_aux(const char *key
, const char *value
, void *cb
)
848 switch (store
.state
) {
850 if (matches(key
, value
)) {
851 if (store
.seen
== 1 && store
.multi_replace
== 0) {
852 warning("%s has multiple values", key
);
853 } else if (store
.seen
>= MAX_MATCHES
) {
854 error("too many matches for %s", key
);
858 store
.offset
[store
.seen
] = ftell(config_file
);
864 * What we are looking for is in store.key (both
865 * section and var), and its section part is baselen
866 * long. We found key (again, both section and var).
867 * We would want to know if this key is in the same
868 * section as what we are looking for. We already
869 * know we are in the same section as what should
872 ep
= strrchr(key
, '.');
873 section_len
= ep
- key
;
875 if ((section_len
!= store
.baselen
) ||
876 memcmp(key
, store
.key
, section_len
+1)) {
877 store
.state
= SECTION_END_SEEN
;
882 * Do not increment matches: this is no match, but we
883 * just made sure we are in the desired section.
885 store
.offset
[store
.seen
] = ftell(config_file
);
887 case SECTION_END_SEEN
:
889 if (matches(key
, value
)) {
890 store
.offset
[store
.seen
] = ftell(config_file
);
891 store
.state
= KEY_SEEN
;
894 if (strrchr(key
, '.') - key
== store
.baselen
&&
895 !strncmp(key
, store
.key
, store
.baselen
)) {
896 store
.state
= SECTION_SEEN
;
897 store
.offset
[store
.seen
] = ftell(config_file
);
904 static int write_error(const char *filename
)
906 error("failed to write new configuration file %s", filename
);
908 /* Same error code as "failed to rename". */
912 static int store_write_section(int fd
, const char *key
)
916 struct strbuf sb
= STRBUF_INIT
;
918 dot
= memchr(key
, '.', store
.baselen
);
920 strbuf_addf(&sb
, "[%.*s \"", (int)(dot
- key
), key
);
921 for (i
= dot
- key
+ 1; i
< store
.baselen
; i
++) {
922 if (key
[i
] == '"' || key
[i
] == '\\')
923 strbuf_addch(&sb
, '\\');
924 strbuf_addch(&sb
, key
[i
]);
926 strbuf_addstr(&sb
, "\"]\n");
928 strbuf_addf(&sb
, "[%.*s]\n", store
.baselen
, key
);
931 success
= write_in_full(fd
, sb
.buf
, sb
.len
) == sb
.len
;
937 static int store_write_pair(int fd
, const char *key
, const char *value
)
940 int length
= strlen(key
+ store
.baselen
+ 1);
941 const char *quote
= "";
942 struct strbuf sb
= STRBUF_INIT
;
945 * Check to see if the value needs to be surrounded with a dq pair.
946 * Note that problematic characters are always backslash-quoted; this
947 * check is about not losing leading or trailing SP and strings that
948 * follow beginning-of-comment characters (i.e. ';' and '#') by the
949 * configuration parser.
953 for (i
= 0; value
[i
]; i
++)
954 if (value
[i
] == ';' || value
[i
] == '#')
956 if (i
&& value
[i
- 1] == ' ')
959 strbuf_addf(&sb
, "\t%.*s = %s",
960 length
, key
+ store
.baselen
+ 1, quote
);
962 for (i
= 0; value
[i
]; i
++)
965 strbuf_addstr(&sb
, "\\n");
968 strbuf_addstr(&sb
, "\\t");
972 strbuf_addch(&sb
, '\\');
974 strbuf_addch(&sb
, value
[i
]);
977 strbuf_addf(&sb
, "%s\n", quote
);
979 success
= write_in_full(fd
, sb
.buf
, sb
.len
) == sb
.len
;
985 static ssize_t
find_beginning_of_line(const char *contents
, size_t size
,
986 size_t offset_
, int *found_bracket
)
988 size_t equal_offset
= size
, bracket_offset
= size
;
992 for (offset
= offset_
-2; offset
> 0
993 && contents
[offset
] != '\n'; offset
--)
994 switch (contents
[offset
]) {
995 case '=': equal_offset
= offset
; break;
996 case ']': bracket_offset
= offset
; break;
998 if (offset
> 0 && contents
[offset
-1] == '\\') {
1002 if (bracket_offset
< equal_offset
) {
1004 offset
= bracket_offset
+1;
1011 int git_config_set(const char *key
, const char *value
)
1013 return git_config_set_multivar(key
, value
, NULL
, 0);
1017 * If value==NULL, unset in (remove from) config,
1018 * if value_regex!=NULL, disregard key/value pairs where value does not match.
1019 * if multi_replace==0, nothing, or only one matching key/value is replaced,
1020 * else all matching key/values (regardless how many) are removed,
1021 * before the new pair is written.
1023 * Returns 0 on success.
1025 * This function does this:
1027 * - it locks the config file by creating ".git/config.lock"
1029 * - it then parses the config using store_aux() as validator to find
1030 * the position on the key/value pair to replace. If it is to be unset,
1031 * it must be found exactly once.
1033 * - the config file is mmap()ed and the part before the match (if any) is
1034 * written to the lock file, then the changed part and the rest.
1036 * - the config file is removed and the lock file rename()d to it.
1039 int git_config_set_multivar(const char *key
, const char *value
,
1040 const char *value_regex
, int multi_replace
)
1045 char *config_filename
;
1046 struct lock_file
*lock
= NULL
;
1047 const char *last_dot
= strrchr(key
, '.');
1049 if (config_exclusive_filename
)
1050 config_filename
= xstrdup(config_exclusive_filename
);
1052 config_filename
= git_pathdup("config");
1055 * Since "key" actually contains the section name and the real
1056 * key name separated by a dot, we have to know where the dot is.
1059 if (last_dot
== NULL
) {
1060 error("key does not contain a section: %s", key
);
1064 store
.baselen
= last_dot
- key
;
1066 store
.multi_replace
= multi_replace
;
1069 * Validate the key and while at it, lower case it for matching.
1071 store
.key
= xmalloc(strlen(key
) + 1);
1073 for (i
= 0; key
[i
]; i
++) {
1074 unsigned char c
= key
[i
];
1077 /* Leave the extended basename untouched.. */
1078 if (!dot
|| i
> store
.baselen
) {
1079 if (!iskeychar(c
) || (i
== store
.baselen
+1 && !isalpha(c
))) {
1080 error("invalid key: %s", key
);
1086 } else if (c
== '\n') {
1087 error("invalid key (newline): %s", key
);
1097 * The lock serves a purpose in addition to locking: the new
1098 * contents of .git/config will be written into it.
1100 lock
= xcalloc(sizeof(struct lock_file
), 1);
1101 fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
1103 error("could not lock config file %s: %s", config_filename
, strerror(errno
));
1110 * If .git/config does not exist yet, write a minimal version.
1112 in_fd
= open(config_filename
, O_RDONLY
);
1116 if ( ENOENT
!= errno
) {
1117 error("opening %s: %s", config_filename
,
1119 ret
= 3; /* same as "invalid config file" */
1122 /* if nothing to unset, error out */
1123 if (value
== NULL
) {
1128 store
.key
= (char *)key
;
1129 if (!store_write_section(fd
, key
) ||
1130 !store_write_pair(fd
, key
, value
))
1135 size_t contents_sz
, copy_begin
, copy_end
;
1136 int i
, new_line
= 0;
1138 if (value_regex
== NULL
)
1139 store
.value_regex
= NULL
;
1141 if (value_regex
[0] == '!') {
1142 store
.do_not_match
= 1;
1145 store
.do_not_match
= 0;
1147 store
.value_regex
= (regex_t
*)xmalloc(sizeof(regex_t
));
1148 if (regcomp(store
.value_regex
, value_regex
,
1150 error("invalid pattern: %s", value_regex
);
1151 free(store
.value_regex
);
1157 store
.offset
[0] = 0;
1158 store
.state
= START
;
1162 * After this, store.offset will contain the *end* offset
1163 * of the last match, or remain at 0 if no match was found.
1164 * As a side effect, we make sure to transform only a valid
1165 * existing config file.
1167 if (git_config_from_file(store_aux
, config_filename
, NULL
)) {
1168 error("invalid config file %s", config_filename
);
1170 if (store
.value_regex
!= NULL
) {
1171 regfree(store
.value_regex
);
1172 free(store
.value_regex
);
1179 if (store
.value_regex
!= NULL
) {
1180 regfree(store
.value_regex
);
1181 free(store
.value_regex
);
1184 /* if nothing to unset, or too many matches, error out */
1185 if ((store
.seen
== 0 && value
== NULL
) ||
1186 (store
.seen
> 1 && multi_replace
== 0)) {
1192 contents_sz
= xsize_t(st
.st_size
);
1193 contents
= xmmap(NULL
, contents_sz
, PROT_READ
,
1194 MAP_PRIVATE
, in_fd
, 0);
1197 if (store
.seen
== 0)
1200 for (i
= 0, copy_begin
= 0; i
< store
.seen
; i
++) {
1201 if (store
.offset
[i
] == 0) {
1202 store
.offset
[i
] = copy_end
= contents_sz
;
1203 } else if (store
.state
!= KEY_SEEN
) {
1204 copy_end
= store
.offset
[i
];
1206 copy_end
= find_beginning_of_line(
1207 contents
, contents_sz
,
1208 store
.offset
[i
]-2, &new_line
);
1210 if (copy_end
> 0 && contents
[copy_end
-1] != '\n')
1213 /* write the first part of the config */
1214 if (copy_end
> copy_begin
) {
1215 if (write_in_full(fd
, contents
+ copy_begin
,
1216 copy_end
- copy_begin
) <
1217 copy_end
- copy_begin
)
1220 write_str_in_full(fd
, "\n") != 1)
1223 copy_begin
= store
.offset
[i
];
1226 /* write the pair (value == NULL means unset) */
1227 if (value
!= NULL
) {
1228 if (store
.state
== START
) {
1229 if (!store_write_section(fd
, key
))
1232 if (!store_write_pair(fd
, key
, value
))
1236 /* write the rest of the config */
1237 if (copy_begin
< contents_sz
)
1238 if (write_in_full(fd
, contents
+ copy_begin
,
1239 contents_sz
- copy_begin
) <
1240 contents_sz
- copy_begin
)
1243 munmap(contents
, contents_sz
);
1246 if (commit_lock_file(lock
) < 0) {
1247 error("could not commit config file %s", config_filename
);
1253 * lock is committed, so don't try to roll it back below.
1254 * NOTE: Since lockfile.c keeps a linked list of all created
1255 * lock_file structures, it isn't safe to free(lock). It's
1256 * better to just leave it hanging around.
1263 rollback_lock_file(lock
);
1264 free(config_filename
);
1268 ret
= write_error(lock
->filename
);
1273 static int section_name_match (const char *buf
, const char *name
)
1275 int i
= 0, j
= 0, dot
= 0;
1278 for (i
= 1; buf
[i
] && buf
[i
] != ']'; i
++) {
1279 if (!dot
&& isspace(buf
[i
])) {
1281 if (name
[j
++] != '.')
1283 for (i
++; isspace(buf
[i
]); i
++)
1289 if (buf
[i
] == '\\' && dot
)
1291 else if (buf
[i
] == '"' && dot
) {
1292 for (i
++; isspace(buf
[i
]); i
++)
1296 if (buf
[i
] != name
[j
++])
1299 if (buf
[i
] == ']' && name
[j
] == 0) {
1301 * We match, now just find the right length offset by
1302 * gobbling up any whitespace after it, as well
1305 for (; buf
[i
] && isspace(buf
[i
]); i
++)
1312 /* if new_name == NULL, the section is removed instead */
1313 int git_config_rename_section(const char *old_name
, const char *new_name
)
1315 int ret
= 0, remove
= 0;
1316 char *config_filename
;
1317 struct lock_file
*lock
= xcalloc(sizeof(struct lock_file
), 1);
1321 if (config_exclusive_filename
)
1322 config_filename
= xstrdup(config_exclusive_filename
);
1324 config_filename
= git_pathdup("config");
1325 out_fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
1327 ret
= error("could not lock config file %s", config_filename
);
1331 if (!(config_file
= fopen(config_filename
, "rb"))) {
1332 /* no config file means nothing to rename, no error */
1333 goto unlock_and_out
;
1336 while (fgets(buf
, sizeof(buf
), config_file
)) {
1340 for (i
= 0; buf
[i
] && isspace(buf
[i
]); i
++)
1342 if (buf
[i
] == '[') {
1343 /* it's a section */
1344 int offset
= section_name_match(&buf
[i
], old_name
);
1347 if (new_name
== NULL
) {
1351 store
.baselen
= strlen(new_name
);
1352 if (!store_write_section(out_fd
, new_name
)) {
1353 ret
= write_error(lock
->filename
);
1357 * We wrote out the new section, with
1358 * a newline, now skip the old
1361 output
+= offset
+ i
;
1362 if (strlen(output
) > 0) {
1364 * More content means there's
1365 * a declaration to put on the
1366 * next line; indent with a
1377 length
= strlen(output
);
1378 if (write_in_full(out_fd
, output
, length
) != length
) {
1379 ret
= write_error(lock
->filename
);
1383 fclose(config_file
);
1385 if (commit_lock_file(lock
) < 0)
1386 ret
= error("could not commit config file %s", config_filename
);
1388 free(config_filename
);
1393 * Call this to report error for your variable that should not
1394 * get a boolean value (i.e. "[my] var" means "true").
1396 int config_error_nonbool(const char *var
)
1398 return error("Missing value for '%s'", var
);