1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-29 21:50:58 +01:00

Check tarball cache validity

This commit is contained in:
Eelco Dolstra 2023-02-03 12:42:32 +01:00
parent 4142982140
commit ca26ce994b
5 changed files with 48 additions and 11 deletions

View file

@ -1,3 +1,4 @@
#include "git-utils.hh"
#include "input-accessor.hh"
#include <span>
@ -43,6 +44,14 @@ static Repository openRepo(const CanonPath & path)
return Repository(_repo);
}
git_oid hashToOID(const Hash & hash)
{
git_oid oid;
if (git_oid_fromstr(&oid, hash.gitRev().c_str()))
throw Error("cannot convert '%s' to a Git OID", hash.gitRev());
return oid;
}
struct GitInputAccessor : InputAccessor
{
Repository repo;
@ -51,9 +60,7 @@ struct GitInputAccessor : InputAccessor
GitInputAccessor(Repository && repo_, const Hash & rev)
: repo(std::move(repo_))
{
git_oid oid;
if (git_oid_fromstr(&oid, rev.gitRev().c_str()))
throw Error("cannot convert '%s' to a Git OID", rev.gitRev());
auto oid = hashToOID(rev);
git_object * obj = nullptr;
if (git_object_lookup(&obj, repo.get(), &oid, GIT_OBJECT_ANY)) {
@ -389,4 +396,20 @@ ref<InputAccessor> makeTarballCacheAccessor(const Hash & rev)
return make_ref<GitInputAccessor>(openTarballCache(), rev);
}
bool tarballCacheContains(const Hash & treeHash)
{
auto repo = openTarballCache();
auto oid = hashToOID(treeHash);
git_object * obj = nullptr;
if (auto errCode = git_object_lookup(&obj, repo.get(), &oid, GIT_OBJECT_TREE)) {
if (errCode == GIT_ENOTFOUND) return false;
auto err = git_error_last();
throw Error("getting Git object '%s': %s", treeHash.gitRev(), err->message);
}
return true;
}
}