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

Factor out TreeInfo

This commit is contained in:
Eelco Dolstra 2020-02-01 16:41:54 +01:00
parent 5d70b454be
commit 8451298b35
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
13 changed files with 100 additions and 68 deletions

View file

@ -83,11 +83,11 @@ std::pair<Tree, std::shared_ptr<const Input>> Input::fetchTree(ref<Store> store)
if (tree.actualPath == "")
tree.actualPath = store->toRealPath(store->printStorePath(tree.storePath));
if (!tree.narHash)
tree.narHash = store->queryPathInfo(tree.storePath)->narHash;
if (!tree.info.narHash)
tree.info.narHash = store->queryPathInfo(tree.storePath)->narHash;
if (input->narHash)
assert(input->narHash == tree.narHash);
assert(input->narHash == tree.info.narHash);
if (narHash && narHash != input->narHash)
throw Error("NAR hash mismatch in input '%s', expected '%s', got '%s'",

View file

@ -3,6 +3,7 @@
#include "types.hh"
#include "hash.hh"
#include "path.hh"
#include "tree-info.hh"
#include <memory>
#include <variant>
@ -19,10 +20,7 @@ struct Tree
{
Path actualPath;
StorePath storePath;
Hash narHash;
std::optional<Hash> rev;
std::optional<uint64_t> revCount;
std::optional<time_t> lastModified;
TreeInfo info;
};
struct Input : std::enable_shared_from_this<Input>

View file

@ -28,11 +28,11 @@ static void cacheGitInfo(Store & store, const std::string & name, const Tree & t
nlohmann::json json;
json["storePath"] = store.printStorePath(tree.storePath);
json["name"] = name;
json["rev"] = tree.rev->gitRev();
json["revCount"] = *tree.revCount;
json["lastModified"] = *tree.lastModified;
json["rev"] = tree.info.rev->gitRev();
json["revCount"] = *tree.info.revCount;
json["lastModified"] = *tree.info.lastModified;
auto cacheInfoPath = getCacheInfoPathFor(name, *tree.rev);
auto cacheInfoPath = getCacheInfoPathFor(name, *tree.info.rev);
createDirs(dirOf(cacheInfoPath));
writeFile(cacheInfoPath, json.dump());
}
@ -53,9 +53,11 @@ static std::optional<Tree> lookupGitInfo(
Tree tree{
.actualPath = store->toRealPath(store->printStorePath(storePath)),
.storePath = std::move(storePath),
.rev = rev,
.revCount = json["revCount"],
.lastModified = json["lastModified"],
.info = TreeInfo {
.rev = rev,
.revCount = json["revCount"],
.lastModified = json["lastModified"],
}
};
return tree;
}
@ -237,10 +239,12 @@ struct GitInput : Input
auto tree = Tree {
.actualPath = store->printStorePath(storePath),
.storePath = std::move(storePath),
.revCount = haveCommits ? std::stoull(runProgram("git", true, { "-C", actualUrl, "rev-list", "--count", "HEAD" })) : 0,
// FIXME: maybe we should use the timestamp of the last
// modified dirty file?
.lastModified = haveCommits ? std::stoull(runProgram("git", true, { "-C", actualUrl, "log", "-1", "--format=%ct", "HEAD" })) : 0,
.info = TreeInfo {
.revCount = haveCommits ? std::stoull(runProgram("git", true, { "-C", actualUrl, "rev-list", "--count", "HEAD" })) : 0,
// FIXME: maybe we should use the timestamp of the last
// modified dirty file?
.lastModified = haveCommits ? std::stoull(runProgram("git", true, { "-C", actualUrl, "log", "-1", "--format=%ct", "HEAD" })) : 0,
}
};
return {std::move(tree), input};
@ -349,9 +353,11 @@ struct GitInput : Input
auto tree = Tree {
.actualPath = store->toRealPath(store->printStorePath(storePath)),
.storePath = std::move(storePath),
.rev = input->rev,
.revCount = revCount,
.lastModified = lastModified,
.info = TreeInfo {
.rev = input->rev,
.revCount = revCount,
.lastModified = lastModified
}
};
cacheGitInfo(*store, name, tree);

View file

@ -113,8 +113,10 @@ struct GitHubInput : Input
Tree result{
.actualPath = dresult.path,
.storePath = store->parseStorePath(dresult.storePath),
.rev = *rev,
.lastModified = *dresult.lastModified
.info = TreeInfo {
.rev = *rev,
.lastModified = *dresult.lastModified,
},
};
#if 0

View file

@ -215,12 +215,17 @@ struct MercurialInput : Input
if (store->isValidPath(storePath)) {
printTalkative("using cached Mercurial store path '%s'", store->printStorePath(storePath));
return {Tree {
.actualPath = store->printStorePath(storePath),
.storePath = std::move(storePath),
.rev = input->rev,
.revCount = revCount,
}, input};
return {
Tree {
.actualPath = store->printStorePath(storePath),
.storePath = std::move(storePath),
.info = TreeInfo {
.rev = input->rev,
.revCount = revCount,
},
},
input
};
}
} catch (SysError & e) {
@ -246,12 +251,17 @@ struct MercurialInput : Input
writeFile(storeLink, json.dump());
return {Tree {
.actualPath = store->printStorePath(storePath),
.storePath = std::move(storePath),
.rev = input->rev,
.revCount = revCount,
}, input};
return {
Tree {
.actualPath = store->printStorePath(storePath),
.storePath = std::move(storePath),
.info = TreeInfo {
.rev = input->rev,
.revCount = revCount
}
},
input
};
}
};

View file

@ -72,7 +72,9 @@ struct TarballInput : Input
Tree {
.actualPath = res.path,
.storePath = std::move(storePath),
.lastModified = *res.lastModified
.info = TreeInfo {
.lastModified = *res.lastModified,
},
},
input
};

View file

@ -0,0 +1,13 @@
#pragma once
namespace nix {
struct TreeInfo
{
Hash narHash;
std::optional<Hash> rev;
std::optional<uint64_t> revCount;
std::optional<time_t> lastModified;
};
}