nix-super/src/libexpr/primops/flakeref.cc

252 lines
7.8 KiB
C++
Raw Normal View History

2019-02-12 19:23:11 +02:00
#include "flakeref.hh"
#include "store-api.hh"
2019-02-12 19:23:11 +02:00
#include <regex>
namespace nix {
// A Git ref (i.e. branch or tag name).
const static std::string refRegex = "[a-zA-Z0-9][a-zA-Z0-9_.-]*"; // FIXME: check
2019-02-12 19:23:11 +02:00
// 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 + ")*";
// 'dir' path elements cannot start with a '.'. We also reject
// potentially dangerous characters like ';'.
const static std::string subDirElemRegex = "(?:[a-zA-Z0-9_-]+[a-zA-Z0-9._-]*)";
const static std::string subDirRegex = subDirElemRegex + "(?:/" + subDirElemRegex + ")*";
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 + ")?",
2019-02-12 19:23:11 +02:00
std::regex::ECMAScript);
static std::regex uriRegex(
"((" + schemeRegex + "):" +
"(?://(" + authorityRegex + "))?" +
"(" + pathRegex + "))",
2019-02-12 19:23:11 +02:00
std::regex::ECMAScript);
static std::regex refRegex2(refRegex, std::regex::ECMAScript);
static std::regex subDirRegex2(subDirRegex, std::regex::ECMAScript);
auto [uri, params] = splitUriAndParams(uri_);
auto handleSubdir = [&](const std::string & name, const std::string & value) {
if (name == "dir") {
if (value != "" && !std::regex_match(value, subDirRegex2))
throw BadFlakeRef("flake '%s' has invalid subdirectory '%s'", uri, value);
subdir = value;
return true;
} else
return false;
};
auto handleGitParams = [&](const std::string & name, const std::string & value) {
if (name == "rev") {
if (!std::regex_match(value, revRegex))
throw BadFlakeRef("invalid Git revision '%s'", value);
rev = Hash(value, htSHA1);
} else if (name == "ref") {
if (!std::regex_match(value, refRegex2))
throw BadFlakeRef("invalid Git ref '%s'", value);
ref = value;
} else if (handleSubdir(name, value))
;
else return false;
return true;
};
2019-02-12 19:23:11 +02:00
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
}
for (auto & param : params) {
if (handleSubdir(param.first, param.second))
;
else
throw BadFlakeRef("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
}
2019-02-12 19:23:11 +02:00
data = d;
}
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 : params) {
if (handleGitParams(param.first, param.second))
;
else
2019-02-12 19:23:11 +02:00
// FIXME: should probably pass through unknown parameters
throw BadFlakeRef("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
2019-02-12 19:23:11 +02:00
}
2019-04-06 21:45:35 +03:00
if (rev && !ref)
throw BadFlakeRef("flake URI '%s' lacks a Git ref", uri);
2019-02-12 19:23:11 +02:00
data = d;
}
else if ((hasPrefix(uri, "/") || (allowRelative && (hasPrefix(uri, "./") || hasPrefix(uri, "../") || uri == ".")))
&& uri.find(':') == std::string::npos)
{
IsPath d;
if (allowRelative) {
d.path = absPath(uri);
while (true) {
if (pathExists(d.path + "/.git")) break;
subdir = baseNameOf(d.path) + (subdir.empty() ? "" : "/" + subdir);
d.path = dirOf(d.path);
if (d.path == "/")
throw BadFlakeRef("path '%s' does not reference a Git repository", uri);
}
} else
d.path = canonPath(uri);
data = d;
for (auto & param : params) {
if (handleGitParams(param.first, param.second))
;
else
throw BadFlakeRef("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
}
}
2019-02-12 19:23:11 +02:00
else
throw BadFlakeRef("'%s' is not a valid flake reference", uri);
2019-02-12 19:23:11 +02:00
}
std::string FlakeRef::to_string() const
{
2019-04-06 21:45:35 +03:00
std::string string;
bool first = true;
auto addParam =
[&](const std::string & name, std::string value) {
string += first ? '?' : '&';
first = false;
string += name;
string += '=';
string += value; // FIXME: escaping
};
if (auto refData = std::get_if<FlakeRef::IsAlias>(&data)) {
string = refData->alias;
if (ref) string += '/' + *ref;
if (rev) string += '/' + rev->gitRev();
}
else if (auto refData = std::get_if<FlakeRef::IsPath>(&data)) {
string = refData->path;
if (ref) addParam("ref", *ref);
if (rev) addParam("rev", rev->gitRev());
if (subdir != "") addParam("dir", subdir);
}
2019-02-12 19:23:11 +02:00
else if (auto refData = std::get_if<FlakeRef::IsGitHub>(&data)) {
assert(!(ref && rev));
2019-04-06 21:45:35 +03:00
string = "github:" + refData->owner + "/" + refData->repo;
if (ref) { string += '/'; string += *ref; }
if (rev) { string += '/'; string += rev->gitRev(); }
if (subdir != "") addParam("dir", subdir);
2019-02-12 19:23:11 +02:00
}
else if (auto refData = std::get_if<FlakeRef::IsGit>(&data)) {
assert(!rev || ref);
2019-04-06 21:45:35 +03:00
string = refData->uri;
2019-02-12 19:23:11 +02:00
if (ref) {
addParam("ref", *ref);
if (rev)
addParam("rev", rev->gitRev());
}
2019-04-06 21:45:35 +03:00
if (subdir != "") addParam("dir", subdir);
}
else abort();
assert(FlakeRef(string) == *this);
2019-04-06 21:45:35 +03:00
return string;
2019-02-12 19:23:11 +02:00
}
std::ostream & operator << (std::ostream & str, const FlakeRef & flakeRef)
{
str << flakeRef.to_string();
return str;
}
bool FlakeRef::isImmutable() const
{
2019-04-06 21:45:35 +03:00
return (bool) rev;
}
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;
}
std::optional<FlakeRef> parseFlakeRef(
const std::string & uri, bool allowRelative)
{
try {
return FlakeRef(uri, allowRelative);
} catch (BadFlakeRef & e) {
return {};
}
}
2019-02-12 19:23:11 +02:00
}