mirror of
https://github.com/NixOS/nix
synced 2025-06-25 02:21:16 +02:00
getLfsEndpointUrl(): Use our RAII helpers
This commit is contained in:
parent
c210efa9ae
commit
d78daaa416
3 changed files with 31 additions and 29 deletions
|
@ -1,4 +1,5 @@
|
|||
#include "git-lfs-fetch.hh"
|
||||
#include "git-utils.hh"
|
||||
#include "filetransfer.hh"
|
||||
#include "processes.hh"
|
||||
#include "url.hh"
|
||||
|
@ -60,22 +61,23 @@ static std::string getLfsApiToken(const ParsedURL & url)
|
|||
return queryResp["header"]["Authorization"].get<std::string>();
|
||||
}
|
||||
|
||||
typedef std::unique_ptr<git_config, Deleter<git_config_free>> GitConfig;
|
||||
typedef std::unique_ptr<git_config_entry, Deleter<git_config_entry_free>> GitConfigEntry;
|
||||
|
||||
static std::string getLfsEndpointUrl(git_repository * repo)
|
||||
{
|
||||
// FIXME: use Deleter
|
||||
git_config * config = nullptr;
|
||||
if (git_repository_config(&config, repo)) {
|
||||
git_config_entry * entry = nullptr;
|
||||
if (!git_config_get_entry(&entry, config, "lfs.url")) {
|
||||
GitConfig config;
|
||||
if (git_repository_config(Setter(config), repo)) {
|
||||
GitConfigEntry entry;
|
||||
if (!git_config_get_entry(Setter(entry), config.get(), "lfs.url")) {
|
||||
auto value = std::string(entry->value);
|
||||
if (!value.empty()) {
|
||||
debug("Found explicit lfs.url value: %s", value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
git_config_entry_free(entry);
|
||||
}
|
||||
git_config_free(config);
|
||||
|
||||
git_remote * remote = nullptr;
|
||||
if (git_remote_lookup(&remote, repo, "origin"))
|
||||
return "";
|
||||
|
|
|
@ -61,14 +61,6 @@ namespace nix {
|
|||
|
||||
struct GitSourceAccessor;
|
||||
|
||||
// Some wrapper types that ensure that the git_*_free functions get called.
|
||||
template<auto del>
|
||||
struct Deleter
|
||||
{
|
||||
template <typename T>
|
||||
void operator()(T * p) const { del(p); };
|
||||
};
|
||||
|
||||
typedef std::unique_ptr<git_repository, Deleter<git_repository_free>> Repository;
|
||||
typedef std::unique_ptr<git_tree_entry, Deleter<git_tree_entry_free>> TreeEntry;
|
||||
typedef std::unique_ptr<git_tree, Deleter<git_tree_free>> Tree;
|
||||
|
@ -86,20 +78,6 @@ typedef std::unique_ptr<git_odb, Deleter<git_odb_free>> ObjectDb;
|
|||
typedef std::unique_ptr<git_packbuilder, Deleter<git_packbuilder_free>> PackBuilder;
|
||||
typedef std::unique_ptr<git_indexer, Deleter<git_indexer_free>> Indexer;
|
||||
|
||||
// A helper to ensure that we don't leak objects returned by libgit2.
|
||||
template<typename T>
|
||||
struct Setter
|
||||
{
|
||||
T & t;
|
||||
typename T::pointer p = nullptr;
|
||||
|
||||
Setter(T & t) : t(t) { }
|
||||
|
||||
~Setter() { if (p) t = T(p); }
|
||||
|
||||
operator typename T::pointer * () { return &p; }
|
||||
};
|
||||
|
||||
Hash toHash(const git_oid & oid)
|
||||
{
|
||||
#ifdef GIT_EXPERIMENTAL_SHA256
|
||||
|
|
|
@ -127,4 +127,26 @@ struct GitRepo
|
|||
|
||||
ref<GitRepo> getTarballCache();
|
||||
|
||||
// A helper to ensure that the `git_*_free` functions get called.
|
||||
template<auto del>
|
||||
struct Deleter
|
||||
{
|
||||
template <typename T>
|
||||
void operator()(T * p) const { del(p); };
|
||||
};
|
||||
|
||||
// A helper to ensure that we don't leak objects returned by libgit2.
|
||||
template<typename T>
|
||||
struct Setter
|
||||
{
|
||||
T & t;
|
||||
typename T::pointer p = nullptr;
|
||||
|
||||
Setter(T & t) : t(t) { }
|
||||
|
||||
~Setter() { if (p) t = T(p); }
|
||||
|
||||
operator typename T::pointer * () { return &p; }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue