X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=config.c;h=d3bcaa023d1f238d8fa9b2c416ae7448eff36d17;hb=d909e0761c234b28aac77566368c1ee5451a856a;hp=9918b9351d8b8b9b33fea479bd4f4997a5d6528a;hpb=eb4e67288bb9d66fb46092fa30d72c4bdded0c2a;p=git.git diff --git a/config.c b/config.c index 9918b9351..d3bcaa023 100644 --- a/config.c +++ b/config.c @@ -12,23 +12,22 @@ #define MAXNAME (256) -static FILE *config_file; -static const char *config_file_name; -static int config_linenr; -static int config_file_eof; +typedef struct config_file { + struct config_file *prev; + FILE *f; + const char *name; + int linenr; + int eof; + struct strbuf value; + char var[MAXNAME]; +} config_file; + +static config_file *cf; + static int zlib_compression_seen; const char *config_exclusive_filename = NULL; -struct config_item -{ - struct config_item *next; - char *name; - char *value; -}; -static struct config_item *config_parameters; -static struct config_item **config_parameters_tail = &config_parameters; - static void lowercase(char *p) { for (; *p; p++) @@ -48,34 +47,31 @@ void git_config_push_parameter(const char *text) strbuf_release(&env); } -int git_config_parse_parameter(const char *text) +int git_config_parse_parameter(const char *text, + config_fn_t fn, void *data) { - struct config_item *ct; - struct strbuf tmp = STRBUF_INIT; struct strbuf **pair; - strbuf_addstr(&tmp, text); - pair = strbuf_split(&tmp, '='); + pair = strbuf_split_str(text, '=', 2); + if (!pair[0]) + return error("bogus config parameter: %s", text); if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') strbuf_setlen(pair[0], pair[0]->len - 1); strbuf_trim(pair[0]); if (!pair[0]->len) { strbuf_list_free(pair); - return -1; + return error("bogus config parameter: %s", text); } - ct = xcalloc(1, sizeof(struct config_item)); - ct->name = strbuf_detach(pair[0], NULL); - if (pair[1]) { - strbuf_trim(pair[1]); - ct->value = strbuf_detach(pair[1], NULL); + lowercase(pair[0]->buf); + if (fn(pair[0]->buf, pair[1] ? pair[1]->buf : NULL, data) < 0) { + strbuf_list_free(pair); + return -1; } strbuf_list_free(pair); - lowercase(ct->name); - *config_parameters_tail = ct; - config_parameters_tail = &ct->next; return 0; } -int git_config_parse_environment(void) { +int git_config_from_parameters(config_fn_t fn, void *data) +{ const char *env = getenv(CONFIG_DATA_ENVIRONMENT); char *envw; const char **argv = NULL; @@ -93,8 +89,7 @@ int git_config_parse_environment(void) { } for (i = 0; i < nr; i++) { - if (git_config_parse_parameter(argv[i]) < 0) { - error("bogus config parameter: %s", argv[i]); + if (git_config_parse_parameter(argv[i], fn, data) < 0) { free(argv); free(envw); return -1; @@ -103,7 +98,7 @@ int git_config_parse_environment(void) { free(argv); free(envw); - return 0; + return nr > 0; } static int get_next_char(void) @@ -112,7 +107,7 @@ static int get_next_char(void) FILE *f; c = '\n'; - if ((f = config_file) != NULL) { + if (cf && ((f = cf->f) != NULL)) { c = fgetc(f); if (c == '\r') { /* DOS like systems */ @@ -123,9 +118,9 @@ static int get_next_char(void) } } if (c == '\n') - config_linenr++; + cf->linenr++; if (c == EOF) { - config_file_eof = 1; + cf->eof = 1; c = '\n'; } } @@ -134,23 +129,20 @@ static int get_next_char(void) static char *parse_value(void) { - static char value[1024]; - int quote = 0, comment = 0, len = 0, space = 0; + int quote = 0, comment = 0, space = 0; + strbuf_reset(&cf->value); for (;;) { int c = get_next_char(); - if (len >= sizeof(value) - 1) - return NULL; if (c == '\n') { if (quote) return NULL; - value[len] = 0; - return value; + return cf->value.buf; } if (comment) continue; if (isspace(c) && !quote) { - if (len) + if (cf->value.len) space++; continue; } @@ -161,7 +153,7 @@ static char *parse_value(void) } } for (; space; space--) - value[len++] = ' '; + strbuf_addch(&cf->value, ' '); if (c == '\\') { c = get_next_char(); switch (c) { @@ -183,14 +175,14 @@ static char *parse_value(void) default: return NULL; } - value[len++] = c; + strbuf_addch(&cf->value, c); continue; } if (c == '"') { quote = 1-quote; continue; } - value[len++] = c; + strbuf_addch(&cf->value, c); } } @@ -207,7 +199,7 @@ static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) /* Get the full name */ for (;;) { c = get_next_char(); - if (config_file_eof) + if (cf->eof) break; if (!iskeychar(c)) break; @@ -271,7 +263,7 @@ static int get_base_var(char *name) for (;;) { int c = get_next_char(); - if (config_file_eof) + if (cf->eof) return -1; if (c == ']') return baselen; @@ -289,7 +281,7 @@ static int git_parse_file(config_fn_t fn, void *data) { int comment = 0; int baselen = 0; - static char var[MAXNAME]; + char *var = cf->var; /* U+FEFF Byte Order Mark in UTF8 */ static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf"; @@ -313,7 +305,7 @@ static int git_parse_file(config_fn_t fn, void *data) } } if (c == '\n') { - if (config_file_eof) + if (cf->eof) return 0; comment = 0; continue; @@ -338,7 +330,7 @@ static int git_parse_file(config_fn_t fn, void *data) if (get_value(fn, data, var, baselen+1) < 0) break; } - die("bad config file line %d in %s", config_linenr, config_file_name); + die("bad config file line %d in %s", cf->linenr, cf->name); } static int parse_unit_factor(const char *end, unsigned long *val) @@ -389,8 +381,8 @@ int git_parse_ulong(const char *value, unsigned long *ret) static void die_bad_config(const char *name) { - if (config_file_name) - die("bad config value for '%s' in %s", name, config_file_name); + if (cf && cf->name) + die("bad config value for '%s' in %s", name, cf->name); die("bad config value for '%s'", name); } @@ -429,13 +421,11 @@ static int git_config_maybe_bool_text(const char *name, const char *value) int git_config_maybe_bool(const char *name, const char *value) { - int v = git_config_maybe_bool_text(name, value); + long v = git_config_maybe_bool_text(name, value); if (0 <= v) return v; - if (!strcmp(value, "0")) - return 0; - if (!strcmp(value, "1")) - return 1; + if (git_parse_long(value, &v)) + return !!v; return -1; } @@ -501,6 +491,9 @@ static int git_default_core_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.attributesfile")) + return git_config_pathname(&git_attributes_file, var, value); + if (!strcmp(var, "core.bare")) { is_bare_repository_cfg = git_config_bool(var, value); return 0; @@ -526,6 +519,14 @@ static int git_default_core_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.abbrev")) { + int abbrev = git_config_int(var, value); + if (abbrev < minimum_abbrev || abbrev > 40) + return -1; + default_abbrev = abbrev; + return 0; + } + if (!strcmp(var, "core.loosecompression")) { int level = git_config_int(var, value); if (level == -1) @@ -562,6 +563,12 @@ static int git_default_core_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.bigfilethreshold")) { + long n = git_config_int(var, value); + big_file_threshold = 0 < n ? n : 0; + return 0; + } + if (!strcmp(var, "core.packedgitlimit")) { packed_git_limit = git_config_int(var, value); return 0; @@ -572,9 +579,12 @@ static int git_default_core_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.logpackaccess")) + return git_config_string(&log_pack_access, var, value); + if (!strcmp(var, "core.autocrlf")) { if (value && !strcasecmp(value, "input")) { - if (eol == EOL_CRLF) + if (core_eol == EOL_CRLF) return error("core.autocrlf=input conflicts with core.eol=crlf"); auto_crlf = AUTO_CRLF_INPUT; return 0; @@ -594,14 +604,14 @@ static int git_default_core_config(const char *var, const char *value) if (!strcmp(var, "core.eol")) { if (value && !strcasecmp(value, "lf")) - eol = EOL_LF; + core_eol = EOL_LF; else if (value && !strcasecmp(value, "crlf")) - eol = EOL_CRLF; + core_eol = EOL_CRLF; else if (value && !strcasecmp(value, "native")) - eol = EOL_NATIVE; + core_eol = EOL_NATIVE; else - eol = EOL_UNSET; - if (eol == EOL_CRLF && auto_crlf == AUTO_CRLF_INPUT) + core_eol = EOL_UNSET; + if (core_eol == EOL_CRLF && auto_crlf == AUTO_CRLF_INPUT) return error("core.autocrlf=input conflicts with core.eol=crlf"); return 0; } @@ -732,8 +742,10 @@ static int git_default_push_config(const char *var, const char *value) push_default = PUSH_DEFAULT_NOTHING; else if (!strcmp(value, "matching")) push_default = PUSH_DEFAULT_MATCHING; - else if (!strcmp(value, "tracking")) - push_default = PUSH_DEFAULT_TRACKING; + else if (!strcmp(value, "upstream")) + push_default = PUSH_DEFAULT_UPSTREAM; + else if (!strcmp(value, "tracking")) /* deprecated */ + push_default = PUSH_DEFAULT_UPSTREAM; else if (!strcmp(value, "current")) push_default = PUSH_DEFAULT_CURRENT; else { @@ -796,13 +808,24 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data) ret = -1; if (f) { - config_file = f; - config_file_name = filename; - config_linenr = 1; - config_file_eof = 0; + config_file top; + + /* push config-file parsing state stack */ + top.prev = cf; + top.f = f; + top.name = filename; + top.linenr = 1; + top.eof = 0; + strbuf_init(&top.value, 1024); + cf = ⊤ + ret = git_parse_file(fn, data); + + /* pop config-file parsing state stack */ + strbuf_release(&top.value); + cf = top.prev; + fclose(f); - config_file_name = NULL; } return ret; } @@ -826,31 +849,9 @@ int git_config_system(void) return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); } -int git_config_global(void) -{ - return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0); -} - -int git_config_from_parameters(config_fn_t fn, void *data) -{ - static int loaded_environment; - const struct config_item *ct; - - if (!loaded_environment) { - if (git_config_parse_environment() < 0) - return -1; - loaded_environment = 1; - } - for (ct = config_parameters; ct; ct = ct->next) - if (fn(ct->name, ct->value, data) < 0) - return -1; - return 0; -} - -int git_config(config_fn_t fn, void *data) +int git_config_early(config_fn_t fn, void *data, const char *repo_config) { int ret = 0, found = 0; - char *repo_config = NULL; const char *home = NULL; /* Setting $GIT_CONFIG makes git read _only_ the given config file. */ @@ -863,7 +864,7 @@ int git_config(config_fn_t fn, void *data) } home = getenv("HOME"); - if (git_config_global() && home) { + if (home) { char *user_config = xstrdup(mkpath("%s/.gitconfig", home)); if (!access(user_config, R_OK)) { ret += git_config_from_file(fn, user_config, data); @@ -872,19 +873,34 @@ int git_config(config_fn_t fn, void *data) free(user_config); } - repo_config = git_pathdup("config"); - if (!access(repo_config, R_OK)) { + if (repo_config && !access(repo_config, R_OK)) { ret += git_config_from_file(fn, repo_config, data); found += 1; } - free(repo_config); - ret += git_config_from_parameters(fn, data); - if (config_parameters) - found += 1; + switch (git_config_from_parameters(fn, data)) { + case -1: /* error */ + die("unable to parse command-line config"); + break; + case 0: /* found nothing */ + break; + default: /* found at least one item */ + found++; + break; + } - if (found == 0) - return -1; + return ret == 0 ? found : ret; +} + +int git_config(config_fn_t fn, void *data) +{ + char *repo_config = NULL; + int ret; + + repo_config = git_pathdup("config"); + ret = git_config_early(fn, data, repo_config); + if (repo_config) + free(repo_config); return ret; } @@ -917,6 +933,7 @@ static int store_aux(const char *key, const char *value, void *cb) { const char *ep; size_t section_len; + FILE *f = cf->f; switch (store.state) { case KEY_SEEN: @@ -928,7 +945,7 @@ static int store_aux(const char *key, const char *value, void *cb) return 1; } - store.offset[store.seen] = ftell(config_file); + store.offset[store.seen] = ftell(f); store.seen++; } break; @@ -955,19 +972,19 @@ static int store_aux(const char *key, const char *value, void *cb) * Do not increment matches: this is no match, but we * just made sure we are in the desired section. */ - store.offset[store.seen] = ftell(config_file); + store.offset[store.seen] = ftell(f); /* fallthru */ case SECTION_END_SEEN: case START: if (matches(key, value)) { - store.offset[store.seen] = ftell(config_file); + store.offset[store.seen] = ftell(f); store.state = KEY_SEEN; store.seen++; } else { if (strrchr(key, '.') - key == store.baselen && !strncmp(key, store.key, store.baselen)) { store.state = SECTION_SEEN; - store.offset[store.seen] = ftell(config_file); + store.offset[store.seen] = ftell(f); } } } @@ -1086,6 +1103,75 @@ int git_config_set(const char *key, const char *value) return git_config_set_multivar(key, value, NULL, 0); } +/* + * Auxiliary function to sanity-check and split the key into the section + * identifier and variable name. + * + * Returns 0 on success, -1 when there is an invalid character in the key and + * -2 if there is no section name in the key. + * + * store_key - pointer to char* which will hold a copy of the key with + * lowercase section and variable name + * baselen - pointer to int which will hold the length of the + * section + subsection part, can be NULL + */ +int git_config_parse_key(const char *key, char **store_key, int *baselen_) +{ + int i, dot, baselen; + const char *last_dot = strrchr(key, '.'); + + /* + * Since "key" actually contains the section name and the real + * key name separated by a dot, we have to know where the dot is. + */ + + if (last_dot == NULL || last_dot == key) { + error("key does not contain a section: %s", key); + return -CONFIG_NO_SECTION_OR_NAME; + } + + if (!last_dot[1]) { + error("key does not contain variable name: %s", key); + return -CONFIG_NO_SECTION_OR_NAME; + } + + baselen = last_dot - key; + if (baselen_) + *baselen_ = baselen; + + /* + * Validate the key and while at it, lower case it for matching. + */ + *store_key = xmalloc(strlen(key) + 1); + + dot = 0; + for (i = 0; key[i]; i++) { + unsigned char c = key[i]; + if (c == '.') + dot = 1; + /* Leave the extended basename untouched.. */ + if (!dot || i > baselen) { + if (!iskeychar(c) || + (i == baselen + 1 && !isalpha(c))) { + error("invalid key: %s", key); + goto out_free_ret_1; + } + c = tolower(c); + } else if (c == '\n') { + error("invalid key (newline): %s", key); + goto out_free_ret_1; + } + (*store_key)[i] = c; + } + (*store_key)[i] = 0; + + return 0; + +out_free_ret_1: + free(*store_key); + return -CONFIG_INVALID_KEY; +} + /* * If value==NULL, unset in (remove from) config, * if value_regex!=NULL, disregard key/value pairs where value does not match. @@ -1112,59 +1198,23 @@ int git_config_set(const char *key, const char *value) int git_config_set_multivar(const char *key, const char *value, const char *value_regex, int multi_replace) { - int i, dot; int fd = -1, in_fd; int ret; char *config_filename; struct lock_file *lock = NULL; - const char *last_dot = strrchr(key, '.'); if (config_exclusive_filename) config_filename = xstrdup(config_exclusive_filename); else config_filename = git_pathdup("config"); - /* - * Since "key" actually contains the section name and the real - * key name separated by a dot, we have to know where the dot is. - */ - - if (last_dot == NULL) { - error("key does not contain a section: %s", key); - ret = 2; + /* parse-key returns negative; flip the sign to feed exit(3) */ + ret = 0 - git_config_parse_key(key, &store.key, &store.baselen); + if (ret) goto out_free; - } - store.baselen = last_dot - key; store.multi_replace = multi_replace; - /* - * Validate the key and while at it, lower case it for matching. - */ - store.key = xmalloc(strlen(key) + 1); - dot = 0; - for (i = 0; key[i]; i++) { - unsigned char c = key[i]; - if (c == '.') - dot = 1; - /* Leave the extended basename untouched.. */ - if (!dot || i > store.baselen) { - if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) { - error("invalid key: %s", key); - free(store.key); - ret = 1; - goto out_free; - } - c = tolower(c); - } else if (c == '\n') { - error("invalid key (newline): %s", key); - free(store.key); - ret = 1; - goto out_free; - } - store.key[i] = c; - } - store.key[i] = 0; /* * The lock serves a purpose in addition to locking: the new @@ -1175,7 +1225,7 @@ int git_config_set_multivar(const char *key, const char *value, if (fd < 0) { error("could not lock config file %s: %s", config_filename, strerror(errno)); free(store.key); - ret = -1; + ret = CONFIG_NO_LOCK; goto out_free; } @@ -1189,12 +1239,12 @@ int git_config_set_multivar(const char *key, const char *value, if ( ENOENT != errno ) { error("opening %s: %s", config_filename, strerror(errno)); - ret = 3; /* same as "invalid config file" */ + ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */ goto out_free; } /* if nothing to unset, error out */ if (value == NULL) { - ret = 5; + ret = CONFIG_NOTHING_SET; goto out_free; } @@ -1222,7 +1272,7 @@ int git_config_set_multivar(const char *key, const char *value, REG_EXTENDED)) { error("invalid pattern: %s", value_regex); free(store.value_regex); - ret = 6; + ret = CONFIG_INVALID_PATTERN; goto out_free; } } @@ -1244,7 +1294,7 @@ int git_config_set_multivar(const char *key, const char *value, regfree(store.value_regex); free(store.value_regex); } - ret = 3; + ret = CONFIG_INVALID_FILE; goto out_free; } @@ -1257,7 +1307,7 @@ int git_config_set_multivar(const char *key, const char *value, /* if nothing to unset, or too many matches, error out */ if ((store.seen == 0 && value == NULL) || (store.seen > 1 && multi_replace == 0)) { - ret = 5; + ret = CONFIG_NOTHING_SET; goto out_free; } @@ -1318,7 +1368,7 @@ int git_config_set_multivar(const char *key, const char *value, if (commit_lock_file(lock) < 0) { error("could not commit config file %s", config_filename); - ret = 4; + ret = CONFIG_NO_WRITE; goto out_free; } @@ -1390,6 +1440,7 @@ int git_config_rename_section(const char *old_name, const char *new_name) struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1); int out_fd; char buf[1024]; + FILE *config_file; if (config_exclusive_filename) config_filename = xstrdup(config_exclusive_filename); @@ -1454,10 +1505,10 @@ int git_config_rename_section(const char *old_name, const char *new_name) } } fclose(config_file); - unlock_and_out: +unlock_and_out: if (commit_lock_file(lock) < 0) ret = error("could not commit config file %s", config_filename); - out: +out: free(config_filename); return ret; }