diff --git a/src/libfetchers/git-lfs-fetch.cc b/src/libfetchers/git-lfs-fetch.cc index 27bd558c2..e9b690f8f 100644 --- a/src/libfetchers/git-lfs-fetch.cc +++ b/src/libfetchers/git-lfs-fetch.cc @@ -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(); } +typedef std::unique_ptr> GitConfig; +typedef std::unique_ptr> 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 ""; diff --git a/src/libfetchers/git-utils.cc b/src/libfetchers/git-utils.cc index 0f56b3c79..dca97a871 100644 --- a/src/libfetchers/git-utils.cc +++ b/src/libfetchers/git-utils.cc @@ -61,14 +61,6 @@ namespace nix { struct GitSourceAccessor; -// Some wrapper types that ensure that the git_*_free functions get called. -template -struct Deleter -{ - template - void operator()(T * p) const { del(p); }; -}; - typedef std::unique_ptr> Repository; typedef std::unique_ptr> TreeEntry; typedef std::unique_ptr> Tree; @@ -86,20 +78,6 @@ typedef std::unique_ptr> ObjectDb; typedef std::unique_ptr> PackBuilder; typedef std::unique_ptr> Indexer; -// A helper to ensure that we don't leak objects returned by libgit2. -template -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 diff --git a/src/libfetchers/git-utils.hh b/src/libfetchers/git-utils.hh index 04c8208ec..ba0e69146 100644 --- a/src/libfetchers/git-utils.hh +++ b/src/libfetchers/git-utils.hh @@ -127,4 +127,26 @@ struct GitRepo ref getTarballCache(); +// A helper to ensure that the `git_*_free` functions get called. +template +struct Deleter +{ + template + void operator()(T * p) const { del(p); }; +}; + +// A helper to ensure that we don't leak objects returned by libgit2. +template +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; } +}; + }