mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-11 00:12:07 +00:00
repository: introduce a convenience config snapshot method
Accessing the repository's config and immediately taking a snapshot of it is a common operation, so let's provide a convenience function for it.
This commit is contained in:
parent
2280b388c9
commit
ac99d86ba5
@ -409,12 +409,24 @@ GIT_EXTERN(int) git_repository_is_bare(git_repository *repo);
|
|||||||
* The configuration file must be freed once it's no longer
|
* The configuration file must be freed once it's no longer
|
||||||
* being used by the user.
|
* being used by the user.
|
||||||
*
|
*
|
||||||
* @param out Pointer to store the loaded config file
|
* @param out Pointer to store the loaded configuration
|
||||||
* @param repo A repository object
|
* @param repo A repository object
|
||||||
* @return 0, or an error code
|
* @return 0, or an error code
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(int) git_repository_config(git_config **out, git_repository *repo);
|
GIT_EXTERN(int) git_repository_config(git_config **out, git_repository *repo);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a snapshot of the repository's configuration
|
||||||
|
*
|
||||||
|
* Convenience function to take a snapshot from the repository's
|
||||||
|
* configuration.
|
||||||
|
*
|
||||||
|
* @param out Pointer to store the loaded configuration
|
||||||
|
* @param repo the repository
|
||||||
|
* @return 0, or an error code
|
||||||
|
*/
|
||||||
|
GIT_EXTERN(int) git_repository_config_snapshot(git_config **out, git_repository *repo);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Object Database for this repository.
|
* Get the Object Database for this repository.
|
||||||
*
|
*
|
||||||
|
@ -332,7 +332,7 @@ int git_branch_upstream_name(
|
|||||||
int error = -1;
|
int error = -1;
|
||||||
git_remote *remote = NULL;
|
git_remote *remote = NULL;
|
||||||
const git_refspec *refspec;
|
const git_refspec *refspec;
|
||||||
git_config *config, *repo_config;
|
git_config *config;
|
||||||
|
|
||||||
assert(out && refname);
|
assert(out && refname);
|
||||||
|
|
||||||
@ -341,10 +341,7 @@ int git_branch_upstream_name(
|
|||||||
if (!git_reference__is_branch(refname))
|
if (!git_reference__is_branch(refname))
|
||||||
return not_a_local_branch(refname);
|
return not_a_local_branch(refname);
|
||||||
|
|
||||||
if ((error = git_repository_config__weakptr(&repo_config, repo)) < 0)
|
if ((error = git_repository_config_snapshot(&config, repo)) < 0)
|
||||||
return error;
|
|
||||||
|
|
||||||
if ((error = git_config_snapshot(&config, repo_config)) < 0)
|
|
||||||
return error;
|
return error;
|
||||||
|
|
||||||
if ((error = retrieve_upstream_configuration(
|
if ((error = retrieve_upstream_configuration(
|
||||||
|
@ -234,14 +234,11 @@ static int git_diff_driver_load(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if you can't read config for repo, just use default driver */
|
/* if you can't read config for repo, just use default driver */
|
||||||
if (git_repository_config__weakptr(&repo_cfg, repo) < 0) {
|
if (git_repository_config_snapshot(&cfg, repo) < 0) {
|
||||||
giterr_clear();
|
giterr_clear();
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((error = git_config_snapshot(&cfg, repo_cfg)) < 0)
|
|
||||||
return error;
|
|
||||||
|
|
||||||
drv = git__calloc(1, sizeof(git_diff_driver) + namelen + 1);
|
drv = git__calloc(1, sizeof(git_diff_driver) + namelen + 1);
|
||||||
GITERR_CHECK_ALLOC(drv);
|
GITERR_CHECK_ALLOC(drv);
|
||||||
drv->type = DIFF_DRIVER_AUTO;
|
drv->type = DIFF_DRIVER_AUTO;
|
||||||
|
@ -86,14 +86,11 @@ static unsigned name_hash(const char *name)
|
|||||||
|
|
||||||
static int packbuilder_config(git_packbuilder *pb)
|
static int packbuilder_config(git_packbuilder *pb)
|
||||||
{
|
{
|
||||||
git_config *config, *repo_config;
|
git_config *config;
|
||||||
int ret;
|
int ret;
|
||||||
int64_t val;
|
int64_t val;
|
||||||
|
|
||||||
if ((ret = git_repository_config__weakptr(&repo_config, pb->repo)) < 0)
|
if ((ret = git_repository_config_snapshot(&config, pb->repo)) < 0)
|
||||||
return ret;
|
|
||||||
|
|
||||||
if ((ret = git_config_snapshot(&config, repo_config)) < 0)
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
#define config_get(KEY,DST,DFLT) do { \
|
#define config_get(KEY,DST,DFLT) do { \
|
||||||
|
@ -347,7 +347,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
|
|||||||
git_buf buf = GIT_BUF_INIT;
|
git_buf buf = GIT_BUF_INIT;
|
||||||
const char *val;
|
const char *val;
|
||||||
int error = 0;
|
int error = 0;
|
||||||
git_config *config, *repo_config;
|
git_config *config;
|
||||||
struct refspec_cb_data data = { NULL };
|
struct refspec_cb_data data = { NULL };
|
||||||
bool optional_setting_found = false, found;
|
bool optional_setting_found = false, found;
|
||||||
|
|
||||||
@ -356,10 +356,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
|
|||||||
if ((error = ensure_remote_name_is_valid(name)) < 0)
|
if ((error = ensure_remote_name_is_valid(name)) < 0)
|
||||||
return error;
|
return error;
|
||||||
|
|
||||||
if (git_repository_config__weakptr(&repo_config, repo) < 0)
|
if ((error = git_repository_config_snapshot(&config, repo)) < 0)
|
||||||
return -1;
|
|
||||||
|
|
||||||
if ((error = git_config_snapshot(&config, repo_config)) < 0)
|
|
||||||
return error;
|
return error;
|
||||||
|
|
||||||
remote = git__malloc(sizeof(git_remote));
|
remote = git__malloc(sizeof(git_remote));
|
||||||
|
@ -439,7 +439,7 @@ int git_repository_open_ext(
|
|||||||
int error;
|
int error;
|
||||||
git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT;
|
git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT;
|
||||||
git_repository *repo;
|
git_repository *repo;
|
||||||
git_config *repo_config, *config;
|
git_config *config;
|
||||||
|
|
||||||
if (repo_ptr)
|
if (repo_ptr)
|
||||||
*repo_ptr = NULL;
|
*repo_ptr = NULL;
|
||||||
@ -454,10 +454,7 @@ int git_repository_open_ext(
|
|||||||
repo->path_repository = git_buf_detach(&path);
|
repo->path_repository = git_buf_detach(&path);
|
||||||
GITERR_CHECK_ALLOC(repo->path_repository);
|
GITERR_CHECK_ALLOC(repo->path_repository);
|
||||||
|
|
||||||
if ((error = git_repository_config__weakptr(&repo_config, repo)) < 0)
|
if ((error = git_repository_config_snapshot(&config, repo)) < 0)
|
||||||
return error;
|
|
||||||
|
|
||||||
if ((error = git_config_snapshot(&config, repo_config)) < 0)
|
|
||||||
return error;
|
return error;
|
||||||
|
|
||||||
if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0)
|
if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0)
|
||||||
@ -624,6 +621,16 @@ int git_repository_config(git_config **out, git_repository *repo)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int git_repository_config_snapshot(git_config **out, git_repository *repo)
|
||||||
|
{
|
||||||
|
git_config *weak;
|
||||||
|
|
||||||
|
if (git_repository_config__weakptr(&weak, repo) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return git_config_snapshot(out, weak);
|
||||||
|
}
|
||||||
|
|
||||||
void git_repository_set_config(git_repository *repo, git_config *config)
|
void git_repository_set_config(git_repository *repo, git_config *config)
|
||||||
{
|
{
|
||||||
assert(repo && config);
|
assert(repo && config);
|
||||||
|
@ -141,13 +141,10 @@ int git_signature_now(git_signature **sig_out, const char *name, const char *ema
|
|||||||
int git_signature_default(git_signature **out, git_repository *repo)
|
int git_signature_default(git_signature **out, git_repository *repo)
|
||||||
{
|
{
|
||||||
int error;
|
int error;
|
||||||
git_config *cfg, *repo_cfg;
|
git_config *cfg;
|
||||||
const char *user_name, *user_email;
|
const char *user_name, *user_email;
|
||||||
|
|
||||||
if ((error = git_repository_config__weakptr(&repo_cfg, repo)) < 0)
|
if ((error = git_repository_config_snapshot(&cfg, repo)) < 0)
|
||||||
return error;
|
|
||||||
|
|
||||||
if ((error = git_config_snapshot(&cfg, repo_cfg)) < 0)
|
|
||||||
return error;
|
return error;
|
||||||
|
|
||||||
if (!(error = git_config_get_string(&user_name, cfg, "user.name")) &&
|
if (!(error = git_config_get_string(&user_name, cfg, "user.name")) &&
|
||||||
|
Loading…
Reference in New Issue
Block a user