mirror of
https://github.com/NixOS/nix.git
synced 2025-11-29 05:31:00 +01:00
Check tarball cache validity
This commit is contained in:
parent
4142982140
commit
ca26ce994b
5 changed files with 48 additions and 11 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
15
src/libfetchers/git-utils.hh
Normal file
15
src/libfetchers/git-utils.hh
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
#include "input-accessor.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
ref<InputAccessor> makeGitInputAccessor(const CanonPath & path, const Hash & rev);
|
||||
|
||||
Hash importTarball(Source & source);
|
||||
|
||||
ref<InputAccessor> makeTarballCacheAccessor(const Hash & rev);
|
||||
|
||||
bool tarballCacheContains(const Hash & treeHash);
|
||||
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@
|
|||
#include "util.hh"
|
||||
#include "git.hh"
|
||||
#include "fs-input-accessor.hh"
|
||||
#include "git-utils.hh"
|
||||
|
||||
#include "fetch-settings.hh"
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#include "fetch-settings.hh"
|
||||
#include "input-accessor.hh"
|
||||
#include "tarball.hh"
|
||||
#include "git-utils.hh"
|
||||
|
||||
#include <optional>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
|
@ -196,8 +197,10 @@ struct GitArchiveInputScheme : InputScheme
|
|||
|
||||
if (auto treeHashS = cache->queryFact(treeHashKey)) {
|
||||
auto treeHash = Hash::parseAny(*treeHashS, htSHA1);
|
||||
// FIXME: verify that treeHash exists in the tarball cache.
|
||||
return {std::move(input), treeHash};
|
||||
if (tarballCacheContains(treeHash))
|
||||
return {std::move(input), treeHash};
|
||||
else
|
||||
debug("Git tree with hash '%s' has disappeared from the cache, refetching...", treeHash.gitRev());
|
||||
}
|
||||
|
||||
/* Stream the tarball into the tarball cache. */
|
||||
|
|
@ -272,6 +275,7 @@ struct GitHubInputScheme : GitArchiveInputScheme
|
|||
return getStrAttr(input.attrs, "repo");
|
||||
}
|
||||
|
||||
/* .commit.tree.sha, .commit.committer.date */
|
||||
Hash getRevFromRef(nix::ref<Store> store, const Input & input) const override
|
||||
{
|
||||
auto host = getHost(input);
|
||||
|
|
|
|||
|
|
@ -117,12 +117,6 @@ ref<InputAccessor> makePatchingInputAccessor(
|
|||
ref<InputAccessor> next,
|
||||
const std::vector<std::string> & patches);
|
||||
|
||||
ref<InputAccessor> makeGitInputAccessor(const CanonPath & path, const Hash & rev);
|
||||
|
||||
Hash importTarball(Source & source);
|
||||
|
||||
ref<InputAccessor> makeTarballCacheAccessor(const Hash & rev);
|
||||
|
||||
struct SourcePath
|
||||
{
|
||||
ref<InputAccessor> accessor;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue