Commit 5d831887 by Carlos Martín Nieto

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.
parent a4b75dcf
......@@ -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
......
......@@ -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,
......
#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);
}
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