2019-02-12 17:23:11 +00:00
|
|
|
#include "flakeref.hh"
|
2019-05-31 17:01:11 +00:00
|
|
|
#include "store-api.hh"
|
2019-02-12 17:23:11 +00:00
|
|
|
|
|
|
|
#include <regex>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
// A Git ref (i.e. branch or tag name).
|
2019-04-16 14:24:51 +00:00
|
|
|
const static std::string refRegex = "[a-zA-Z0-9][a-zA-Z0-9_.-]*"; // FIXME: check
|
2019-02-12 17:23:11 +00: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-09-20 12:46:37 +00:00
|
|
|
const static std::string flakeId = "[a-zA-Z][a-zA-Z0-9_-]*";
|
2019-02-12 17:23:11 +00: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.
|
2019-09-05 15:15:09 +00:00
|
|
|
const static std::string schemeRegex = "[a-z+]+";
|
2019-02-12 17:23:11 +00:00
|
|
|
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 + ")*";
|
|
|
|
|
2019-05-01 18:23:39 +00:00
|
|
|
// '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 + ")*";
|
|
|
|
|
2019-05-01 18:43:16 +00:00
|
|
|
|
2019-05-31 17:01:11 +00:00
|
|
|
FlakeRef::FlakeRef(const std::string & uri_, bool allowRelative)
|
2019-02-12 17:23:11 +00:00
|
|
|
{
|
|
|
|
// FIXME: could combine this into one regex.
|
|
|
|
|
|
|
|
static std::regex flakeRegex(
|
2019-09-20 12:46:37 +00:00
|
|
|
"(?:flake:)?(" + flakeId + ")(?:/(?:" + refAndOrRevRegex + "))?",
|
2019-02-12 17:23:11 +00:00
|
|
|
std::regex::ECMAScript);
|
|
|
|
|
|
|
|
static std::regex githubRegex(
|
2019-05-31 17:01:11 +00:00
|
|
|
"github:(" + ownerRegex + ")/(" + repoRegex + ")(?:/" + revOrRefRegex + ")?",
|
2019-02-12 17:23:11 +00:00
|
|
|
std::regex::ECMAScript);
|
|
|
|
|
|
|
|
static std::regex uriRegex(
|
|
|
|
"((" + schemeRegex + "):" +
|
|
|
|
"(?://(" + authorityRegex + "))?" +
|
2019-05-31 17:01:11 +00:00
|
|
|
"(" + pathRegex + "))",
|
2019-02-12 17:23:11 +00:00
|
|
|
std::regex::ECMAScript);
|
|
|
|
|
|
|
|
static std::regex refRegex2(refRegex, std::regex::ECMAScript);
|
|
|
|
|
2019-05-01 18:23:39 +00:00
|
|
|
static std::regex subDirRegex2(subDirRegex, std::regex::ECMAScript);
|
|
|
|
|
2019-06-03 19:51:06 +00:00
|
|
|
auto [uri2, params] = splitUriAndParams(uri_);
|
|
|
|
std::string uri(uri2);
|
2019-05-31 17:01:11 +00:00
|
|
|
|
|
|
|
auto handleSubdir = [&](const std::string & name, const std::string & value) {
|
|
|
|
if (name == "dir") {
|
|
|
|
if (value != "" && !std::regex_match(value, subDirRegex2))
|
2019-05-31 18:53:23 +00:00
|
|
|
throw BadFlakeRef("flake '%s' has invalid subdirectory '%s'", uri, value);
|
2019-05-31 17:01:11 +00:00
|
|
|
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))
|
2019-05-31 18:53:23 +00:00
|
|
|
throw BadFlakeRef("invalid Git revision '%s'", value);
|
2019-05-31 17:01:11 +00:00
|
|
|
rev = Hash(value, htSHA1);
|
|
|
|
} else if (name == "ref") {
|
|
|
|
if (!std::regex_match(value, refRegex2))
|
2019-05-31 18:53:23 +00:00
|
|
|
throw BadFlakeRef("invalid Git ref '%s'", value);
|
2019-05-31 17:01:11 +00:00
|
|
|
ref = value;
|
|
|
|
} else if (handleSubdir(name, value))
|
|
|
|
;
|
|
|
|
else return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-09-27 13:31:09 +00:00
|
|
|
std::smatch match;
|
|
|
|
if (std::regex_match(uri, match, flakeRegex)) {
|
2019-09-20 12:46:37 +00:00
|
|
|
IsId d;
|
|
|
|
d.id = match[1];
|
2019-02-12 17:23:11 +00:00
|
|
|
if (match[2].matched)
|
2019-04-06 18:45:35 +00:00
|
|
|
rev = Hash(match[2], htSHA1);
|
2019-02-12 17:23:11 +00:00
|
|
|
else if (match[3].matched) {
|
2019-04-06 18:45:35 +00:00
|
|
|
ref = match[3];
|
2019-02-12 17:23:11 +00:00
|
|
|
if (match[4].matched)
|
2019-04-06 18:45:35 +00:00
|
|
|
rev = Hash(match[4], htSHA1);
|
2019-02-12 17:23:11 +00:00
|
|
|
}
|
|
|
|
data = d;
|
|
|
|
}
|
|
|
|
|
2019-09-27 13:31:09 +00:00
|
|
|
else if (std::regex_match(uri, match, githubRegex)) {
|
2019-02-12 17:23:11 +00:00
|
|
|
IsGitHub d;
|
|
|
|
d.owner = match[1];
|
|
|
|
d.repo = match[2];
|
|
|
|
if (match[3].matched)
|
2019-04-06 18:45:35 +00:00
|
|
|
rev = Hash(match[3], htSHA1);
|
2019-02-12 17:23:11 +00:00
|
|
|
else if (match[4].matched) {
|
2019-04-06 18:45:35 +00:00
|
|
|
ref = match[4];
|
2019-02-12 17:23:11 +00:00
|
|
|
}
|
2019-05-31 17:01:11 +00:00
|
|
|
for (auto & param : params) {
|
|
|
|
if (handleSubdir(param.first, param.second))
|
|
|
|
;
|
|
|
|
else
|
2019-05-31 18:53:23 +00:00
|
|
|
throw BadFlakeRef("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
|
2019-05-01 18:43:16 +00:00
|
|
|
}
|
2019-02-12 17:23:11 +00:00
|
|
|
data = d;
|
|
|
|
}
|
|
|
|
|
2019-09-27 13:31:09 +00:00
|
|
|
else if (std::regex_match(uri, match, uriRegex)) {
|
2019-09-05 15:15:09 +00:00
|
|
|
auto & scheme = match[2];
|
|
|
|
if (scheme == "git" ||
|
|
|
|
scheme == "git+http" ||
|
|
|
|
scheme == "git+https" ||
|
|
|
|
scheme == "git+ssh" ||
|
|
|
|
scheme == "git+file" ||
|
|
|
|
scheme == "file")
|
|
|
|
{
|
|
|
|
IsGit d;
|
|
|
|
d.uri = match[1];
|
|
|
|
for (auto & param : params) {
|
|
|
|
if (handleGitParams(param.first, param.second))
|
|
|
|
;
|
|
|
|
else
|
|
|
|
// FIXME: should probably pass through unknown parameters
|
|
|
|
throw BadFlakeRef("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
|
|
|
|
}
|
|
|
|
if (rev && !ref)
|
|
|
|
throw BadFlakeRef("flake URI '%s' lacks a Git ref", uri);
|
|
|
|
data = d;
|
|
|
|
} else
|
|
|
|
throw BadFlakeRef("unsupported URI scheme '%s' in flake reference '%s'", scheme, uri);
|
2019-02-12 17:23:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 18:53:23 +00:00
|
|
|
else if ((hasPrefix(uri, "/") || (allowRelative && (hasPrefix(uri, "./") || hasPrefix(uri, "../") || uri == ".")))
|
|
|
|
&& uri.find(':') == std::string::npos)
|
|
|
|
{
|
2019-04-08 20:46:25 +00:00
|
|
|
IsPath d;
|
2019-05-31 19:52:02 +00:00
|
|
|
if (allowRelative) {
|
|
|
|
d.path = absPath(uri);
|
2019-06-21 13:29:05 +00:00
|
|
|
try {
|
|
|
|
if (!S_ISDIR(lstat(d.path).st_mode))
|
2019-09-02 15:33:07 +00:00
|
|
|
throw MissingFlake("path '%s' is not a flake (sub)directory", d.path);
|
2019-06-21 13:29:05 +00:00
|
|
|
} catch (SysError & e) {
|
|
|
|
if (e.errNo == ENOENT || e.errNo == EISDIR)
|
2019-09-02 15:33:07 +00:00
|
|
|
throw MissingFlake("flake '%s' does not exist", d.path);
|
2019-06-21 13:29:05 +00:00
|
|
|
throw;
|
|
|
|
}
|
2019-05-31 19:52:02 +00:00
|
|
|
while (true) {
|
|
|
|
if (pathExists(d.path + "/.git")) break;
|
2019-12-11 13:53:30 +00:00
|
|
|
subdir = std::string(baseNameOf(d.path)) + (subdir.empty() ? "" : "/" + subdir);
|
2019-05-31 19:52:02 +00:00
|
|
|
d.path = dirOf(d.path);
|
|
|
|
if (d.path == "/")
|
2019-09-02 15:33:07 +00:00
|
|
|
throw MissingFlake("path '%s' is not a flake (because it does not reference a Git repository)", uri);
|
2019-05-31 19:52:02 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
d.path = canonPath(uri);
|
2019-04-08 20:46:25 +00:00
|
|
|
data = d;
|
2019-05-31 17:01:11 +00:00
|
|
|
for (auto & param : params) {
|
|
|
|
if (handleGitParams(param.first, param.second))
|
|
|
|
;
|
|
|
|
else
|
2019-05-31 18:53:23 +00:00
|
|
|
throw BadFlakeRef("invalid Git flakeref parameter '%s', in '%s'", param.first, uri);
|
2019-05-31 17:01:11 +00:00
|
|
|
}
|
2019-04-08 20:46:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:23:11 +00:00
|
|
|
else
|
2019-05-31 18:53:23 +00:00
|
|
|
throw BadFlakeRef("'%s' is not a valid flake reference", uri);
|
2019-02-12 17:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FlakeRef::to_string() const
|
|
|
|
{
|
2019-04-06 18:45:35 +00:00
|
|
|
std::string string;
|
2019-05-07 21:06:15 +00:00
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
auto addParam =
|
|
|
|
[&](const std::string & name, std::string value) {
|
|
|
|
string += first ? '?' : '&';
|
|
|
|
first = false;
|
|
|
|
string += name;
|
|
|
|
string += '=';
|
|
|
|
string += value; // FIXME: escaping
|
|
|
|
};
|
|
|
|
|
2019-09-20 12:46:37 +00:00
|
|
|
if (auto refData = std::get_if<FlakeRef::IsId>(&data)) {
|
|
|
|
string = refData->id;
|
2019-05-07 21:06:15 +00:00
|
|
|
if (ref) string += '/' + *ref;
|
2019-05-08 20:08:38 +00:00
|
|
|
if (rev) string += '/' + rev->gitRev();
|
2019-05-07 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (auto refData = std::get_if<FlakeRef::IsPath>(&data)) {
|
2019-05-31 17:01:11 +00:00
|
|
|
string = refData->path;
|
2019-05-08 20:08:38 +00:00
|
|
|
if (ref) addParam("ref", *ref);
|
|
|
|
if (rev) addParam("rev", rev->gitRev());
|
2019-05-31 17:01:11 +00:00
|
|
|
if (subdir != "") addParam("dir", subdir);
|
2019-05-07 21:06:15 +00:00
|
|
|
}
|
2019-02-12 17:23:11 +00:00
|
|
|
|
|
|
|
else if (auto refData = std::get_if<FlakeRef::IsGitHub>(&data)) {
|
2019-04-16 13:40:58 +00:00
|
|
|
assert(!(ref && rev));
|
2019-04-06 18:45:35 +00:00
|
|
|
string = "github:" + refData->owner + "/" + refData->repo;
|
2019-05-07 21:06:15 +00:00
|
|
|
if (ref) { string += '/'; string += *ref; }
|
2019-05-08 20:08:38 +00:00
|
|
|
if (rev) { string += '/'; string += rev->gitRev(); }
|
2019-05-07 21:06:15 +00:00
|
|
|
if (subdir != "") addParam("dir", subdir);
|
2019-02-12 17:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (auto refData = std::get_if<FlakeRef::IsGit>(&data)) {
|
2019-04-19 09:16:14 +00:00
|
|
|
assert(!rev || ref);
|
2019-04-06 18:45:35 +00:00
|
|
|
string = refData->uri;
|
2019-02-12 17:23:11 +00:00
|
|
|
|
2019-05-07 21:06:15 +00:00
|
|
|
if (ref) {
|
|
|
|
addParam("ref", *ref);
|
|
|
|
if (rev)
|
2019-05-08 20:08:38 +00:00
|
|
|
addParam("rev", rev->gitRev());
|
2019-05-07 21:06:15 +00:00
|
|
|
}
|
2019-04-06 18:45:35 +00:00
|
|
|
|
2019-05-07 21:06:15 +00:00
|
|
|
if (subdir != "") addParam("dir", subdir);
|
|
|
|
}
|
2019-05-01 18:23:39 +00:00
|
|
|
|
2019-05-07 21:06:15 +00:00
|
|
|
else abort();
|
2019-05-01 18:23:39 +00:00
|
|
|
|
2019-05-23 21:39:58 +00:00
|
|
|
assert(FlakeRef(string) == *this);
|
|
|
|
|
2019-04-06 18:45:35 +00:00
|
|
|
return string;
|
2019-02-12 17:23:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 09:43:56 +00:00
|
|
|
std::ostream & operator << (std::ostream & str, const FlakeRef & flakeRef)
|
|
|
|
{
|
|
|
|
str << flakeRef.to_string();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2019-02-12 20:05:44 +00:00
|
|
|
bool FlakeRef::isImmutable() const
|
|
|
|
{
|
2019-04-06 18:45:35 +00:00
|
|
|
return (bool) rev;
|
2019-02-12 20:05:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 06:05:05 +00:00
|
|
|
FlakeRef FlakeRef::baseRef() const // Removes the ref and rev from a FlakeRef.
|
|
|
|
{
|
|
|
|
FlakeRef result(*this);
|
2019-04-06 18:45:35 +00:00
|
|
|
result.ref = std::nullopt;
|
|
|
|
result.rev = std::nullopt;
|
2019-03-10 06:05:05 +00:00
|
|
|
return result;
|
|
|
|
}
|
2019-05-31 18:53:23 +00:00
|
|
|
|
2019-09-18 21:59:45 +00:00
|
|
|
bool FlakeRef::contains(const FlakeRef & other) const
|
|
|
|
{
|
|
|
|
if (!(data == other.data))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ref && ref != other.ref)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (rev && rev != other.rev)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (subdir != other.subdir)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-31 18:53:23 +00:00
|
|
|
std::optional<FlakeRef> parseFlakeRef(
|
|
|
|
const std::string & uri, bool allowRelative)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return FlakeRef(uri, allowRelative);
|
|
|
|
} catch (BadFlakeRef & e) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 17:23:11 +00:00
|
|
|
}
|