Implement git_config_open_global Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
diff --git a/include/git2/config.h b/include/git2/config.h
index 3ebbe64..4e10a5c 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -51,6 +51,13 @@ GIT_EXTERN(int) git_config_new(git_config **out);
GIT_EXTERN(int) git_config_open_bare(git_config **cfg_out, const char *path);
/**
+ * Open the global configuration file at $HOME/.gitconfig
+ *
+ * @param cfg pointer to the configuration
+ */
+GIT_EXTERN(int) git_config_open_global(git_config **cfg);
+
+/**
*
*/
GIT_EXTERN(int) git_config_add_backend(git_config *cfg, git_config_backend *backend, int priority);
diff --git a/src/config.c b/src/config.c
index cd0a73c..0ade5cf 100644
--- a/src/config.c
+++ b/src/config.c
@@ -70,6 +70,40 @@ int git_config_open_bare(git_config **out, const char *path)
return error;
}
+int git_config_open_global(git_config **out)
+{
+ char *home = NULL, *filename = NULL;
+ const char *gitconfig = ".gitconfig";
+ int filename_len, ret, error;
+
+ home = git__strdup(getenv("HOME"));
+ if (home == NULL)
+ return GIT_ENOMEM;
+
+ 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);
+ 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:
+ free(home);
+ free(filename);
+ return error;
+}
+
void git_config_free(git_config *cfg)
{
unsigned int i;