forked from lix-project/lix
1bdeef8395
- This way we improve error messages on infinite recursion - Demo: ```nix let x = builtins.fetchMercurial x; in x ``` - Before: ```bash $ nix-instantiate --show-trace --strict error: infinite recursion encountered ``` - After: ```bash nix-instantiate --show-trace --strict error: infinite recursion encountered at /data/github/kamadorueda/test/default.nix:2:7: 1| let 2| x = builtins.fetchMercurial x; | ^ 3| in ``` Mentions: #3505
93 lines
3.2 KiB
C++
93 lines
3.2 KiB
C++
#include "primops.hh"
|
|
#include "eval-inline.hh"
|
|
#include "store-api.hh"
|
|
#include "fetchers.hh"
|
|
#include "url.hh"
|
|
#include "url-parts.hh"
|
|
|
|
namespace nix {
|
|
|
|
static void prim_fetchMercurial(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
|
{
|
|
std::string url;
|
|
std::optional<Hash> rev;
|
|
std::optional<std::string> ref;
|
|
std::string name = "source";
|
|
PathSet context;
|
|
|
|
state.forceValue(*args[0], pos);
|
|
|
|
if (args[0]->type() == nAttrs) {
|
|
|
|
state.forceAttrs(*args[0], pos);
|
|
|
|
for (auto & attr : *args[0]->attrs) {
|
|
string n(attr.name);
|
|
if (n == "url")
|
|
url = state.coerceToString(*attr.pos, *attr.value, context, false, false);
|
|
else if (n == "rev") {
|
|
// Ugly: unlike fetchGit, here the "rev" attribute can
|
|
// be both a revision or a branch/tag name.
|
|
auto value = state.forceStringNoCtx(*attr.value, *attr.pos);
|
|
if (std::regex_match(value, revRegex))
|
|
rev = Hash::parseAny(value, htSHA1);
|
|
else
|
|
ref = value;
|
|
}
|
|
else if (n == "name")
|
|
name = state.forceStringNoCtx(*attr.value, *attr.pos);
|
|
else
|
|
throw EvalError({
|
|
.msg = hintfmt("unsupported argument '%s' to 'fetchMercurial'", attr.name),
|
|
.errPos = *attr.pos
|
|
});
|
|
}
|
|
|
|
if (url.empty())
|
|
throw EvalError({
|
|
.msg = hintfmt("'url' argument required"),
|
|
.errPos = pos
|
|
});
|
|
|
|
} else
|
|
url = state.coerceToString(pos, *args[0], context, false, false);
|
|
|
|
// FIXME: git externals probably can be used to bypass the URI
|
|
// whitelist. Ah well.
|
|
state.checkURI(url);
|
|
|
|
if (evalSettings.pureEval && !rev)
|
|
throw Error("in pure evaluation mode, 'fetchMercurial' requires a Mercurial revision");
|
|
|
|
fetchers::Attrs attrs;
|
|
attrs.insert_or_assign("type", "hg");
|
|
attrs.insert_or_assign("url", url.find("://") != std::string::npos ? url : "file://" + url);
|
|
attrs.insert_or_assign("name", name);
|
|
if (ref) attrs.insert_or_assign("ref", *ref);
|
|
if (rev) attrs.insert_or_assign("rev", rev->gitRev());
|
|
auto input = fetchers::Input::fromAttrs(std::move(attrs));
|
|
|
|
// FIXME: use name
|
|
auto [tree, input2] = input.fetch(state.store);
|
|
|
|
state.mkAttrs(v, 8);
|
|
auto storePath = state.store->printStorePath(tree.storePath);
|
|
mkString(*state.allocAttr(v, state.sOutPath), storePath, PathSet({storePath}));
|
|
if (input2.getRef())
|
|
mkString(*state.allocAttr(v, state.symbols.create("branch")), *input2.getRef());
|
|
// Backward compatibility: set 'rev' to
|
|
// 0000000000000000000000000000000000000000 for a dirty tree.
|
|
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 (auto revCount = input2.getRevCount())
|
|
mkInt(*state.allocAttr(v, state.symbols.create("revCount")), *revCount);
|
|
v.attrs->sort();
|
|
|
|
state.allowPath(tree.storePath);
|
|
}
|
|
|
|
static RegisterPrimOp r_fetchMercurial("fetchMercurial", 1, prim_fetchMercurial);
|
|
|
|
}
|