Store SourceInfo in Flake and NonFlake

This deduplicates some shared fields. Factoring out the commonality is
useful in places like makeFlakeValue().
This commit is contained in:
Eelco Dolstra 2019-05-28 12:58:28 +02:00
parent de36cf3db9
commit 6d7efcfaeb
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
4 changed files with 29 additions and 32 deletions

View file

@ -368,19 +368,19 @@ Flake getFlake(EvalState & state, const FlakeRef & flakeRef, bool impureIsAllowe
// Get the `NonFlake` corresponding to a `FlakeRef`. // Get the `NonFlake` corresponding to a `FlakeRef`.
NonFlake getNonFlake(EvalState & state, const FlakeRef & flakeRef, FlakeAlias alias, bool impureIsAllowed = false) NonFlake getNonFlake(EvalState & state, const FlakeRef & flakeRef, FlakeAlias alias, bool impureIsAllowed = false)
{ {
SourceInfo sourceInfo = fetchFlake(state, flakeRef, impureIsAllowed); auto sourceInfo = fetchFlake(state, flakeRef, impureIsAllowed);
debug("got non-flake source '%s' with flakeref %s", sourceInfo.storePath, sourceInfo.resolvedRef.to_string()); debug("got non-flake source '%s' with flakeref %s", sourceInfo.storePath, sourceInfo.resolvedRef.to_string());
FlakeRef resolvedRef = sourceInfo.resolvedRef; FlakeRef resolvedRef = sourceInfo.resolvedRef;
NonFlake nonFlake(flakeRef, sourceInfo); NonFlake nonFlake(flakeRef, sourceInfo);
state.store->assertStorePath(nonFlake.storePath); state.store->assertStorePath(nonFlake.sourceInfo.storePath);
if (state.allowedPaths) if (state.allowedPaths)
state.allowedPaths->insert(nonFlake.storePath); state.allowedPaths->insert(nonFlake.sourceInfo.storePath);
nonFlake.hash = state.store->queryPathInfo(sourceInfo.storePath)->narHash; nonFlake.hash = state.store->queryPathInfo(nonFlake.sourceInfo.storePath)->narHash;
nonFlake.alias = alias; nonFlake.alias = alias;
@ -480,7 +480,7 @@ ResolvedFlake resolveFlake(EvalState & state, const FlakeRef & topRef, HandleLoc
if (!recreateLockFile (handleLockFile)) { if (!recreateLockFile (handleLockFile)) {
// If recreateLockFile, start with an empty lockfile // If recreateLockFile, start with an empty lockfile
oldLockFile = readLockFile(flake.storePath + "/flake.lock"); // FIXME: symlink attack oldLockFile = readLockFile(flake.sourceInfo.storePath + "/flake.lock"); // FIXME: symlink attack
} }
LockFile lockFile(oldLockFile); LockFile lockFile(oldLockFile);
@ -527,15 +527,16 @@ void callFlake(EvalState & state, const ResolvedFlake & resFlake, Value & v)
auto vNonFlake = state.allocAttr(v, nonFlake.alias); auto vNonFlake = state.allocAttr(v, nonFlake.alias);
state.mkAttrs(*vNonFlake, 4); state.mkAttrs(*vNonFlake, 4);
state.store->isValidPath(nonFlake.storePath); state.store->isValidPath(nonFlake.sourceInfo.storePath);
mkString(*state.allocAttr(*vNonFlake, state.sOutPath), nonFlake.storePath, {nonFlake.storePath}); mkString(*state.allocAttr(*vNonFlake, state.sOutPath),
nonFlake.sourceInfo.storePath, {nonFlake.sourceInfo.storePath});
// FIXME: add rev, shortRev, revCount, ... // FIXME: add rev, shortRev, revCount, ...
} }
mkString(*state.allocAttr(v, state.sDescription), resFlake.flake.description); mkString(*state.allocAttr(v, state.sDescription), resFlake.flake.description);
auto & path = resFlake.flake.storePath; auto & path = resFlake.flake.sourceInfo.storePath;
state.store->isValidPath(path); state.store->isValidPath(path);
mkString(*state.allocAttr(v, state.sOutPath), path, {path}); mkString(*state.allocAttr(v, state.sOutPath), path, {path});
@ -546,8 +547,8 @@ void callFlake(EvalState & state, const ResolvedFlake & resFlake, Value & v)
resFlake.flake.resolvedRef.rev->gitShortRev()); resFlake.flake.resolvedRef.rev->gitShortRev());
} }
if (resFlake.flake.revCount) if (resFlake.flake.sourceInfo.revCount)
mkInt(*state.allocAttr(v, state.symbols.create("revCount")), *resFlake.flake.revCount); mkInt(*state.allocAttr(v, state.symbols.create("revCount")), *resFlake.flake.sourceInfo.revCount);
auto vProvides = state.allocAttr(v, state.symbols.create("provides")); auto vProvides = state.allocAttr(v, state.symbols.create("provides"));
mkApp(*vProvides, *resFlake.flake.vProvides, v); mkApp(*vProvides, *resFlake.flake.vProvides, v);

View file

@ -94,17 +94,15 @@ struct Flake
FlakeRef originalRef; FlakeRef originalRef;
FlakeRef resolvedRef; FlakeRef resolvedRef;
std::string description; std::string description;
std::optional<uint64_t> revCount; SourceInfo sourceInfo;
Path storePath;
Hash hash; // content hash Hash hash; // content hash
std::vector<FlakeRef> requires; std::vector<FlakeRef> requires;
std::map<FlakeAlias, FlakeRef> nonFlakeRequires; std::map<FlakeAlias, FlakeRef> nonFlakeRequires;
Value * vProvides; // FIXME: gc Value * vProvides; // FIXME: gc
// date
unsigned int epoch; unsigned int epoch;
Flake(const FlakeRef & origRef, const SourceInfo & sourceInfo) : originalRef(origRef), Flake(const FlakeRef & origRef, const SourceInfo & sourceInfo) : originalRef(origRef),
resolvedRef(sourceInfo.resolvedRef), revCount(sourceInfo.revCount), storePath(sourceInfo.storePath) {}; resolvedRef(sourceInfo.resolvedRef), sourceInfo(sourceInfo) {};
}; };
struct NonFlake struct NonFlake
@ -112,12 +110,10 @@ struct NonFlake
FlakeAlias alias; FlakeAlias alias;
FlakeRef originalRef; FlakeRef originalRef;
FlakeRef resolvedRef; FlakeRef resolvedRef;
std::optional<uint64_t> revCount; SourceInfo sourceInfo;
Hash hash; Hash hash;
Path storePath; NonFlake(const FlakeRef & origRef, const SourceInfo & sourceInfo) :
// date originalRef(origRef), resolvedRef(sourceInfo.resolvedRef), sourceInfo(sourceInfo) {};
NonFlake(const FlakeRef & origRef, const SourceInfo & sourceInfo) : originalRef(origRef),
resolvedRef(sourceInfo.resolvedRef), revCount(sourceInfo.revCount), storePath(sourceInfo.storePath) {};
}; };
Flake getFlake(EvalState &, const FlakeRef &, bool impureIsAllowed); Flake getFlake(EvalState &, const FlakeRef &, bool impureIsAllowed);

View file

