Commit 29c4cb09 by Carlos Martín Nieto

Use config snapshotting

This way we can assume we have a consistent view of the config situation
when we're looking up remote, branch, pack-objects, etc.
parent 55ebd7d3
...@@ -306,17 +306,13 @@ int git_branch_name( ...@@ -306,17 +306,13 @@ int git_branch_name(
static int retrieve_upstream_configuration( static int retrieve_upstream_configuration(
const char **out, const char **out,
git_repository *repo, const git_config *config,
const char *canonical_branch_name, const char *canonical_branch_name,
const char *format) const char *format)
{ {
git_config *config;
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
int error; int error;
if (git_repository_config__weakptr(&config, repo) < 0)
return -1;
if (git_buf_printf(&buf, format, if (git_buf_printf(&buf, format,
canonical_branch_name + strlen(GIT_REFS_HEADS_DIR)) < 0) canonical_branch_name + strlen(GIT_REFS_HEADS_DIR)) < 0)
return -1; return -1;
...@@ -336,6 +332,7 @@ int git_branch_upstream_name( ...@@ -336,6 +332,7 @@ int git_branch_upstream_name(
int error = -1; int error = -1;
git_remote *remote = NULL; git_remote *remote = NULL;
const git_refspec *refspec; const git_refspec *refspec;
git_config *config, *repo_config;
assert(out && refname); assert(out && refname);
...@@ -344,12 +341,18 @@ int git_branch_upstream_name( ...@@ -344,12 +341,18 @@ int git_branch_upstream_name(
if (!git_reference__is_branch(refname)) if (!git_reference__is_branch(refname))
return not_a_local_branch(refname); return not_a_local_branch(refname);
if ((error = git_repository_config__weakptr(&repo_config, repo)) < 0)
return error;
if ((error = git_config_snapshot(&config, repo_config)) < 0)
return error;
if ((error = retrieve_upstream_configuration( if ((error = retrieve_upstream_configuration(
&remote_name, repo, refname, "branch.%s.remote")) < 0) &remote_name, config, refname, "branch.%s.remote")) < 0)
goto cleanup; goto cleanup;
if ((error = retrieve_upstream_configuration( if ((error = retrieve_upstream_configuration(
&merge_name, repo, refname, "branch.%s.merge")) < 0) &merge_name, config, refname, "branch.%s.merge")) < 0)
goto cleanup; goto cleanup;
if (!*remote_name || !*merge_name) { if (!*remote_name || !*merge_name) {
...@@ -378,6 +381,7 @@ int git_branch_upstream_name( ...@@ -378,6 +381,7 @@ int git_branch_upstream_name(
error = git_buf_set(out, git_buf_cstr(&buf), git_buf_len(&buf)); error = git_buf_set(out, git_buf_cstr(&buf), git_buf_len(&buf));
cleanup: cleanup:
git_config_free(config);
git_remote_free(remote); git_remote_free(remote);
git_buf_free(&buf); git_buf_free(&buf);
return error; return error;
......
...@@ -219,7 +219,7 @@ static int git_diff_driver_load( ...@@ -219,7 +219,7 @@ static int git_diff_driver_load(
git_diff_driver *drv = NULL; git_diff_driver *drv = NULL;
size_t namelen = strlen(driver_name); size_t namelen = strlen(driver_name);
khiter_t pos; khiter_t pos;
git_config *cfg; git_config *cfg, *repo_cfg;
git_buf name = GIT_BUF_INIT; git_buf name = GIT_BUF_INIT;
const git_config_entry *ce; const git_config_entry *ce;
bool found_driver = false; bool found_driver = false;
...@@ -234,11 +234,14 @@ static int git_diff_driver_load( ...@@ -234,11 +234,14 @@ static int git_diff_driver_load(
} }
/* if you can't read config for repo, just use default driver */ /* if you can't read config for repo, just use default driver */
if (git_repository_config__weakptr(&cfg, repo) < 0) { if (git_repository_config__weakptr(&repo_cfg, repo) < 0) {
giterr_clear(); giterr_clear();
goto done; goto done;
} }
if ((error = git_config_snapshot(&cfg, repo_cfg)) < 0)
return error;
drv = git__calloc(1, sizeof(git_diff_driver) + namelen + 1); drv = git__calloc(1, sizeof(git_diff_driver) + namelen + 1);
GITERR_CHECK_ALLOC(drv); GITERR_CHECK_ALLOC(drv);
drv->type = DIFF_DRIVER_AUTO; drv->type = DIFF_DRIVER_AUTO;
...@@ -321,6 +324,7 @@ static int git_diff_driver_load( ...@@ -321,6 +324,7 @@ static int git_diff_driver_load(
done: done:
git_buf_free(&name); git_buf_free(&name);
git_config_free(cfg);
if (!*out) { if (!*out) {
int error2 = git_diff_driver_builtin(out, reg, driver_name); int error2 = git_diff_driver_builtin(out, reg, driver_name);
......
...@@ -86,12 +86,15 @@ static unsigned name_hash(const char *name) ...@@ -86,12 +86,15 @@ static unsigned name_hash(const char *name)
static int packbuilder_config(git_packbuilder *pb) static int packbuilder_config(git_packbuilder *pb)
{ {
git_config *config; git_config *config, *repo_config;
int ret; int ret;
int64_t val; int64_t val;
if (git_repository_config__weakptr(&config, pb->repo) < 0) if ((ret = git_repository_config__weakptr(&repo_config, pb->repo)) < 0)
return -1; return ret;
if ((ret = git_config_snapshot(&config, repo_config)) < 0)
return ret;
#define config_get(KEY,DST,DFLT) do { \ #define config_get(KEY,DST,DFLT) do { \
ret = git_config_get_int64(&val, config, KEY); \ ret = git_config_get_int64(&val, config, KEY); \
...@@ -109,6 +112,8 @@ static int packbuilder_config(git_packbuilder *pb) ...@@ -109,6 +112,8 @@ static int packbuilder_config(git_packbuilder *pb)
#undef config_get #undef config_get
git_config_free(config);
return 0; return 0;
} }
......
...@@ -347,7 +347,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) ...@@ -347,7 +347,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
const char *val; const char *val;
int error = 0; int error = 0;
git_config *config; git_config *config, *repo_config;
struct refspec_cb_data data = { NULL }; struct refspec_cb_data data = { NULL };
bool optional_setting_found = false, found; bool optional_setting_found = false, found;
...@@ -356,9 +356,12 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) ...@@ -356,9 +356,12 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
if ((error = ensure_remote_name_is_valid(name)) < 0) if ((error = ensure_remote_name_is_valid(name)) < 0)
return error; return error;
if (git_repository_config__weakptr(&config, repo) < 0) if (git_repository_config__weakptr(&repo_config, repo) < 0)
return -1; return -1;
if ((error = git_config_snapshot(&config, repo_config)) < 0)
return error;
remote = git__malloc(sizeof(git_remote)); remote = git__malloc(sizeof(git_remote));
GITERR_CHECK_ALLOC(remote); GITERR_CHECK_ALLOC(remote);
...@@ -437,6 +440,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name) ...@@ -437,6 +440,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
*out = remote; *out = remote;
cleanup: cleanup:
git_config_free(config);
git_buf_free(&buf); git_buf_free(&buf);
if (error < 0) if (error < 0)
......
...@@ -165,13 +165,9 @@ int git_repository_new(git_repository **out) ...@@ -165,13 +165,9 @@ int git_repository_new(git_repository **out)
return 0; return 0;
} }
static int load_config_data(git_repository *repo) static int load_config_data(git_repository *repo, const git_config *config)
{ {
int is_bare; int is_bare;
git_config *config;
if (git_repository_config__weakptr(&config, repo) < 0)
return -1;
/* Try to figure out if it's bare, default to non-bare if it's not set */ /* Try to figure out if it's bare, default to non-bare if it's not set */
if (git_config_get_bool(&is_bare, config, "core.bare") < 0) if (git_config_get_bool(&is_bare, config, "core.bare") < 0)
...@@ -182,19 +178,15 @@ static int load_config_data(git_repository *repo) ...@@ -182,19 +178,15 @@ static int load_config_data(git_repository *repo)
return 0; return 0;
} }
static int load_workdir(git_repository *repo, git_buf *parent_path) static int load_workdir(git_repository *repo, git_config *config, git_buf *parent_path)
{ {
int error; int error;
git_config *config;
const git_config_entry *ce; const git_config_entry *ce;
git_buf worktree = GIT_BUF_INIT; git_buf worktree = GIT_BUF_INIT;
if (repo->is_bare) if (repo->is_bare)
return 0; return 0;
if ((error = git_repository_config__weakptr(&config, repo)) < 0)
return error;
if ((error = git_config__lookup_entry( if ((error = git_config__lookup_entry(
&ce, config, "core.worktree", false)) < 0) &ce, config, "core.worktree", false)) < 0)
return error; return error;
...@@ -447,6 +439,7 @@ int git_repository_open_ext( ...@@ -447,6 +439,7 @@ int git_repository_open_ext(
int error; int error;
git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT;
git_repository *repo; git_repository *repo;
git_config *repo_config, *config;
if (repo_ptr) if (repo_ptr)
*repo_ptr = NULL; *repo_ptr = NULL;
...@@ -461,15 +454,23 @@ int git_repository_open_ext( ...@@ -461,15 +454,23 @@ int git_repository_open_ext(
repo->path_repository = git_buf_detach(&path); repo->path_repository = git_buf_detach(&path);
GITERR_CHECK_ALLOC(repo->path_repository); GITERR_CHECK_ALLOC(repo->path_repository);
if ((error = git_repository_config__weakptr(&repo_config, repo)) < 0)
return error;
if ((error = git_config_snapshot(&config, repo_config)) < 0)
return error;
if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0) if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0)
repo->is_bare = 1; repo->is_bare = 1;
else if ((error = load_config_data(repo)) < 0 || else if ((error = load_config_data(repo, config)) < 0 ||
(error = load_workdir(repo, &parent)) < 0) (error = load_workdir(repo, config, &parent)) < 0)
{ {
git_config_free(config);
git_repository_free(repo); git_repository_free(repo);
return error; return error;
} }
git_config_free(config);
git_buf_free(&parent); git_buf_free(&parent);
*repo_ptr = repo; *repo_ptr = repo;
return 0; return 0;
......
...@@ -141,10 +141,13 @@ int git_signature_now(git_signature **sig_out, const char *name, const char *ema ...@@ -141,10 +141,13 @@ int git_signature_now(git_signature **sig_out, const char *name, const char *ema
int git_signature_default(git_signature **out, git_repository *repo) int git_signature_default(git_signature **out, git_repository *repo)
{ {
int error; int error;
git_config *cfg; git_config *cfg, *repo_cfg;
const char *user_name, *user_email; const char *user_name, *user_email;
if ((error = git_repository_config(&cfg, repo)) < 0) if ((error = git_repository_config__weakptr(&repo_cfg, repo)) < 0)
return error;
if ((error = git_config_snapshot(&cfg, repo_cfg)) < 0)
return error; return error;
if (!(error = git_config_get_string(&user_name, cfg, "user.name")) && if (!(error = git_config_get_string(&user_name, cfg, "user.name")) &&
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment