gitweb: Mark commits with no "previous" in 'blame' view
[git/mingw/j6t.git] / config.c
blob1682273c12ab042d73fa32caf30d18fb13ef85e3
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
7 */
8 #include "cache.h"
9 #include "exec_cmd.h"
11 #define MAXNAME (256)
13 static FILE *config_file;
14 static const char *config_file_name;
15 static int config_linenr;
16 static int config_file_eof;
17 static int zlib_compression_seen;
19 const char *config_exclusive_filename = NULL;
21 static int get_next_char(void)
23 int c;
24 FILE *f;
26 c = '\n';
27 if ((f = config_file) != NULL) {
28 c = fgetc(f);
29 if (c == '\r') {
30 /* DOS like systems */
31 c = fgetc(f);
32 if (c != '\n') {
33 ungetc(c, f);
34 c = '\r';
37 if (c == '\n')
38 config_linenr++;
39 if (c == EOF) {
40 config_file_eof = 1;
41 c = '\n';
44 return c;
47 static char *parse_value(void)
49 static char value[1024];
50 int quote = 0, comment = 0, len = 0, space = 0;
52 for (;;) {
53 int c = get_next_char();
54 if (len >= sizeof(value) - 1)
55 return NULL;
56 if (c == '\n') {
57 if (quote)
58 return NULL;
59 value[len] = 0;
60 return value;
62 if (comment)
63 continue;
64 if (isspace(c) && !quote) {
65 space = 1;
66 continue;
68 if (!quote) {
69 if (c == ';' || c == '#') {
70 comment = 1;
71 continue;
74 if (space) {
75 if (len)
76 value[len++] = ' ';
77 space = 0;
79 if (c == '\\') {
80 c = get_next_char();
81 switch (c) {
82 case '\n':
83 continue;
84 case 't':
85 c = '\t';
86 break;
87 case 'b':
88 c = '\b';
89 break;
90 case 'n':
91 c = '\n';
92 break;
93 /* Some characters escape as themselves */
94 case '\\': case '"':
95 break;
96 /* Reject unknown escape sequences */
97 default:
98 return NULL;
100 value[len++] = c;
101 continue;
103 if (c == '"') {
104 quote = 1-quote;
105 continue;
107 value[len++] = c;
111 static inline int iskeychar(int c)
113 return isalnum(c) || c == '-';
116 static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
118 int c;
119 char *value;
121 /* Get the full name */
122 for (;;) {
123 c = get_next_char();
124 if (config_file_eof)
125 break;
126 if (!iskeychar(c))
127 break;
128 name[len++] = tolower(c);
129 if (len >= MAXNAME)
130 return -1;
132 name[len] = 0;
133 while (c == ' ' || c == '\t')
134 c = get_next_char();
136 value = NULL;
137 if (c != '\n') {
138 if (c != '=')
139 return -1;
140 value = parse_value();
141 if (!value)
142 return -1;
144 return fn(name, value, data);
147 static int get_extended_base_var(char *name, int baselen, int c)
149 do {
150 if (c == '\n')
151 return -1;
152 c = get_next_char();
153 } while (isspace(c));
155 /* We require the format to be '[base "extension"]' */
156 if (c != '"')
157 return -1;
158 name[baselen++] = '.';
160 for (;;) {
161 int c = get_next_char();
162 if (c == '\n')
163 return -1;
164 if (c == '"')
165 break;
166 if (c == '\\') {
167 c = get_next_char();
168 if (c == '\n')
169 return -1;
171 name[baselen++] = c;
172 if (baselen > MAXNAME / 2)
173 return -1;
176 /* Final ']' */
177 if (get_next_char() != ']')
178 return -1;
179 return baselen;
182 static int get_base_var(char *name)
184 int baselen = 0;
186 for (;;) {
187 int c = get_next_char();
188 if (config_file_eof)
189 return -1;
190 if (c == ']')
191 return baselen;
192 if (isspace(c))
193 return get_extended_base_var(name, baselen, c);
194 if (!iskeychar(c) && c != '.')
195 return -1;
196 if (baselen > MAXNAME / 2)
197 return -1;
198 name[baselen++] = tolower(c);
202 static int git_parse_file(config_fn_t fn, void *data)
204 int comment = 0;
205 int baselen = 0;
206 static char var[MAXNAME];
208 /* U+FEFF Byte Order Mark in UTF8 */
209 static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
210 const unsigned char *bomptr = utf8_bom;
212 for (;;) {
213 int c = get_next_char();
214 if (bomptr && *bomptr) {
215 /* We are at the file beginning; skip UTF8-encoded BOM
216 * if present. Sane editors won't put this in on their
217 * own, but e.g. Windows Notepad will do it happily. */
218 if ((unsigned char) c == *bomptr) {
219 bomptr++;
220 continue;
221 } else {
222 /* Do not tolerate partial BOM. */
223 if (bomptr != utf8_bom)
224 break;
225 /* No BOM at file beginning. Cool. */
226 bomptr = NULL;
229 if (c == '\n') {
230 if (config_file_eof)
231 return 0;
232 comment = 0;
233 continue;
235 if (comment || isspace(c))
236 continue;
237 if (c == '#' || c == ';') {
238 comment = 1;
239 continue;
241 if (c == '[') {
242 baselen = get_base_var(var);
243 if (baselen <= 0)
244 break;
245 var[baselen++] = '.';
246 var[baselen] = 0;
247 continue;
249 if (!isalpha(c))
250 break;
251 var[baselen] = tolower(c);
252 if (get_value(fn, data, var, baselen+1) < 0)
253 break;
255 die("bad config file line %d in %s", config_linenr, config_file_name);
258 static int parse_unit_factor(const char *end, unsigned long *val)
260 if (!*end)
261 return 1;
262 else if (!strcasecmp(end, "k")) {
263 *val *= 1024;
264 return 1;
266 else if (!strcasecmp(end, "m")) {
267 *val *= 1024 * 1024;
268 return 1;
270 else if (!strcasecmp(end, "g")) {
271 *val *= 1024 * 1024 * 1024;
272 return 1;
274 return 0;
277 static int git_parse_long(const char *value, long *ret)
279 if (value && *value) {
280 char *end;
281 long val = strtol(value, &end, 0);
282 unsigned long factor = 1;
283 if (!parse_unit_factor(end, &factor))
284 return 0;
285 *ret = val * factor;
286 return 1;
288 return 0;
291 int git_parse_ulong(const char *value, unsigned long *ret)
293 if (value && *value) {
294 char *end;
295 unsigned long val = strtoul(value, &end, 0);
296 if (!parse_unit_factor(end, &val))
297 return 0;
298 *ret = val;
299 return 1;
301 return 0;
304 static void die_bad_config(const char *name)
306 if (config_file_name)
307 die("bad config value for '%s' in %s", name, config_file_name);
308 die("bad config value for '%s'", name);
311 int git_config_int(const char *name, const char *value)
313 long ret = 0;
314 if (!git_parse_long(value, &ret))
315 die_bad_config(name);
316 return ret;
319 unsigned long git_config_ulong(const char *name, const char *value)
321 unsigned long ret;
322 if (!git_parse_ulong(value, &ret))
323 die_bad_config(name);
324 return ret;
327 int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
329 *is_bool = 1;
330 if (!value)
331 return 1;
332 if (!*value)
333 return 0;
334 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
335 return 1;
336 if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
337 return 0;
338 *is_bool = 0;
339 return git_config_int(name, value);
342 int git_config_bool(const char *name, const char *value)
344 int discard;
345 return !!git_config_bool_or_int(name, value, &discard);
348 int git_config_string(const char **dest, const char *var, const char *value)
350 if (!value)
351 return config_error_nonbool(var);
352 *dest = xstrdup(value);
353 return 0;
356 static int git_default_core_config(const char *var, const char *value)
358 /* This needs a better name */
359 if (!strcmp(var, "core.filemode")) {
360 trust_executable_bit = git_config_bool(var, value);
361 return 0;
363 if (!strcmp(var, "core.trustctime")) {
364 trust_ctime = git_config_bool(var, value);
365 return 0;
368 if (!strcmp(var, "core.quotepath")) {
369 quote_path_fully = git_config_bool(var, value);
370 return 0;
373 if (!strcmp(var, "core.symlinks")) {
374 has_symlinks = git_config_bool(var, value);
375 return 0;
378 if (!strcmp(var, "core.ignorecase")) {
379 ignore_case = git_config_bool(var, value);
380 return 0;
383 if (!strcmp(var, "core.bare")) {
384 is_bare_repository_cfg = git_config_bool(var, value);
385 return 0;
388 if (!strcmp(var, "core.ignorestat")) {
389 assume_unchanged = git_config_bool(var, value);
390 return 0;
393 if (!strcmp(var, "core.prefersymlinkrefs")) {
394 prefer_symlink_refs = git_config_bool(var, value);
395 return 0;
398 if (!strcmp(var, "core.logallrefupdates")) {
399 log_all_ref_updates = git_config_bool(var, value);
400 return 0;
403 if (!strcmp(var, "core.warnambiguousrefs")) {
404 warn_ambiguous_refs = git_config_bool(var, value);
405 return 0;
408 if (!strcmp(var, "core.loosecompression")) {
409 int level = git_config_int(var, value);
410 if (level == -1)
411 level = Z_DEFAULT_COMPRESSION;
412 else if (level < 0 || level > Z_BEST_COMPRESSION)
413 die("bad zlib compression level %d", level);
414 zlib_compression_level = level;
415 zlib_compression_seen = 1;
416 return 0;
419 if (!strcmp(var, "core.compression")) {
420 int level = git_config_int(var, value);
421 if (level == -1)
422 level = Z_DEFAULT_COMPRESSION;
423 else if (level < 0 || level > Z_BEST_COMPRESSION)
424 die("bad zlib compression level %d", level);
425 core_compression_level = level;
426 core_compression_seen = 1;
427 if (!zlib_compression_seen)
428 zlib_compression_level = level;
429 return 0;
432 if (!strcmp(var, "core.packedgitwindowsize")) {
433 int pgsz_x2 = getpagesize() * 2;
434 packed_git_window_size = git_config_int(var, value);
436 /* This value must be multiple of (pagesize * 2) */
437 packed_git_window_size /= pgsz_x2;
438 if (packed_git_window_size < 1)
439 packed_git_window_size = 1;
440 packed_git_window_size *= pgsz_x2;
441 return 0;
444 if (!strcmp(var, "core.packedgitlimit")) {
445 packed_git_limit = git_config_int(var, value);
446 return 0;
449 if (!strcmp(var, "core.deltabasecachelimit")) {
450 delta_base_cache_limit = git_config_int(var, value);
451 return 0;
454 if (!strcmp(var, "core.autocrlf")) {
455 if (value && !strcasecmp(value, "input")) {
456 auto_crlf = -1;
457 return 0;
459 auto_crlf = git_config_bool(var, value);
460 return 0;
463 if (!strcmp(var, "core.safecrlf")) {
464 if (value && !strcasecmp(value, "warn")) {
465 safe_crlf = SAFE_CRLF_WARN;
466 return 0;
468 safe_crlf = git_config_bool(var, value);
469 return 0;
472 if (!strcmp(var, "core.pager"))
473 return git_config_string(&pager_program, var, value);
475 if (!strcmp(var, "core.editor"))
476 return git_config_string(&editor_program, var, value);
478 if (!strcmp(var, "core.excludesfile"))
479 return git_config_string(&excludes_file, var, value);
481 if (!strcmp(var, "core.whitespace")) {
482 if (!value)
483 return config_error_nonbool(var);
484 whitespace_rule_cfg = parse_whitespace_rule(value);
485 return 0;
488 if (!strcmp(var, "core.fsyncobjectfiles")) {
489 fsync_object_files = git_config_bool(var, value);
490 return 0;
493 if (!strcmp(var, "core.preloadindex")) {
494 core_preload_index = git_config_bool(var, value);
495 return 0;
498 if (!strcmp(var, "core.createobject")) {
499 if (!strcmp(value, "rename"))
500 object_creation_mode = OBJECT_CREATION_USES_RENAMES;
501 else if (!strcmp(value, "link"))
502 object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
503 else
504 die("Invalid mode for object creation: %s", value);
505 return 0;
508 /* Add other config variables here and to Documentation/config.txt. */
509 return 0;
512 static int git_default_user_config(const char *var, const char *value)
514 if (!strcmp(var, "user.name")) {
515 if (!value)
516 return config_error_nonbool(var);
517 strlcpy(git_default_name, value, sizeof(git_default_name));
518 if (git_default_email[0])
519 user_ident_explicitly_given = 1;
520 return 0;
523 if (!strcmp(var, "user.email")) {
524 if (!value)
525 return config_error_nonbool(var);
526 strlcpy(git_default_email, value, sizeof(git_default_email));
527 if (git_default_name[0])
528 user_ident_explicitly_given = 1;
529 return 0;
532 /* Add other config variables here and to Documentation/config.txt. */
533 return 0;
536 static int git_default_i18n_config(const char *var, const char *value)
538 if (!strcmp(var, "i18n.commitencoding"))
539 return git_config_string(&git_commit_encoding, var, value);
541 if (!strcmp(var, "i18n.logoutputencoding"))
542 return git_config_string(&git_log_output_encoding, var, value);
544 /* Add other config variables here and to Documentation/config.txt. */
545 return 0;
548 static int git_default_branch_config(const char *var, const char *value)
550 if (!strcmp(var, "branch.autosetupmerge")) {
551 if (value && !strcasecmp(value, "always")) {
552 git_branch_track = BRANCH_TRACK_ALWAYS;
553 return 0;
555 git_branch_track = git_config_bool(var, value);
556 return 0;
558 if (!strcmp(var, "branch.autosetuprebase")) {
559 if (!value)
560 return config_error_nonbool(var);
561 else if (!strcmp(value, "never"))
562 autorebase = AUTOREBASE_NEVER;
563 else if (!strcmp(value, "local"))
564 autorebase = AUTOREBASE_LOCAL;
565 else if (!strcmp(value, "remote"))
566 autorebase = AUTOREBASE_REMOTE;
567 else if (!strcmp(value, "always"))
568 autorebase = AUTOREBASE_ALWAYS;
569 else
570 return error("Malformed value for %s", var);
571 return 0;
574 /* Add other config variables here and to Documentation/config.txt. */
575 return 0;
578 static int git_default_push_config(const char *var, const char *value)
580 if (!strcmp(var, "push.default")) {
581 if (!value)
582 return config_error_nonbool(var);
583 else if (!strcmp(value, "nothing"))
584 push_default = PUSH_DEFAULT_NOTHING;
585 else if (!strcmp(value, "matching"))
586 push_default = PUSH_DEFAULT_MATCHING;
587 else if (!strcmp(value, "tracking"))
588 push_default = PUSH_DEFAULT_TRACKING;
589 else if (!strcmp(value, "current"))
590 push_default = PUSH_DEFAULT_CURRENT;
591 else {
592 error("Malformed value for %s: %s", var, value);
593 return error("Must be one of nothing, matching, "
594 "tracking or current.");
596 return 0;
599 /* Add other config variables here and to Documentation/config.txt. */
600 return 0;
603 static int git_default_mailmap_config(const char *var, const char *value)
605 if (!strcmp(var, "mailmap.file"))
606 return git_config_string(&git_mailmap_file, var, value);
608 /* Add other config variables here and to Documentation/config.txt. */
609 return 0;
612 int git_default_config(const char *var, const char *value, void *dummy)
614 if (!prefixcmp(var, "core."))
615 return git_default_core_config(var, value);
617 if (!prefixcmp(var, "user."))
618 return git_default_user_config(var, value);
620 if (!prefixcmp(var, "i18n."))
621 return git_default_i18n_config(var, value);
623 if (!prefixcmp(var, "branch."))
624 return git_default_branch_config(var, value);
626 if (!prefixcmp(var, "push."))
627 return git_default_push_config(var, value);
629 if (!prefixcmp(var, "mailmap."))
630 return git_default_mailmap_config(var, value);
632 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
633 pager_use_color = git_config_bool(var,value);
634 return 0;
637 /* Add other config variables here and to Documentation/config.txt. */
638 return 0;
641 int git_config_from_file(config_fn_t fn, const char *filename, void *data)
643 int ret;
644 FILE *f = fopen(filename, "r");
646 ret = -1;
647 if (f) {
648 config_file = f;
649 config_file_name = filename;
650 config_linenr = 1;
651 config_file_eof = 0;
652 ret = git_parse_file(fn, data);
653 fclose(f);
654 config_file_name = NULL;
656 return ret;
659 const char *git_etc_gitconfig(void)
661 static const char *system_wide;
662 if (!system_wide)
663 system_wide = system_path(ETC_GITCONFIG);
664 return system_wide;
667 static int git_env_bool(const char *k, int def)
669 const char *v = getenv(k);
670 return v ? git_config_bool(k, v) : def;
673 int git_config_system(void)
675 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
678 int git_config_global(void)
680 return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
683 int git_config(config_fn_t fn, void *data)
685 int ret = 0, found = 0;
686 char *repo_config = NULL;
687 const char *home = NULL;
689 /* Setting $GIT_CONFIG makes git read _only_ the given config file. */
690 if (config_exclusive_filename)
691 return git_config_from_file(fn, config_exclusive_filename, data);
692 if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) {
693 ret += git_config_from_file(fn, git_etc_gitconfig(),
694 data);
695 found += 1;
698 home = getenv("HOME");
699 if (git_config_global() && home) {
700 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
701 if (!access(user_config, R_OK)) {
702 ret += git_config_from_file(fn, user_config, data);
703 found += 1;
705 free(user_config);
708 repo_config = git_pathdup("config");
709 if (!access(repo_config, R_OK)) {
710 ret += git_config_from_file(fn, repo_config, data);
711 found += 1;
713 free(repo_config);
714 if (found == 0)
715 return -1;
716 return ret;
720 * Find all the stuff for git_config_set() below.
723 #define MAX_MATCHES 512
725 static struct {
726 int baselen;
727 char *key;
728 int do_not_match;
729 regex_t *value_regex;
730 int multi_replace;
731 size_t offset[MAX_MATCHES];
732 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
733 int seen;
734 } store;
736 static int matches(const char *key, const char *value)
738 return !strcmp(key, store.key) &&
739 (store.value_regex == NULL ||
740 (store.do_not_match ^
741 !regexec(store.value_regex, value, 0, NULL, 0)));
744 static int store_aux(const char *key, const char *value, void *cb)
746 const char *ep;
747 size_t section_len;
749 switch (store.state) {
750 case KEY_SEEN:
751 if (matches(key, value)) {
752 if (store.seen == 1 && store.multi_replace == 0) {
753 warning("%s has multiple values", key);
754 } else if (store.seen >= MAX_MATCHES) {
755 error("too many matches for %s", key);
756 return 1;
759 store.offset[store.seen] = ftell(config_file);
760 store.seen++;
762 break;
763 case SECTION_SEEN:
765 * What we are looking for is in store.key (both
766 * section and var), and its section part is baselen
767 * long. We found key (again, both section and var).
768 * We would want to know if this key is in the same
769 * section as what we are looking for. We already
770 * know we are in the same section as what should
771 * hold store.key.
773 ep = strrchr(key, '.');
774 section_len = ep - key;
776 if ((section_len != store.baselen) ||
777 memcmp(key, store.key, section_len+1)) {
778 store.state = SECTION_END_SEEN;
779 break;
783 * Do not increment matches: this is no match, but we
784 * just made sure we are in the desired section.
786 store.offset[store.seen] = ftell(config_file);
787 /* fallthru */
788 case SECTION_END_SEEN:
789 case START:
790 if (matches(key, value)) {
791 store.offset[store.seen] = ftell(config_file);
792 store.state = KEY_SEEN;
793 store.seen++;
794 } else {
795 if (strrchr(key, '.') - key == store.baselen &&
796 !strncmp(key, store.key, store.baselen)) {
797 store.state = SECTION_SEEN;
798 store.offset[store.seen] = ftell(config_file);
802 return 0;
805 static int write_error(const char *filename)
807 error("failed to write new configuration file %s", filename);
809 /* Same error code as "failed to rename". */
810 return 4;
813 static int store_write_section(int fd, const char *key)
815 const char *dot;
816 int i, success;
817 struct strbuf sb = STRBUF_INIT;
819 dot = memchr(key, '.', store.baselen);
820 if (dot) {
821 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
822 for (i = dot - key + 1; i < store.baselen; i++) {
823 if (key[i] == '"' || key[i] == '\\')
824 strbuf_addch(&sb, '\\');
825 strbuf_addch(&sb, key[i]);
827 strbuf_addstr(&sb, "\"]\n");
828 } else {
829 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
832 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
833 strbuf_release(&sb);
835 return success;
838 static int store_write_pair(int fd, const char *key, const char *value)
840 int i, success;
841 int length = strlen(key + store.baselen + 1);
842 const char *quote = "";
843 struct strbuf sb = STRBUF_INIT;
846 * Check to see if the value needs to be surrounded with a dq pair.
847 * Note that problematic characters are always backslash-quoted; this
848 * check is about not losing leading or trailing SP and strings that
849 * follow beginning-of-comment characters (i.e. ';' and '#') by the
850 * configuration parser.
852 if (value[0] == ' ')
853 quote = "\"";
854 for (i = 0; value[i]; i++)
855 if (value[i] == ';' || value[i] == '#')
856 quote = "\"";
857 if (i && value[i - 1] == ' ')
858 quote = "\"";
860 strbuf_addf(&sb, "\t%.*s = %s",
861 length, key + store.baselen + 1, quote);
863 for (i = 0; value[i]; i++)
864 switch (value[i]) {
865 case '\n':
866 strbuf_addstr(&sb, "\\n");
867 break;
868 case '\t':
869 strbuf_addstr(&sb, "\\t");
870 break;
871 case '"':
872 case '\\':
873 strbuf_addch(&sb, '\\');
874 default:
875 strbuf_addch(&sb, value[i]);
876 break;
878 strbuf_addf(&sb, "%s\n", quote);
880 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
881 strbuf_release(&sb);
883 return success;
886 static ssize_t find_beginning_of_line(const char *contents, size_t size,
887 size_t offset_, int *found_bracket)
889 size_t equal_offset = size, bracket_offset = size;
890 ssize_t offset;
892 contline:
893 for (offset = offset_-2; offset > 0
894 && contents[offset] != '\n'; offset--)
895 switch (contents[offset]) {
896 case '=': equal_offset = offset; break;
897 case ']': bracket_offset = offset; break;
899 if (offset > 0 && contents[offset-1] == '\\') {
900 offset_ = offset;
901 goto contline;
903 if (bracket_offset < equal_offset) {
904 *found_bracket = 1;
905 offset = bracket_offset+1;
906 } else
907 offset++;
909 return offset;
912 int git_config_set(const char *key, const char *value)
914 return git_config_set_multivar(key, value, NULL, 0);
918 * If value==NULL, unset in (remove from) config,
919 * if value_regex!=NULL, disregard key/value pairs where value does not match.
920 * if multi_replace==0, nothing, or only one matching key/value is replaced,
921 * else all matching key/values (regardless how many) are removed,
922 * before the new pair is written.
924 * Returns 0 on success.
926 * This function does this:
928 * - it locks the config file by creating ".git/config.lock"
930 * - it then parses the config using store_aux() as validator to find
931 * the position on the key/value pair to replace. If it is to be unset,
932 * it must be found exactly once.
934 * - the config file is mmap()ed and the part before the match (if any) is
935 * written to the lock file, then the changed part and the rest.
937 * - the config file is removed and the lock file rename()d to it.
940 int git_config_set_multivar(const char *key, const char *value,
941 const char *value_regex, int multi_replace)
943 int i, dot;
944 int fd = -1, in_fd;
945 int ret;
946 char *config_filename;
947 struct lock_file *lock = NULL;
948 const char *last_dot = strrchr(key, '.');
950 if (config_exclusive_filename)
951 config_filename = xstrdup(config_exclusive_filename);
952 else
953 config_filename = git_pathdup("config");
956 * Since "key" actually contains the section name and the real
957 * key name separated by a dot, we have to know where the dot is.
960 if (last_dot == NULL) {
961 error("key does not contain a section: %s", key);
962 ret = 2;
963 goto out_free;
965 store.baselen = last_dot - key;
967 store.multi_replace = multi_replace;
970 * Validate the key and while at it, lower case it for matching.
972 store.key = xmalloc(strlen(key) + 1);
973 dot = 0;
974 for (i = 0; key[i]; i++) {
975 unsigned char c = key[i];
976 if (c == '.')
977 dot = 1;
978 /* Leave the extended basename untouched.. */
979 if (!dot || i > store.baselen) {
980 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
981 error("invalid key: %s", key);
982 free(store.key);
983 ret = 1;
984 goto out_free;
986 c = tolower(c);
987 } else if (c == '\n') {
988 error("invalid key (newline): %s", key);
989 free(store.key);
990 ret = 1;
991 goto out_free;
993 store.key[i] = c;
995 store.key[i] = 0;
998 * The lock serves a purpose in addition to locking: the new
999 * contents of .git/config will be written into it.
1001 lock = xcalloc(sizeof(struct lock_file), 1);
1002 fd = hold_lock_file_for_update(lock, config_filename, 0);
1003 if (fd < 0) {
1004 error("could not lock config file %s: %s", config_filename, strerror(errno));
1005 free(store.key);
1006 ret = -1;
1007 goto out_free;
1011 * If .git/config does not exist yet, write a minimal version.
1013 in_fd = open(config_filename, O_RDONLY);
1014 if ( in_fd < 0 ) {
1015 free(store.key);
1017 if ( ENOENT != errno ) {
1018 error("opening %s: %s", config_filename,
1019 strerror(errno));
1020 ret = 3; /* same as "invalid config file" */
1021 goto out_free;
1023 /* if nothing to unset, error out */
1024 if (value == NULL) {
1025 ret = 5;
1026 goto out_free;
1029 store.key = (char *)key;
1030 if (!store_write_section(fd, key) ||
1031 !store_write_pair(fd, key, value))
1032 goto write_err_out;
1033 } else {
1034 struct stat st;
1035 char *contents;
1036 size_t contents_sz, copy_begin, copy_end;
1037 int i, new_line = 0;
1039 if (value_regex == NULL)
1040 store.value_regex = NULL;
1041 else {
1042 if (value_regex[0] == '!') {
1043 store.do_not_match = 1;
1044 value_regex++;
1045 } else
1046 store.do_not_match = 0;
1048 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
1049 if (regcomp(store.value_regex, value_regex,
1050 REG_EXTENDED)) {
1051 error("invalid pattern: %s", value_regex);
1052 free(store.value_regex);
1053 ret = 6;
1054 goto out_free;
1058 store.offset[0] = 0;
1059 store.state = START;
1060 store.seen = 0;
1063 * After this, store.offset will contain the *end* offset
1064 * of the last match, or remain at 0 if no match was found.
1065 * As a side effect, we make sure to transform only a valid
1066 * existing config file.
1068 if (git_config_from_file(store_aux, config_filename, NULL)) {
1069 error("invalid config file %s", config_filename);
1070 free(store.key);
1071 if (store.value_regex != NULL) {
1072 regfree(store.value_regex);
1073 free(store.value_regex);
1075 ret = 3;
1076 goto out_free;
1079 free(store.key);
1080 if (store.value_regex != NULL) {
1081 regfree(store.value_regex);
1082 free(store.value_regex);
1085 /* if nothing to unset, or too many matches, error out */
1086 if ((store.seen == 0 && value == NULL) ||
1087 (store.seen > 1 && multi_replace == 0)) {
1088 ret = 5;
1089 goto out_free;
1092 fstat(in_fd, &st);
1093 contents_sz = xsize_t(st.st_size);
1094 contents = xmmap(NULL, contents_sz, PROT_READ,
1095 MAP_PRIVATE, in_fd, 0);
1096 close(in_fd);
1098 if (store.seen == 0)
1099 store.seen = 1;
1101 for (i = 0, copy_begin = 0; i < store.seen; i++) {
1102 if (store.offset[i] == 0) {
1103 store.offset[i] = copy_end = contents_sz;
1104 } else if (store.state != KEY_SEEN) {
1105 copy_end = store.offset[i];
1106 } else
1107 copy_end = find_beginning_of_line(
1108 contents, contents_sz,
1109 store.offset[i]-2, &new_line);
1111 if (copy_end > 0 && contents[copy_end-1] != '\n')
1112 new_line = 1;
1114 /* write the first part of the config */
1115 if (copy_end > copy_begin) {
1116 if (write_in_full(fd, contents + copy_begin,
1117 copy_end - copy_begin) <
1118 copy_end - copy_begin)
1119 goto write_err_out;
1120 if (new_line &&
1121 write_in_full(fd, "\n", 1) != 1)
1122 goto write_err_out;
1124 copy_begin = store.offset[i];
1127 /* write the pair (value == NULL means unset) */
1128 if (value != NULL) {
1129 if (store.state == START) {
1130 if (!store_write_section(fd, key))
1131 goto write_err_out;
1133 if (!store_write_pair(fd, key, value))
1134 goto write_err_out;
1137 /* write the rest of the config */
1138 if (copy_begin < contents_sz)
1139 if (write_in_full(fd, contents + copy_begin,
1140 contents_sz - copy_begin) <
1141 contents_sz - copy_begin)
1142 goto write_err_out;
1144 munmap(contents, contents_sz);
1147 if (commit_lock_file(lock) < 0) {
1148 error("could not commit config file %s", config_filename);
1149 ret = 4;
1150 goto out_free;
1154 * lock is committed, so don't try to roll it back below.
1155 * NOTE: Since lockfile.c keeps a linked list of all created
1156 * lock_file structures, it isn't safe to free(lock). It's
1157 * better to just leave it hanging around.
1159 lock = NULL;
1160 ret = 0;
1162 out_free:
1163 if (lock)
1164 rollback_lock_file(lock);
1165 free(config_filename);
1166 return ret;
1168 write_err_out:
1169 ret = write_error(lock->filename);
1170 goto out_free;
1174 static int section_name_match (const char *buf, const char *name)
1176 int i = 0, j = 0, dot = 0;
1177 for (; buf[i] && buf[i] != ']'; i++) {
1178 if (!dot && isspace(buf[i])) {
1179 dot = 1;
1180 if (name[j++] != '.')
1181 break;
1182 for (i++; isspace(buf[i]); i++)
1183 ; /* do nothing */
1184 if (buf[i] != '"')
1185 break;
1186 continue;
1188 if (buf[i] == '\\' && dot)
1189 i++;
1190 else if (buf[i] == '"' && dot) {
1191 for (i++; isspace(buf[i]); i++)
1192 ; /* do_nothing */
1193 break;
1195 if (buf[i] != name[j++])
1196 break;
1198 return (buf[i] == ']' && name[j] == 0);
1201 /* if new_name == NULL, the section is removed instead */
1202 int git_config_rename_section(const char *old_name, const char *new_name)
1204 int ret = 0, remove = 0;
1205 char *config_filename;
1206 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
1207 int out_fd;
1208 char buf[1024];
1210 if (config_exclusive_filename)
1211 config_filename = xstrdup(config_exclusive_filename);
1212 else
1213 config_filename = git_pathdup("config");
1214 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
1215 if (out_fd < 0) {
1216 ret = error("could not lock config file %s", config_filename);
1217 goto out;
1220 if (!(config_file = fopen(config_filename, "rb"))) {
1221 /* no config file means nothing to rename, no error */
1222 goto unlock_and_out;
1225 while (fgets(buf, sizeof(buf), config_file)) {
1226 int i;
1227 int length;
1228 for (i = 0; buf[i] && isspace(buf[i]); i++)
1229 ; /* do nothing */
1230 if (buf[i] == '[') {
1231 /* it's a section */
1232 if (section_name_match (&buf[i+1], old_name)) {
1233 ret++;
1234 if (new_name == NULL) {
1235 remove = 1;
1236 continue;
1238 store.baselen = strlen(new_name);
1239 if (!store_write_section(out_fd, new_name)) {
1240 ret = write_error(lock->filename);
1241 goto out;
1243 continue;
1245 remove = 0;
1247 if (remove)
1248 continue;
1249 length = strlen(buf);
1250 if (write_in_full(out_fd, buf, length) != length) {
1251 ret = write_error(lock->filename);
1252 goto out;
1255 fclose(config_file);
1256 unlock_and_out:
1257 if (commit_lock_file(lock) < 0)
1258 ret = error("could not commit config file %s", config_filename);
1259 out:
1260 free(config_filename);
1261 return ret;
1265 * Call this to report error for your variable that should not
1266 * get a boolean value (i.e. "[my] var" means "true").
1268 int config_error_nonbool(const char *var)
1270 return error("Missing value for '%s'", var);