Took ref and rev out of FlakeRef

This commit is contained in:
Nick Van den Broeck 2019-04-06 20:45:35 +02:00
parent 3ec0c82fab
commit f39670c631
3 changed files with 41 additions and 87 deletions

View file

@ -201,10 +201,8 @@ static FlakeRef lookupFlake(EvalState & state, const FlakeRef & flakeRef,
auto newRef = FlakeRef(i->second.ref); auto newRef = FlakeRef(i->second.ref);
if (!newRef.isDirect()) if (!newRef.isDirect())
throw Error("found indirect flake URI '%s' in the flake registry", i->second.ref.to_string()); throw Error("found indirect flake URI '%s' in the flake registry", i->second.ref.to_string());
if (refData->ref) if (flakeRef.ref) newRef.setRef(*flakeRef.ref);
newRef.setRef(*refData->ref); if (flakeRef.rev) newRef.setRev(*flakeRef.rev);
if (refData->rev)
newRef.setRev(*refData->rev);
return newRef; return newRef;
} }
} }
@ -220,13 +218,13 @@ struct FlakeSourceInfo
std::optional<uint64_t> revCount; std::optional<uint64_t> revCount;
}; };
static FlakeSourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef, bool impureIsAllowed = false) static FlakeSourceInfo fetchFlake(EvalState & state, const FlakeRef flakeRef, bool impureIsAllowed = false)
{ {
FlakeRef directFlakeRef = lookupFlake(state, flakeRef, state.getFlakeRegistries()); FlakeRef fRef = lookupFlake(state, flakeRef, state.getFlakeRegistries());
if (auto refData = std::get_if<FlakeRef::IsGitHub>(&directFlakeRef.data)) { if (auto refData = std::get_if<FlakeRef::IsGitHub>(&fRef.data)) {
if (evalSettings.pureEval && !impureIsAllowed && !directFlakeRef.isImmutable()) if (evalSettings.pureEval && !impureIsAllowed && !fRef.isImmutable())
throw Error("requested to fetch FlakeRef '%s' purely, which is mutable", directFlakeRef.to_string()); throw Error("requested to fetch FlakeRef '%s' purely, which is mutable", fRef.to_string());
// FIXME: use regular /archive URLs instead? api.github.com // FIXME: use regular /archive URLs instead? api.github.com
// might have stricter rate limits. // might have stricter rate limits.
@ -235,14 +233,11 @@ static FlakeSourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef,
auto url = fmt("https://api.github.com/repos/%s/%s/tarball/%s", auto url = fmt("https://api.github.com/repos/%s/%s/tarball/%s",
refData->owner, refData->repo, refData->owner, refData->repo,
refData->rev fRef.rev ? fRef.rev->to_string(Base16, false)
? refData->rev->to_string(Base16, false) : fRef.ref ? *fRef.ref : "master");
: refData->ref
? *refData->ref
: "master");
auto result = getDownloader()->downloadCached(state.store, url, true, "source", auto result = getDownloader()->downloadCached(state.store, url, true, "source",
Hash(), nullptr, refData->rev ? 1000000000 : settings.tarballTtl); Hash(), nullptr, fRef.rev ? 1000000000 : settings.tarballTtl);
if (!result.etag) if (!result.etag)
throw Error("did not receive an ETag header from '%s'", url); throw Error("did not receive an ETag header from '%s'", url);
@ -257,9 +252,9 @@ static FlakeSourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef,
return info; return info;
} }
else if (auto refData = std::get_if<FlakeRef::IsGit>(&directFlakeRef.data)) { else if (auto refData = std::get_if<FlakeRef::IsGit>(&fRef.data)) {
auto gitInfo = exportGit(state.store, refData->uri, refData->ref, auto gitInfo = exportGit(state.store, refData->uri, fRef.ref,
refData->rev ? refData->rev->to_string(Base16, false) : "", "source"); fRef.rev ? fRef.rev->to_string(Base16, false) : "", "source");
FlakeSourceInfo info; FlakeSourceInfo info;
info.storePath = gitInfo.storePath; info.storePath = gitInfo.storePath;
info.rev = Hash(gitInfo.rev, htSHA1); info.rev = Hash(gitInfo.rev, htSHA1);

View file

@ -58,11 +58,11 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
IsFlakeId d; IsFlakeId d;
d.id = match[1]; d.id = match[1];
if (match[2].matched) if (match[2].matched)
d.rev = Hash(match[2], htSHA1); rev = Hash(match[2], htSHA1);
else if (match[3].matched) { else if (match[3].matched) {
d.ref = match[3]; ref = match[3];
if (match[4].matched) if (match[4].matched)
d.rev = Hash(match[4], htSHA1); rev = Hash(match[4], htSHA1);
} }
data = d; data = d;
} }
@ -72,9 +72,9 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
d.owner = match[1]; d.owner = match[1];
d.repo = match[2]; d.repo = match[2];
if (match[3].matched) if (match[3].matched)
d.rev = Hash(match[3], htSHA1); rev = Hash(match[3], htSHA1);
else if (match[4].matched) { else if (match[4].matched) {
d.ref = match[4]; ref = match[4];
} }
data = d; data = d;
} }
@ -92,16 +92,16 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
if (name == "rev") { if (name == "rev") {
if (!std::regex_match(value, revRegex)) if (!std::regex_match(value, revRegex))
throw Error("invalid Git revision '%s'", value); throw Error("invalid Git revision '%s'", value);
d.rev = Hash(value, htSHA1); rev = Hash(value, htSHA1);
} else if (name == "ref") { } else if (name == "ref") {
if (!std::regex_match(value, refRegex2)) if (!std::regex_match(value, refRegex2))
throw Error("invalid Git ref '%s'", value); throw Error("invalid Git ref '%s'", value);
d.ref = value; ref = value;
} else } else
// FIXME: should probably pass through unknown parameters // FIXME: should probably pass through unknown parameters
throw Error("invalid Git flake reference parameter '%s', in '%s'", name, uri); throw Error("invalid Git flake reference parameter '%s', in '%s'", name, uri);
} }
if (d.rev && !d.ref) if (rev && !ref)
throw Error("flake URI '%s' lacks a Git ref", uri); throw Error("flake URI '%s' lacks a Git ref", uri);
data = d; data = d;
} }
@ -118,27 +118,18 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
std::string FlakeRef::to_string() const std::string FlakeRef::to_string() const
{ {
if (auto refData = std::get_if<FlakeRef::IsFlakeId>(&data)) { std::string string;
return if (auto refData = std::get_if<FlakeRef::IsFlakeId>(&data))
"flake:" + refData->id + string = "flake:" + refData->id;
(refData->ref ? "/" + *refData->ref : "") +
(refData->rev ? "/" + refData->rev->to_string(Base16, false) : "");
}
else if (auto refData = std::get_if<FlakeRef::IsGitHub>(&data)) { else if (auto refData = std::get_if<FlakeRef::IsGitHub>(&data)) {
assert(!refData->ref || !refData->rev); assert(!ref || !rev);
return string = "github:" + refData->owner + "/" + refData->repo;
"github:" + refData->owner + "/" + refData->repo +
(refData->ref ? "/" + *refData->ref : "") +
(refData->rev ? "/" + refData->rev->to_string(Base16, false) : "");
} }
else if (auto refData = std::get_if<FlakeRef::IsGit>(&data)) { else if (auto refData = std::get_if<FlakeRef::IsGit>(&data)) {
assert(refData->ref || !refData->rev); assert(ref || !rev);
return string = refData->uri;
refData->uri +
(refData->ref ? "?ref=" + *refData->ref : "") +
(refData->rev ? "&rev=" + refData->rev->to_string(Base16, false) : "");
} }
else if (auto refData = std::get_if<FlakeRef::IsPath>(&data)) { else if (auto refData = std::get_if<FlakeRef::IsPath>(&data)) {
@ -146,38 +137,22 @@ std::string FlakeRef::to_string() const
} }
else abort(); else abort();
string += (ref ? "/" + *ref : "") +
(rev ? "/" + rev->to_string(Base16, false) : "");
return string;
} }
bool FlakeRef::isImmutable() const bool FlakeRef::isImmutable() const
{ {
if (auto refData = std::get_if<FlakeRef::IsFlakeId>(&data)) return (bool) rev;
return (bool) refData->rev;
else if (auto refData = std::get_if<FlakeRef::IsGitHub>(&data))
return (bool) refData->rev;
else if (auto refData = std::get_if<FlakeRef::IsGit>(&data))
return (bool) refData->rev;
else if (std::get_if<FlakeRef::IsPath>(&data))
return false;
else abort();
} }
FlakeRef FlakeRef::baseRef() const // Removes the ref and rev from a FlakeRef. FlakeRef FlakeRef::baseRef() const // Removes the ref and rev from a FlakeRef.
{ {
FlakeRef result(*this); FlakeRef result(*this);
if (auto refData = std::get_if<FlakeRef::IsGitHub>(&result.data)) { result.ref = std::nullopt;
refData->ref = std::nullopt; result.rev = std::nullopt;
refData->rev = std::nullopt;
} else if (auto refData = std::get_if<FlakeRef::IsGit>(&result.data)) {
refData->ref = std::nullopt;
refData->rev = std::nullopt;
} else if (auto refData = std::get_if<FlakeRef::IsGit>(&result.data)) {
refData->ref = std::nullopt;
refData->rev = std::nullopt;
}
return result; return result;
} }
} }

