Commit 5d8318875fc7234d00140ce1f18b67bc4703e5f4

Carlos Martín Nieto 2013-05-07T00:10:02

config: convenience function to open global/xdg The rules for which one to open is a bit silly, so let's make it easier for our users.

diff --git a/include/git2/config.h b/include/git2/config.h
index 5a2f956..724788a 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -195,6 +195,21 @@ GIT_EXTERN(int) git_config_open_level(
     unsigned int level);
 
 /**
+ * Open the global/XDG configuration file according to git's rules
+ *
+ * Git allows you to store your global configuration at
+ * `$HOME/.config` or `$XDG_CONFIG_HOME/git/config`. For backwards
+ * compatability, the XDG file shouldn't be used unless the use has
+ * created it explicitly. With this function you'll open the correct
+ * one to write to.
+ *
+ * @param out pointer in which to store the config object
+ * @param config the config object in which to look
+ */
+GIT_EXTERN(int) git_config_open_global(git_config **out, git_config *config);
+
+
+/**
  * Reload changed config files
  *
  * A config file may be changed on disk out from under the in-memory
diff --git a/src/config.c b/src/config.c
index 99aa00f..bd629f7 100644
--- a/src/config.c
+++ b/src/config.c
@@ -227,6 +227,14 @@ static int git_config__add_internal(
 	return 0;
 }
 
+int git_config_open_global(git_config **cfg_out, git_config *cfg)
+{
+	if (!git_config_open_level(cfg_out, cfg, GIT_CONFIG_LEVEL_XDG))
+		return 0;
+
+	return git_config_open_level(cfg_out, cfg, GIT_CONFIG_LEVEL_GLOBAL);
+}
+
 int git_config_open_level(
     git_config **cfg_out,
     const git_config *cfg_parent,
diff --git a/tests-clar/config/global.c b/tests-clar/config/global.c
new file mode 100644
index 0000000..2ecdf97
--- /dev/null
+++ b/tests-clar/config/global.c
@@ -0,0 +1,67 @@
+#include "clar_libgit2.h"
+#include "buffer.h"
+#include "fileops.h"
+
+void test_config_global__initialize(void)
+{
+	git_buf path = GIT_BUF_INIT;
+
+	cl_must_pass(p_mkdir("home", 0777));
+	cl_git_pass(git_path_prettify(&path, "home", NULL));
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
+
+	cl_must_pass(p_mkdir("xdg", 0777));
+	cl_git_pass(git_path_prettify(&path, "xdg", NULL));
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
+
+	cl_git_pass(git_libgit2_opts(
+		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, NULL));
+
+	git_buf_free(&path);
+}
+
+void test_config_global__cleanup(void)
+{
+	cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));
+	cl_git_pass(git_futils_rmdir_r("xdg", NULL, GIT_RMDIR_REMOVE_FILES));
+}
+
+void test_config_global__open_global(void)
+{
+	git_config *cfg, *global, *selected, *dummy;
+
+	cl_git_pass(git_config_open_default(&cfg));
+	cl_git_pass(git_config_open_level(&global, cfg, GIT_CONFIG_LEVEL_GLOBAL));
+	cl_git_fail(git_config_open_level(&dummy, cfg, GIT_CONFIG_LEVEL_XDG));
+	cl_git_pass(git_config_open_global(&selected, cfg));
+
+	git_config_free(selected);
+	git_config_free(global);
+	git_config_free(cfg);
+}
+
+void test_config_global__open_xdg(void)
+{
+	git_config *cfg, *xdg, *selected;
+	const char *val, *str = "teststring";
+	const char *key = "this.variable";
+
+	p_setenv("XDG_CONFIG_HOME", "xdg", 1);
+
+	cl_must_pass(p_mkdir("xdg/git/", 0777));
+	cl_git_mkfile("xdg/git/config", "");
+
+	cl_git_pass(git_config_open_default(&cfg));
+	cl_git_pass(git_config_open_level(&xdg, cfg, GIT_CONFIG_LEVEL_XDG));
+	cl_git_pass(git_config_open_global(&selected, cfg));
+
+	cl_git_pass(git_config_set_string(xdg, key, str));
+	cl_git_pass(git_config_get_string(&val, selected, key));
+	cl_assert_equal_s(str, val);
+
+	git_config_free(selected);
+	git_config_free(xdg);
+	git_config_free(cfg);
+}