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 get_next_char(void)
22 if ((f
= config_file
) != NULL
) {
25 /* DOS like systems */
42 static char *parse_value(void)
44 static char value
[1024];
45 int quote
= 0, comment
= 0, len
= 0, space
= 0;
48 int c
= get_next_char();
49 if (len
>= sizeof(value
))
59 if (isspace(c
) && !quote
) {
64 if (c
== ';' || c
== '#') {
88 /* Some characters escape as themselves */
91 /* Reject unknown escape sequences */
106 static inline int iskeychar(int c
)
108 return isalnum(c
) || c
== '-';
111 static int get_value(config_fn_t fn
, char *name
, unsigned int len
)
116 /* Get the full name */
123 name
[len
++] = tolower(c
);
128 while (c
== ' ' || c
== '\t')
135 value
= parse_value();
139 return fn(name
, value
);
142 static int get_extended_base_var(char *name
, int baselen
, int c
)
148 } while (isspace(c
));
150 /* We require the format to be '[base "extension"]' */
153 name
[baselen
++] = '.';
156 int c
= get_next_char();
167 if (baselen
> MAXNAME
/ 2)
172 if (get_next_char() != ']')
177 static int get_base_var(char *name
)
182 int c
= get_next_char();
188 return get_extended_base_var(name
, baselen
, c
);
189 if (!iskeychar(c
) && c
!= '.')
191 if (baselen
> MAXNAME
/ 2)
193 name
[baselen
++] = tolower(c
);
197 static int git_parse_file(config_fn_t fn
)
201 static char var
[MAXNAME
];
204 int c
= get_next_char();
212 if (comment
|| isspace(c
))
214 if (c
== '#' || c
== ';') {
219 baselen
= get_base_var(var
);
222 var
[baselen
++] = '.';
228 var
[baselen
] = tolower(c
);
229 if (get_value(fn
, var
, baselen
+1) < 0)
232 die("bad config file line %d in %s", config_linenr
, config_file_name
);
235 int git_config_int(const char *name
, const char *value
)
237 if (value
&& *value
) {
239 int val
= strtol(value
, &end
, 0);
242 if (!strcasecmp(end
, "k"))
244 if (!strcasecmp(end
, "m"))
245 return val
* 1024 * 1024;
246 if (!strcasecmp(end
, "g"))
247 return val
* 1024 * 1024 * 1024;
249 die("bad config value for '%s' in %s", name
, config_file_name
);
252 int git_config_bool(const char *name
, const char *value
)
258 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes"))
260 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no"))
262 return git_config_int(name
, value
) != 0;
265 int git_default_config(const char *var
, const char *value
)
267 /* This needs a better name */
268 if (!strcmp(var
, "core.filemode")) {
269 trust_executable_bit
= git_config_bool(var
, value
);
273 if (!strcmp(var
, "core.symlinks")) {
274 has_symlinks
= git_config_bool(var
, value
);
278 if (!strcmp(var
, "core.bare")) {
279 is_bare_repository_cfg
= git_config_bool(var
, value
);
283 if (!strcmp(var
, "core.ignorestat")) {
284 assume_unchanged
= git_config_bool(var
, value
);
288 if (!strcmp(var
, "core.prefersymlinkrefs")) {
289 prefer_symlink_refs
= git_config_bool(var
, value
);
293 if (!strcmp(var
, "core.logallrefupdates")) {
294 log_all_ref_updates
= git_config_bool(var
, value
);
298 if (!strcmp(var
, "core.warnambiguousrefs")) {
299 warn_ambiguous_refs
= git_config_bool(var
, value
);
303 if (!strcmp(var
, "core.legacyheaders")) {
304 use_legacy_headers
= git_config_bool(var
, value
);
308 if (!strcmp(var
, "core.compression")) {
309 int level
= git_config_int(var
, value
);
311 level
= Z_DEFAULT_COMPRESSION
;
312 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
313 die("bad zlib compression level %d", level
);
314 zlib_compression_level
= level
;
318 if (!strcmp(var
, "core.packedgitwindowsize")) {
319 int pgsz_x2
= getpagesize() * 2;
320 packed_git_window_size
= git_config_int(var
, value
);
322 /* This value must be multiple of (pagesize * 2) */
323 packed_git_window_size
/= pgsz_x2
;
324 if (packed_git_window_size
< 1)
325 packed_git_window_size
= 1;
326 packed_git_window_size
*= pgsz_x2
;
330 if (!strcmp(var
, "core.packedgitlimit")) {
331 packed_git_limit
= git_config_int(var
, value
);
335 if (!strcmp(var
, "core.deltabasecachelimit")) {
336 delta_base_cache_limit
= git_config_int(var
, value
);
340 if (!strcmp(var
, "core.autocrlf")) {
341 if (value
&& !strcasecmp(value
, "input")) {
345 auto_crlf
= git_config_bool(var
, value
);
349 if (!strcmp(var
, "user.name")) {
350 strlcpy(git_default_name
, value
, sizeof(git_default_name
));
354 if (!strcmp(var
, "user.email")) {
355 strlcpy(git_default_email
, value
, sizeof(git_default_email
));
359 if (!strcmp(var
, "i18n.commitencoding")) {
360 git_commit_encoding
= xstrdup(value
);
364 if (!strcmp(var
, "i18n.logoutputencoding")) {
365 git_log_output_encoding
= xstrdup(value
);
370 if (!strcmp(var
, "pager.color") || !strcmp(var
, "color.pager")) {
371 pager_use_color
= git_config_bool(var
,value
);
375 /* Add other config variables here and to Documentation/config.txt. */
379 int git_config_from_file(config_fn_t fn
, const char *filename
)
382 FILE *f
= fopen(filename
, "r");
387 config_file_name
= filename
;
389 ret
= git_parse_file(fn
);
391 config_file_name
= NULL
;
396 const char *git_etc_gitconfig(void)
398 return ETC_GITCONFIG
;
401 int git_config(config_fn_t fn
)
404 char *repo_config
= NULL
;
405 const char *home
= NULL
, *filename
;
407 /* $GIT_CONFIG makes git read _only_ the given config file,
408 * $GIT_CONFIG_LOCAL will make it process it in addition to the
409 * global config file, the same way it would the per-repository
410 * config file otherwise. */
411 filename
= getenv(CONFIG_ENVIRONMENT
);
413 if (!access(git_etc_gitconfig(), R_OK
))
414 ret
+= git_config_from_file(fn
, git_etc_gitconfig());
415 home
= getenv("HOME");
416 filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
418 filename
= repo_config
= xstrdup(git_path("config"));
422 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
423 if (!access(user_config
, R_OK
))
424 ret
= git_config_from_file(fn
, user_config
);
428 ret
+= git_config_from_file(fn
, filename
);
434 * Find all the stuff for git_config_set() below.
437 #define MAX_MATCHES 512
443 regex_t
* value_regex
;
445 size_t offset
[MAX_MATCHES
];
446 enum { START
, SECTION_SEEN
, SECTION_END_SEEN
, KEY_SEEN
} state
;
450 static int matches(const char* key
, const char* value
)
452 return !strcmp(key
, store
.key
) &&
453 (store
.value_regex
== NULL
||
454 (store
.do_not_match
^
455 !regexec(store
.value_regex
, value
, 0, NULL
, 0)));
458 static int store_aux(const char* key
, const char* value
)
463 switch (store
.state
) {
465 if (matches(key
, value
)) {
466 if (store
.seen
== 1 && store
.multi_replace
== 0) {
468 "Warning: %s has multiple values\n",
470 } else if (store
.seen
>= MAX_MATCHES
) {
471 fprintf(stderr
, "Too many matches\n");
475 store
.offset
[store
.seen
] = ftell(config_file
);
481 * What we are looking for is in store.key (both
482 * section and var), and its section part is baselen
483 * long. We found key (again, both section and var).
484 * We would want to know if this key is in the same
485 * section as what we are looking for. We already
486 * know we are in the same section as what should
489 ep
= strrchr(key
, '.');
490 section_len
= ep
- key
;
492 if ((section_len
!= store
.baselen
) ||
493 memcmp(key
, store
.key
, section_len
+1)) {
494 store
.state
= SECTION_END_SEEN
;
499 * Do not increment matches: this is no match, but we
500 * just made sure we are in the desired section.
502 store
.offset
[store
.seen
] = ftell(config_file
);
504 case SECTION_END_SEEN
:
506 if (matches(key
, value
)) {
507 store
.offset
[store
.seen
] = ftell(config_file
);
508 store
.state
= KEY_SEEN
;
511 if (strrchr(key
, '.') - key
== store
.baselen
&&
512 !strncmp(key
, store
.key
, store
.baselen
)) {
513 store
.state
= SECTION_SEEN
;
514 store
.offset
[store
.seen
] = ftell(config_file
);
521 static int write_error()
523 fprintf(stderr
, "Failed to write new configuration file\n");
525 /* Same error code as "failed to rename". */
529 static int store_write_section(int fd
, const char* key
)
531 const char *dot
= strchr(key
, '.');
532 int len1
= store
.baselen
, len2
= -1;
534 dot
= strchr(key
, '.');
536 int dotlen
= dot
- key
;
538 len2
= len1
- dotlen
- 1;
543 if (write_in_full(fd
, "[", 1) != 1 ||
544 write_in_full(fd
, key
, len1
) != len1
)
547 if (write_in_full(fd
, " \"", 2) != 2)
549 while (--len2
>= 0) {
550 unsigned char c
= *++dot
;
552 if (write_in_full(fd
, "\\", 1) != 1)
554 if (write_in_full(fd
, &c
, 1) != 1)
557 if (write_in_full(fd
, "\"", 1) != 1)
560 if (write_in_full(fd
, "]\n", 2) != 2)
566 static int store_write_pair(int fd
, const char* key
, const char* value
)
569 int length
= strlen(key
+store
.baselen
+1);
572 /* Check to see if the value needs to be quoted. */
575 for (i
= 0; value
[i
]; i
++)
576 if (value
[i
] == ';' || value
[i
] == '#')
578 if (value
[i
-1] == ' ')
581 if (write_in_full(fd
, "\t", 1) != 1 ||
582 write_in_full(fd
, key
+store
.baselen
+1, length
) != length
||
583 write_in_full(fd
, " = ", 3) != 3)
585 if (quote
&& write_in_full(fd
, "\"", 1) != 1)
587 for (i
= 0; value
[i
]; i
++)
590 if (write_in_full(fd
, "\\n", 2) != 2)
594 if (write_in_full(fd
, "\\t", 2) != 2)
599 if (write_in_full(fd
, "\\", 1) != 1)
602 if (write_in_full(fd
, value
+i
, 1) != 1)
606 if (quote
&& write_in_full(fd
, "\"", 1) != 1)
608 if (write_in_full(fd
, "\n", 1) != 1)
613 static ssize_t
find_beginning_of_line(const char* contents
, size_t size
,
614 size_t offset_
, int* found_bracket
)
616 size_t equal_offset
= size
, bracket_offset
= size
;
619 for (offset
= offset_
-2; offset
> 0
620 && contents
[offset
] != '\n'; offset
--)
621 switch (contents
[offset
]) {
622 case '=': equal_offset
= offset
; break;
623 case ']': bracket_offset
= offset
; break;
625 if (bracket_offset
< equal_offset
) {
627 offset
= bracket_offset
+1;
634 int git_config_set(const char* key
, const char* value
)
636 return git_config_set_multivar(key
, value
, NULL
, 0);
640 * If value==NULL, unset in (remove from) config,
641 * if value_regex!=NULL, disregard key/value pairs where value does not match.
642 * if multi_replace==0, nothing, or only one matching key/value is replaced,
643 * else all matching key/values (regardless how many) are removed,
644 * before the new pair is written.
646 * Returns 0 on success.
648 * This function does this:
650 * - it locks the config file by creating ".git/config.lock"
652 * - it then parses the config using store_aux() as validator to find
653 * the position on the key/value pair to replace. If it is to be unset,
654 * it must be found exactly once.
656 * - the config file is mmap()ed and the part before the match (if any) is
657 * written to the lock file, then the changed part and the rest.
659 * - the config file is removed and the lock file rename()d to it.
662 int git_config_set_multivar(const char* key
, const char* value
,
663 const char* value_regex
, int multi_replace
)
668 char* config_filename
;
670 const char* last_dot
= strrchr(key
, '.');
672 config_filename
= getenv(CONFIG_ENVIRONMENT
);
673 if (!config_filename
) {
674 config_filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
675 if (!config_filename
)
676 config_filename
= git_path("config");
678 config_filename
= xstrdup(config_filename
);
679 lock_file
= xstrdup(mkpath("%s.lock", config_filename
));
682 * Since "key" actually contains the section name and the real
683 * key name separated by a dot, we have to know where the dot is.
686 if (last_dot
== NULL
) {
687 fprintf(stderr
, "key does not contain a section: %s\n", key
);
691 store
.baselen
= last_dot
- key
;
693 store
.multi_replace
= multi_replace
;
696 * Validate the key and while at it, lower case it for matching.
698 store
.key
= xmalloc(strlen(key
) + 1);
700 for (i
= 0; key
[i
]; i
++) {
701 unsigned char c
= key
[i
];
704 /* Leave the extended basename untouched.. */
705 if (!dot
|| i
> store
.baselen
) {
706 if (!iskeychar(c
) || (i
== store
.baselen
+1 && !isalpha(c
))) {
707 fprintf(stderr
, "invalid key: %s\n", key
);
713 } else if (c
== '\n') {
714 fprintf(stderr
, "invalid key (newline): %s\n", key
);
724 * The lock_file serves a purpose in addition to locking: the new
725 * contents of .git/config will be written into it.
727 fd
= open(lock_file
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
728 if (fd
< 0 || adjust_shared_perm(lock_file
)) {
729 fprintf(stderr
, "could not lock config file\n");
736 * If .git/config does not exist yet, write a minimal version.
738 in_fd
= open(config_filename
, O_RDONLY
);
742 if ( ENOENT
!= errno
) {
743 error("opening %s: %s", config_filename
,
745 ret
= 3; /* same as "invalid config file" */
748 /* if nothing to unset, error out */
754 store
.key
= (char*)key
;
755 if (!store_write_section(fd
, key
) ||
756 !store_write_pair(fd
, key
, value
))
761 size_t contents_sz
, copy_begin
, copy_end
;
764 if (value_regex
== NULL
)
765 store
.value_regex
= NULL
;
767 if (value_regex
[0] == '!') {
768 store
.do_not_match
= 1;
771 store
.do_not_match
= 0;
773 store
.value_regex
= (regex_t
*)xmalloc(sizeof(regex_t
));
774 if (regcomp(store
.value_regex
, value_regex
,
776 fprintf(stderr
, "Invalid pattern: %s\n",
778 free(store
.value_regex
);
789 * After this, store.offset will contain the *end* offset
790 * of the last match, or remain at 0 if no match was found.
791 * As a side effect, we make sure to transform only a valid
792 * existing config file.
794 if (git_config_from_file(store_aux
, config_filename
)) {
795 fprintf(stderr
, "invalid config file\n");
797 if (store
.value_regex
!= NULL
) {
798 regfree(store
.value_regex
);
799 free(store
.value_regex
);
806 if (store
.value_regex
!= NULL
) {
807 regfree(store
.value_regex
);
808 free(store
.value_regex
);
811 /* if nothing to unset, or too many matches, error out */
812 if ((store
.seen
== 0 && value
== NULL
) ||
813 (store
.seen
> 1 && multi_replace
== 0)) {
819 contents_sz
= xsize_t(st
.st_size
);
820 contents
= xmmap(NULL
, contents_sz
, PROT_READ
,
821 MAP_PRIVATE
, in_fd
, 0);
827 for (i
= 0, copy_begin
= 0; i
< store
.seen
; i
++) {
828 if (store
.offset
[i
] == 0) {
829 store
.offset
[i
] = copy_end
= contents_sz
;
830 } else if (store
.state
!= KEY_SEEN
) {
831 copy_end
= store
.offset
[i
];
833 copy_end
= find_beginning_of_line(
834 contents
, contents_sz
,
835 store
.offset
[i
]-2, &new_line
);
837 /* write the first part of the config */
838 if (copy_end
> copy_begin
) {
839 if (write_in_full(fd
, contents
+ copy_begin
,
840 copy_end
- copy_begin
) <
841 copy_end
- copy_begin
)
844 write_in_full(fd
, "\n", 1) != 1)
847 copy_begin
= store
.offset
[i
];
850 /* write the pair (value == NULL means unset) */
852 if (store
.state
== START
) {
853 if (!store_write_section(fd
, key
))
856 if (!store_write_pair(fd
, key
, value
))
860 /* write the rest of the config */
861 if (copy_begin
< contents_sz
)
862 if (write_in_full(fd
, contents
+ copy_begin
,
863 contents_sz
- copy_begin
) <
864 contents_sz
- copy_begin
)
867 munmap(contents
, contents_sz
);
868 unlink(config_filename
);
873 if (rename(lock_file
, config_filename
) < 0) {
874 fprintf(stderr
, "Could not rename the lock file?\n");
884 free(config_filename
);
897 static int section_name_match (const char *buf
, const char *name
)
899 int i
= 0, j
= 0, dot
= 0;
900 for (; buf
[i
] && buf
[i
] != ']'; i
++) {
901 if (!dot
&& isspace(buf
[i
])) {
903 if (name
[j
++] != '.')
905 for (i
++; isspace(buf
[i
]); i
++)
911 if (buf
[i
] == '\\' && dot
)
913 else if (buf
[i
] == '"' && dot
) {
914 for (i
++; isspace(buf
[i
]); i
++)
918 if (buf
[i
] != name
[j
++])
921 return (buf
[i
] == ']' && name
[j
] == 0);
924 /* if new_name == NULL, the section is removed instead */
925 int git_config_rename_section(const char *old_name
, const char *new_name
)
927 int ret
= 0, remove
= 0;
928 char *config_filename
;
929 struct lock_file
*lock
= xcalloc(sizeof(struct lock_file
), 1);
933 config_filename
= getenv(CONFIG_ENVIRONMENT
);
934 if (!config_filename
) {
935 config_filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
936 if (!config_filename
)
937 config_filename
= git_path("config");
939 config_filename
= xstrdup(config_filename
);
940 out_fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
942 ret
= error("Could not lock config file!");
946 if (!(config_file
= fopen(config_filename
, "rb"))) {
947 /* no config file means nothing to rename, no error */
951 while (fgets(buf
, sizeof(buf
), config_file
)) {
954 for (i
= 0; buf
[i
] && isspace(buf
[i
]); i
++)
958 if (section_name_match (&buf
[i
+1], old_name
)) {
960 if (new_name
== NULL
) {
964 store
.baselen
= strlen(new_name
);
965 if (!store_write_section(out_fd
, new_name
)) {
975 length
= strlen(buf
);
976 if (write_in_full(out_fd
, buf
, length
) != length
) {
983 if (close(out_fd
) || commit_lock_file(lock
) < 0)
984 ret
= error("Cannot commit config file!");
986 free(config_filename
);