View file

@ -101,25 +101,23 @@ typedef std::string FlakeId;
struct FlakeRef struct FlakeRef
{ {
std::optional<std::string> ref;
std::optional<Hash> rev;
struct IsFlakeId struct IsFlakeId
{ {
FlakeId id; FlakeId id;
std::optional<std::string> ref;
std::optional<Hash> rev;
}; };
struct IsGitHub struct IsGitHub
{ {
std::string owner, repo; std::string owner, repo;
std::optional<std::string> ref;
std::optional<Hash> rev;
}; };
// Git, Tarball
struct IsGit struct IsGit
{ {
std::string uri; std::string uri;
std::optional<std::string> ref;
std::optional<Hash> rev;
}; };
struct IsPath struct IsPath
@ -161,22 +159,8 @@ struct FlakeRef
FlakeRef baseRef() const; FlakeRef baseRef() const;
void setRef(std::optional<std::string> ref) { void setRef(std::optional<std::string> ref) { ref = ref; }
if (auto refData = std::get_if<IsGit>(&data))
refData->ref = ref;
else if (auto refData = std::get_if<IsGitHub>(&data))
refData->ref = ref;
else if (auto refData = std::get_if<IsFlakeId>(&data))
refData->ref = ref;
}
void setRev(std::optional<Hash> rev) { void setRev(std::optional<Hash> rev) { rev = rev; }
if (auto refData = std::get_if<IsGit>(&data))
refData->rev = rev;
else if (auto refData = std::get_if<IsGitHub>(&data))
refData->rev = rev;
else if (auto refData = std::get_if<IsFlakeId>(&data))
refData->rev = rev;
}
}; };
} }