summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2017-05-23 11:51:00 +0200
committerPatrick Steinhardt <ps@pks.im>2017-10-09 11:19:42 +0200
commit529e873cef18ec98246d32d28c7a0b0e3467fe27 (patch)
treef810490f62966f39ea9c86e041b8102956d2306f /src
parentd02cf564a012ea8f6d4d4fd70a3102b94058f759 (diff)
downloadlibgit2-529e873cef18ec98246d32d28c7a0b0e3467fe27.tar.gz
config: pass repository when opening config files
Our current configuration logic is completely oblivious of any repository, but only cares for actual file paths. Unfortunately, we are forced to break this assumption by the introduction of conditional includes, which are evaluated in the context of a repository. Right now, only one conditional exists with "gitdir:" -- it will only include the configuration if the current repository's git directory matches the value passed to "gitdir:". To support these conditionals, we have to break our API and make the repository available when opening a configuration file. This commit extends the `open` call of configuration backends to include another repository and adjusts existing code to have it available. This includes the user-visible functions `git_config_add_file_ondisk` and `git_config_add_backend`.
Diffstat (limited to 'src')
-rw-r--r--src/config.c18
-rw-r--r--src/config_file.c11
-rw-r--r--src/config_file.h4
-rw-r--r--src/repository.c12
-rw-r--r--src/submodule.c2
5 files changed, 26 insertions, 21 deletions
diff --git a/src/config.c b/src/config.c
index 602e0e827..1bc11b99f 100644
--- a/src/config.c
+++ b/src/config.c
@@ -99,6 +99,7 @@ int git_config_add_file_ondisk(
git_config *cfg,
const char *path,
git_config_level_t level,
+ const git_repository *repo,
int force)
{
git_config_backend *file = NULL;
@@ -116,7 +117,7 @@ int git_config_add_file_ondisk(
if (git_config_file__ondisk(&file, path) < 0)
return -1;
- if ((res = git_config_add_backend(cfg, file, level, force)) < 0) {
+ if ((res = git_config_add_backend(cfg, file, level, repo, force)) < 0) {
/*
* free manually; the file is not owned by the config
* instance yet and will not be freed on cleanup
@@ -138,7 +139,7 @@ int git_config_open_ondisk(git_config **out, const char *path)
if (git_config_new(&config) < 0)
return -1;
- if ((error = git_config_add_file_ondisk(config, path, GIT_CONFIG_LEVEL_LOCAL, 0)) < 0)
+ if ((error = git_config_add_file_ondisk(config, path, GIT_CONFIG_LEVEL_LOCAL, NULL, 0)) < 0)
git_config_free(config);
else
*out = config;
@@ -164,7 +165,7 @@ int git_config_snapshot(git_config **out, git_config *in)
if ((error = internal->file->snapshot(&b, internal->file)) < 0)
break;
- if ((error = git_config_add_backend(config, b, internal->level, 0)) < 0) {
+ if ((error = git_config_add_backend(config, b, internal->level, NULL, 0)) < 0) {
b->free(b);
break;
}
@@ -307,6 +308,7 @@ int git_config_add_backend(
git_config *cfg,
git_config_backend *file,
git_config_level_t level,
+ const git_repository *repo,
int force)
{
file_internal *internal;
@@ -316,7 +318,7 @@ int git_config_add_backend(
GITERR_CHECK_VERSION(file, GIT_CONFIG_BACKEND_VERSION, "git_config_backend");
- if ((result = file->open(file, level)) < 0)
+ if ((result = file->open(file, level, repo)) < 0)
return result;
internal = git__malloc(sizeof(file_internal));
@@ -1147,20 +1149,20 @@ int git_config_open_default(git_config **out)
if (!git_config_find_global(&buf) || !git_config__global_location(&buf)) {
error = git_config_add_file_ondisk(cfg, buf.ptr,
- GIT_CONFIG_LEVEL_GLOBAL, 0);
+ GIT_CONFIG_LEVEL_GLOBAL, NULL, 0);
}
if (!error && !git_config_find_xdg(&buf))
error = git_config_add_file_ondisk(cfg, buf.ptr,
- GIT_CONFIG_LEVEL_XDG, 0);
+ GIT_CONFIG_LEVEL_XDG, NULL, 0);
if (!error && !git_config_find_system(&buf))
error = git_config_add_file_ondisk(cfg, buf.ptr,
- GIT_CONFIG_LEVEL_SYSTEM, 0);
+ GIT_CONFIG_LEVEL_SYSTEM, NULL, 0);
if (!error && !git_config_find_programdata(&buf))
error = git_config_add_file_ondisk(cfg, buf.ptr,
- GIT_CONFIG_LEVEL_PROGRAMDATA, 0);
+ GIT_CONFIG_LEVEL_PROGRAMDATA, NULL, 0);
git_buf_free(&buf);
diff --git a/src/config_file.c b/src/config_file.c
index 00d6d9e13..4146bd8b2 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -105,6 +105,7 @@ typedef struct {
diskfile_header header;
git_config_level_t level;
+ const git_repository *repo;
bool locked;
git_filebuf locked_buf;
@@ -281,12 +282,13 @@ static void config_file_clear(struct config_file *file)
git__free(file->path);
}
-static int config_open(git_config_backend *cfg, git_config_level_t level)
+static int config_open(git_config_backend *cfg, git_config_level_t level, const git_repository *repo)
{
int res;
diskfile_backend *b = (diskfile_backend *)cfg;
b->level = level;
+ b->repo = repo;
if ((res = refcounted_strmap_alloc(&b->header.values)) < 0)
return res;
@@ -439,7 +441,7 @@ static int config_iterator_new(
if ((error = config_snapshot(&snapshot, backend)) < 0)
return error;
- if ((error = snapshot->open(snapshot, b->level)) < 0)
+ if ((error = snapshot->open(snapshot, b->level, b->repo)) < 0)
return error;
it = git__calloc(1, sizeof(git_config_file_iter));
@@ -831,7 +833,7 @@ static void backend_readonly_free(git_config_backend *_backend)
git__free(backend);
}
-static int config_readonly_open(git_config_backend *cfg, git_config_level_t level)
+static int config_readonly_open(git_config_backend *cfg, git_config_level_t level, const git_repository *repo)
{
diskfile_readonly_backend *b = (diskfile_readonly_backend *) cfg;
diskfile_backend *src = b->snapshot_from;
@@ -842,8 +844,9 @@ static int config_readonly_open(git_config_backend *cfg, git_config_level_t leve
if (!src_header->parent.readonly && (error = config_refresh(&src_header->parent)) < 0)
return error;
- /* We're just copying data, don't care about the level */
+ /* We're just copying data, don't care about the level or repo*/
GIT_UNUSED(level);
+ GIT_UNUSED(repo);
if ((src_map = refcounted_strmap_take(src_header)) == NULL)
return -1;
diff --git a/src/config_file.h b/src/config_file.h
index 11b8118f5..25ef45e5b 100644
--- a/src/config_file.h
+++ b/src/config_file.h
@@ -12,9 +12,9 @@
#include "git2/sys/config.h"
#include "git2/config.h"
-GIT_INLINE(int) git_config_file_open(git_config_backend *cfg, unsigned int level)
+GIT_INLINE(int) git_config_file_open(git_config_backend *cfg, unsigned int level, const git_repository *repo)
{
- return cfg->open(cfg, level);
+ return cfg->open(cfg, level, repo);
}
GIT_INLINE(void) git_config_file_free(git_config_backend *cfg)
diff --git a/src/repository.c b/src/repository.c
index d128b847a..fe0696355 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -946,7 +946,7 @@ static int load_config(
return error;
if ((error = git_repository_item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG)) == 0)
- error = git_config_add_file_ondisk(cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0);
+ error = git_config_add_file_ondisk(cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, repo, 0);
if (error && error != GIT_ENOTFOUND)
goto on_error;
@@ -955,25 +955,25 @@ static int load_config(
if (global_config_path != NULL &&
(error = git_config_add_file_ondisk(
- cfg, global_config_path, GIT_CONFIG_LEVEL_GLOBAL, 0)) < 0 &&
+ cfg, global_config_path, GIT_CONFIG_LEVEL_GLOBAL, repo, 0)) < 0 &&
error != GIT_ENOTFOUND)
goto on_error;
if (xdg_config_path != NULL &&
(error = git_config_add_file_ondisk(
- cfg, xdg_config_path, GIT_CONFIG_LEVEL_XDG, 0)) < 0 &&
+ cfg, xdg_config_path, GIT_CONFIG_LEVEL_XDG, repo, 0)) < 0 &&
error != GIT_ENOTFOUND)
goto on_error;
if (system_config_path != NULL &&
(error = git_config_add_file_ondisk(
- cfg, system_config_path, GIT_CONFIG_LEVEL_SYSTEM, 0)) < 0 &&
+ cfg, system_config_path, GIT_CONFIG_LEVEL_SYSTEM, repo, 0)) < 0 &&
error != GIT_ENOTFOUND)
goto on_error;
if (programdata_path != NULL &&
(error = git_config_add_file_ondisk(
- cfg, programdata_path, GIT_CONFIG_LEVEL_PROGRAMDATA, 0)) < 0 &&
+ cfg, programdata_path, GIT_CONFIG_LEVEL_PROGRAMDATA, repo, 0)) < 0 &&
error != GIT_ENOTFOUND)
goto on_error;
@@ -1475,7 +1475,7 @@ static int repo_local_config(
giterr_clear();
if (!(error = git_config_add_file_ondisk(
- parent, cfg_path, GIT_CONFIG_LEVEL_LOCAL, false)))
+ parent, cfg_path, GIT_CONFIG_LEVEL_LOCAL, repo, false)))
error = git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL);
}
diff --git a/src/submodule.c b/src/submodule.c
index 6c3e5f6bd..3ec0307b3 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -1960,7 +1960,7 @@ static git_config_backend *open_gitmodules(
if (git_config_file__ondisk(&mods, path.ptr) < 0)
mods = NULL;
/* open should only fail here if the file is malformed */
- else if (git_config_file_open(mods, GIT_CONFIG_LEVEL_LOCAL) < 0) {
+ else if (git_config_file_open(mods, GIT_CONFIG_LEVEL_LOCAL, repo) < 0) {
git_config_file_free(mods);
mods = NULL;
}