From 887730aab39dd63a37d49d72a07c6441ea3b2f92 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sat, 1 Feb 2020 23:54:20 +0100 Subject: [PATCH] Remove superfluous TreeInfo::rev field --- src/libexpr/flake/flake.cc | 16 ++++++++----- src/libexpr/flake/lockfile.cc | 6 ----- src/libexpr/primops/fetchGit.cc | 4 ++-- src/libexpr/primops/fetchMercurial.cc | 2 +- src/libstore/fetchers/git.cc | 33 +++++++++++++++------------ src/libstore/fetchers/github.cc | 1 - src/libstore/fetchers/mercurial.cc | 2 -- src/libstore/fetchers/tree-info.hh | 2 -- src/nix/flake.cc | 8 +++---- 9 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/libexpr/flake/flake.cc b/src/libexpr/flake/flake.cc index 59728e38d..27e390fcd 100644 --- a/src/libexpr/flake/flake.cc +++ b/src/libexpr/flake/flake.cc @@ -567,17 +567,21 @@ LockedFlake lockFlake( return LockedFlake { .flake = std::move(flake), .lockFile = std::move(newLockFile) }; } -static void emitSourceInfoAttrs(EvalState & state, const fetchers::Tree & sourceInfo, Value & vAttrs) +static void emitSourceInfoAttrs( + EvalState & state, + const FlakeRef & flakeRef, + const fetchers::Tree & sourceInfo, + Value & vAttrs) { assert(state.store->isValidPath(sourceInfo.storePath)); auto pathS = state.store->printStorePath(sourceInfo.storePath); mkString(*state.allocAttr(vAttrs, state.sOutPath), pathS, {pathS}); - if (sourceInfo.info.rev) { + if (auto rev = flakeRef.input->getRev()) { mkString(*state.allocAttr(vAttrs, state.symbols.create("rev")), - sourceInfo.info.rev->gitRev()); + rev->gitRev()); mkString(*state.allocAttr(vAttrs, state.symbols.create("shortRev")), - sourceInfo.info.rev->gitShortRev()); + rev->gitShortRev()); } if (sourceInfo.info.revCount) @@ -639,7 +643,7 @@ static void prim_callFlake(EvalState & state, const Pos & pos, Value * * args, V mkString(*state.allocAttr(v, state.sOutPath), pathS, {pathS}); - emitSourceInfoAttrs(state, sourceInfo, v); + emitSourceInfoAttrs(state, resolvedRef, sourceInfo, v); v.attrs->sort(); } @@ -672,7 +676,7 @@ void callFlake(EvalState & state, auto & vSourceInfo = *state.allocValue(); state.mkAttrs(vSourceInfo, 8); - emitSourceInfoAttrs(state, *flake.sourceInfo, vSourceInfo); + emitSourceInfoAttrs(state, flake.resolvedRef, *flake.sourceInfo, vSourceInfo); vSourceInfo.attrs->sort(); vInputs.attrs->push_back(Attr(state.sSelf, &vRes)); diff --git a/src/libexpr/flake/lockfile.cc b/src/libexpr/flake/lockfile.cc index 6773b2844..e2586ab93 100644 --- a/src/libexpr/flake/lockfile.cc +++ b/src/libexpr/flake/lockfile.cc @@ -58,10 +58,6 @@ static TreeInfo parseTreeInfo(const nlohmann::json & json) else throw Error("attribute 'narHash' missing in lock file"); - j = i2.find("rev"); - if (j != i2.end()) - info.rev = Hash((std::string) *j, htSHA1); - j = i2.find("revCount"); if (j != i2.end()) info.revCount = *j; @@ -97,8 +93,6 @@ static nlohmann::json treeInfoToJson(const TreeInfo & info) nlohmann::json json; assert(info.narHash); json["narHash"] = info.narHash.to_string(SRI); - if (info.rev) - json["rev"] = info.rev->gitRev(); if (info.revCount) json["revCount"] = *info.revCount; if (info.lastModified) diff --git a/src/libexpr/primops/fetchGit.cc b/src/libexpr/primops/fetchGit.cc index 2e24a79f7..18774371b 100644 --- a/src/libexpr/primops/fetchGit.cc +++ b/src/libexpr/primops/fetchGit.cc @@ -57,14 +57,14 @@ static void prim_fetchGit(EvalState & state, const Pos & pos, Value * * args, Va // FIXME: use name auto input = inputFromURL(parsedUrl); - auto tree = input->fetchTree(state.store).first; + auto [tree, input2] = input->fetchTree(state.store); state.mkAttrs(v, 8); auto storePath = state.store->printStorePath(tree.storePath); mkString(*state.allocAttr(v, state.sOutPath), storePath, PathSet({storePath})); // Backward compatibility: set 'rev' to // 0000000000000000000000000000000000000000 for a dirty tree. - auto rev2 = tree.info.rev.value_or(Hash(htSHA1)); + auto rev2 = input2->getRev().value_or(Hash(htSHA1)); mkString(*state.allocAttr(v, state.symbols.create("rev")), rev2.gitRev()); mkString(*state.allocAttr(v, state.symbols.create("shortRev")), rev2.gitShortRev()); assert(tree.info.revCount); diff --git a/src/libexpr/primops/fetchMercurial.cc b/src/libexpr/primops/fetchMercurial.cc index fea46e7c5..5d6b65c3b 100644 --- a/src/libexpr/primops/fetchMercurial.cc +++ b/src/libexpr/primops/fetchMercurial.cc @@ -73,7 +73,7 @@ static void prim_fetchMercurial(EvalState & state, const Pos & pos, Value * * ar mkString(*state.allocAttr(v, state.symbols.create("branch")), *input2->getRef()); // Backward compatibility: set 'rev' to // 0000000000000000000000000000000000000000 for a dirty tree. - auto rev2 = tree.info.rev.value_or(Hash(htSHA1)); + auto rev2 = input2->getRev().value_or(Hash(htSHA1)); mkString(*state.allocAttr(v, state.symbols.create("rev")), rev2.gitRev()); mkString(*state.allocAttr(v, state.symbols.create("shortRev")), std::string(rev2.gitRev(), 0, 12)); if (tree.info.revCount) diff --git a/src/libstore/fetchers/git.cc b/src/libstore/fetchers/git.cc index 4ad0f6f34..4f302cd0f 100644 --- a/src/libstore/fetchers/git.cc +++ b/src/libstore/fetchers/git.cc @@ -23,21 +23,25 @@ static Path getCacheInfoPathFor(const std::string & name, const Hash & rev) return cacheDir + "/" + linkName + ".link"; } -static void cacheGitInfo(Store & store, const std::string & name, const Tree & tree) +static void cacheGitInfo( + Store & store, + const std::string & name, + const Tree & tree, + const Hash & rev) { nlohmann::json json; json["storePath"] = store.printStorePath(tree.storePath); json["name"] = name; - json["rev"] = tree.info.rev->gitRev(); + json["rev"] = rev.gitRev(); json["revCount"] = *tree.info.revCount; json["lastModified"] = *tree.info.lastModified; - auto cacheInfoPath = getCacheInfoPathFor(name, *tree.info.rev); + auto cacheInfoPath = getCacheInfoPathFor(name, rev); createDirs(dirOf(cacheInfoPath)); writeFile(cacheInfoPath, json.dump()); } -static std::optional lookupGitInfo( +static std::optional> lookupGitInfo( ref store, const std::string & name, const Hash & rev) @@ -50,16 +54,14 @@ static std::optional lookupGitInfo( auto storePath = store->parseStorePath((std::string) json["storePath"]); if (store->isValidPath(storePath)) { - Tree tree{ + return {{rev, Tree{ .actualPath = store->toRealPath(store->printStorePath(storePath)), .storePath = std::move(storePath), .info = TreeInfo { - .rev = rev, .revCount = json["revCount"], .lastModified = json["lastModified"], } - }; - return tree; + }}}; } } catch (SysError & e) { @@ -181,8 +183,10 @@ struct GitInput : Input assert(!rev || rev->type == htSHA1); if (rev) { - if (auto tree = lookupGitInfo(store, name, *rev)) - return {std::move(*tree), input}; + if (auto tree = lookupGitInfo(store, name, *rev)) { + input->rev = tree->first; + return {std::move(tree->second), input}; + } } auto [isLocal, actualUrl_] = getActualUrl(); @@ -326,8 +330,10 @@ struct GitInput : Input input->rev = Hash(chomp(readFile(localRefFile)), htSHA1); } - if (auto tree = lookupGitInfo(store, name, *input->rev)) - return {std::move(*tree), input}; + if (auto tree = lookupGitInfo(store, name, *input->rev)) { + assert(*input->rev == tree->first); + return {std::move(tree->second), input}; + } // FIXME: check whether rev is an ancestor of ref. @@ -354,13 +360,12 @@ struct GitInput : Input .actualPath = store->toRealPath(store->printStorePath(storePath)), .storePath = std::move(storePath), .info = TreeInfo { - .rev = input->rev, .revCount = revCount, .lastModified = lastModified } }; - cacheGitInfo(*store, name, tree); + cacheGitInfo(*store, name, tree, *input->rev); return {std::move(tree), input}; } diff --git a/src/libstore/fetchers/github.cc b/src/libstore/fetchers/github.cc index a4d39d17d..0a000e83f 100644 --- a/src/libstore/fetchers/github.cc +++ b/src/libstore/fetchers/github.cc @@ -114,7 +114,6 @@ struct GitHubInput : Input .actualPath = dresult.path, .storePath = store->parseStorePath(dresult.storePath), .info = TreeInfo { - .rev = *rev, .lastModified = *dresult.lastModified, }, }; diff --git a/src/libstore/fetchers/mercurial.cc b/src/libstore/fetchers/mercurial.cc index b415b7944..0eb81d014 100644 --- a/src/libstore/fetchers/mercurial.cc +++ b/src/libstore/fetchers/mercurial.cc @@ -220,7 +220,6 @@ struct MercurialInput : Input .actualPath = store->printStorePath(storePath), .storePath = std::move(storePath), .info = TreeInfo { - .rev = input->rev, .revCount = revCount, }, }, @@ -256,7 +255,6 @@ struct MercurialInput : Input .actualPath = store->printStorePath(storePath), .storePath = std::move(storePath), .info = TreeInfo { - .rev = input->rev, .revCount = revCount } }, diff --git a/src/libstore/fetchers/tree-info.hh b/src/libstore/fetchers/tree-info.hh index 61500facb..28ead8588 100644 --- a/src/libstore/fetchers/tree-info.hh +++ b/src/libstore/fetchers/tree-info.hh @@ -5,7 +5,6 @@ namespace nix { struct TreeInfo { Hash narHash; - std::optional rev; // FIXME: remove std::optional revCount; std::optional lastModified; @@ -13,7 +12,6 @@ struct TreeInfo { return narHash == other.narHash - && rev == other.rev && revCount == other.revCount && lastModified == other.lastModified; } diff --git a/src/nix/flake.cc b/src/nix/flake.cc index d3c03e214..7924cec7c 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -84,8 +84,8 @@ static void printFlakeInfo(const Store & store, const Flake & flake) if (flake.description) std::cout << fmt("Description: %s\n", *flake.description); std::cout << fmt("Path: %s\n", store.printStorePath(flake.sourceInfo->storePath)); - if (flake.sourceInfo->info.rev) - std::cout << fmt("Revision: %s\n", flake.sourceInfo->info.rev->to_string(Base16, false)); + if (auto rev = flake.resolvedRef.input->getRev()) + std::cout << fmt("Revision: %s\n", rev->to_string(Base16, false)); if (flake.sourceInfo->info.revCount) std::cout << fmt("Revisions: %s\n", *flake.sourceInfo->info.revCount); if (flake.sourceInfo->info.lastModified) @@ -100,8 +100,8 @@ static nlohmann::json flakeToJson(const Store & store, const Flake & flake) j["description"] = *flake.description; j["edition"] = flake.edition; j["url"] = flake.resolvedRef.input->to_string(); - if (flake.sourceInfo->info.rev) - j["revision"] = flake.sourceInfo->info.rev->to_string(Base16, false); + if (auto rev = flake.resolvedRef.input->getRev()) + j["revision"] = rev->to_string(Base16, false); if (flake.sourceInfo->info.revCount) j["revCount"] = *flake.sourceInfo->info.revCount; if (flake.sourceInfo->info.lastModified)