2020-03-30 12:03:28 +00:00
|
|
|
#include "fetchers.hh"
|
|
|
|
#include "cache.hh"
|
2020-01-21 22:49:32 +00:00
|
|
|
#include "globals.hh"
|
|
|
|
#include "tarfile.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
using namespace std::string_literals;
|
|
|
|
|
|
|
|
namespace nix::fetchers {
|
|
|
|
|
|
|
|
struct MercurialInput : Input
|
|
|
|
{
|
|
|
|
ParsedURL url;
|
|
|
|
std::optional<std::string> ref;
|
|
|
|
std::optional<Hash> rev;
|
|
|
|
|
|
|
|
MercurialInput(const ParsedURL & url) : url(url)
|
2020-01-31 18:16:40 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
std::string type() const override { return "hg"; }
|
2020-01-21 22:49:32 +00:00
|
|
|
|
|
|
|
bool operator ==(const Input & other) const override
|
|
|
|
{
|
|
|
|
auto other2 = dynamic_cast<const MercurialInput *>(&other);
|
|
|
|
return
|
|
|
|
other2
|
2020-01-29 21:53:03 +00:00
|
|
|
&& url == other2->url
|
2020-01-21 22:49:32 +00:00
|
|
|
&& 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 22:49:32 +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 22:49:32 +00:00
|
|
|
{
|
|
|
|
ParsedURL url2(url);
|
2020-02-02 12:06:00 +00:00
|
|
|
url2.scheme = "hg+" + url2.scheme;
|
2020-01-21 22:49:32 +00:00
|
|
|
if (rev) url2.query.insert_or_assign("rev", rev->gitRev());
|
|
|
|
if (ref) url2.query.insert_or_assign("ref", *ref);
|
2020-03-28 17:05:50 +00:00
|
|
|
return url;
|
2020-01-21 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:16:40 +00:00
|
|
|
Attrs toAttrsInternal() const override
|
|
|
|
{
|
|
|
|
Attrs attrs;
|
|
|
|
attrs.emplace("url", url.to_string());
|
|
|
|
if (ref)
|
|
|
|
attrs.emplace("ref", *ref);
|
|
|
|
if (rev)
|
|
|
|
attrs.emplace("rev", rev->gitRev());
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2020-01-21 22:49:32 +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();
|
|
|
|
|
|
|
|
auto res = std::make_shared<MercurialInput>(*this);
|
|
|
|
|
|
|
|
if (ref) res->ref = ref;
|
|
|
|
if (rev) res->rev = rev;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Path> getSourcePath() const
|
|
|
|
{
|
2020-02-02 12:06:00 +00:00
|
|
|
if (url.scheme == "file" && !ref && !rev)
|
2020-01-21 22:49:32 +00:00
|
|
|
return url.path;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-02-05 13:48:49 +00:00
|
|
|
void markChangedFile(std::string_view file, std::optional<std::string> commitMsg) const override
|
2020-02-02 15:32:46 +00:00
|
|
|
{
|
|
|
|
auto sourcePath = getSourcePath();
|
|
|
|
assert(sourcePath);
|
2020-02-05 13:48:49 +00:00
|
|
|
|
2020-02-02 15:32:46 +00:00
|
|
|
// FIXME: shut up if file is already tracked.
|
|
|
|
runProgram("hg", true,
|
2020-02-05 13:48:49 +00:00
|
|
|
{ "add", *sourcePath + "/" + std::string(file) });
|
|
|
|
|
|
|
|
if (commitMsg)
|
|
|
|
runProgram("hg", true,
|
|
|
|
{ "commit", *sourcePath + "/" + std::string(file), "-m", *commitMsg });
|
2020-02-02 15:32:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 22:49:32 +00:00
|
|
|
std::pair<bool, std::string> getActualUrl() const
|
|
|
|
{
|
2020-02-02 12:06:00 +00:00
|
|
|
bool isLocal = url.scheme == "file";
|
|
|
|
return {isLocal, isLocal ? url.path : url.base};
|
2020-01-21 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<Tree, std::shared_ptr<const Input>> fetchTreeInternal(nix::ref<Store> store) const override
|
|
|
|
{
|
|
|
|
auto name = "source";
|
|
|
|
|
|
|
|
auto input = std::make_shared<MercurialInput>(*this);
|
|
|
|
|
2020-01-22 20:26:19 +00:00
|
|
|
auto [isLocal, actualUrl_] = getActualUrl();
|
|
|
|
auto actualUrl = actualUrl_; // work around clang bug
|
2020-01-21 22:49:32 +00:00
|
|
|
|
|
|
|
// FIXME: return lastModified.
|
|
|
|
|
2020-01-27 12:45:49 +00:00
|
|
|
// FIXME: don't clone local repositories.
|
|
|
|
|
2020-01-21 22:49:32 +00:00
|
|
|
if (!input->ref && !input->rev && isLocal && pathExists(actualUrl + "/.hg")) {
|
|
|
|
|
|
|
|
bool clean = runProgram("hg", true, { "status", "-R", actualUrl, "--modified", "--added", "--removed" }) == "";
|
|
|
|
|
|
|
|
if (!clean) {
|
|
|
|
|
|
|
|
/* This is an unclean working tree. So copy all tracked
|
|
|
|
files. */
|
|
|
|
|
|
|
|
if (!settings.allowDirty)
|
|
|
|
throw Error("Mercurial tree '%s' is unclean", actualUrl);
|
|
|
|
|
|
|
|
if (settings.warnDirty)
|
|
|
|
warn("Mercurial tree '%s' is unclean", actualUrl);
|
|
|
|
|
|
|
|
input->ref = chomp(runProgram("hg", true, { "branch", "-R", actualUrl }));
|
|
|
|
|
|
|
|
auto files = tokenizeString<std::set<std::string>>(
|
|
|
|
runProgram("hg", true, { "status", "-R", actualUrl, "--clean", "--modified", "--added", "--no-status", "--print0" }), "\0"s);
|
|
|
|
|
|
|
|
PathFilter filter = [&](const Path & p) -> bool {
|
|
|
|
assert(hasPrefix(p, actualUrl));
|
|
|
|
std::string file(p, actualUrl.size() + 1);
|
|
|
|
|
|
|
|
auto st = lstat(p);
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
auto prefix = file + "/";
|
|
|
|
auto i = files.lower_bound(prefix);
|
|
|
|
return i != files.end() && hasPrefix(*i, prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
return files.count(file);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto storePath = store->addToStore("source", actualUrl, true, htSHA256, filter);
|
|
|
|
|
|
|
|
return {Tree {
|
|
|
|
.actualPath = store->printStorePath(storePath),
|
|
|
|
.storePath = std::move(storePath),
|
|
|
|
}, input};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!input->ref) input->ref = "default";
|
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
auto getImmutableAttrs = [&]()
|
|
|
|
{
|
|
|
|
return Attrs({
|
|
|
|
{"type", "hg"},
|
|
|
|
{"name", name},
|
|
|
|
{"rev", input->rev->gitRev()},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
auto makeResult = [&](const Attrs & infoAttrs, StorePath && storePath)
|
|
|
|
-> std::pair<Tree, std::shared_ptr<const Input>>
|
|
|
|
{
|
2020-03-17 21:32:26 +00:00
|
|
|
assert(input->rev);
|
2020-03-17 19:54:36 +00:00
|
|
|
assert(!rev || rev == input->rev);
|
|
|
|
return {
|
|
|
|
Tree{
|
|
|
|
.actualPath = store->toRealPath(storePath),
|
|
|
|
.storePath = std::move(storePath),
|
|
|
|
.info = TreeInfo {
|
|
|
|
.revCount = getIntAttr(infoAttrs, "revCount"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
if (input->rev) {
|
|
|
|
if (auto res = getCache()->lookup(store, getImmutableAttrs()))
|
|
|
|
return makeResult(res->first, std::move(res->second));
|
|
|
|
}
|
2020-01-21 22:49:32 +00:00
|
|
|
|
|
|
|
assert(input->rev || input->ref);
|
|
|
|
auto revOrRef = input->rev ? input->rev->gitRev() : *input->ref;
|
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
Attrs mutableAttrs({
|
|
|
|
{"type", "hg"},
|
|
|
|
{"name", name},
|
|
|
|
{"url", actualUrl},
|
|
|
|
{"ref", *input->ref},
|
|
|
|
});
|
|
|
|
|
2020-03-17 21:32:26 +00:00
|
|
|
if (auto res = getCache()->lookup(store, mutableAttrs)) {
|
|
|
|
auto rev2 = Hash(getStrAttr(res->first, "rev"), htSHA1);
|
|
|
|
if (!rev || rev == rev2) {
|
|
|
|
input->rev = rev2;
|
|
|
|
return makeResult(res->first, std::move(res->second));
|
|
|
|
}
|
|
|
|
}
|
2020-01-21 22:49:32 +00:00
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
Path cacheDir = fmt("%s/nix/hg/%s", getCacheDir(), hashString(htSHA256, actualUrl).to_string(Base32, false));
|
|
|
|
|
|
|
|
/* If this is a commit hash that we already have, we don't
|
|
|
|
have to pull again. */
|
|
|
|
if (!(input->rev
|
|
|
|
&& pathExists(cacheDir)
|
|
|
|
&& runProgram(
|
|
|
|
RunOptions("hg", { "log", "-R", cacheDir, "-r", input->rev->gitRev(), "--template", "1" })
|
|
|
|
.killStderr(true)).second == "1"))
|
2020-01-21 22:49:32 +00:00
|
|
|
{
|
2020-03-17 19:54:36 +00:00
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("fetching Mercurial repository '%s'", actualUrl));
|
|
|
|
|
|
|
|
if (pathExists(cacheDir)) {
|
|
|
|
try {
|
|
|
|
runProgram("hg", true, { "pull", "-R", cacheDir, "--", actualUrl });
|
|
|
|
}
|
|
|
|
catch (ExecError & e) {
|
|
|
|
string transJournal = cacheDir + "/.hg/store/journal";
|
|
|
|
/* hg throws "abandoned transaction" error only if this file exists */
|
|
|
|
if (pathExists(transJournal)) {
|
|
|
|
runProgram("hg", true, { "recover", "-R", cacheDir });
|
2020-01-21 22:49:32 +00:00
|
|
|
runProgram("hg", true, { "pull", "-R", cacheDir, "--", actualUrl });
|
2020-03-17 19:54:36 +00:00
|
|
|
} else {
|
|
|
|
throw ExecError(e.status, fmt("'hg pull' %s", statusToString(e.status)));
|
2020-01-21 22:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-17 19:54:36 +00:00
|
|
|
} else {
|
|
|
|
createDirs(dirOf(cacheDir));
|
|
|
|
runProgram("hg", true, { "clone", "--noupdate", "--", actualUrl, cacheDir });
|
2020-01-21 22:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tokens = tokenizeString<std::vector<std::string>>(
|
|
|
|
runProgram("hg", true, { "log", "-R", cacheDir, "-r", revOrRef, "--template", "{node} {rev} {branch}" }));
|
|
|
|
assert(tokens.size() == 3);
|
|
|
|
|
|
|
|
input->rev = Hash(tokens[0], htSHA1);
|
|
|
|
auto revCount = std::stoull(tokens[1]);
|
|
|
|
input->ref = tokens[2];
|
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
if (auto res = getCache()->lookup(store, getImmutableAttrs()))
|
|
|
|
return makeResult(res->first, std::move(res->second));
|
2020-01-21 22:49:32 +00:00
|
|
|
|
|
|
|
Path tmpDir = createTempDir();
|
|
|
|
AutoDelete delTmpDir(tmpDir, true);
|
|
|
|
|
|
|
|
runProgram("hg", true, { "archive", "-R", cacheDir, "-r", input->rev->gitRev(), tmpDir });
|
|
|
|
|
|
|
|
deletePath(tmpDir + "/.hg_archival.txt");
|
|
|
|
|
|
|
|
auto storePath = store->addToStore(name, tmpDir);
|
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
Attrs infoAttrs({
|
|
|
|
{"rev", input->rev->gitRev()},
|
2020-03-17 20:34:38 +00:00
|
|
|
{"revCount", (int64_t) revCount},
|
2020-03-17 19:54:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!this->rev)
|
|
|
|
getCache()->add(
|
|
|
|
store,
|
|
|
|
mutableAttrs,
|
|
|
|
infoAttrs,
|
|
|
|
storePath,
|
|
|
|
false);
|
|
|
|
|
|
|
|
getCache()->add(
|
|
|
|
store,
|
|
|
|
getImmutableAttrs(),
|
|
|
|
infoAttrs,
|
|
|
|
storePath,
|
|
|
|
true);
|
|
|
|
|
|
|
|
return makeResult(infoAttrs, std::move(storePath));
|
2020-01-21 22:49:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MercurialInputScheme : InputScheme
|
|
|
|
{
|
|
|
|
std::unique_ptr<Input> inputFromURL(const ParsedURL & url) override
|
|
|
|
{
|
|
|
|
if (url.scheme != "hg+http" &&
|
|
|
|
url.scheme != "hg+https" &&
|
|
|
|
url.scheme != "hg+ssh" &&
|
|
|
|
url.scheme != "hg+file") return nullptr;
|
|
|
|
|
2020-01-31 18:35:28 +00:00
|
|
|
auto url2(url);
|
2020-02-02 12:06:00 +00:00
|
|
|
url2.scheme = std::string(url2.scheme, 3);
|
2020-01-31 18:35:28 +00:00
|
|
|
url2.query.clear();
|
2020-01-21 22:49:32 +00:00
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
Attrs attrs;
|
2020-01-31 18:35:28 +00:00
|
|
|
attrs.emplace("type", "hg");
|
2020-01-29 21:53:03 +00:00
|
|
|
|
2020-01-21 22:49:32 +00:00
|
|
|
for (auto &[name, value] : url.query) {
|
2020-01-31 18:35:28 +00:00
|
|
|
if (name == "rev" || name == "ref")
|
|
|
|
attrs.emplace(name, value);
|
|
|
|
else
|
|
|
|
url2.query.emplace(name, value);
|
2020-01-21 22:49:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:35:28 +00:00
|
|
|
attrs.emplace("url", url2.to_string());
|
|
|
|
|
|
|
|
return inputFromAttrs(attrs);
|
2020-01-21 22:49:32 +00:00
|
|
|
}
|
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
|
|
|
{
|
|
|
|
if (maybeGetStrAttr(attrs, "type") != "hg") return {};
|
2020-01-31 18:35:28 +00:00
|
|
|
|
|
|
|
for (auto & [name, value] : attrs)
|
|
|
|
if (name != "type" && name != "url" && name != "ref" && name != "rev")
|
|
|
|
throw Error("unsupported Mercurial input attribute '%s'", name);
|
|
|
|
|
2020-01-31 18:16:40 +00:00
|
|
|
auto input = std::make_unique<MercurialInput>(parseURL(getStrAttr(attrs, "url")));
|
2020-01-31 18:35:28 +00:00
|
|
|
if (auto ref = maybeGetStrAttr(attrs, "ref")) {
|
|
|
|
if (!std::regex_match(*ref, refRegex))
|
|
|
|
throw BadURL("invalid Mercurial branch/tag name '%s'", *ref);
|
|
|
|
input->ref = *ref;
|
|
|
|
}
|
2020-01-31 18:16:40 +00:00
|
|
|
if (auto rev = maybeGetStrAttr(attrs, "rev"))
|
|
|
|
input->rev = Hash(*rev, htSHA1);
|
|
|
|
return input;
|
|
|
|
}
|
2020-01-21 22:49:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static auto r1 = OnStartup([] { registerInputScheme(std::make_unique<MercurialInputScheme>()); });
|
|
|
|
|
|
|
|
}
|