mirror of
https://github.com/NixOS/nix
synced 2025-06-27 21:01:16 +02:00
Add date of last commit to SourceInfo
This is primarily useful for version string generation, where we need a monotonically increasing number. The revcount is the preferred thing to use, but isn't available for GitHub flakes (since it requires fetching the entire history). The last commit timestamp OTOH can be extracted from GitHub tarballs.
This commit is contained in:
parent
479757dc15
commit
0f840483c7
7 changed files with 61 additions and 30 deletions
|
@ -8,6 +8,8 @@
|
|||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <regex>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace nix {
|
||||
|
@ -232,6 +234,18 @@ static SourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef, bool
|
|||
if (evalSettings.pureEval && !impureIsAllowed && !resolvedRef.isImmutable())
|
||||
throw Error("requested to fetch mutable flake '%s' in pure mode", resolvedRef);
|
||||
|
||||
auto doGit = [&](const GitInfo & gitInfo) {
|
||||
FlakeRef ref(resolvedRef.baseRef());
|
||||
ref.ref = gitInfo.ref;
|
||||
ref.rev = gitInfo.rev;
|
||||
SourceInfo info(ref);
|
||||
info.storePath = gitInfo.storePath;
|
||||
info.revCount = gitInfo.revCount;
|
||||
info.narHash = state.store->queryPathInfo(info.storePath)->narHash;
|
||||
info.lastModified = gitInfo.lastModified;
|
||||
return info;
|
||||
};
|
||||
|
||||
// This only downloads only one revision of the repo, not the entire history.
|
||||
if (auto refData = std::get_if<FlakeRef::IsGitHub>(&resolvedRef.data)) {
|
||||
|
||||
|
@ -270,29 +284,13 @@ static SourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef, bool
|
|||
|
||||
// This downloads the entire git history
|
||||
else if (auto refData = std::get_if<FlakeRef::IsGit>(&resolvedRef.data)) {
|
||||
auto gitInfo = exportGit(state.store, refData->uri, resolvedRef.ref, resolvedRef.rev, "source");
|
||||
FlakeRef ref(resolvedRef.baseRef());
|
||||
ref.ref = gitInfo.ref;
|
||||
ref.rev = gitInfo.rev;
|
||||
SourceInfo info(ref);
|
||||
info.storePath = gitInfo.storePath;
|
||||
info.revCount = gitInfo.revCount;
|
||||
info.narHash = state.store->queryPathInfo(info.storePath)->narHash;
|
||||
return info;
|
||||
return doGit(exportGit(state.store, refData->uri, resolvedRef.ref, resolvedRef.rev, "source"));
|
||||
}
|
||||
|
||||
else if (auto refData = std::get_if<FlakeRef::IsPath>(&resolvedRef.data)) {
|
||||
if (!pathExists(refData->path + "/.git"))
|
||||
throw Error("flake '%s' does not reference a Git repository", refData->path);
|
||||
auto gitInfo = exportGit(state.store, refData->path, {}, {}, "source");
|
||||
FlakeRef ref(resolvedRef.baseRef());
|
||||
ref.ref = gitInfo.ref;
|
||||
ref.rev = gitInfo.rev;
|
||||
SourceInfo info(ref);
|
||||
info.storePath = gitInfo.storePath;
|
||||
info.revCount = gitInfo.revCount;
|
||||
info.narHash = state.store->queryPathInfo(info.storePath)->narHash;
|
||||
return info;
|
||||
return doGit(exportGit(state.store, refData->path, {}, {}, "source"));
|
||||
}
|
||||
|
||||
else abort();
|
||||
|
@ -529,6 +527,11 @@ static void emitSourceInfoAttrs(EvalState & state, const SourceInfo & sourceInfo
|
|||
|
||||
if (sourceInfo.revCount)
|
||||
mkInt(*state.allocAttr(vAttrs, state.symbols.create("revCount")), *sourceInfo.revCount);
|
||||
|
||||
if (sourceInfo.lastModified)
|
||||
mkString(*state.allocAttr(vAttrs, state.symbols.create("lastModified")),
|
||||
fmt("%s",
|
||||
std::put_time(std::gmtime(&*sourceInfo.lastModified), "%Y%m%d%H%M%S")));
|
||||
}
|
||||
|
||||
void callFlake(EvalState & state, const ResolvedFlake & resFlake, Value & v)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue