summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'config.c')
-rw-r--r--config.c629
1 files changed, 488 insertions, 141 deletions
diff --git a/config.c b/config.c
index c644061..68d3294 100644
--- a/config.c
+++ b/config.c
@@ -7,16 +7,160 @@
*/
#include "cache.h"
#include "exec_cmd.h"
+#include "strbuf.h"
+#include "quote.h"
#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;
+#define MAX_INCLUDE_DEPTH 10
+static const char include_depth_advice[] =
+"exceeded maximum include depth (%d) while including\n"
+" %s\n"
+"from\n"
+" %s\n"
+"Do you have circular includes?";
+static int handle_path_include(const char *path, struct config_include_data *inc)
+{
+ int ret = 0;
+ struct strbuf buf = STRBUF_INIT;
+
+ /*
+ * Use an absolute path as-is, but interpret relative paths
+ * based on the including config file.
+ */
+ if (!is_absolute_path(path)) {
+ char *slash;
+
+ if (!cf || !cf->name)
+ return error("relative config includes must come from files");
+
+ slash = find_last_dir_sep(cf->name);
+ if (slash)
+ strbuf_add(&buf, cf->name, slash - cf->name + 1);
+ strbuf_addstr(&buf, path);
+ path = buf.buf;
+ }
+
+ if (!access(path, R_OK)) {
+ if (++inc->depth > MAX_INCLUDE_DEPTH)
+ die(include_depth_advice, MAX_INCLUDE_DEPTH, path,
+ cf && cf->name ? cf->name : "the command line");
+ ret = git_config_from_file(git_config_include, path, inc);
+ inc->depth--;
+ }
+ strbuf_release(&buf);
+ return ret;
+}
+
+int git_config_include(const char *var, const char *value, void *data)
+{
+ struct config_include_data *inc = data;
+ const char *type;
+ int ret;
+
+ /*
+ * Pass along all values, including "include" directives; this makes it
+ * possible to query information on the includes themselves.
+ */
+ ret = inc->fn(var, value, inc->data);
+ if (ret < 0)
+ return ret;
+
+ type = skip_prefix(var, "include.");
+ if (!type)
+ return ret;
+
+ if (!strcmp(type, "path"))
+ ret = handle_path_include(value, inc);
+ return ret;
+}
+
+static void lowercase(char *p)
+{
+ for (; *p; p++)
+ *p = tolower(*p);
+}
+
+void git_config_push_parameter(const char *text)
+{
+ struct strbuf env = STRBUF_INIT;
+ const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
+ if (old) {
+ strbuf_addstr(&env, old);
+ strbuf_addch(&env, ' ');
+ }
+ sq_quote_buf(&env, text);
+ setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
+ strbuf_release(&env);
+}
+
+int git_config_parse_parameter(const char *text,
+ config_fn_t fn, void *data)
+{
+ struct strbuf **pair;
+ 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 error("bogus config parameter: %s", text);
+ }
+ 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);
+ return 0;
+}
+
+int git_config_from_parameters(config_fn_t fn, void *data)
+{
+ const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
+ char *envw;
+ const char **argv = NULL;
+ int nr = 0, alloc = 0;
+ int i;
+
+ if (!env)
+ return 0;
+ /* sq_dequote will write over it */
+ envw = xstrdup(env);
+
+ if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
+ free(envw);
+ return error("bogus format in " CONFIG_DATA_ENVIRONMENT);
+ }
+
+ for (i = 0; i < nr; i++) {
+ if (git_config_parse_parameter(argv[i], fn, data) < 0) {
+ free(argv);
+ free(envw);
+ return -1;
+ }
+ }
+
+ free(argv);
+ free(envw);
+ return nr > 0;
+}
static int get_next_char(void)
{
@@ -24,7 +168,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 */
@@ -35,9 +179,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';
}
}
@@ -46,23 +190,22 @@ 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)
+ if (quote) {
+ cf->linenr--;
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;
}
@@ -73,7 +216,7 @@ static char *parse_value(void)
}
}
for (; space; space--)
- value[len++] = ' ';
+ strbuf_addch(&cf->value, ' ');
if (c == '\\') {
c = get_next_char();
switch (c) {
@@ -95,14 +238,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);
}
}
@@ -119,7 +262,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;
@@ -146,7 +289,7 @@ static int get_extended_base_var(char *name, int baselen, int c)
{
do {
if (c == '\n')
- return -1;
+ goto error_incomplete_line;
c = get_next_char();
} while (isspace(c));
@@ -158,13 +301,13 @@ static int get_extended_base_var(char *name, int baselen, int c)
for (;;) {
int c = get_next_char();
if (c == '\n')
- return -1;
+ goto error_incomplete_line;
if (c == '"')
break;
if (c == '\\') {
c = get_next_char();
if (c == '\n')
- return -1;
+ goto error_incomplete_line;
}
name[baselen++] = c;
if (baselen > MAXNAME / 2)
@@ -175,6 +318,9 @@ static int get_extended_base_var(char *name, int baselen, int c)
if (get_next_char() != ']')
return -1;
return baselen;
+error_incomplete_line:
+ cf->linenr--;
+ return -1;
}
static int get_base_var(char *name)
@@ -183,7 +329,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;
@@ -201,7 +347,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";
@@ -225,7 +371,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;
@@ -250,10 +396,10 @@ 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)
+static int parse_unit_factor(const char *end, uintmax_t *val)
{
if (!*end)
return 1;
@@ -276,11 +422,23 @@ static int git_parse_long(const char *value, long *ret)
{
if (value && *value) {
char *end;
- long val = strtol(value, &end, 0);
- unsigned long factor = 1;
+ intmax_t val;
+ uintmax_t uval;
+ uintmax_t factor = 1;
+
+ errno = 0;
+ val = strtoimax(value, &end, 0);
+ if (errno == ERANGE)
+ return 0;
if (!parse_unit_factor(end, &factor))
return 0;
- *ret = val * factor;
+ uval = abs(val);
+ uval *= factor;
+ if ((uval > maximum_signed_value_of_type(long)) ||
+ (abs(val) > uval))
+ return 0;
+ val *= factor;
+ *ret = val;
return 1;
}
return 0;
@@ -290,9 +448,19 @@ int git_parse_ulong(const char *value, unsigned long *ret)
{
if (value && *value) {
char *end;
- unsigned long val = strtoul(value, &end, 0);
+ uintmax_t val;
+ uintmax_t oldval;
+
+ errno = 0;
+ val = strtoumax(value, &end, 0);
+ if (errno == ERANGE)
+ return 0;
+ oldval = val;
if (!parse_unit_factor(end, &val))
return 0;
+ if ((val > maximum_unsigned_value_of_type(long)) ||
+ (oldval > val))
+ return 0;
*ret = val;
return 1;
}
@@ -301,8 +469,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);
}
@@ -322,17 +490,40 @@ unsigned long git_config_ulong(const char *name, const char *value)
return ret;
}
-int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
+static int git_config_maybe_bool_text(const char *name, const char *value)
{
- *is_bool = 1;
if (!value)
return 1;
if (!*value)
return 0;
- if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
+ if (!strcasecmp(value, "true")
+ || !strcasecmp(value, "yes")
+ || !strcasecmp(value, "on"))
return 1;
- if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
+ if (!strcasecmp(value, "false")
+ || !strcasecmp(value, "no")
+ || !strcasecmp(value, "off"))
return 0;
+ return -1;
+}
+
+int git_config_maybe_bool(const char *name, const char *value)
+{
+ long v = git_config_maybe_bool_text(name, value);
+ if (0 <= v)
+ return v;
+ if (git_parse_long(value, &v))
+ return !!v;
+ return -1;
+}
+
+int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
+{
+ int v = git_config_maybe_bool_text(name, value);
+ if (0 <= v) {
+ *is_bool = 1;
+ return v;
+ }
*is_bool = 0;
return git_config_int(name, value);
}
@@ -351,6 +542,16 @@ int git_config_string(const char **dest, const char *var, const char *value)
return 0;
}
+int git_config_pathname(const char **dest, const char *var, const char *value)
+{
+ if (!value)
+ return config_error_nonbool(var);
+ *dest = expand_user_path(value);
+ if (!*dest)
+ die("Failed to expand user dir in: '%s'", value);
+ return 0;
+}
+
static int git_default_core_config(const char *var, const char *value)
{
/* This needs a better name */
@@ -378,6 +579,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;
@@ -403,6 +607,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)
@@ -429,7 +641,7 @@ static int git_default_core_config(const char *var, const char *value)
if (!strcmp(var, "core.packedgitwindowsize")) {
int pgsz_x2 = getpagesize() * 2;
- packed_git_window_size = git_config_int(var, value);
+ packed_git_window_size = git_config_ulong(var, value);
/* This value must be multiple of (pagesize * 2) */
packed_git_window_size /= pgsz_x2;
@@ -439,19 +651,29 @@ static int git_default_core_config(const char *var, const char *value)
return 0;
}
+ if (!strcmp(var, "core.bigfilethreshold")) {
+ big_file_threshold = git_config_ulong(var, value);
+ return 0;
+ }
+
if (!strcmp(var, "core.packedgitlimit")) {
- packed_git_limit = git_config_int(var, value);
+ packed_git_limit = git_config_ulong(var, value);
return 0;
}
if (!strcmp(var, "core.deltabasecachelimit")) {
- delta_base_cache_limit = git_config_int(var, value);
+ delta_base_cache_limit = git_config_ulong(var, 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")) {
- auto_crlf = -1;
+ if (core_eol == EOL_CRLF)
+ return error("core.autocrlf=input conflicts with core.eol=crlf");
+ auto_crlf = AUTO_CRLF_INPUT;
return 0;
}
auto_crlf = git_config_bool(var, value);
@@ -467,14 +689,36 @@ static int git_default_core_config(const char *var, const char *value)
return 0;
}
+ if (!strcmp(var, "core.eol")) {
+ if (value && !strcasecmp(value, "lf"))
+ core_eol = EOL_LF;
+ else if (value && !strcasecmp(value, "crlf"))
+ core_eol = EOL_CRLF;
+ else if (value && !strcasecmp(value, "native"))
+ core_eol = EOL_NATIVE;
+ else
+ 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;
+ }
+
+ if (!strcmp(var, "core.notesref")) {
+ notes_ref_name = xstrdup(value);
+ return 0;
+ }
+
if (!strcmp(var, "core.pager"))
return git_config_string(&pager_program, var, value);
if (!strcmp(var, "core.editor"))
return git_config_string(&editor_program, var, value);
+ if (!strcmp(var, "core.askpass"))
+ return git_config_string(&askpass_program, var, value);
+
if (!strcmp(var, "core.excludesfile"))
- return git_config_string(&excludes_file, var, value);
+ return git_config_pathname(&excludes_file, var, value);
if (!strcmp(var, "core.whitespace")) {
if (!value)
@@ -503,6 +747,11 @@ static int git_default_core_config(const char *var, const char *value)
return 0;
}
+ if (!strcmp(var, "core.sparsecheckout")) {
+ core_apply_sparse_checkout = git_config_bool(var, value);
+ return 0;
+ }
+
/* Add other config variables here and to Documentation/config.txt. */
return 0;
}
@@ -513,8 +762,7 @@ static int git_default_user_config(const char *var, const char *value)
if (!value)
return config_error_nonbool(var);
strlcpy(git_default_name, value, sizeof(git_default_name));
- if (git_default_email[0])
- user_ident_explicitly_given = 1;
+ user_ident_explicitly_given |= IDENT_NAME_GIVEN;
return 0;
}
@@ -522,8 +770,7 @@ static int git_default_user_config(const char *var, const char *value)
if (!value)
return config_error_nonbool(var);
strlcpy(git_default_email, value, sizeof(git_default_email));
- if (git_default_name[0])
- user_ident_explicitly_given = 1;
+ user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
return 0;
}
@@ -582,8 +829,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 {
@@ -635,6 +884,10 @@ int git_default_config(const char *var, const char *value, void *dummy)
return 0;
}
+ if (!strcmp(var, "pack.packsizelimit")) {
+ pack_size_limit_cfg = git_config_ulong(var, value);
+ return 0;
+ }
/* Add other config variables here and to Documentation/config.txt. */
return 0;
}
@@ -646,13 +899,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 = &top;
+
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;
}
@@ -665,7 +929,7 @@ const char *git_etc_gitconfig(void)
return system_wide;
}
-static int git_env_bool(const char *k, int def)
+int git_env_bool(const char *k, int def)
{
const char *v = getenv(k);
return v ? git_config_bool(k, v) : def;
@@ -676,20 +940,11 @@ 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(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. */
- if (config_exclusive_filename)
- return git_config_from_file(fn, config_exclusive_filename, data);
if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) {
ret += git_config_from_file(fn, git_etc_gitconfig(),
data);
@@ -697,26 +952,67 @@ int git_config(config_fn_t fn, void *data)
}
home = getenv("HOME");
- if (git_config_global() && home) {
- char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
+ if (home) {
+ char buf[PATH_MAX];
+ char *user_config = mksnpath(buf, sizeof(buf), "%s/.gitconfig", home);
if (!access(user_config, R_OK)) {
ret += git_config_from_file(fn, user_config, data);
found += 1;
}
- 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);
- if (found == 0)
- return -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;
+ }
+
+ return ret == 0 ? found : ret;
+}
+
+int git_config_with_options(config_fn_t fn, void *data,
+ const char *filename, int respect_includes)
+{
+ char *repo_config = NULL;
+ int ret;
+ struct config_include_data inc = CONFIG_INCLUDE_INIT;
+
+ if (respect_includes) {
+ inc.fn = fn;
+ inc.data = data;
+ fn = git_config_include;
+ data = &inc;
+ }
+
+ /*
+ * If we have a specific filename, use it. Otherwise, follow the
+ * regular lookup sequence.
+ */
+ if (filename)
+ return git_config_from_file(fn, filename, data);
+
+ repo_config = git_pathdup("config");
+ ret = git_config_early(fn, data, repo_config);
+ if (repo_config)
+ free(repo_config);
return ret;
}
+int git_config(config_fn_t fn, void *data)
+{
+ return git_config_with_options(fn, data, NULL, 1);
+}
+
/*
* Find all the stuff for git_config_set() below.
*/
@@ -746,6 +1042,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:
@@ -757,7 +1054,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;
@@ -784,19 +1081,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);
}
}
}
@@ -910,90 +1207,127 @@ contline:
return offset;
}
+int git_config_set_in_file(const char *config_filename,
+ const char *key, const char *value)
+{
+ return git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
+}
+
int git_config_set(const char *key, const char *value)
{
return git_config_set_multivar(key, value, NULL, 0);
}
/*
- * If value==NULL, unset in (remove from) config,
- * if value_regex!=NULL, disregard key/value pairs where value does not match.
- * if multi_replace==0, nothing, or only one matching key/value is replaced,
- * else all matching key/values (regardless how many) are removed,
- * before the new pair is written.
- *
- * Returns 0 on success.
- *
- * This function does this:
- *
- * - it locks the config file by creating ".git/config.lock"
- *
- * - it then parses the config using store_aux() as validator to find
- * the position on the key/value pair to replace. If it is to be unset,
- * it must be found exactly once.
+ * Auxiliary function to sanity-check and split the key into the section
+ * identifier and variable name.
*
- * - the config file is mmap()ed and the part before the match (if any) is
- * written to the lock file, then the changed part and the rest.
- *
- * - the config file is removed and the lock file rename()d to it.
+ * 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_set_multivar(const char *key, const char *value,
- const char *value_regex, int multi_replace)
+int git_config_parse_key(const char *key, char **store_key, int *baselen_)
{
- int i, dot;
- int fd = -1, in_fd;
- int ret;
- char *config_filename;
- struct lock_file *lock = NULL;
+ int i, dot, baselen;
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) {
+ if (last_dot == NULL || last_dot == key) {
error("key does not contain a section: %s", key);
- ret = 2;
- goto out_free;
+ return -CONFIG_NO_SECTION_OR_NAME;
}
- store.baselen = last_dot - key;
- store.multi_replace = multi_replace;
+ 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);
+ *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))) {
+ if (!dot || i > baselen) {
+ if (!iskeychar(c) ||
+ (i == baselen + 1 && !isalpha(c))) {
error("invalid key: %s", key);
- free(store.key);
- ret = 1;
- goto out_free;
+ goto out_free_ret_1;
}
c = tolower(c);
} else if (c == '\n') {
error("invalid key (newline): %s", key);
- free(store.key);
- ret = 1;
- goto out_free;
+ goto out_free_ret_1;
}
- store.key[i] = c;
+ (*store_key)[i] = c;
}
- store.key[i] = 0;
+ (*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.
+ * if multi_replace==0, nothing, or only one matching key/value is replaced,
+ * else all matching key/values (regardless how many) are removed,
+ * before the new pair is written.
+ *
+ * Returns 0 on success.
+ *
+ * This function does this:
+ *
+ * - it locks the config file by creating ".git/config.lock"
+ *
+ * - it then parses the config using store_aux() as validator to find
+ * the position on the key/value pair to replace. If it is to be unset,
+ * it must be found exactly once.
+ *
+ * - the config file is mmap()ed and the part before the match (if any) is
+ * written to the lock file, then the changed part and the rest.
+ *
+ * - the config file is removed and the lock file rename()d to it.
+ *
+ */
+int git_config_set_multivar_in_file(const char *config_filename,
+ const char *key, const char *value,
+ const char *value_regex, int multi_replace)
+{
+ int fd = -1, in_fd;
+ int ret;
+ struct lock_file *lock = NULL;
+ char *filename_buf = NULL;
+
+ /* 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.multi_replace = multi_replace;
+
+ if (!config_filename)
+ config_filename = filename_buf = git_pathdup("config");
/*
* The lock serves a purpose in addition to locking: the new
@@ -1004,7 +1338,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;
}
@@ -1018,12 +1352,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;
}
@@ -1051,7 +1385,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;
}
}
@@ -1073,7 +1407,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;
}
@@ -1086,7 +1420,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;
}
@@ -1147,7 +1481,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;
}
@@ -1163,7 +1497,7 @@ int git_config_set_multivar(const char *key, const char *value,
out_free:
if (lock)
rollback_lock_file(lock);
- free(config_filename);
+ free(filename_buf);
return ret;
write_err_out:
@@ -1172,6 +1506,13 @@ write_err_out:
}
+int git_config_set_multivar(const char *key, const char *value,
+ const char *value_regex, int multi_replace)
+{
+ return git_config_set_multivar_in_file(NULL, key, value, value_regex,
+ multi_replace);
+}
+
static int section_name_match (const char *buf, const char *name)
{
int i = 0, j = 0, dot = 0;
@@ -1212,18 +1553,19 @@ static int section_name_match (const char *buf, const char *name)
}
/* if new_name == NULL, the section is removed instead */
-int git_config_rename_section(const char *old_name, const char *new_name)
+int git_config_rename_section_in_file(const char *config_filename,
+ const char *old_name, const char *new_name)
{
int ret = 0, remove = 0;
- char *config_filename;
+ char *filename_buf = NULL;
struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
int out_fd;
char buf[1024];
+ FILE *config_file;
+
+ if (!config_filename)
+ config_filename = filename_buf = git_pathdup("config");
- if (config_exclusive_filename)
- config_filename = xstrdup(config_exclusive_filename);
- else
- config_filename = git_pathdup("config");
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
if (out_fd < 0) {
ret = error("could not lock config file %s", config_filename);
@@ -1283,14 +1625,19 @@ 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:
- free(config_filename);
+out:
+ free(filename_buf);
return ret;
}
+int git_config_rename_section(const char *old_name, const char *new_name)
+{
+ return git_config_rename_section_in_file(NULL, old_name, new_name);
+}
+
/*
* Call this to report error for your variable that should not
* get a boolean value (i.e. "[my] var" means "true").