2020-04-08 12:12:22 +00:00
|
|
|
#include "filetransfer.hh"
|
2020-03-30 12:03:28 +00:00
|
|
|
#include "cache.hh"
|
|
|
|
#include "fetchers.hh"
|
2020-01-21 15:27:53 +00:00
|
|
|
#include "globals.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
namespace nix::fetchers {
|
|
|
|
|
2020-05-29 12:23:32 +00:00
|
|
|
struct GitArchiveInput : Input
|
2020-01-21 15:27:53 +00:00
|
|
|
{
|
|
|
|
std::string owner;
|
|
|
|
std::string repo;
|
|
|
|
std::optional<std::string> ref;
|
|
|
|
std::optional<Hash> rev;
|
|
|
|
|
2020-05-29 12:23:32 +00:00
|
|
|
virtual std::shared_ptr<GitArchiveInput> _clone() const = 0;
|
2020-01-31 18:16:40 +00:00
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
bool operator ==(const Input & other) const override
|
|
|
|
{
|
2020-05-29 12:23:32 +00:00
|
|
|
auto other2 = dynamic_cast<const GitArchiveInput *>(&other);
|
2020-01-21 15:27:53 +00:00
|
|
|
return
|
|
|
|
other2
|
|
|
|
&& owner == other2->owner
|
|
|
|
&& repo == other2->repo
|
|
|
|
&& rev == other2->rev
|
|
|
|
&& ref == other2->ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isImmutable() const override
|
|
|
|
{
|
2020-04-02 17:04:33 +00:00
|
|
|
return (bool) rev || narHash;
|
2020-01-21 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> getRef() const override { return ref; }
|
|
|
|
|
|
|
|
std::optional<Hash> getRev() const override { return rev; }
|
|
|
|
|
2020-03-28 17:05:50 +00:00
|
|
|
ParsedURL toURL() const override
|
2020-01-21 15:27:53 +00:00
|
|
|
{
|
2020-03-28 17:05:50 +00:00
|
|
|
auto path = owner + "/" + repo;
|
2020-01-21 15:27:53 +00:00
|
|
|
assert(!(ref && rev));
|
2020-03-28 17:05:50 +00:00
|
|
|
if (ref) path += "/" + *ref;
|
|
|
|
if (rev) path += "/" + rev->to_string(Base16, false);
|
|
|
|
return ParsedURL {
|
2020-05-29 12:23:32 +00:00
|
|
|
.scheme = type(),
|
2020-03-28 17:05:50 +00:00
|
|
|
.path = path,
|
|
|
|
};
|
2020-01-21 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:16:40 +00:00
|
|
|
Attrs toAttrsInternal() const override
|
|
|
|
{
|
|
|
|
Attrs attrs;
|
|
|
|
attrs.emplace("owner", owner);
|
|
|
|
attrs.emplace("repo", repo);
|
|
|
|
if (ref)
|
|
|
|
attrs.emplace("ref", *ref);
|
|
|
|
if (rev)
|
|
|
|
attrs.emplace("rev", rev->gitRev());
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2020-05-29 12:23:32 +00:00
|
|
|
virtual Hash getRevFromRef(nix::ref<Store> store, std::string_view ref) const = 0;
|
|
|
|
|
|
|
|
virtual std::string getDownloadUrl() const = 0;
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
std::pair<Tree, std::shared_ptr<const Input>> fetchTreeInternal(nix::ref<Store> store) const override
|
|
|
|
{
|
|
|
|
auto rev = this->rev;
|
2020-03-17 19:54:36 +00:00
|
|
|
auto ref = this->ref.value_or("master");
|
2020-01-21 15:27:53 +00:00
|
|
|
|
2020-05-29 12:23:32 +00:00
|
|
|
if (!rev) rev = getRevFromRef(store, ref);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
2020-05-29 12:23:32 +00:00
|
|
|
auto input = _clone();
|
2020-03-17 19:54:36 +00:00
|
|
|
input->ref = {};
|
|
|
|
input->rev = *rev;
|
|
|
|
|
|
|
|
Attrs immutableAttrs({
|
|
|
|
{"type", "git-tarball"},
|
|
|
|
{"rev", rev->gitRev()},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (auto res = getCache()->lookup(store, immutableAttrs)) {
|
|
|
|
return {
|
|
|
|
Tree{
|
|
|
|
.actualPath = store->toRealPath(res->second),
|
|
|
|
.storePath = std::move(res->second),
|
|
|
|
.info = TreeInfo {
|
|
|
|
.lastModified = getIntAttr(res->first, "lastModified"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-29 12:23:32 +00:00
|
|
|
auto url = input->getDownloadUrl();
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
|
2020-03-18 13:08:25 +00:00
|
|
|
auto tree = downloadTarball(store, url, "source", true);
|
2020-03-17 19:54:36 +00:00
|
|
|
|
|
|
|
getCache()->add(
|
|
|
|
store,
|
|
|
|
immutableAttrs,
|
2020-03-18 13:08:25 +00:00
|
|
|
{
|
|
|
|
{"rev", rev->gitRev()},
|
|
|
|
{"lastModified", *tree.info.lastModified}
|
|
|
|
},
|
|
|
|
tree.storePath,
|
2020-03-17 19:54:36 +00:00
|
|
|
true);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
2020-03-18 13:08:25 +00:00
|
|
|
return {std::move(tree), input};
|
2020-01-21 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const Input> applyOverrides(
|
|
|
|
std::optional<std::string> ref,
|
|
|
|
std::optional<Hash> rev) const override
|
|
|
|
{
|
|
|
|
if (!ref && !rev) return shared_from_this();
|
|
|
|
|
2020-05-29 12:23:32 +00:00
|
|
|
auto res = _clone();
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
if (ref) res->ref = ref;
|
|
|
|
if (rev) res->rev = rev;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-29 12:23:32 +00:00
|
|
|
struct GitArchiveInputScheme : InputScheme
|
2020-01-21 15:27:53 +00:00
|
|
|
{
|
2020-05-29 12:23:32 +00:00
|
|
|
std::string type;
|
|
|
|
|
|
|
|
GitArchiveInputScheme(std::string && type) : type(type)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual std::unique_ptr<GitArchiveInput> create() = 0;
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
std::unique_ptr<Input> inputFromURL(const ParsedURL & url) override
|
|
|
|
{
|
2020-05-29 12:23:32 +00:00
|
|
|
if (url.scheme != type) return nullptr;
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
auto path = tokenizeString<std::vector<std::string>>(url.path, "/");
|
2020-05-29 12:23:32 +00:00
|
|
|
auto input = create();
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
if (path.size() == 2) {
|
|
|
|
} else if (path.size() == 3) {
|
|
|
|
if (std::regex_match(path[2], revRegex))
|
|
|
|
input->rev = Hash(path[2], htSHA1);
|
|
|
|
else if (std::regex_match(path[2], refRegex))
|
|
|
|
input->ref = path[2];
|
|
|
|
else
|
2020-05-29 12:23:32 +00:00
|
|
|
throw BadURL("in URL '%s', '%s' is not a commit hash or branch/tag name", url.url, path[2]);
|
2020-01-21 15:27:53 +00:00
|
|
|
} else
|
2020-05-29 12:23:32 +00:00
|
|
|
throw BadURL("URL '%s' is invalid", url.url);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
for (auto &[name, value] : url.query) {
|
|
|
|
if (name == "rev") {
|
|
|
|
if (input->rev)
|
2020-05-29 12:23:32 +00:00
|
|
|
throw BadURL("URL '%s' contains multiple commit hashes", url.url);
|
2020-01-21 15:27:53 +00:00
|
|
|
input->rev = Hash(value, htSHA1);
|
|
|
|
}
|
|
|
|
else if (name == "ref") {
|
|
|
|
if (!std::regex_match(value, refRegex))
|
2020-05-29 12:23:32 +00:00
|
|
|
throw BadURL("URL '%s' contains an invalid branch/tag name", url.url);
|
2020-01-21 15:27:53 +00:00
|
|
|
if (input->ref)
|
2020-05-29 12:23:32 +00:00
|
|
|
throw BadURL("URL '%s' contains multiple branch/tag names", url.url);
|
2020-01-21 15:27:53 +00:00
|
|
|
input->ref = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input->ref && input->rev)
|
2020-05-29 12:23:32 +00:00
|
|
|
throw BadURL("URL '%s' contains both a commit hash and a branch/tag name", url.url);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
input->owner = path[0];
|
|
|
|
input->repo = path[1];
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
2020-01-31 18:16:40 +00:00
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
std::unique_ptr<Input> inputFromAttrs(const Attrs & attrs) override
|
2020-01-31 18:16:40 +00:00
|
|
|
{
|
2020-05-29 12:23:32 +00:00
|
|
|
if (maybeGetStrAttr(attrs, "type") != type) return {};
|
2020-01-31 18:35:28 +00:00
|
|
|
|
|
|
|
for (auto & [name, value] : attrs)
|
|
|
|
if (name != "type" && name != "owner" && name != "repo" && name != "ref" && name != "rev")
|
2020-05-29 12:23:32 +00:00
|
|
|
throw Error("unsupported input attribute '%s'", name);
|
2020-01-31 18:35:28 +00:00
|
|
|
|
2020-05-29 12:23:32 +00:00
|
|
|
auto input = create();
|
2020-01-31 18:16:40 +00:00
|
|
|
input->owner = getStrAttr(attrs, "owner");
|
|
|
|
input->repo = getStrAttr(attrs, "repo");
|
|
|
|
input->ref = maybeGetStrAttr(attrs, "ref");
|
|
|
|
if (auto rev = maybeGetStrAttr(attrs, "rev"))
|
|
|
|
input->rev = Hash(*rev, htSHA1);
|
|
|
|
return input;
|
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
};
|
|
|
|
|
2020-05-29 12:23:32 +00:00
|
|
|
struct GitHubInput : GitArchiveInput
|
|
|
|
{
|
|
|
|
std::string type() const override { return "github"; }
|
|
|
|
|
|
|
|
std::shared_ptr<GitArchiveInput> _clone() const override
|
|
|
|
{ return std::make_shared<GitHubInput>(*this); }
|
|
|
|
|
|
|
|
Hash getRevFromRef(nix::ref<Store> store, std::string_view ref) const override
|
|
|
|
{
|
|
|
|
auto url = fmt("https://api.github.com/repos/%s/%s/commits/%s",
|
|
|
|
owner, repo, ref);
|
|
|
|
auto json = nlohmann::json::parse(
|
|
|
|
readFile(
|
|
|
|
store->toRealPath(
|
|
|
|
downloadFile(store, url, "source", false).storePath)));
|
|
|
|
auto rev = Hash(json["sha"], htSHA1);
|
|
|
|
debug("HEAD revision for '%s' is %s", url, rev.gitRev());
|
|
|
|
return rev;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getDownloadUrl() const override
|
|
|
|
{
|
|
|
|
// FIXME: use regular /archive URLs instead? api.github.com
|
|
|
|
// might have stricter rate limits.
|
|
|
|
|
|
|
|
auto url = fmt("https://api.github.com/repos/%s/%s/tarball/%s",
|
|
|
|
owner, repo, rev->to_string(Base16, false));
|
|
|
|
|
|
|
|
std::string accessToken = settings.githubAccessToken.get();
|
|
|
|
if (accessToken != "")
|
|
|
|
url += "?access_token=" + accessToken;
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clone(const Path & destDir) const override
|
|
|
|
{
|
|
|
|
std::shared_ptr<const Input> input = inputFromURL(fmt("git+ssh://git@github.com/%s/%s.git", owner, repo));
|
|
|
|
input = input->applyOverrides(ref.value_or("master"), rev);
|
|
|
|
input->clone(destDir);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GitHubInputScheme : GitArchiveInputScheme
|
|
|
|
{
|
|
|
|
GitHubInputScheme() : GitArchiveInputScheme("github") { }
|
|
|
|
|
|
|
|
std::unique_ptr<GitArchiveInput> create() override
|
|
|
|
{
|
|
|
|
return std::make_unique<GitHubInput>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GitLabInput : GitArchiveInput
|
|
|
|
{
|
|
|
|
std::string type() const override { return "gitlab"; }
|
|
|
|
|
|
|
|
std::shared_ptr<GitArchiveInput> _clone() const override
|
|
|
|
{ return std::make_shared<GitLabInput>(*this); }
|
|
|
|
|
|
|
|
Hash getRevFromRef(nix::ref<Store> store, std::string_view ref) const override
|
|
|
|
{
|
|
|
|
auto url = fmt("https://gitlab.com/api/v4/projects/%s%%2F%s/repository/branches/%s",
|
|
|
|
owner, repo, ref);
|
|
|
|
auto json = nlohmann::json::parse(
|
|
|
|
readFile(
|
|
|
|
store->toRealPath(
|
|
|
|
downloadFile(store, url, "source", false).storePath)));
|
|
|
|
auto rev = Hash(json["commit"]["id"], htSHA1);
|
|
|
|
debug("HEAD revision for '%s' is %s", url, rev.gitRev());
|
|
|
|
return rev;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getDownloadUrl() const override
|
|
|
|
{
|
|
|
|
// FIXME: This endpoint has a rate limit threshold of 5 requests per minute.
|
|
|
|
|
|
|
|
auto url = fmt("https://gitlab.com/api/v4/projects/%s%%2F%s/repository/archive.tar.gz?sha=%s",
|
|
|
|
owner, repo, rev->to_string(Base16, false));
|
|
|
|
|
|
|
|
/* # FIXME: add privat token auth (`curl --header "PRIVATE-TOKEN: <your_access_token>"`)
|
|
|
|
std::string accessToken = settings.githubAccessToken.get();
|
|
|
|
if (accessToken != "")
|
|
|
|
url += "?access_token=" + accessToken;*/
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clone(const Path & destDir) const override
|
|
|
|
{
|
|
|
|
std::shared_ptr<const Input> input = inputFromURL(fmt("git+ssh://git@gitlab.com/%s/%s.git", owner, repo));
|
|
|
|
input = input->applyOverrides(ref.value_or("master"), rev);
|
|
|
|
input->clone(destDir);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GitLabInputScheme : GitArchiveInputScheme
|
|
|
|
{
|
|
|
|
GitLabInputScheme() : GitArchiveInputScheme("gitlab") { }
|
|
|
|
|
|
|
|
std::unique_ptr<GitArchiveInput> create() override
|
|
|
|
{
|
|
|
|
return std::make_unique<GitLabInput>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
static auto r1 = OnStartup([] { registerInputScheme(std::make_unique<GitHubInputScheme>()); });
|
2020-05-29 12:23:32 +00:00
|
|
|
static auto r2 = OnStartup([] { registerInputScheme(std::make_unique<GitLabInputScheme>()); });
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
}
|