Improve FlakeRef::to_string()

We were incorrectly using path syntax (i.e. /<ref>/<rev>) for Git
repositories. This is only valid for GitHub flakerefs.
This commit is contained in:
Eelco Dolstra 2019-05-07 23:06:15 +02:00
parent 3c171851a8
commit 2a41a567e2

View file

@ -147,31 +147,53 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
std::string FlakeRef::to_string() const std::string FlakeRef::to_string() const
{ {
std::string string; std::string string;
bool first = true;
if (auto refData = std::get_if<FlakeRef::IsAlias>(&data)) 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; string = refData->alias;
if (ref) string += '/' + *ref;
if (rev) string += '/' + rev->to_string(Base16, false);
}
else if (auto refData = std::get_if<FlakeRef::IsPath>(&data)) {
assert(subdir == "");
assert(!rev);
assert(!ref);
return refData->path;
}
else if (auto refData = std::get_if<FlakeRef::IsGitHub>(&data)) { else if (auto refData = std::get_if<FlakeRef::IsGitHub>(&data)) {
assert(!(ref && rev)); assert(!(ref && rev));
string = "github:" + refData->owner + "/" + refData->repo; string = "github:" + refData->owner + "/" + refData->repo;
if (ref) { string += '/'; string += *ref; }
if (rev) { string += '/'; string += rev->to_string(Base16, false); }
if (subdir != "") addParam("dir", subdir);
} }
else if (auto refData = std::get_if<FlakeRef::IsGit>(&data)) { else if (auto refData = std::get_if<FlakeRef::IsGit>(&data)) {
assert(!rev || ref); assert(!rev || ref);
string = refData->uri; string = refData->uri;
if (ref) {
addParam("ref", *ref);
if (rev)
addParam("rev", rev->to_string(Base16, false));
}
if (subdir != "") addParam("dir", subdir);
} }
else if (auto refData = std::get_if<FlakeRef::IsPath>(&data))
return refData->path;
else abort(); else abort();
// FIXME: need to use ?rev etc. for IsGit URIs.
string += (ref ? "/" + *ref : "") +
(rev ? "/" + rev->to_string(Base16, false) : "");
if (subdir != "") string += "?dir=" + subdir;
return string; return string;
} }