2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
12 static FILE *config_file
;
13 static const char *config_file_name
;
14 static int config_linenr
;
15 static int zlib_compression_seen
;
17 static int get_next_char(void)
23 if ((f
= config_file
) != NULL
) {
26 /* DOS like systems */
43 static char *parse_value(void)
45 static char value
[1024];
46 int quote
= 0, comment
= 0, len
= 0, space
= 0;
49 int c
= get_next_char();
50 if (len
>= sizeof(value
))
60 if (isspace(c
) && !quote
) {
65 if (c
== ';' || c
== '#') {
89 /* Some characters escape as themselves */
92 /* Reject unknown escape sequences */
107 static inline int iskeychar(int c
)
109 return isalnum(c
) || c
== '-';
112 static int get_value(config_fn_t fn
, char *name
, unsigned int len
)
117 /* Get the full name */
124 name
[len
++] = tolower(c
);
129 while (c
== ' ' || c
== '\t')
136 value
= parse_value();
140 return fn(name
, value
);
143 static int get_extended_base_var(char *name
, int baselen
, int c
)
149 } while (isspace(c
));
151 /* We require the format to be '[base "extension"]' */
154 name
[baselen
++] = '.';
157 int c
= get_next_char();
168 if (baselen
> MAXNAME
/ 2)
173 if (get_next_char() != ']')
178 static int get_base_var(char *name
)
183 int c
= get_next_char();
189 return get_extended_base_var(name
, baselen
, c
);
190 if (!iskeychar(c
) && c
!= '.')
192 if (baselen
> MAXNAME
/ 2)
194 name
[baselen
++] = tolower(c
);
198 static int git_parse_file(config_fn_t fn
)
202 static char var
[MAXNAME
];
205 int c
= get_next_char();
213 if (comment
|| isspace(c
))
215 if (c
== '#' || c
== ';') {
220 baselen
= get_base_var(var
);
223 var
[baselen
++] = '.';
229 var
[baselen
] = tolower(c
);
230 if (get_value(fn
, var
, baselen
+1) < 0)
233 die("bad config file line %d in %s", config_linenr
, config_file_name
);
236 int git_config_int(const char *name
, const char *value
)
238 if (value
&& *value
) {
240 int val
= strtol(value
, &end
, 0);
243 if (!strcasecmp(end
, "k"))
245 if (!strcasecmp(end
, "m"))
246 return val
* 1024 * 1024;
247 if (!strcasecmp(end
, "g"))
248 return val
* 1024 * 1024 * 1024;
250 die("bad config value for '%s' in %s", name
, config_file_name
);
253 int git_config_bool(const char *name
, const char *value
)
259 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes"))
261 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no"))
263 return git_config_int(name
, value
) != 0;
266 int git_default_config(const char *var
, const char *value
)
268 /* This needs a better name */
269 if (!strcmp(var
, "core.filemode")) {
270 trust_executable_bit
= git_config_bool(var
, value
);
274 if (!strcmp(var
, "core.quotepath")) {
275 quote_path_fully
= git_config_bool(var
, value
);
279 if (!strcmp(var
, "core.symlinks")) {
280 has_symlinks
= git_config_bool(var
, value
);
284 if (!strcmp(var
, "core.bare")) {
285 is_bare_repository_cfg
= git_config_bool(var
, value
);
289 if (!strcmp(var
, "core.ignorestat")) {
290 assume_unchanged
= git_config_bool(var
, value
);
294 if (!strcmp(var
, "core.prefersymlinkrefs")) {
295 prefer_symlink_refs
= git_config_bool(var
, value
);
299 if (!strcmp(var
, "core.logallrefupdates")) {
300 log_all_ref_updates
= git_config_bool(var
, value
);
304 if (!strcmp(var
, "core.warnambiguousrefs")) {
305 warn_ambiguous_refs
= git_config_bool(var
, value
);
309 if (!strcmp(var
, "core.loosecompression")) {
310 int level
= git_config_int(var
, value
);
312 level
= Z_DEFAULT_COMPRESSION
;
313 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
314 die("bad zlib compression level %d", level
);
315 zlib_compression_level
= level
;
316 zlib_compression_seen
= 1;
320 if (!strcmp(var
, "core.compression")) {
321 int level
= git_config_int(var
, value
);
323 level
= Z_DEFAULT_COMPRESSION
;
324 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
325 die("bad zlib compression level %d", level
);
326 core_compression_level
= level
;
327 core_compression_seen
= 1;
328 if (!zlib_compression_seen
)
329 zlib_compression_level
= level
;
333 if (!strcmp(var
, "core.packedgitwindowsize")) {
334 int pgsz_x2
= getpagesize() * 2;
335 packed_git_window_size
= git_config_int(var
, value
);
337 /* This value must be multiple of (pagesize * 2) */
338 packed_git_window_size
/= pgsz_x2
;
339 if (packed_git_window_size
< 1)
340 packed_git_window_size
= 1;
341 packed_git_window_size
*= pgsz_x2
;
345 if (!strcmp(var
, "core.packedgitlimit")) {
346 packed_git_limit
= git_config_int(var
, value
);
350 if (!strcmp(var
, "core.deltabasecachelimit")) {
351 delta_base_cache_limit
= git_config_int(var
, value
);
355 if (!strcmp(var
, "core.autocrlf")) {
356 if (value
&& !strcasecmp(value
, "input")) {
360 auto_crlf
= git_config_bool(var
, value
);
364 if (!strcmp(var
, "user.name")) {
365 strlcpy(git_default_name
, value
, sizeof(git_default_name
));
369 if (!strcmp(var
, "user.email")) {
370 strlcpy(git_default_email
, value
, sizeof(git_default_email
));
374 if (!strcmp(var
, "i18n.commitencoding")) {
375 git_commit_encoding
= xstrdup(value
);
379 if (!strcmp(var
, "i18n.logoutputencoding")) {
380 git_log_output_encoding
= xstrdup(value
);
385 if (!strcmp(var
, "pager.color") || !strcmp(var
, "color.pager")) {
386 pager_use_color
= git_config_bool(var
,value
);
390 /* Add other config variables here and to Documentation/config.txt. */
394 int git_config_from_file(config_fn_t fn
, const char *filename
)
397 FILE *f
= fopen(filename
, "r");
402 config_file_name
= filename
;
404 ret
= git_parse_file(fn
);
406 config_file_name
= NULL
;
411 int git_config(config_fn_t fn
)
414 char *repo_config
= NULL
;
415 const char *home
= NULL
, *filename
;
417 /* $GIT_CONFIG makes git read _only_ the given config file,
418 * $GIT_CONFIG_LOCAL will make it process it in addition to the
419 * global config file, the same way it would the per-repository
420 * config file otherwise. */
421 filename
= getenv(CONFIG_ENVIRONMENT
);
423 if (!access(ETC_GITCONFIG
, R_OK
))
424 ret
+= git_config_from_file(fn
, ETC_GITCONFIG
);
425 home
= getenv("HOME");
426 filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
428 filename
= repo_config
= xstrdup(git_path("config"));
432 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
433 if (!access(user_config
, R_OK
))
434 ret
= git_config_from_file(fn
, user_config
);
438 ret
+= git_config_from_file(fn
, filename
);
444 * Find all the stuff for git_config_set() below.
447 #define MAX_MATCHES 512
453 regex_t
* value_regex
;
455 size_t offset
[MAX_MATCHES
];
456 enum { START
, SECTION_SEEN
, SECTION_END_SEEN
, KEY_SEEN
} state
;
460 static int matches(const char* key
, const char* value
)
462 return !strcmp(key
, store
.key
) &&
463 (store
.value_regex
== NULL
||
464 (store
.do_not_match
^
465 !regexec(store
.value_regex
, value
, 0, NULL
, 0)));
468 static int store_aux(const char* key
, const char* value
)
473 switch (store
.state
) {
475 if (matches(key
, value
)) {
476 if (store
.seen
== 1 && store
.multi_replace
== 0) {
478 "Warning: %s has multiple values\n",
480 } else if (store
.seen
>= MAX_MATCHES
) {
481 fprintf(stderr
, "Too many matches\n");
485 store
.offset
[store
.seen
] = ftell(config_file
);
491 * What we are looking for is in store.key (both
492 * section and var), and its section part is baselen
493 * long. We found key (again, both section and var).
494 * We would want to know if this key is in the same
495 * section as what we are looking for. We already
496 * know we are in the same section as what should
499 ep
= strrchr(key
, '.');
500 section_len
= ep
- key
;
502 if ((section_len
!= store
.baselen
) ||
503 memcmp(key
, store
.key
, section_len
+1)) {
504 store
.state
= SECTION_END_SEEN
;
509 * Do not increment matches: this is no match, but we
510 * just made sure we are in the desired section.
512 store
.offset
[store
.seen
] = ftell(config_file
);
514 case SECTION_END_SEEN
:
516 if (matches(key
, value
)) {
517 store
.offset
[store
.seen
] = ftell(config_file
);
518 store
.state
= KEY_SEEN
;
521 if (strrchr(key
, '.') - key
== store
.baselen
&&
522 !strncmp(key
, store
.key
, store
.baselen
)) {
523 store
.state
= SECTION_SEEN
;
524 store
.offset
[store
.seen
] = ftell(config_file
);
531 static int write_error(void)
533 fprintf(stderr
, "Failed to write new configuration file\n");
535 /* Same error code as "failed to rename". */
539 static int store_write_section(int fd
, const char* key
)
541 const char *dot
= strchr(key
, '.');
542 int len1
= store
.baselen
, len2
= -1;
544 dot
= strchr(key
, '.');
546 int dotlen
= dot
- key
;
548 len2
= len1
- dotlen
- 1;
553 if (write_in_full(fd
, "[", 1) != 1 ||
554 write_in_full(fd
, key
, len1
) != len1
)
557 if (write_in_full(fd
, " \"", 2) != 2)
559 while (--len2
>= 0) {
560 unsigned char c
= *++dot
;
562 if (write_in_full(fd
, "\\", 1) != 1)
564 if (write_in_full(fd
, &c
, 1) != 1)
567 if (write_in_full(fd
, "\"", 1) != 1)
570 if (write_in_full(fd
, "]\n", 2) != 2)
576 static int store_write_pair(int fd
, const char* key
, const char* value
)
579 int length
= strlen(key
+store
.baselen
+1);
582 /* Check to see if the value needs to be quoted. */
585 for (i
= 0; value
[i
]; i
++)
586 if (value
[i
] == ';' || value
[i
] == '#')
588 if (value
[i
-1] == ' ')
591 if (write_in_full(fd
, "\t", 1) != 1 ||
592 write_in_full(fd
, key
+store
.baselen
+1, length
) != length
||
593 write_in_full(fd
, " = ", 3) != 3)
595 if (quote
&& write_in_full(fd
, "\"", 1) != 1)
597 for (i
= 0; value
[i
]; i
++)
600 if (write_in_full(fd
, "\\n", 2) != 2)
604 if (write_in_full(fd
, "\\t", 2) != 2)
609 if (write_in_full(fd
, "\\", 1) != 1)
612 if (write_in_full(fd
, value
+i
, 1) != 1)
616 if (quote
&& write_in_full(fd
, "\"", 1) != 1)
618 if (write_in_full(fd
, "\n", 1) != 1)
623 static ssize_t
find_beginning_of_line(const char* contents
, size_t size
,
624 size_t offset_
, int* found_bracket
)
626 size_t equal_offset
= size
, bracket_offset
= size
;
629 for (offset
= offset_
-2; offset
> 0
630 && contents
[offset
] != '\n'; offset
--)
631 switch (contents
[offset
]) {
632 case '=': equal_offset
= offset
; break;
633 case ']': bracket_offset
= offset
; break;
635 if (bracket_offset
< equal_offset
) {
637 offset
= bracket_offset
+1;
644 int git_config_set(const char* key
, const char* value
)
646 return git_config_set_multivar(key
, value
, NULL
, 0);
650 * If value==NULL, unset in (remove from) config,
651 * if value_regex!=NULL, disregard key/value pairs where value does not match.
652 * if multi_replace==0, nothing, or only one matching key/value is replaced,
653 * else all matching key/values (regardless how many) are removed,
654 * before the new pair is written.
656 * Returns 0 on success.
658 * This function does this:
660 * - it locks the config file by creating ".git/config.lock"
662 * - it then parses the config using store_aux() as validator to find
663 * the position on the key/value pair to replace. If it is to be unset,
664 * it must be found exactly once.
666 * - the config file is mmap()ed and the part before the match (if any) is
667 * written to the lock file, then the changed part and the rest.
669 * - the config file is removed and the lock file rename()d to it.
672 int git_config_set_multivar(const char* key
, const char* value
,
673 const char* value_regex
, int multi_replace
)
678 char* config_filename
;
680 const char* last_dot
= strrchr(key
, '.');
682 config_filename
= getenv(CONFIG_ENVIRONMENT
);
683 if (!config_filename
) {
684 config_filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
685 if (!config_filename
)
686 config_filename
= git_path("config");
688 config_filename
= xstrdup(config_filename
);
689 lock_file
= xstrdup(mkpath("%s.lock", config_filename
));
692 * Since "key" actually contains the section name and the real
693 * key name separated by a dot, we have to know where the dot is.
696 if (last_dot
== NULL
) {
697 fprintf(stderr
, "key does not contain a section: %s\n", key
);
701 store
.baselen
= last_dot
- key
;
703 store
.multi_replace
= multi_replace
;
706 * Validate the key and while at it, lower case it for matching.
708 store
.key
= xmalloc(strlen(key
) + 1);
710 for (i
= 0; key
[i
]; i
++) {
711 unsigned char c
= key
[i
];
714 /* Leave the extended basename untouched.. */
715 if (!dot
|| i
> store
.baselen
) {
716 if (!iskeychar(c
) || (i
== store
.baselen
+1 && !isalpha(c
))) {
717 fprintf(stderr
, "invalid key: %s\n", key
);
723 } else if (c
== '\n') {
724 fprintf(stderr
, "invalid key (newline): %s\n", key
);
734 * The lock_file serves a purpose in addition to locking: the new
735 * contents of .git/config will be written into it.
737 fd
= open(lock_file
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
738 if (fd
< 0 || adjust_shared_perm(lock_file
)) {
739 fprintf(stderr
, "could not lock config file\n");
746 * If .git/config does not exist yet, write a minimal version.
748 in_fd
= open(config_filename
, O_RDONLY
);
752 if ( ENOENT
!= errno
) {
753 error("opening %s: %s", config_filename
,
755 ret
= 3; /* same as "invalid config file" */
758 /* if nothing to unset, error out */
764 store
.key
= (char*)key
;
765 if (!store_write_section(fd
, key
) ||
766 !store_write_pair(fd
, key
, value
))
771 size_t contents_sz
, copy_begin
, copy_end
;
774 if (value_regex
== NULL
)
775 store
.value_regex
= NULL
;
777 if (value_regex
[0] == '!') {
778 store
.do_not_match
= 1;
781 store
.do_not_match
= 0;
783 store
.value_regex
= (regex_t
*)xmalloc(sizeof(regex_t
));
784 if (regcomp(store
.value_regex
, value_regex
,
786 fprintf(stderr
, "Invalid pattern: %s\n",
788 free(store
.value_regex
);
799 * After this, store.offset will contain the *end* offset
800 * of the last match, or remain at 0 if no match was found.
801 * As a side effect, we make sure to transform only a valid
802 * existing config file.
804 if (git_config_from_file(store_aux
, config_filename
)) {
805 fprintf(stderr
, "invalid config file\n");
807 if (store
.value_regex
!= NULL
) {
808 regfree(store
.value_regex
);
809 free(store
.value_regex
);
816 if (store
.value_regex
!= NULL
) {
817 regfree(store
.value_regex
);
818 free(store
.value_regex
);
821 /* if nothing to unset, or too many matches, error out */
822 if ((store
.seen
== 0 && value
== NULL
) ||
823 (store
.seen
> 1 && multi_replace
== 0)) {
829 contents_sz
= xsize_t(st
.st_size
);
830 contents
= xmmap(NULL
, contents_sz
, PROT_READ
,
831 MAP_PRIVATE
, in_fd
, 0);
837 for (i
= 0, copy_begin
= 0; i
< store
.seen
; i
++) {
838 if (store
.offset
[i
] == 0) {
839 store
.offset
[i
] = copy_end
= contents_sz
;
840 } else if (store
.state
!= KEY_SEEN
) {
841 copy_end
= store
.offset
[i
];
843 copy_end
= find_beginning_of_line(
844 contents
, contents_sz
,
845 store
.offset
[i
]-2, &new_line
);
847 /* write the first part of the config */
848 if (copy_end
> copy_begin
) {
849 if (write_in_full(fd
, contents
+ copy_begin
,
850 copy_end
- copy_begin
) <
851 copy_end
- copy_begin
)
854 write_in_full(fd
, "\n", 1) != 1)
857 copy_begin
= store
.offset
[i
];
860 /* write the pair (value == NULL means unset) */
862 if (store
.state
== START
) {
863 if (!store_write_section(fd
, key
))
866 if (!store_write_pair(fd
, key
, value
))
870 /* write the rest of the config */
871 if (copy_begin
< contents_sz
)
872 if (write_in_full(fd
, contents
+ copy_begin
,
873 contents_sz
- copy_begin
) <
874 contents_sz
- copy_begin
)
877 munmap(contents
, contents_sz
);
878 unlink(config_filename
);
881 if (rename(lock_file
, config_filename
) < 0) {
882 fprintf(stderr
, "Could not rename the lock file?\n");
892 free(config_filename
);
905 static int section_name_match (const char *buf
, const char *name
)
907 int i
= 0, j
= 0, dot
= 0;
908 for (; buf
[i
] && buf
[i
] != ']'; i
++) {
909 if (!dot
&& isspace(buf
[i
])) {
911 if (name
[j
++] != '.')
913 for (i
++; isspace(buf
[i
]); i
++)
919 if (buf
[i
] == '\\' && dot
)
921 else if (buf
[i
] == '"' && dot
) {
922 for (i
++; isspace(buf
[i
]); i
++)
926 if (buf
[i
] != name
[j
++])
929 return (buf
[i
] == ']' && name
[j
] == 0);
932 /* if new_name == NULL, the section is removed instead */
933 int git_config_rename_section(const char *old_name
, const char *new_name
)
935 int ret
= 0, remove
= 0;
936 char *config_filename
;
937 struct lock_file
*lock
= xcalloc(sizeof(struct lock_file
), 1);
941 config_filename
= getenv(CONFIG_ENVIRONMENT
);
942 if (!config_filename
) {
943 config_filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
944 if (!config_filename
)
945 config_filename
= git_path("config");
947 config_filename
= xstrdup(config_filename
);
948 out_fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
950 ret
= error("Could not lock config file!");
954 if (!(config_file
= fopen(config_filename
, "rb"))) {
955 /* no config file means nothing to rename, no error */
959 while (fgets(buf
, sizeof(buf
), config_file
)) {
962 for (i
= 0; buf
[i
] && isspace(buf
[i
]); i
++)
966 if (section_name_match (&buf
[i
+1], old_name
)) {
968 if (new_name
== NULL
) {
972 store
.baselen
= strlen(new_name
);
973 if (!store_write_section(out_fd
, new_name
)) {
983 length
= strlen(buf
);
984 if (write_in_full(out_fd
, buf
, length
) != length
) {
991 if (close(out_fd
) || commit_lock_file(lock
) < 0)
992 ret
= error("Cannot commit config file!");
994 free(config_filename
);