2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
13 int git_ignore_local_config = 0;
14 static FILE *config_file;
15 static const char *config_file_name;
16 static int config_linenr;
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 int get_value(config_fn_t fn, char *name, unsigned int len)
112 /* Get the full name */
119 name[len++] = tolower(c);
124 while (c == ' ' || c == '\t')
131 value = parse_value();
135 return fn(name, value);
138 static int get_extended_base_var(char *name, int baselen, int c)
144 } while (isspace(c));
146 /* We require the format to be '[base "extension"]' */
149 name[baselen++] = '.';
152 int c = get_next_char();
163 if (baselen > MAXNAME / 2)
168 if (get_next_char() != ']')
173 static int get_base_var(char *name)
178 int c = get_next_char();
184 return get_extended_base_var(name, baselen, c);
185 if (!isalnum(c) && c != '.')
187 if (baselen > MAXNAME / 2)
189 name[baselen++] = tolower(c);
193 static int git_parse_file(config_fn_t fn)
197 static char var[MAXNAME];
200 int c = get_next_char();
208 if (comment || isspace(c))
210 if (c == '#' || c == ';') {
215 baselen = get_base_var(var);
218 var[baselen++] = '.';
224 var[baselen] = tolower(c);
225 if (get_value(fn, var, baselen+1) < 0)
228 die("bad config file line %d in %s", config_linenr, config_file_name);
231 int git_config_int(const char *name, const char *value)
233 if (value && *value) {
235 int val = strtol(value, &end, 0);
239 die("bad config value for '%s' in %s", name, config_file_name);
242 int git_config_bool(const char *name, const char *value)
248 if (!strcasecmp(value, "true"))
250 if (!strcasecmp(value, "false"))
252 return git_config_int(name, value) != 0;
255 int git_default_config(const char *var, const char *value)
257 /* This needs a better name */
258 if (!strcmp(var, "core.filemode")) {
259 trust_executable_bit = git_config_bool(var, value);
263 if (!strcmp(var, "core.ignorestat")) {
264 assume_unchanged = git_config_bool(var, value);
268 if (!strcmp(var, "core.prefersymlinkrefs")) {
269 prefer_symlink_refs = git_config_bool(var, value);
273 if (!strcmp(var, "core.logallrefupdates")) {
274 log_all_ref_updates = git_config_bool(var, value);
278 if (!strcmp(var, "core.warnambiguousrefs")) {
279 warn_ambiguous_refs = git_config_bool(var, value);
283 if (!strcmp(var, "user.name")) {
284 strncpy(git_default_name, value, sizeof(git_default_name));
288 if (!strcmp(var, "user.email")) {
289 strncpy(git_default_email, value, sizeof(git_default_email));
293 if (!strcmp(var, "i18n.commitencoding")) {
294 strncpy(git_commit_encoding, value, sizeof(git_commit_encoding));
298 /* Add other config variables here and to Documentation/config.txt. */
302 int git_config_from_file(config_fn_t fn, const char *filename)
305 FILE *f = fopen(filename, "r");
310 config_file_name = filename;
312 ret = git_parse_file(fn);
314 config_file_name = NULL;
319 int git_config(config_fn_t fn)
322 const char *home = getenv("HOME");
325 ret = git_config_from_file(fn, mkpath("%s/.gitconfig", home));
326 /* ignore if global config does not exist */
331 if (!git_ignore_local_config)
332 ret += git_config_from_file(fn, git_path("config"));
337 * Find all the stuff for git_config_set() below.
340 #define MAX_MATCHES 512
346 regex_t* value_regex;
348 off_t offset[MAX_MATCHES];
349 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
353 static int matches(const char* key, const char* value)
355 return !strcmp(key, store.key) &&
356 (store.value_regex == NULL ||
357 (store.do_not_match ^
358 !regexec(store.value_regex, value, 0, NULL, 0)));
361 static int store_aux(const char* key, const char* value)
363 switch (store.state) {
365 if (matches(key, value)) {
366 if (store.seen == 1 && store.multi_replace == 0) {
368 "Warning: %s has multiple values\n",
370 } else if (store.seen >= MAX_MATCHES) {
371 fprintf(stderr, "Too many matches\n");
375 store.offset[store.seen] = ftell(config_file);
380 if (strncmp(key, store.key, store.baselen+1)) {
381 store.state = SECTION_END_SEEN;
384 /* do not increment matches: this is no match */
385 store.offset[store.seen] = ftell(config_file);
387 case SECTION_END_SEEN:
389 if (matches(key, value)) {
390 store.offset[store.seen] = ftell(config_file);
391 store.state = KEY_SEEN;
394 if (strrchr(key, '.') - key == store.baselen &&
395 !strncmp(key, store.key, store.baselen)) {
396 store.state = SECTION_SEEN;
397 store.offset[store.seen] = ftell(config_file);
404 static void store_write_section(int fd, const char* key)
406 const char *dot = strchr(key, '.');
407 int len1 = store.baselen, len2 = -1;
409 dot = strchr(key, '.');
411 int dotlen = dot - key;
413 len2 = len1 - dotlen - 1;
419 write(fd, key, len1);
422 while (--len2 >= 0) {
423 unsigned char c = *++dot;
433 static void store_write_pair(int fd, const char* key, const char* value)
438 write(fd, key+store.baselen+1,
439 strlen(key+store.baselen+1));
441 for (i = 0; value[i]; i++)
443 case '\n': write(fd, "\\n", 2); break;
444 case '\t': write(fd, "\\t", 2); break;
445 case '"': case '\\': write(fd, "\\", 1);
446 default: write(fd, value+i, 1);
451 static int find_beginning_of_line(const char* contents, int size,
452 int offset_, int* found_bracket)
454 int equal_offset = size, bracket_offset = size;
457 for (offset = offset_-2; offset > 0
458 && contents[offset] != '\n'; offset--)
459 switch (contents[offset]) {
460 case '=': equal_offset = offset; break;
461 case ']': bracket_offset = offset; break;
463 if (bracket_offset < equal_offset) {
465 offset = bracket_offset+1;
472 int git_config_set(const char* key, const char* value)
474 return git_config_set_multivar(key, value, NULL, 0);
478 * If value==NULL, unset in (remove from) config,
479 * if value_regex!=NULL, disregard key/value pairs where value does not match.
480 * if multi_replace==0, nothing, or only one matching key/value is replaced,
481 * else all matching key/values (regardless how many) are removed,
482 * before the new pair is written.
484 * Returns 0 on success.
486 * This function does this:
488 * - it locks the config file by creating ".git/config.lock"
490 * - it then parses the config using store_aux() as validator to find
491 * the position on the key/value pair to replace. If it is to be unset,
492 * it must be found exactly once.
494 * - the config file is mmap()ed and the part before the match (if any) is
495 * written to the lock file, then the changed part and the rest.
497 * - the config file is removed and the lock file rename()d to it.
500 int git_config_set_multivar(const char* key, const char* value,
501 const char* value_regex, int multi_replace)
506 char *config_filename, *lock_file;
507 const char* last_dot = strrchr(key, '.');
509 if (git_ignore_local_config) {
510 const char *home = getenv("HOME");
513 config_filename = strdup(mkpath("%s/.gitconfig", home));
514 lock_file = strdup(mkpath("%s/.gitconfig.lock", home));
516 config_filename = strdup(git_path("config"));
517 lock_file = strdup(git_path("config.lock"));
521 * Since "key" actually contains the section name and the real
522 * key name separated by a dot, we have to know where the dot is.
525 if (last_dot == NULL) {
526 fprintf(stderr, "key does not contain a section: %s\n", key);
530 store.baselen = last_dot - key;
532 store.multi_replace = multi_replace;
535 * Validate the key and while at it, lower case it for matching.
537 store.key = (char*)malloc(strlen(key)+1);
539 for (i = 0; key[i]; i++) {
540 unsigned char c = key[i];
543 /* Leave the extended basename untouched.. */
544 if (!dot || i > store.baselen) {
545 if (!isalnum(c) || (i == store.baselen+1 && !isalpha(c))) {
546 fprintf(stderr, "invalid key: %s\n", key);
558 * The lock_file serves a purpose in addition to locking: the new
559 * contents of .git/config will be written into it.
561 fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
562 if (fd < 0 || adjust_shared_perm(lock_file)) {
563 fprintf(stderr, "could not lock config file\n");
570 * If .git/config does not exist yet, write a minimal version.
572 in_fd = open(config_filename, O_RDONLY);
576 if ( ENOENT != errno ) {
577 error("opening %s: %s", config_filename,
579 ret = 3; /* same as "invalid config file" */
582 /* if nothing to unset, error out */
588 store.key = (char*)key;
589 store_write_section(fd, key);
590 store_write_pair(fd, key, value);
594 int i, copy_begin, copy_end, new_line = 0;
596 if (value_regex == NULL)
597 store.value_regex = NULL;
599 if (value_regex[0] == '!') {
600 store.do_not_match = 1;
603 store.do_not_match = 0;
605 store.value_regex = (regex_t*)malloc(sizeof(regex_t));
606 if (regcomp(store.value_regex, value_regex,
608 fprintf(stderr, "Invalid pattern: %s\n",
610 free(store.value_regex);
621 * After this, store.offset will contain the *end* offset
622 * of the last match, or remain at 0 if no match was found.
623 * As a side effect, we make sure to transform only a valid
624 * existing config file.
626 if (git_config_from_file(store_aux, config_filename)) {
627 fprintf(stderr, "invalid config file: %s\n",
630 if (store.value_regex != NULL) {
631 regfree(store.value_regex);
632 free(store.value_regex);
639 if (store.value_regex != NULL) {
640 regfree(store.value_regex);
641 free(store.value_regex);
644 /* if nothing to unset, or too many matches, error out */
645 if ((store.seen == 0 && value == NULL) ||
646 (store.seen > 1 && multi_replace == 0)) {
652 contents = mmap(NULL, st.st_size, PROT_READ,
653 MAP_PRIVATE, in_fd, 0);
659 for (i = 0, copy_begin = 0; i < store.seen; i++) {
660 if (store.offset[i] == 0) {
661 store.offset[i] = copy_end = st.st_size;
662 } else if (store.state != KEY_SEEN) {
663 copy_end = store.offset[i];
665 copy_end = find_beginning_of_line(
666 contents, st.st_size,
667 store.offset[i]-2, &new_line);
669 /* write the first part of the config */
670 if (copy_end > copy_begin) {
671 write(fd, contents + copy_begin,
672 copy_end - copy_begin);
676 copy_begin = store.offset[i];
679 /* write the pair (value == NULL means unset) */
681 if (store.state == START)
682 store_write_section(fd, key);
683 store_write_pair(fd, key, value);
686 /* write the rest of the config */
687 if (copy_begin < st.st_size)
688 write(fd, contents + copy_begin,
689 st.st_size - copy_begin);
691 munmap(contents, st.st_size);
692 unlink(config_filename);
695 if (rename(lock_file, config_filename) < 0) {
696 fprintf(stderr, "Could not rename the lock file?\n");
707 free(config_filename);