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

GitArchiveInputScheme: Restore the lastModified attribute

This commit is contained in:
Eelco Dolstra 2023-02-03 16:04:19 +01:00
parent 219510b6ab
commit 7b1cda9ca2
3 changed files with 36 additions and 21 deletions

View file

@ -243,7 +243,7 @@ static Repository openTarballCache()
}
}
Hash importTarball(Source & source)
TarballInfo importTarball(Source & source)
{
auto repo = openTarballCache();
@ -308,6 +308,8 @@ Hash importTarball(Source & source)
size_t componentsToStrip = 1;
time_t lastModified = 0;
for (;;) {
// FIXME: merge with extract_archive
struct archive_entry * entry;
@ -321,6 +323,8 @@ Hash importTarball(Source & source)
else
archive.check(r);
lastModified = std::max(lastModified, archive_entry_mtime(entry));
auto pathComponents = tokenizeString<std::vector<std::string>>(path, "/");
std::span<const std::string> pathComponents2{pathComponents};
@ -388,7 +392,10 @@ Hash importTarball(Source & source)
auto [oid, _name] = popBuilder();
return Hash::parseAny(git_oid_tostr_s(&oid), htSHA1);
return TarballInfo {
.treeHash = Hash::parseAny(git_oid_tostr_s(&oid), htSHA1),
.lastModified = lastModified
};
}
ref<InputAccessor> makeTarballCacheAccessor(const Hash & rev)

View file

@ -6,7 +6,13 @@ namespace nix {
ref<InputAccessor> makeGitInputAccessor(const CanonPath & path, const Hash & rev);
Hash importTarball(Source & source);
struct TarballInfo
{
Hash treeHash;
time_t lastModified;
};
TarballInfo importTarball(Source & source);
ref<InputAccessor> makeTarballCacheAccessor(const Hash & rev);

View file

@ -168,6 +168,8 @@ struct GitArchiveInputScheme : InputScheme
void checkLocks(const Input & specified, const Input & final) const override
{
InputScheme::checkLocks(specified, final);
if (auto prevTreeHash = getTreeHash(specified)) {
if (getTreeHash(final) != prevTreeHash)
throw Error("Git tree hash mismatch in input '%s', expected '%s'",
@ -201,7 +203,7 @@ struct GitArchiveInputScheme : InputScheme
virtual DownloadUrl getDownloadUrl(const Input & input) const = 0;
std::pair<Input, Hash> downloadArchive(ref<Store> store, Input input) const
std::pair<Input, TarballInfo> downloadArchive(ref<Store> store, Input input) const
{
if (!maybeGetStrAttr(input.attrs, "ref")) input.attrs.insert_or_assign("ref", "HEAD");
@ -214,13 +216,17 @@ struct GitArchiveInputScheme : InputScheme
auto cache = getCache();
auto treeHashKey = fmt("git-rev-to-tree-hash-%s", rev->gitRev());
auto lastModifiedKey = fmt("git-rev-to-last-modified-%s", rev->gitRev());
if (auto treeHashS = cache->queryFact(treeHashKey)) {
auto treeHash = Hash::parseAny(*treeHashS, htSHA1);
if (tarballCacheContains(treeHash))
return {std::move(input), treeHash};
else
debug("Git tree with hash '%s' has disappeared from the cache, refetching...", treeHash.gitRev());
if (auto lastModifiedS = cache->queryFact(lastModifiedKey)) {
auto treeHash = Hash::parseAny(*treeHashS, htSHA1);
auto lastModified = string2Int<time_t>(*lastModifiedS).value();
if (tarballCacheContains(treeHash))
return {std::move(input), TarballInfo { .treeHash = treeHash, .lastModified = lastModified }};
else
debug("Git tree with hash '%s' has disappeared from the cache, refetching...", treeHash.gitRev());
}
}
/* Stream the tarball into the tarball cache. */
@ -232,26 +238,22 @@ struct GitArchiveInputScheme : InputScheme
getFileTransfer()->download(std::move(req), sink);
});
auto treeHash = importTarball(*source);
auto tarballInfo = importTarball(*source);
cache->upsertFact(treeHashKey, treeHash.gitRev());
cache->upsertFact(treeHashKey, tarballInfo.treeHash.gitRev());
cache->upsertFact(lastModifiedKey, std::to_string(tarballInfo.lastModified));
return {std::move(input), treeHash};
return {std::move(input), tarballInfo};
}
std::pair<ref<InputAccessor>, Input> getAccessor(ref<Store> store, const Input & _input) const override
{
auto [input, treeHash] = downloadArchive(store, _input);
auto [input, tarballInfo] = downloadArchive(store, _input);
input.attrs.insert_or_assign("treeHash", treeHash.gitRev());
input.attrs.insert_or_assign("treeHash", tarballInfo.treeHash.gitRev());
input.attrs.insert_or_assign("lastModified", uint64_t(tarballInfo.lastModified));
auto accessor = makeTarballCacheAccessor(treeHash);
#if 0
auto lastModified = accessor->getLastModified();
assert(lastModified);
input.attrs.insert_or_assign("lastModified", uint64_t(*lastModified));
#endif
auto accessor = makeTarballCacheAccessor(tarballInfo.treeHash);
accessor->setPathDisplay("«" + input.to_string() + "»");