2019-02-12 19:23:11 +02:00
|
|
|
#include "flakeref.hh"
|
|
|
|
|
|
|
|
#include <regex>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
// A Git ref (i.e. branch or tag name).
|
|
|
|
const static std::string refRegex = "[a-zA-Z][a-zA-Z0-9_.-]*"; // FIXME: check
|
|
|
|
|
|
|
|
// A Git revision (a SHA-1 commit hash).
|
|
|
|
const static std::string revRegexS = "[0-9a-fA-F]{40}";
|
|
|
|
std::regex revRegex(revRegexS, std::regex::ECMAScript);
|
|
|
|
|
|
|
|
// A Git ref or revision.
|
|
|
|
const static std::string revOrRefRegex = "(?:(" + revRegexS + ")|(" + refRegex + "))";
|
|
|
|
|
|
|
|
// A rev ("e72daba8250068216d79d2aeef40d4d95aff6666"), or a ref
|
|
|
|
// optionally followed by a rev (e.g. "master" or
|
|
|
|
// "master/e72daba8250068216d79d2aeef40d4d95aff6666").
|
|
|
|
const static std::string refAndOrRevRegex = "(?:(" + revRegexS + ")|(?:(" + refRegex + ")(?:/(" + revRegexS + "))?))";
|
|
|
|
|
2019-03-21 10:30:16 +02:00
|
|
|
const static std::string flakeAlias = "[a-zA-Z][a-zA-Z0-9_-]*";
|
2019-02-12 19:23:11 +02:00
|
|
|
|
|
|
|
// GitHub references.
|
|
|
|
const static std::string ownerRegex = "[a-zA-Z][a-zA-Z0-9_-]*";
|
|
|
|
const static std::string repoRegex = "[a-zA-Z][a-zA-Z0-9_-]*";
|
|
|
|
|
|
|
|
// URI stuff.
|
|
|
|
const static std::string schemeRegex = "(?:http|https|ssh|git|file)";
|
|
|
|
const static std::string authorityRegex = "[a-zA-Z0-9._~-]*";
|
|
|
|
const static std::string segmentRegex = "[a-zA-Z0-9._~-]+";
|
|
|
|
const static std::string pathRegex = "/?" + segmentRegex + "(?:/" + segmentRegex + ")*";
|
|
|
|
const static std::string paramRegex = "[a-z]+=[a-zA-Z0-9._-]*";
|
|
|
|
|
2019-04-09 00:47:29 +03:00
|
|
|
FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
|
2019-02-12 19:23:11 +02:00
|
|
|
{
|
|
|
|
// FIXME: could combine this into one regex.
|
|
|
|
|
|
|
|
static std::regex flakeRegex(
|
2019-03-21 10:30:16 +02:00
|
|
|
"(?:flake:)?(" + flakeAlias + ")(?:/(?:" + refAndOrRevRegex + "))?",
|
2019-02-12 19:23:11 +02:00
|
|
|
std::regex::ECMAScript);
|
|
|
|
|
|
|
|
static std::regex githubRegex(
|
|
|
|
"github:(" + ownerRegex + ")/(" + repoRegex + ")(?:/" + revOrRefRegex + ")?",
|
|
|
|
std::regex::ECMAScript);
|
|
|
|
|
|
|
|
static std::regex uriRegex(
|
|
|
|
"((" + schemeRegex + "):" +
|
|
|
|
"(?://(" + authorityRegex + "))?" +
|
|
|
|
"(" + pathRegex + "))" +
|
|
|
|
"(?:[?](" + paramRegex + "(?:&" + paramRegex + ")*))?",
|
|
|
|
std::regex::ECMAScript);
|
|
|
|
|
|
|
|
static std::regex refRegex2(refRegex, std::regex::ECMAScript);
|
|
|
|
|
|
|
|
std::cmatch match;
|
|
|
|
if (std::regex_match(uri.c_str(), match, flakeRegex)) {
|
2019-03-21 10:30:16 +02:00
|
|
|
IsAlias d;
|
|
|
|
d.alias = match[1];
|
2019-02-12 19:23:11 +02:00
|
|
|
if (match[2].matched)
|
2019-04-06 21:45:35 +03:00
|
|
|
rev = Hash(match[2], htSHA1);
|
2019-02-12 19:23:11 +02:00
|
|
|
else if (match[3].matched) {
|
2019-04-06 21:45:35 +03:00
|
|
|
ref = match[3];
|
2019-02-12 19:23:11 +02:00
|
|
|
if (match[4].matched)
|
2019-04-06 21:45:35 +03:00
|
|
|
rev = Hash(match[4], htSHA1);
|
2019-02-12 19:23:11 +02:00
|
|
|
}
|
|
|
|
data = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (std::regex_match(uri.c_str(), match, githubRegex)) {
|
|
|
|
IsGitHub d;
|
|
|
|
d.owner = match[1];
|
|
|
|
d.repo = match[2];
|
|
|
|
if (match[3].matched)
|
2019-04-06 21:45:35 +03:00
|
|
|
rev = Hash(match[3], htSHA1);
|
2019-02-12 19:23:11 +02:00
|
|
|
else if (match[4].matched) {
|
2019-04-06 21:45:35 +03:00
|
|
|
ref = match[4];
|
2019-02-12 19:23:11 +02:00
|
|
|
}
|
|
|
|
data = d;
|
|
|
|
}
|
|
|
|
|
2019-02-12 23:06:19 +02:00
|
|
|
else if (std::regex_match(uri.c_str(), match, uriRegex)
|
|
|
|
&& (match[2] == "file" || hasSuffix(match[4], ".git")))
|
|
|
|
{
|
2019-02-12 19:23:11 +02:00
|
|
|
IsGit d;
|
|
|
|
d.uri = match[1];
|
|
|
|
for (auto & param : tokenizeString<Strings>(match[5], "&")) {
|
|
|
|
auto n = param.find('=');
|
|
|
|
assert(n != param.npos);
|
|
|
|
std::string name(param, 0, n);
|
|
|
|
std::string value(param, n + 1);
|
|
|
|
if (name == "rev") {
|
|
|
|
if (!std::regex_match(value, revRegex))
|
|
|
|
throw Error("invalid Git revision '%s'", value);
|
2019-04-06 21:45:35 +03:00
|
|
|
rev = Hash(value, htSHA1);
|
2019-02-12 19:23:11 +02:00
|
|
|
} else if (name == "ref") {
|
|
|
|
if (!std::regex_match(value, refRegex2))
|
|
|
|
throw Error("invalid Git ref '%s'", value);
|
2019-04-06 21:45:35 +03:00
|
|
|
ref = value;
|
2019-02-12 19:23:11 +02:00
|
|
|
} else
|
|
|
|
// FIXME: should probably pass through unknown parameters
|
|
|
|
throw Error("invalid Git flake reference parameter '%s', in '%s'", name, uri);
|
|
|
|
}
|
2019-04-06 21:45:35 +03:00
|
|
|
if (rev && !ref)
|
2019-02-12 19:23:11 +02:00
|
|
|
throw Error("flake URI '%s' lacks a Git ref", uri);
|
|
|
|
data = d;
|
|
|
|
}
|
|
|
|
|
2019-04-09 00:47:29 +03:00
|
|
|
else if (hasPrefix(uri, "/") || (allowRelative && (hasPrefix(uri, "./") || uri == "."))) {
|
2019-04-08 23:46:25 +03:00
|
|
|
IsPath d;
|
2019-04-09 00:47:29 +03:00
|
|
|
d.path = allowRelative ? absPath(uri) : canonPath(uri);
|
2019-04-08 23:46:25 +03:00
|
|
|
data = d;
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:23:11 +02:00
|
|
|
else
|
|
|
|
throw Error("'%s' is not a valid flake reference", uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FlakeRef::to_string() const
|
|
|
|
{
|
2019-04-06 21:45:35 +03:00
|
|
|
std::string string;
|
2019-03-21 10:30:16 +02:00
|
|
|
if (auto refData = std::get_if<FlakeRef::IsAlias>(&data))
|
2019-04-15 15:13:10 +03:00
|
|
|
string = refData->alias;
|
2019-02-12 19:23:11 +02:00
|
|
|
|
|
|
|
else if (auto refData = std::get_if<FlakeRef::IsGitHub>(&data)) {
|
2019-04-06 21:45:35 +03:00
|
|
|
assert(!ref || !rev);
|
|
|
|
string = "github:" + refData->owner + "/" + refData->repo;
|
2019-02-12 19:23:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (auto refData = std::get_if<FlakeRef::IsGit>(&data)) {
|
2019-04-06 21:45:35 +03:00
|
|
|
assert(ref || !rev);
|
|
|
|
string = refData->uri;
|
2019-02-12 19:23:11 +02:00
|
|
|
}
|
|
|
|
|
2019-03-21 10:30:16 +02:00
|
|
|
else if (auto refData = std::get_if<FlakeRef::IsPath>(&data))
|
2019-04-08 23:46:25 +03:00
|
|
|
return refData->path;
|
|
|
|
|
2019-02-12 19:23:11 +02:00
|
|
|
else abort();
|
2019-04-06 21:45:35 +03:00
|
|
|
|
|
|
|
string += (ref ? "/" + *ref : "") +
|
|
|
|
(rev ? "/" + rev->to_string(Base16, false) : "");
|
|
|
|
return string;
|
2019-02-12 19:23:11 +02:00
|
|
|
}
|
|
|
|
|
2019-02-12 22:05:44 +02:00
|
|
|
bool FlakeRef::isImmutable() const
|
|
|
|
{
|
2019-04-06 21:45:35 +03:00
|
|
|
return (bool) rev;
|
2019-02-12 22:05:44 +02:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:05:05 +02:00
|
|
|
FlakeRef FlakeRef::baseRef() const // Removes the ref and rev from a FlakeRef.
|
|
|
|
{
|
|
|
|
FlakeRef result(*this);
|
2019-04-06 21:45:35 +03:00
|
|
|
result.ref = std::nullopt;
|
|
|
|
result.rev = std::nullopt;
|
2019-03-10 08:05:05 +02:00
|
|
|
return result;
|
|
|
|
}
|
2019-02-12 19:23:11 +02:00
|
|
|
}
|