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.symlinks")) {
275 has_symlinks
= git_config_bool(var
, value
);
279 if (!strcmp(var
, "core.bare")) {
280 is_bare_repository_cfg
= git_config_bool(var
, value
);
284 if (!strcmp(var
, "core.ignorestat")) {
285 assume_unchanged
= git_config_bool(var
, value
);
289 if (!strcmp(var
, "core.prefersymlinkrefs")) {
290 prefer_symlink_refs
= git_config_bool(var
, value
);
294 if (!strcmp(var
, "core.logallrefupdates")) {
295 log_all_ref_updates
= git_config_bool(var
, value
);
299 if (!strcmp(var
, "core.warnambiguousrefs")) {
300 warn_ambiguous_refs
= git_config_bool(var
, value
);
304 if (!strcmp(var
, "core.loosecompression")) {
305 int level
= git_config_int(var
, value
);
307 level
= Z_DEFAULT_COMPRESSION
;
308 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
309 die("bad zlib compression level %d", level
);
310 zlib_compression_level
= level
;
311 zlib_compression_seen
= 1;
315 if (!strcmp(var
, "core.compression")) {
316 int level
= git_config_int(var
, value
);
318 level
= Z_DEFAULT_COMPRESSION
;
319 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
320 die("bad zlib compression level %d", level
);
321 core_compression_level
= level
;
322 core_compression_seen
= 1;
323 if (!zlib_compression_seen
)
324 zlib_compression_level
= level
;
328 if (!strcmp(var
, "core.packedgitwindowsize")) {
329 int pgsz_x2
= getpagesize() * 2;
330 packed_git_window_size
= git_config_int(var
, value
);
332 /* This value must be multiple of (pagesize * 2) */
333 packed_git_window_size
/= pgsz_x2
;
334 if (packed_git_window_size
< 1)
335 packed_git_window_size
= 1;
336 packed_git_window_size
*= pgsz_x2
;
340 if (!strcmp(var
, "core.packedgitlimit")) {
341 packed_git_limit
= git_config_int(var
, value
);
345 if (!strcmp(var
, "core.deltabasecachelimit")) {
346 delta_base_cache_limit
= git_config_int(var
, value
);
350 if (!strcmp(var
, "core.autocrlf")) {
351 if (value
&& !strcasecmp(value
, "input")) {
355 auto_crlf
= git_config_bool(var
, value
);
359 if (!strcmp(var
, "user.name")) {
360 strlcpy(git_default_name
, value
, sizeof(git_default_name
));
364 if (!strcmp(var
, "user.email")) {
365 strlcpy(git_default_email
, value
, sizeof(git_default_email
));
369 if (!strcmp(var
, "i18n.commitencoding")) {
370 git_commit_encoding
= xstrdup(value
);
374 if (!strcmp(var
, "i18n.logoutputencoding")) {
375 git_log_output_encoding
= xstrdup(value
);
380 if (!strcmp(var
, "pager.color") || !strcmp(var
, "color.pager")) {
381 pager_use_color
= git_config_bool(var
,value
);
385 /* Add other config variables here and to Documentation/config.txt. */
389 int git_config_from_file(config_fn_t fn
, const char *filename
)
392 FILE *f
= fopen(filename
, "r");
397 config_file_name
= filename
;
399 ret
= git_parse_file(fn
);
401 config_file_name
= NULL
;
406 int git_config(config_fn_t fn
)
409 char *repo_config
= NULL
;
410 const char *home
= NULL
, *filename
;
412 /* $GIT_CONFIG makes git read _only_ the given config file,
413 * $GIT_CONFIG_LOCAL will make it process it in addition to the
414 * global config file, the same way it would the per-repository
415 * config file otherwise. */
416 filename
= getenv(CONFIG_ENVIRONMENT
);
418 if (!access(ETC_GITCONFIG
, R_OK
))
419 ret
+= git_config_from_file(fn
, ETC_GITCONFIG
);
420 home
= getenv("HOME");
421 filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
423 filename
= repo_config
= xstrdup(git_path("config"));
427 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
428 if (!access(user_config
, R_OK
))
429 ret
= git_config_from_file(fn
, user_config
);
433 ret
+= git_config_from_file(fn
, filename
);
439 * Find all the stuff for git_config_set() below.
442 #define MAX_MATCHES 512
448 regex_t
* value_regex
;
450 size_t offset
[MAX_MATCHES
];
451 enum { START
, SECTION_SEEN
, SECTION_END_SEEN
, KEY_SEEN
} state
;
455 static int matches(const char* key
, const char* value
)
457 return !strcmp(key
, store
.key
) &&
458 (store
.value_regex
== NULL
||
459 (store
.do_not_match
^
460 !regexec(store
.value_regex
, value
, 0, NULL
, 0)));
463 static int store_aux(const char* key
, const char* value
)
468 switch (store
.state
) {
470 if (matches(key
, value
)) {
471 if (store
.seen
== 1 && store
.multi_replace
== 0) {
473 "Warning: %s has multiple values\n",
475 } else if (store
.seen
>= MAX_MATCHES
) {
476 fprintf(stderr
, "Too many matches\n");
480 store
.offset
[store
.seen
] = ftell(config_file
);
486 * What we are looking for is in store.key (both
487 * section and var), and its section part is baselen
488 * long. We found key (again, both section and var).
489 * We would want to know if this key is in the same
490 * section as what we are looking for. We already
491 * know we are in the same section as what should
494 ep
= strrchr(key
, '.');
495 section_len
= ep
- key
;
497 if ((section_len
!= store
.baselen
) ||
498 memcmp(key
, store
.key
, section_len
+1)) {
499 store
.state
= SECTION_END_SEEN
;
504 * Do not increment matches: this is no match, but we
505 * just made sure we are in the desired section.
507 store
.offset
[store
.seen
] = ftell(config_file
);
509 case SECTION_END_SEEN
:
511 if (matches(key
, value
)) {
512 store
.offset
[store
.seen
] = ftell(config_file
);
513 store
.state
= KEY_SEEN
;
516 if (strrchr(key
, '.') - key
== store
.baselen
&&
517 !strncmp(key
, store
.key
, store
.baselen
)) {
518 store
.state
= SECTION_SEEN
;
519 store
.offset
[store
.seen
] = ftell(config_file
);
526 static int write_error(void)
528 fprintf(stderr
, "Failed to write new configuration file\n");
530 /* Same error code as "failed to rename". */
534 static int store_write_section(int fd
, const char* key
)
536 const char *dot
= strchr(key
, '.');
537 int len1
= store
.baselen
, len2
= -1;
539 dot
= strchr(key
, '.');
541 int dotlen
= dot
- key
;
543 len2
= len1
- dotlen
- 1;
548 if (write_in_full(fd
, "[", 1) != 1 ||
549 write_in_full(fd
, key
, len1
) != len1
)
552 if (write_in_full(fd
, " \"", 2) != 2)
554 while (--len2
>= 0) {
555 unsigned char c
= *++dot
;
557 if (write_in_full(fd
, "\\", 1) != 1)
559 if (write_in_full(fd
, &c
, 1) != 1)
562 if (write_in_full(fd
, "\"", 1) != 1)
565 if (write_in_full(fd
, "]\n", 2) != 2)
571 static int store_write_pair(int fd
, const char* key
, const char* value
)
574 int length
= strlen(key
+store
.baselen
+1);
577 /* Check to see if the value needs to be quoted. */
580 for (i
= 0; value
[i
]; i
++)
581 if (value
[i
] == ';' || value
[i
] == '#')
583 if (value
[i
-1] == ' ')
586 if (write_in_full(fd
, "\t", 1) != 1 ||
587 write_in_full(fd
, key
+store
.baselen
+1, length
) != length
||
588 write_in_full(fd
, " = ", 3) != 3)
590 if (quote
&& write_in_full(fd
, "\"", 1) != 1)
592 for (i
= 0; value
[i
]; i
++)
595 if (write_in_full(fd
, "\\n", 2) != 2)
599 if (write_in_full(fd
, "\\t", 2) != 2)
604 if (write_in_full(fd
, "\\", 1) != 1)
607 if (write_in_full(fd
, value
+i
, 1) != 1)
611 if (quote
&& write_in_full(fd
, "\"", 1) != 1)
613 if (write_in_full(fd
, "\n", 1) != 1)
618 static ssize_t
find_beginning_of_line(const char* contents
, size_t size
,
619 size_t offset_
, int* found_bracket
)
621 size_t equal_offset
= size
, bracket_offset
= size
;
624 for (offset
= offset_
-2; offset
> 0
625 && contents
[offset
] != '\n'; offset
--)
626 switch (contents
[offset
]) {
627 case '=': equal_offset
= offset
; break;
628 case ']': bracket_offset
= offset
; break;
630 if (bracket_offset
< equal_offset
) {
632 offset
= bracket_offset
+1;
639 int git_config_set(const char* key
, const char* value
)
641 return git_config_set_multivar(key
, value
, NULL
, 0);
645 * If value==NULL, unset in (remove from) config,
646 * if value_regex!=NULL, disregard key/value pairs where value does not match.
647 * if multi_replace==0, nothing, or only one matching key/value is replaced,
648 * else all matching key/values (regardless how many) are removed,
649 * before the new pair is written.
651 * Returns 0 on success.
653 * This function does this:
655 * - it locks the config file by creating ".git/config.lock"
657 * - it then parses the config using store_aux() as validator to find
658 * the position on the key/value pair to replace. If it is to be unset,
659 * it must be found exactly once.
661 * - the config file is mmap()ed and the part before the match (if any) is
662 * written to the lock file, then the changed part and the rest.
664 * - the config file is removed and the lock file rename()d to it.
667 int git_config_set_multivar(const char* key
, const char* value
,
668 const char* value_regex
, int multi_replace
)
673 char* config_filename
;
675 const char* last_dot
= strrchr(key
, '.');
677 config_filename
= getenv(CONFIG_ENVIRONMENT
);
678 if (!config_filename
) {
679 config_filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
680 if (!config_filename
)
681 config_filename
= git_path("config");
683 config_filename
= xstrdup(config_filename
);
684 lock_file
= xstrdup(mkpath("%s.lock", config_filename
));
687 * Since "key" actually contains the section name and the real
688 * key name separated by a dot, we have to know where the dot is.
691 if (last_dot
== NULL
) {
692 fprintf(stderr
, "key does not contain a section: %s\n", key
);
696 store
.baselen
= last_dot
- key
;
698 store
.multi_replace
= multi_replace
;
701 * Validate the key and while at it, lower case it for matching.
703 store
.key
= xmalloc(strlen(key
) + 1);
705 for (i
= 0; key
[i
]; i
++) {
706 unsigned char c
= key
[i
];
709 /* Leave the extended basename untouched.. */
710 if (!dot
|| i
> store
.baselen
) {
711 if (!iskeychar(c
) || (i
== store
.baselen
+1 && !isalpha(c
))) {
712 fprintf(stderr
, "invalid key: %s\n", key
);
718 } else if (c
== '\n') {
719 fprintf(stderr
, "invalid key (newline): %s\n", key
);
729 * The lock_file serves a purpose in addition to locking: the new
730 * contents of .git/config will be written into it.
732 fd
= open(lock_file
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
733 if (fd
< 0 || adjust_shared_perm(lock_file
)) {
734 fprintf(stderr
, "could not lock config file\n");
741 * If .git/config does not exist yet, write a minimal version.
743 in_fd
= open(config_filename
, O_RDONLY
);
747 if ( ENOENT
!= errno
) {
748 error("opening %s: %s", config_filename
,
750 ret
= 3; /* same as "invalid config file" */
753 /* if nothing to unset, error out */
759 store
.key
= (char*)key
;
760 if (!store_write_section(fd
, key
) ||
761 !store_write_pair(fd
, key
, value
))
766 size_t contents_sz
, copy_begin
, copy_end
;
769 if (value_regex
== NULL
)
770 store
.value_regex
= NULL
;
772 if (value_regex
[0] == '!') {
773 store
.do_not_match
= 1;
776 store
.do_not_match
= 0;
778 store
.value_regex
= (regex_t
*)xmalloc(sizeof(regex_t
));
779 if (regcomp(store
.value_regex
, value_regex
,
781 fprintf(stderr
, "Invalid pattern: %s\n",
783 free(store
.value_regex
);
794 * After this, store.offset will contain the *end* offset
795 * of the last match, or remain at 0 if no match was found.
796 * As a side effect, we make sure to transform only a valid
797 * existing config file.
799 if (git_config_from_file(store_aux
, config_filename
)) {
800 fprintf(stderr
, "invalid config file\n");
802 if (store
.value_regex
!= NULL
) {
803 regfree(store
.value_regex
);
804 free(store
.value_regex
);
811 if (store
.value_regex
!= NULL
) {
812 regfree(store
.value_regex
);
813 free(store
.value_regex
);
816 /* if nothing to unset, or too many matches, error out */
817 if ((store
.seen
== 0 && value
== NULL
) ||
818 (store
.seen
> 1 && multi_replace
== 0)) {
824 contents_sz
= xsize_t(st
.st_size
);
825 contents
= xmmap(NULL
, contents_sz
, PROT_READ
,
826 MAP_PRIVATE
, in_fd
, 0);
832 for (i
= 0, copy_begin
= 0; i
< store
.seen
; i
++) {
833 if (store
.offset
[i
] == 0) {
834 store
.offset
[i
] = copy_end
= contents_sz
;
835 } else if (store
.state
!= KEY_SEEN
) {
836 copy_end
= store
.offset
[i
];
838 copy_end
= find_beginning_of_line(
839 contents
, contents_sz
,
840 store
.offset
[i
]-2, &new_line
);
842 /* write the first part of the config */
843 if (copy_end
> copy_begin
) {
844 if (write_in_full(fd
, contents
+ copy_begin
,
845 copy_end
- copy_begin
) <
846 copy_end
- copy_begin
)
849 write_in_full(fd
, "\n", 1) != 1)
852 copy_begin
= store
.offset
[i
];
855 /* write the pair (value == NULL means unset) */
857 if (store
.state
== START
) {
858 if (!store_write_section(fd
, key
))
861 if (!store_write_pair(fd
, key
, value
))
865 /* write the rest of the config */
866 if (copy_begin
< contents_sz
)
867 if (write_in_full(fd
, contents
+ copy_begin
,
868 contents_sz
- copy_begin
) <
869 contents_sz
- copy_begin
)
872 munmap(contents
, contents_sz
);
873 unlink(config_filename
);
876 if (rename(lock_file
, config_filename
) < 0) {
877 fprintf(stderr
, "Could not rename the lock file?\n");
887 free(config_filename
);
900 static int section_name_match (const char *buf
, const char *name
)
902 int i
= 0, j
= 0, dot
= 0;
903 for (; buf
[i
] && buf
[i
] != ']'; i
++) {
904 if (!dot
&& isspace(buf
[i
])) {
906 if (name
[j
++] != '.')
908 for (i
++; isspace(buf
[i
]); i
++)
914 if (buf
[i
] == '\\' && dot
)
916 else if (buf
[i
] == '"' && dot
) {
917 for (i
++; isspace(buf
[i
]); i
++)
921 if (buf
[i
] != name
[j
++])
924 return (buf
[i
] == ']' && name
[j
] == 0);
927 /* if new_name == NULL, the section is removed instead */
928 int git_config_rename_section(const char *old_name
, const char *new_name
)
930 int ret
= 0, remove
= 0;
931 char *config_filename
;
932 struct lock_file
*lock
= xcalloc(sizeof(struct lock_file
), 1);
936 config_filename
= getenv(CONFIG_ENVIRONMENT
);
937 if (!config_filename
) {
938 config_filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
939 if (!config_filename
)
940 config_filename
= git_path("config");
942 config_filename
= xstrdup(config_filename
);
943 out_fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
945 ret
= error("Could not lock config file!");
949 if (!(config_file
= fopen(config_filename
, "rb"))) {
950 /* no config file means nothing to rename, no error */
954 while (fgets(buf
, sizeof(buf
), config_file
)) {
957 for (i
= 0; buf
[i
] && isspace(buf
[i
]); i
++)
961 if (section_name_match (&buf
[i
+1], old_name
)) {
963 if (new_name
== NULL
) {
967 store
.baselen
= strlen(new_name
);
968 if (!store_write_section(out_fd
, new_name
)) {
978 length
= strlen(buf
);
979 if (write_in_full(out_fd
, buf
, length
) != length
) {
986 if (close(out_fd
) || commit_lock_file(lock
) < 0)
987 ret
= error("Cannot commit config file!");
989 free(config_filename
);