Commit 8adbf2ed by Vicent Marti

Rewrite `git_config_open_global`

We have a lot of utility methods that make path building trivial. Use
them!
parent 274f2c21
...@@ -72,36 +72,16 @@ int git_config_open_bare(git_config **out, const char *path) ...@@ -72,36 +72,16 @@ int git_config_open_bare(git_config **out, const char *path)
int git_config_open_global(git_config **out) int git_config_open_global(git_config **out)
{ {
char *home = NULL, *filename = NULL; char full_path[GIT_PATH_MAX];
const char *gitconfig = ".gitconfig"; const char *home;
int filename_len, ret, error;
home = git__strdup(getenv("HOME")); home = getenv("HOME");
if (home == NULL) if (home == NULL)
return GIT_ENOMEM; return git__throw(GIT_EOSERR, "Failed to find $HOME variable");
filename_len = strlen(home) + strlen(gitconfig) + 1;
filename = git__malloc(filename_len + 1);
if (filename == NULL) {
error = GIT_ENOMEM;
goto out;
}
ret = snprintf(filename, filename_len, "%s/%s", home, gitconfig); git__joinpath(full_path, home, GIT_CONFIG_FILENAME);
if (ret < 0) {
error = git__throw(GIT_EOSERR, "Failed to build global filename. OS err: %s", strerror(errno));
goto out;
} else if (ret >= filename_len) {
error = git__throw(GIT_ERROR, "Failed to build global filename. Length calulation wrong");
goto out;
}
error = git_config_open_bare(out, filename);
out: return git_config_open_bare(out, filename);
free(home);
free(filename);
return error;
} }
void git_config_free(git_config *cfg) void git_config_free(git_config *cfg)
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "git2/config.h" #include "git2/config.h"
#include "vector.h" #include "vector.h"
#define GIT_CONFIG_FILENAME ".gitconfig"
struct git_config { struct git_config {
git_vector backends; git_vector backends;
}; };
......
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