@ -80,9 +80,9 @@ void printFlakeInfo(const Flake & flake, bool json) {
j["branch"] = *flake.resolvedRef.ref; j["branch"] = *flake.resolvedRef.ref;
if (flake.resolvedRef.rev) if (flake.resolvedRef.rev)
j["revision"] = flake.resolvedRef.rev->to_string(Base16, false); j["revision"] = flake.resolvedRef.rev->to_string(Base16, false);
if (flake.revCount) if (flake.sourceInfo.revCount)
j["revCount"] = *flake.revCount; j["revCount"] = *flake.sourceInfo.revCount;
j["path"] = flake.storePath; j["path"] = flake.sourceInfo.storePath;
j["epoch"] = flake.epoch; j["epoch"] = flake.epoch;
std::cout << j.dump(4) << std::endl; std::cout << j.dump(4) << std::endl;
} else { } else {
@ -93,9 +93,9 @@ void printFlakeInfo(const Flake & flake, bool json) {
std::cout << "Branch: " << *flake.resolvedRef.ref << "\n"; std::cout << "Branch: " << *flake.resolvedRef.ref << "\n";
if (flake.resolvedRef.rev) if (flake.resolvedRef.rev)
std::cout << "Revision: " << flake.resolvedRef.rev->to_string(Base16, false) << "\n"; std::cout << "Revision: " << flake.resolvedRef.rev->to_string(Base16, false) << "\n";
if (flake.revCount) if (flake.sourceInfo.revCount)
std::cout << "Revcount: " << *flake.revCount << "\n"; std::cout << "Revcount: " << *flake.sourceInfo.revCount << "\n";
std::cout << "Path: " << flake.storePath << "\n"; std::cout << "Path: " << flake.sourceInfo.storePath << "\n";
std::cout << "Epoch: " << flake.epoch << "\n"; std::cout << "Epoch: " << flake.epoch << "\n";
} }
} }
@ -109,9 +109,9 @@ void printNonFlakeInfo(const NonFlake & nonFlake, bool json) {
j["branch"] = *nonFlake.resolvedRef.ref; j["branch"] = *nonFlake.resolvedRef.ref;
if (nonFlake.resolvedRef.rev) if (nonFlake.resolvedRef.rev)
j["revision"] = nonFlake.resolvedRef.rev->to_string(Base16, false); j["revision"] = nonFlake.resolvedRef.rev->to_string(Base16, false);
if (nonFlake.revCount) if (nonFlake.sourceInfo.revCount)
j["revCount"] = *nonFlake.revCount; j["revCount"] = *nonFlake.sourceInfo.revCount;
j["path"] = nonFlake.storePath; j["path"] = nonFlake.sourceInfo.storePath;
std::cout << j.dump(4) << std::endl; std::cout << j.dump(4) << std::endl;
} else { } else {
std::cout << "ID: " << nonFlake.alias << "\n"; std::cout << "ID: " << nonFlake.alias << "\n";
@ -120,9 +120,9 @@ void printNonFlakeInfo(const NonFlake & nonFlake, bool json) {
std::cout << "Branch: " << *nonFlake.resolvedRef.ref; std::cout << "Branch: " << *nonFlake.resolvedRef.ref;
if (nonFlake.resolvedRef.rev) if (nonFlake.resolvedRef.rev)
std::cout << "Revision: " << nonFlake.resolvedRef.rev->to_string(Base16, false) << "\n"; std::cout << "Revision: " << nonFlake.resolvedRef.rev->to_string(Base16, false) << "\n";
if (nonFlake.revCount) if (nonFlake.sourceInfo.revCount)
std::cout << "Revcount: " << *nonFlake.revCount << "\n"; std::cout << "Revcount: " << *nonFlake.sourceInfo.revCount << "\n";
std::cout << "Path: " << nonFlake.storePath << "\n"; std::cout << "Path: " << nonFlake.sourceInfo.storePath << "\n";
} }
} }

View file

@ -177,7 +177,7 @@ void makeFlakeClosureGCRoot(Store & store, const FlakeRef & origFlakeRef, const
const ResolvedFlake & flake = queue.front(); const ResolvedFlake & flake = queue.front();
queue.pop(); queue.pop();
if (!std::get_if<FlakeRef::IsPath>(&flake.flake.resolvedRef.data)) if (!std::get_if<FlakeRef::IsPath>(&flake.flake.resolvedRef.data))
closure.insert(flake.flake.storePath); closure.insert(flake.flake.sourceInfo.storePath);
for (const auto & dep : flake.flakeDeps) for (const auto & dep : flake.flakeDeps)
queue.push(dep.second); queue.push(dep.second);
} }