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 static unsigned long get_unit_factor(const char *end
)
240 else if (!strcasecmp(end
, "k"))
242 else if (!strcasecmp(end
, "m"))
244 else if (!strcasecmp(end
, "g"))
245 return 1024 * 1024 * 1024;
246 die("unknown unit: '%s'", end
);
249 int git_parse_long(const char *value
, long *ret
)
251 if (value
&& *value
) {
253 long val
= strtol(value
, &end
, 0);
254 *ret
= val
* get_unit_factor(end
);
260 int git_parse_ulong(const char *value
, unsigned long *ret
)
262 if (value
&& *value
) {
264 unsigned long val
= strtoul(value
, &end
, 0);
265 *ret
= val
* get_unit_factor(end
);
271 int git_config_int(const char *name
, const char *value
)
274 if (!git_parse_long(value
, &ret
))
275 die("bad config value for '%s' in %s", name
, config_file_name
);
279 unsigned long git_config_ulong(const char *name
, const char *value
)
282 if (!git_parse_ulong(value
, &ret
))
283 die("bad config value for '%s' in %s", name
, config_file_name
);
287 int git_config_bool(const char *name
, const char *value
)
293 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes"))
295 if (!strcasecmp(value
, "false") || !strcasecmp(value
, "no"))
297 return git_config_int(name
, value
) != 0;
300 int git_default_config(const char *var
, const char *value
)
302 /* This needs a better name */
303 if (!strcmp(var
, "core.filemode")) {
304 trust_executable_bit
= git_config_bool(var
, value
);
308 if (!strcmp(var
, "core.quotepath")) {
309 quote_path_fully
= git_config_bool(var
, value
);
313 if (!strcmp(var
, "core.symlinks")) {
314 has_symlinks
= git_config_bool(var
, value
);
318 if (!strcmp(var
, "core.bare")) {
319 is_bare_repository_cfg
= git_config_bool(var
, value
);
323 if (!strcmp(var
, "core.ignorestat")) {
324 assume_unchanged
= git_config_bool(var
, value
);
328 if (!strcmp(var
, "core.prefersymlinkrefs")) {
329 prefer_symlink_refs
= git_config_bool(var
, value
);
333 if (!strcmp(var
, "core.logallrefupdates")) {
334 log_all_ref_updates
= git_config_bool(var
, value
);
338 if (!strcmp(var
, "core.warnambiguousrefs")) {
339 warn_ambiguous_refs
= git_config_bool(var
, value
);
343 if (!strcmp(var
, "core.loosecompression")) {
344 int level
= git_config_int(var
, value
);
346 level
= Z_DEFAULT_COMPRESSION
;
347 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
348 die("bad zlib compression level %d", level
);
349 zlib_compression_level
= level
;
350 zlib_compression_seen
= 1;
354 if (!strcmp(var
, "core.compression")) {
355 int level
= git_config_int(var
, value
);
357 level
= Z_DEFAULT_COMPRESSION
;
358 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
359 die("bad zlib compression level %d", level
);
360 core_compression_level
= level
;
361 core_compression_seen
= 1;
362 if (!zlib_compression_seen
)
363 zlib_compression_level
= level
;
367 if (!strcmp(var
, "core.packedgitwindowsize")) {
368 int pgsz_x2
= getpagesize() * 2;
369 packed_git_window_size
= git_config_int(var
, value
);
371 /* This value must be multiple of (pagesize * 2) */
372 packed_git_window_size
/= pgsz_x2
;
373 if (packed_git_window_size
< 1)
374 packed_git_window_size
= 1;
375 packed_git_window_size
*= pgsz_x2
;
379 if (!strcmp(var
, "core.packedgitlimit")) {
380 packed_git_limit
= git_config_int(var
, value
);
384 if (!strcmp(var
, "core.deltabasecachelimit")) {
385 delta_base_cache_limit
= git_config_int(var
, value
);
389 if (!strcmp(var
, "core.autocrlf")) {
390 if (value
&& !strcasecmp(value
, "input")) {
394 auto_crlf
= git_config_bool(var
, value
);
398 if (!strcmp(var
, "user.name")) {
399 strlcpy(git_default_name
, value
, sizeof(git_default_name
));
403 if (!strcmp(var
, "user.email")) {
404 strlcpy(git_default_email
, value
, sizeof(git_default_email
));
408 if (!strcmp(var
, "i18n.commitencoding")) {
409 git_commit_encoding
= xstrdup(value
);
413 if (!strcmp(var
, "i18n.logoutputencoding")) {
414 git_log_output_encoding
= xstrdup(value
);
419 if (!strcmp(var
, "pager.color") || !strcmp(var
, "color.pager")) {
420 pager_use_color
= git_config_bool(var
,value
);
424 if (!strcmp(var
, "core.pager")) {
425 pager_program
= xstrdup(value
);
429 if (!strcmp(var
, "core.editor")) {
430 editor_program
= xstrdup(value
);
434 if (!strcmp(var
, "core.whitespace")) {
435 whitespace_rule_cfg
= parse_whitespace_rule(value
);
439 /* Add other config variables here and to Documentation/config.txt. */
443 int git_config_from_file(config_fn_t fn
, const char *filename
)
446 FILE *f
= fopen(filename
, "r");
451 config_file_name
= filename
;
453 ret
= git_parse_file(fn
);
455 config_file_name
= NULL
;
460 int git_config(config_fn_t fn
)
463 char *repo_config
= NULL
;
464 const char *home
= NULL
, *filename
;
466 /* $GIT_CONFIG makes git read _only_ the given config file,
467 * $GIT_CONFIG_LOCAL will make it process it in addition to the
468 * global config file, the same way it would the per-repository
469 * config file otherwise. */
470 filename
= getenv(CONFIG_ENVIRONMENT
);
472 if (!access(ETC_GITCONFIG
, R_OK
))
473 ret
+= git_config_from_file(fn
, ETC_GITCONFIG
);
474 home
= getenv("HOME");
475 filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
477 filename
= repo_config
= xstrdup(git_path("config"));
481 char *user_config
= xstrdup(mkpath("%s/.gitconfig", home
));
482 if (!access(user_config
, R_OK
))
483 ret
= git_config_from_file(fn
, user_config
);
487 ret
+= git_config_from_file(fn
, filename
);
493 * Find all the stuff for git_config_set() below.
496 #define MAX_MATCHES 512
502 regex_t
* value_regex
;
504 size_t offset
[MAX_MATCHES
];
505 enum { START
, SECTION_SEEN
, SECTION_END_SEEN
, KEY_SEEN
} state
;
509 static int matches(const char* key
, const char* value
)
511 return !strcmp(key
, store
.key
) &&
512 (store
.value_regex
== NULL
||
513 (store
.do_not_match
^
514 !regexec(store
.value_regex
, value
, 0, NULL
, 0)));
517 static int store_aux(const char* key
, const char* value
)
522 switch (store
.state
) {
524 if (matches(key
, value
)) {
525 if (store
.seen
== 1 && store
.multi_replace
== 0) {
527 "Warning: %s has multiple values\n",
529 } else if (store
.seen
>= MAX_MATCHES
) {
530 fprintf(stderr
, "Too many matches\n");
534 store
.offset
[store
.seen
] = ftell(config_file
);
540 * What we are looking for is in store.key (both
541 * section and var), and its section part is baselen
542 * long. We found key (again, both section and var).
543 * We would want to know if this key is in the same
544 * section as what we are looking for. We already
545 * know we are in the same section as what should
548 ep
= strrchr(key
, '.');
549 section_len
= ep
- key
;
551 if ((section_len
!= store
.baselen
) ||
552 memcmp(key
, store
.key
, section_len
+1)) {
553 store
.state
= SECTION_END_SEEN
;
558 * Do not increment matches: this is no match, but we
559 * just made sure we are in the desired section.
561 store
.offset
[store
.seen
] = ftell(config_file
);
563 case SECTION_END_SEEN
:
565 if (matches(key
, value
)) {
566 store
.offset
[store
.seen
] = ftell(config_file
);
567 store
.state
= KEY_SEEN
;
570 if (strrchr(key
, '.') - key
== store
.baselen
&&
571 !strncmp(key
, store
.key
, store
.baselen
)) {
572 store
.state
= SECTION_SEEN
;
573 store
.offset
[store
.seen
] = ftell(config_file
);
580 static int write_error(void)
582 fprintf(stderr
, "Failed to write new configuration file\n");
584 /* Same error code as "failed to rename". */
588 static int store_write_section(int fd
, const char* key
)
590 const char *dot
= strchr(key
, '.');
591 int len1
= store
.baselen
, len2
= -1;
593 dot
= strchr(key
, '.');
595 int dotlen
= dot
- key
;
597 len2
= len1
- dotlen
- 1;
602 if (write_in_full(fd
, "[", 1) != 1 ||
603 write_in_full(fd
, key
, len1
) != len1
)
606 if (write_in_full(fd
, " \"", 2) != 2)
608 while (--len2
>= 0) {
609 unsigned char c
= *++dot
;
611 if (write_in_full(fd
, "\\", 1) != 1)
613 if (write_in_full(fd
, &c
, 1) != 1)
616 if (write_in_full(fd
, "\"", 1) != 1)
619 if (write_in_full(fd
, "]\n", 2) != 2)
625 static int store_write_pair(int fd
, const char* key
, const char* value
)
628 int length
= strlen(key
+store
.baselen
+1);
631 /* Check to see if the value needs to be quoted. */
634 for (i
= 0; value
[i
]; i
++)
635 if (value
[i
] == ';' || value
[i
] == '#')
637 if (value
[i
-1] == ' ')
640 if (write_in_full(fd
, "\t", 1) != 1 ||
641 write_in_full(fd
, key
+store
.baselen
+1, length
) != length
||
642 write_in_full(fd
, " = ", 3) != 3)
644 if (quote
&& write_in_full(fd
, "\"", 1) != 1)
646 for (i
= 0; value
[i
]; i
++)
649 if (write_in_full(fd
, "\\n", 2) != 2)
653 if (write_in_full(fd
, "\\t", 2) != 2)
658 if (write_in_full(fd
, "\\", 1) != 1)
661 if (write_in_full(fd
, value
+i
, 1) != 1)
665 if (quote
&& write_in_full(fd
, "\"", 1) != 1)
667 if (write_in_full(fd
, "\n", 1) != 1)
672 static ssize_t
find_beginning_of_line(const char* contents
, size_t size
,
673 size_t offset_
, int* found_bracket
)
675 size_t equal_offset
= size
, bracket_offset
= size
;
678 for (offset
= offset_
-2; offset
> 0
679 && contents
[offset
] != '\n'; offset
--)
680 switch (contents
[offset
]) {
681 case '=': equal_offset
= offset
; break;
682 case ']': bracket_offset
= offset
; break;
684 if (bracket_offset
< equal_offset
) {
686 offset
= bracket_offset
+1;
693 int git_config_set(const char* key
, const char* value
)
695 return git_config_set_multivar(key
, value
, NULL
, 0);
699 * If value==NULL, unset in (remove from) config,
700 * if value_regex!=NULL, disregard key/value pairs where value does not match.
701 * if multi_replace==0, nothing, or only one matching key/value is replaced,
702 * else all matching key/values (regardless how many) are removed,
703 * before the new pair is written.
705 * Returns 0 on success.
707 * This function does this:
709 * - it locks the config file by creating ".git/config.lock"
711 * - it then parses the config using store_aux() as validator to find
712 * the position on the key/value pair to replace. If it is to be unset,
713 * it must be found exactly once.
715 * - the config file is mmap()ed and the part before the match (if any) is
716 * written to the lock file, then the changed part and the rest.
718 * - the config file is removed and the lock file rename()d to it.
721 int git_config_set_multivar(const char* key
, const char* value
,
722 const char* value_regex
, int multi_replace
)
727 char* config_filename
;
728 struct lock_file
*lock
= NULL
;
729 const char* last_dot
= strrchr(key
, '.');
731 config_filename
= getenv(CONFIG_ENVIRONMENT
);
732 if (!config_filename
) {
733 config_filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
734 if (!config_filename
)
735 config_filename
= git_path("config");
737 config_filename
= xstrdup(config_filename
);
740 * Since "key" actually contains the section name and the real
741 * key name separated by a dot, we have to know where the dot is.
744 if (last_dot
== NULL
) {
745 fprintf(stderr
, "key does not contain a section: %s\n", key
);
749 store
.baselen
= last_dot
- key
;
751 store
.multi_replace
= multi_replace
;
754 * Validate the key and while at it, lower case it for matching.
756 store
.key
= xmalloc(strlen(key
) + 1);
758 for (i
= 0; key
[i
]; i
++) {
759 unsigned char c
= key
[i
];
762 /* Leave the extended basename untouched.. */
763 if (!dot
|| i
> store
.baselen
) {
764 if (!iskeychar(c
) || (i
== store
.baselen
+1 && !isalpha(c
))) {
765 fprintf(stderr
, "invalid key: %s\n", key
);
771 } else if (c
== '\n') {
772 fprintf(stderr
, "invalid key (newline): %s\n", key
);
782 * The lock serves a purpose in addition to locking: the new
783 * contents of .git/config will be written into it.
785 lock
= xcalloc(sizeof(struct lock_file
), 1);
786 fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
788 fprintf(stderr
, "could not lock config file\n");
795 * If .git/config does not exist yet, write a minimal version.
797 in_fd
= open(config_filename
, O_RDONLY
);
801 if ( ENOENT
!= errno
) {
802 error("opening %s: %s", config_filename
,
804 ret
= 3; /* same as "invalid config file" */
807 /* if nothing to unset, error out */
813 store
.key
= (char*)key
;
814 if (!store_write_section(fd
, key
) ||
815 !store_write_pair(fd
, key
, value
))
820 size_t contents_sz
, copy_begin
, copy_end
;
823 if (value_regex
== NULL
)
824 store
.value_regex
= NULL
;
826 if (value_regex
[0] == '!') {
827 store
.do_not_match
= 1;
830 store
.do_not_match
= 0;
832 store
.value_regex
= (regex_t
*)xmalloc(sizeof(regex_t
));
833 if (regcomp(store
.value_regex
, value_regex
,
835 fprintf(stderr
, "Invalid pattern: %s\n",
837 free(store
.value_regex
);
848 * After this, store.offset will contain the *end* offset
849 * of the last match, or remain at 0 if no match was found.
850 * As a side effect, we make sure to transform only a valid
851 * existing config file.
853 if (git_config_from_file(store_aux
, config_filename
)) {
854 fprintf(stderr
, "invalid config file\n");
856 if (store
.value_regex
!= NULL
) {
857 regfree(store
.value_regex
);
858 free(store
.value_regex
);
865 if (store
.value_regex
!= NULL
) {
866 regfree(store
.value_regex
);
867 free(store
.value_regex
);
870 /* if nothing to unset, or too many matches, error out */
871 if ((store
.seen
== 0 && value
== NULL
) ||
872 (store
.seen
> 1 && multi_replace
== 0)) {
878 contents_sz
= xsize_t(st
.st_size
);
879 contents
= xmmap(NULL
, contents_sz
, PROT_READ
,
880 MAP_PRIVATE
, in_fd
, 0);
886 for (i
= 0, copy_begin
= 0; i
< store
.seen
; i
++) {
887 if (store
.offset
[i
] == 0) {
888 store
.offset
[i
] = copy_end
= contents_sz
;
889 } else if (store
.state
!= KEY_SEEN
) {
890 copy_end
= store
.offset
[i
];
892 copy_end
= find_beginning_of_line(
893 contents
, contents_sz
,
894 store
.offset
[i
]-2, &new_line
);
896 /* write the first part of the config */
897 if (copy_end
> copy_begin
) {
898 if (write_in_full(fd
, contents
+ copy_begin
,
899 copy_end
- copy_begin
) <
900 copy_end
- copy_begin
)
903 write_in_full(fd
, "\n", 1) != 1)
906 copy_begin
= store
.offset
[i
];
909 /* write the pair (value == NULL means unset) */
911 if (store
.state
== START
) {
912 if (!store_write_section(fd
, key
))
915 if (!store_write_pair(fd
, key
, value
))
919 /* write the rest of the config */
920 if (copy_begin
< contents_sz
)
921 if (write_in_full(fd
, contents
+ copy_begin
,
922 contents_sz
- copy_begin
) <
923 contents_sz
- copy_begin
)
926 munmap(contents
, contents_sz
);
929 if (close(fd
) || commit_lock_file(lock
) < 0) {
930 fprintf(stderr
, "Cannot commit config file!\n");
935 /* fd is closed, so don't try to close it below. */
938 * lock is committed, so don't try to roll it back below.
939 * NOTE: Since lockfile.c keeps a linked list of all created
940 * lock_file structures, it isn't safe to free(lock). It's
941 * better to just leave it hanging around.
950 rollback_lock_file(lock
);
951 free(config_filename
);
960 static int section_name_match (const char *buf
, const char *name
)
962 int i
= 0, j
= 0, dot
= 0;
963 for (; buf
[i
] && buf
[i
] != ']'; i
++) {
964 if (!dot
&& isspace(buf
[i
])) {
966 if (name
[j
++] != '.')
968 for (i
++; isspace(buf
[i
]); i
++)
974 if (buf
[i
] == '\\' && dot
)
976 else if (buf
[i
] == '"' && dot
) {
977 for (i
++; isspace(buf
[i
]); i
++)
981 if (buf
[i
] != name
[j
++])
984 return (buf
[i
] == ']' && name
[j
] == 0);
987 /* if new_name == NULL, the section is removed instead */
988 int git_config_rename_section(const char *old_name
, const char *new_name
)
990 int ret
= 0, remove
= 0;
991 char *config_filename
;
992 struct lock_file
*lock
= xcalloc(sizeof(struct lock_file
), 1);
996 config_filename
= getenv(CONFIG_ENVIRONMENT
);
997 if (!config_filename
) {
998 config_filename
= getenv(CONFIG_LOCAL_ENVIRONMENT
);
999 if (!config_filename
)
1000 config_filename
= git_path("config");
1002 config_filename
= xstrdup(config_filename
);
1003 out_fd
= hold_lock_file_for_update(lock
, config_filename
, 0);
1005 ret
= error("Could not lock config file!");
1009 if (!(config_file
= fopen(config_filename
, "rb"))) {
1010 /* no config file means nothing to rename, no error */
1011 goto unlock_and_out
;
1014 while (fgets(buf
, sizeof(buf
), config_file
)) {
1017 for (i
= 0; buf
[i
] && isspace(buf
[i
]); i
++)
1019 if (buf
[i
] == '[') {
1020 /* it's a section */
1021 if (section_name_match (&buf
[i
+1], old_name
)) {
1023 if (new_name
== NULL
) {
1027 store
.baselen
= strlen(new_name
);
1028 if (!store_write_section(out_fd
, new_name
)) {
1029 ret
= write_error();
1038 length
= strlen(buf
);
1039 if (write_in_full(out_fd
, buf
, length
) != length
) {
1040 ret
= write_error();
1044 fclose(config_file
);
1046 if (close(out_fd
) || commit_lock_file(lock
) < 0)
1047 ret
= error("Cannot commit config file!");
1049 free(config_filename
);