1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 06:31:14 +02:00

getLfsEndpointUrl(): Use our RAII helpers

This commit is contained in:
Eelco Dolstra 2025-02-10 15:59:32 +01:00
parent c210efa9ae
commit d78daaa416
3 changed files with 31 additions and 29 deletions

View file

@ -1,4 +1,5 @@
#include "git-lfs-fetch.hh" #include "git-lfs-fetch.hh"
#include "git-utils.hh"
#include "filetransfer.hh" #include "filetransfer.hh"
#include "processes.hh" #include "processes.hh"
#include "url.hh" #include "url.hh"
@ -60,22 +61,23 @@ static std::string getLfsApiToken(const ParsedURL & url)
return queryResp["header"]["Authorization"].get<std::string>(); 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) static std::string getLfsEndpointUrl(git_repository * repo)
{ {
// FIXME: use Deleter GitConfig config;
git_config * config = nullptr; if (git_repository_config(Setter(config), repo)) {
if (git_repository_config(&config, repo)) { GitConfigEntry entry;
git_config_entry * entry = nullptr; if (!git_config_get_entry(Setter(entry), config.get(), "lfs.url")) {
if (!git_config_get_entry(&entry, config, "lfs.url")) {
auto value = std::string(entry->value); auto value = std::string(entry->value);
if (!value.empty()) { if (!value.empty()) {
debug("Found explicit lfs.url value: %s", value); debug("Found explicit lfs.url value: %s", value);
return value; return value;
} }
} }
git_config_entry_free(entry);
} }
git_config_free(config);
git_remote * remote = nullptr; git_remote * remote = nullptr;
if (git_remote_lookup(&remote, repo, "origin")) if (git_remote_lookup(&remote, repo, "origin"))
return ""; return "";

View file

@ -61,14 +61,6 @@ namespace nix {
struct GitSourceAccessor; 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_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_entry, Deleter<git_tree_entry_free>> TreeEntry;
typedef std::unique_ptr<git_tree, Deleter<git_tree_free>> Tree; 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_packbuilder, Deleter<git_packbuilder_free>> PackBuilder;
typedef std::unique_ptr<git_indexer, Deleter<git_indexer_free>> Indexer; 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) Hash toHash(const git_oid & oid)
{ {
#ifdef GIT_EXPERIMENTAL_SHA256 #ifdef GIT_EXPERIMENTAL_SHA256

View file

@ -127,4 +127,26 @@ struct GitRepo
ref<GitRepo> getTarballCache(); 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; }
};
} }