summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-06-18 00:39:39 +0200
committerVicent Marti <tanoku@gmail.com>2011-06-18 00:39:39 +0200
commit07ff881750a073dc17519f3b03f266468e124819 (patch)
tree9ebf4890ad18a6e71a8440f9631a5c0e98c58f51 /src
parentf3dad3acd75651099c0502e7586ef5a44c22684f (diff)
downloadlibgit2-07ff881750a073dc17519f3b03f266468e124819.tar.gz
config: Cleanup external API
Do not mess with environment variables anymore. The new external API has more helper methods, and everything is explicit.
Diffstat (limited to 'src')
-rw-r--r--src/config.c85
-rw-r--r--src/repository.c51
2 files changed, 61 insertions, 75 deletions
diff --git a/src/config.c b/src/config.c
index 39a236ad9..ff7204b05 100644
--- a/src/config.c
+++ b/src/config.c
@@ -37,54 +37,6 @@ typedef struct {
int priority;
} file_internal;
-int git_config_open_file(git_config **out, const char *path)
-{
- git_config_file *file = NULL;
- git_config *cfg = NULL;
- int error = GIT_SUCCESS;
-
- error = git_config_new(&cfg);
- if (error < GIT_SUCCESS)
- return error;
-
- error = git_config_file__ondisk(&file, path);
- if (error < GIT_SUCCESS) {
- git_config_free(cfg);
- return error;
- }
-
- error = git_config_add_file(cfg, file, 1);
- if (error < GIT_SUCCESS) {
- file->free(file);
- git_config_free(cfg);
- return error;
- }
-
- error = file->open(file);
- if (error < GIT_SUCCESS) {
- git_config_free(cfg);
- return git__rethrow(error, "Failed to open config file");
- }
-
- *out = cfg;
-
- return GIT_SUCCESS;
-}
-
-int git_config_open_global(git_config **out)
-{
- char full_path[GIT_PATH_MAX];
- const char *home;
-
- home = getenv("HOME");
- if (home == NULL)
- return git__throw(GIT_EOSERR, "Failed to open global config file. Cannot find $HOME variable");
-
- git__joinpath(full_path, home, GIT_CONFIG_FILENAME);
-
- return git_config_open_file(out, full_path);
-}
-
void git_config_free(git_config *cfg)
{
unsigned int i;
@@ -130,12 +82,49 @@ int git_config_new(git_config **out)
return GIT_SUCCESS;
}
+int git_config_add_file_ondisk(git_config *cfg, const char *path, int priority)
+{
+ git_config_file *file = NULL;
+ int error;
+
+ error = git_config_file__ondisk(&file, path);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ error = git_config_add_file(cfg, file, priority);
+ if (error < GIT_SUCCESS) {
+ file->free(file); /* free manually; the file is not owned by the ODB yet */
+ return error;
+ }
+
+ return GIT_SUCCESS;
+}
+
+int git_config_open_ondisk(git_config **cfg, const char *path)
+{
+ int error;
+
+ error = git_config_new(cfg);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ error = git_config_add_file_ondisk(*cfg, path, 1);
+ if (error < GIT_SUCCESS)
+ git_config_free(*cfg);
+
+ return error;
+}
+
int git_config_add_file(git_config *cfg, git_config_file *file, int priority)
{
file_internal *internal;
+ int error;
assert(cfg && file);
+ if ((error = file->open(file)) < GIT_SUCCESS)
+ return git__throw(error, "Failed to open config file");
+
internal = git__malloc(sizeof(file_internal));
if (internal == NULL)
return GIT_ENOMEM;
diff --git a/src/repository.c b/src/repository.c
index 41fb96b54..bcf393263 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -271,45 +271,42 @@ cleanup:
return git__rethrow(error, "Failed to open repository");
}
-int git_repository_config(git_config **out, git_repository *repo)
+int git_repository_config(
+ git_config **out,
+ git_repository *repo,
+ const char *user_config_path,
+ const char *system_config_path)
{
- git_config *cfg = NULL;
- git_config_file *local = NULL;
- char gitconfig[GIT_PATH_MAX];
- int error = GIT_SUCCESS;
+ char config_path[GIT_PATH_MAX];
+ int error;
- error = git_config_open_global(&cfg);
+ assert(out && repo);
+
+ error = git_config_new(out);
if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to open global config");
+ return error;
- git__joinpath(gitconfig, repo->path_repository, GIT_CONFIG_FILENAME_INREPO);
- error = git_config_file__ondisk(&local, gitconfig);
- if (error < GIT_SUCCESS) {
- error = git__rethrow(error, "Failed to open local config");
+ git__joinpath(config_path, repo->path_repository, GIT_CONFIG_FILENAME_INREPO);
+ error = git_config_add_file_ondisk(*out, config_path, 1);
+ if (error < GIT_SUCCESS)
goto cleanup;
- }
- error = git_config_add_file(cfg, local, 2);
- if (error < GIT_SUCCESS) {
- error = git__rethrow(error, "Failed to add the local config");
- goto cleanup;
+ if (user_config_path != NULL) {
+ error = git_config_add_file_ondisk(*out, user_config_path, 2);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
}
- error = local->open(local);
- if (error < GIT_SUCCESS) {
- error = git__rethrow(error, "Failed to open config file");
- goto cleanup;
+ if (system_config_path != NULL) {
+ error = git_config_add_file_ondisk(*out, system_config_path, 3);
+ if (error < GIT_SUCCESS)
+ goto cleanup;
}
- *out = cfg;
+ return GIT_SUCCESS;
cleanup:
- if (error < GIT_SUCCESS) {
- git_config_free(cfg);
- if (local)
- local->free(local);
- }
-
+ git_config_free(*out);
return error;
}