mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-21 22:21:37 +00:00
Make repo config loading automatic or completely explicit
git_repository_config wants to take the global and system paths again so that one can be explicit if needed. The git_repository_config_autoload function is provided for the cases when it's good enough for the library to guess where those files are located. Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
This commit is contained in:
parent
4c562347ae
commit
40fe5fbea8
@ -284,14 +284,11 @@ GIT_EXTERN(int) git_repository_is_bare(git_repository *repo);
|
|||||||
/**
|
/**
|
||||||
* Retrieve the relevant configuration for a repository
|
* Retrieve the relevant configuration for a repository
|
||||||
*
|
*
|
||||||
* By default he returned `git_config` instance contains the two most
|
* If either the `global_config_path` or `system_config_path`
|
||||||
* common configuration files, the `config' file that may be found
|
* variables are not NULL, the given config files will be also
|
||||||
* inside the repository, and the `$HOME/.gitconfig' "global"
|
* included in the configuration set. The global configuration file is
|
||||||
* configuration file.
|
* located in $HOME/.gitconfig. On most UNIX systems, the system
|
||||||
*
|
* config file file may be found on `$sysconfdir/gitconfig`.
|
||||||
* If the `system_config_path` variable is not NULL, the given config
|
|
||||||
* file will be also included in the configuration set. On most UNIX
|
|
||||||
* systems, this file may be found on `$PREFIX/etc/gitconfig`.
|
|
||||||
*
|
*
|
||||||
* The resulting `git_config` instance will query the files in the following
|
* The resulting `git_config` instance will query the files in the following
|
||||||
* order:
|
* order:
|
||||||
@ -300,20 +297,37 @@ GIT_EXTERN(int) git_repository_is_bare(git_repository *repo);
|
|||||||
* - Global configuration file
|
* - Global configuration file
|
||||||
* - System configuration file
|
* - System configuration file
|
||||||
*
|
*
|
||||||
* The method will fail if any of the passed system config file found
|
* The method will fail if any of the given config files can't be
|
||||||
* or accessed.
|
* found or accessed.
|
||||||
*
|
*
|
||||||
* The returned `git_config` instance is owned by the caller and must
|
* The returned `git_config` instance is owned by the caller and must
|
||||||
* be manually free'd once it's no longer on use.
|
* be manually free'd once it's no longer on use.
|
||||||
*
|
*
|
||||||
* @param out the repository's configuration
|
* @param out the repository's configuration
|
||||||
* @param repo the repository for which to get the config
|
* @param repo the repository for which to get the config
|
||||||
|
* @param system_config_path Path to the global config file
|
||||||
* @param system_config_path Path to the system-wide config file
|
* @param system_config_path Path to the system-wide config file
|
||||||
*/
|
*/
|
||||||
|
|
||||||
GIT_EXTERN(int) git_repository_config(git_config **out,
|
GIT_EXTERN(int) git_repository_config(git_config **out,
|
||||||
git_repository *repo,
|
git_repository *repo,
|
||||||
|
const char *global_config_path,
|
||||||
const char *system_config_path);
|
const char *system_config_path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Automatically load the configuration files
|
||||||
|
*
|
||||||
|
* A wrapper around `git_repository_config` that tries to guess where
|
||||||
|
* the global and system config files are located. No error is
|
||||||
|
* reported if either of these files
|
||||||
|
*
|
||||||
|
* @param out the repository's configuration
|
||||||
|
* @param repo the repository for which to get the config
|
||||||
|
*/
|
||||||
|
int git_repository_config_autoload(
|
||||||
|
git_config **out,
|
||||||
|
git_repository *repo);
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
GIT_END_DECL
|
GIT_END_DECL
|
||||||
#endif
|
#endif
|
||||||
|
@ -270,6 +270,7 @@ cleanup:
|
|||||||
int git_repository_config(
|
int git_repository_config(
|
||||||
git_config **out,
|
git_config **out,
|
||||||
git_repository *repo,
|
git_repository *repo,
|
||||||
|
const char *global_config_path,
|
||||||
const char *system_config_path)
|
const char *system_config_path)
|
||||||
{
|
{
|
||||||
char config_path[GIT_PATH_MAX];
|
char config_path[GIT_PATH_MAX];
|
||||||
@ -286,9 +287,10 @@ int git_repository_config(
|
|||||||
if (error < GIT_SUCCESS)
|
if (error < GIT_SUCCESS)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
error = git_config_find_global(config_path);
|
if (global_config_path != NULL) {
|
||||||
if (error == GIT_SUCCESS) {
|
error = git_config_add_file_ondisk(*out, global_config_path, 2);
|
||||||
error = git_config_add_file_ondisk(*out, config_path, 2);
|
if (error < GIT_SUCCESS)
|
||||||
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (system_config_path != NULL) {
|
if (system_config_path != NULL) {
|
||||||
@ -305,6 +307,24 @@ cleanup:
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int git_repository_config_autoload(
|
||||||
|
git_config **out,
|
||||||
|
git_repository *repo)
|
||||||
|
{
|
||||||
|
char global[GIT_PATH_MAX], system[GIT_PATH_MAX];
|
||||||
|
char *global_path, *system_path;
|
||||||
|
int error;
|
||||||
|
|
||||||
|
|
||||||
|
error = git_config_find_global(global);
|
||||||
|
global_path = error < GIT_SUCCESS ? NULL : global;
|
||||||
|
|
||||||
|
error = git_config_find_system(system);
|
||||||
|
system_path = error < GIT_SUCCESS ? NULL : system;
|
||||||
|
|
||||||
|
return git_repository_config(out, repo, global_path, system_path);
|
||||||
|
}
|
||||||
|
|
||||||
static int discover_repository_dirs(git_repository *repo, const char *path)
|
static int discover_repository_dirs(git_repository *repo, const char *path)
|
||||||
{
|
{
|
||||||
int error;
|
int error;
|
||||||
|
@ -11,7 +11,7 @@ void test_network_remotes__initialize(void)
|
|||||||
{
|
{
|
||||||
cl_fixture_sandbox(REPOSITORY_FOLDER);
|
cl_fixture_sandbox(REPOSITORY_FOLDER);
|
||||||
cl_git_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
cl_git_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
||||||
cl_git_pass(git_repository_config(&cfg, repo, NULL));
|
cl_git_pass(git_repository_config(&cfg, repo, NULL, NULL));
|
||||||
cl_git_pass(git_remote_get(&remote, cfg, "test"));
|
cl_git_pass(git_remote_get(&remote, cfg, "test"));
|
||||||
refspec = git_remote_fetchspec(remote);
|
refspec = git_remote_fetchspec(remote);
|
||||||
cl_assert(refspec != NULL);
|
cl_assert(refspec != NULL);
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
#include "filebuf.h"
|
#include "filebuf.h"
|
||||||
|
|
||||||
#define CONFIG_BASE TEST_RESOURCES "/config"
|
#define CONFIG_BASE TEST_RESOURCES "/config"
|
||||||
|
#define GLOBAL_CONFIG CONFIG_BASE "/.gitconfig"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This one is so we know the code isn't completely broken
|
* This one is so we know the code isn't completely broken
|
||||||
@ -233,40 +234,26 @@ BEGIN_TEST(config10, "a repo's config overrides the global config")
|
|||||||
git_repository *repo;
|
git_repository *repo;
|
||||||
git_config *cfg;
|
git_config *cfg;
|
||||||
int version;
|
int version;
|
||||||
char *old_home;
|
|
||||||
|
|
||||||
old_home = git__strdup(getenv("HOME"));
|
|
||||||
p_setenv("HOME", CONFIG_BASE, 1);
|
|
||||||
|
|
||||||
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
||||||
must_pass(git_repository_config(&cfg, repo, NULL));
|
must_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL));
|
||||||
must_pass(git_config_get_int(cfg, "core.repositoryformatversion", &version));
|
must_pass(git_config_get_int(cfg, "core.repositoryformatversion", &version));
|
||||||
must_be_true(version == 0);
|
must_be_true(version == 0);
|
||||||
git_config_free(cfg);
|
git_config_free(cfg);
|
||||||
git_repository_free(repo);
|
git_repository_free(repo);
|
||||||
|
|
||||||
p_setenv("HOME", old_home, 1);
|
|
||||||
free(old_home);
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
BEGIN_TEST(config11, "fall back to the global config")
|
BEGIN_TEST(config11, "fall back to the global config")
|
||||||
git_repository *repo;
|
git_repository *repo;
|
||||||
git_config *cfg;
|
git_config *cfg;
|
||||||
int num;
|
int num;
|
||||||
char *old_home;
|
|
||||||
|
|
||||||
old_home = git__strdup(getenv("HOME"));
|
|
||||||
p_setenv("HOME", CONFIG_BASE, 1);
|
|
||||||
|
|
||||||
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
||||||
must_pass(git_repository_config(&cfg, repo, NULL));
|
must_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL));
|
||||||
must_pass(git_config_get_int(cfg, "core.something", &num));
|
must_pass(git_config_get_int(cfg, "core.something", &num));
|
||||||
must_be_true(num == 2);
|
must_be_true(num == 2);
|
||||||
git_config_free(cfg);
|
git_config_free(cfg);
|
||||||
git_repository_free(repo);
|
git_repository_free(repo);
|
||||||
|
|
||||||
p_setenv("HOME", old_home, 1);
|
|
||||||
free(old_home);
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
BEGIN_TEST(config12, "delete a value")
|
BEGIN_TEST(config12, "delete a value")
|
||||||
|
@ -32,13 +32,9 @@ BEGIN_TEST(remotes0, "remote parsing works")
|
|||||||
git_remote *remote;
|
git_remote *remote;
|
||||||
git_repository *repo;
|
git_repository *repo;
|
||||||
git_config *cfg;
|
git_config *cfg;
|
||||||
char *old_home;
|
|
||||||
|
|
||||||
old_home = git__strdup(getenv("HOME"));
|
|
||||||
p_setenv("HOME", "/dev/null", 1);
|
|
||||||
|
|
||||||
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
||||||
must_pass(git_repository_config(&cfg, repo, NULL));
|
must_pass(git_repository_config(&cfg, repo, NULL, NULL));
|
||||||
must_pass(git_remote_get(&remote, cfg, "test"));
|
must_pass(git_remote_get(&remote, cfg, "test"));
|
||||||
must_be_true(!strcmp(git_remote_name(remote), "test"));
|
must_be_true(!strcmp(git_remote_name(remote), "test"));
|
||||||
must_be_true(!strcmp(git_remote_url(remote), "git://github.com/libgit2/libgit2"));
|
must_be_true(!strcmp(git_remote_url(remote), "git://github.com/libgit2/libgit2"));
|
||||||
@ -46,9 +42,6 @@ BEGIN_TEST(remotes0, "remote parsing works")
|
|||||||
git_remote_free(remote);
|
git_remote_free(remote);
|
||||||
git_config_free(cfg);
|
git_config_free(cfg);
|
||||||
git_repository_free(repo);
|
git_repository_free(repo);
|
||||||
|
|
||||||
p_setenv("HOME", old_home, 1);
|
|
||||||
free(old_home);
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
BEGIN_TEST(refspec0, "remote with refspec works")
|
BEGIN_TEST(refspec0, "remote with refspec works")
|
||||||
@ -56,13 +49,9 @@ BEGIN_TEST(refspec0, "remote with refspec works")
|
|||||||
git_repository *repo;
|
git_repository *repo;
|
||||||
git_config *cfg;
|
git_config *cfg;
|
||||||
const git_refspec *refspec = NULL;
|
const git_refspec *refspec = NULL;
|
||||||
char *old_home;
|
|
||||||
|
|
||||||
old_home = git__strdup(getenv("HOME"));
|
|
||||||
p_setenv("HOME", "/dev/null", 1);
|
|
||||||
|
|
||||||
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
||||||
must_pass(git_repository_config(&cfg, repo, NULL));
|
must_pass(git_repository_config(&cfg, repo, NULL, NULL));
|
||||||
must_pass(git_remote_get(&remote, cfg, "test"));
|
must_pass(git_remote_get(&remote, cfg, "test"));
|
||||||
refspec = git_remote_fetchspec(remote);
|
refspec = git_remote_fetchspec(remote);
|
||||||
must_be_true(refspec != NULL);
|
must_be_true(refspec != NULL);
|
||||||
@ -71,9 +60,6 @@ BEGIN_TEST(refspec0, "remote with refspec works")
|
|||||||
git_remote_free(remote);
|
git_remote_free(remote);
|
||||||
git_config_free(cfg);
|
git_config_free(cfg);
|
||||||
git_repository_free(repo);
|
git_repository_free(repo);
|
||||||
|
|
||||||
p_setenv("HOME", old_home, 1);
|
|
||||||
free(old_home);
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
BEGIN_TEST(refspec1, "remote fnmatch works as expected")
|
BEGIN_TEST(refspec1, "remote fnmatch works as expected")
|
||||||
@ -81,13 +67,9 @@ BEGIN_TEST(refspec1, "remote fnmatch works as expected")
|
|||||||
git_repository *repo;
|
git_repository *repo;
|
||||||
git_config *cfg;
|
git_config *cfg;
|
||||||
const git_refspec *refspec = NULL;
|
const git_refspec *refspec = NULL;
|
||||||
char *old_home;
|
|
||||||
|
|
||||||
old_home = git__strdup(getenv("HOME"));
|
|
||||||
p_setenv("HOME", "/dev/null", 1);
|
|
||||||
|
|
||||||
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
||||||
must_pass(git_repository_config(&cfg, repo, NULL));
|
must_pass(git_repository_config(&cfg, repo, NULL, NULL));
|
||||||
must_pass(git_remote_get(&remote, cfg, "test"));
|
must_pass(git_remote_get(&remote, cfg, "test"));
|
||||||
refspec = git_remote_fetchspec(remote);
|
refspec = git_remote_fetchspec(remote);
|
||||||
must_be_true(refspec != NULL);
|
must_be_true(refspec != NULL);
|
||||||
@ -96,9 +78,6 @@ BEGIN_TEST(refspec1, "remote fnmatch works as expected")
|
|||||||
git_remote_free(remote);
|
git_remote_free(remote);
|
||||||
git_config_free(cfg);
|
git_config_free(cfg);
|
||||||
git_repository_free(repo);
|
git_repository_free(repo);
|
||||||
|
|
||||||
p_setenv("HOME", old_home, 1);
|
|
||||||
free(old_home);
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
BEGIN_TEST(refspec2, "refspec transform")
|
BEGIN_TEST(refspec2, "refspec transform")
|
||||||
@ -107,13 +86,9 @@ BEGIN_TEST(refspec2, "refspec transform")
|
|||||||
git_config *cfg;
|
git_config *cfg;
|
||||||
const git_refspec *refspec = NULL;
|
const git_refspec *refspec = NULL;
|
||||||
char ref[1024] = {0};
|
char ref[1024] = {0};
|
||||||
char *old_home;
|
|
||||||
|
|
||||||
old_home = git__strdup(getenv("HOME"));
|
|
||||||
p_setenv("HOME", "/dev/null", 1);
|
|
||||||
|
|
||||||
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
|
||||||
must_pass(git_repository_config(&cfg, repo, NULL));
|
must_pass(git_repository_config(&cfg, repo, NULL, NULL));
|
||||||
must_pass(git_remote_get(&remote, cfg, "test"));
|
must_pass(git_remote_get(&remote, cfg, "test"));
|
||||||
refspec = git_remote_fetchspec(remote);
|
refspec = git_remote_fetchspec(remote);
|
||||||
must_be_true(refspec != NULL);
|
must_be_true(refspec != NULL);
|
||||||
@ -122,9 +97,6 @@ BEGIN_TEST(refspec2, "refspec transform")
|
|||||||
git_remote_free(remote);
|
git_remote_free(remote);
|
||||||
git_config_free(cfg);
|
git_config_free(cfg);
|
||||||
git_repository_free(repo);
|
git_repository_free(repo);
|
||||||
|
|
||||||
p_setenv("HOME", old_home, 1);
|
|
||||||
free(old_home);
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
BEGIN_SUITE(remotes)
|
BEGIN_SUITE(remotes)
|
||||||
|
Loading…
Reference in New Issue
Block a user