mirror of
https://github.com/NixOS/nix
synced 2025-06-28 17:51:15 +02:00
downloadCached: Return ETag
This allows fetchFlake() to return the Git revision of a GitHub archive.
This commit is contained in:
parent
6e9182fbc2
commit
529add316c
7 changed files with 64 additions and 26 deletions
|
@ -46,7 +46,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
|
|||
Path lookupFileArg(EvalState & state, string s)
|
||||
{
|
||||
if (isUri(s))
|
||||
return getDownloader()->downloadCached(state.store, s, true);
|
||||
return getDownloader()->downloadCached(state.store, s, true).path;
|
||||
else if (s.size() > 2 && s.at(0) == '<' && s.at(s.size() - 1) == '>') {
|
||||
Path p = s.substr(1, s.size() - 2);
|
||||
return state.findFile(p);
|
||||
|
|
|
@ -657,7 +657,7 @@ std::pair<bool, std::string> EvalState::resolveSearchPathElem(const SearchPathEl
|
|||
|
||||
if (isUri(elem.second)) {
|
||||
try {
|
||||
res = { true, getDownloader()->downloadCached(store, elem.second, true) };
|
||||
res = { true, getDownloader()->downloadCached(store, elem.second, true).path };
|
||||
} catch (DownloadError & e) {
|
||||
printError(format("warning: Nix search path entry '%1%' cannot be downloaded, ignoring") % elem.second);
|
||||
res = { false, "" };
|
||||
|
|
|
@ -2112,7 +2112,7 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
|
|||
if (evalSettings.pureEval && !expectedHash)
|
||||
throw Error("in pure evaluation mode, '%s' requires a 'sha256' argument", who);
|
||||
|
||||
Path res = getDownloader()->downloadCached(state.store, url, unpack, name, expectedHash);
|
||||
Path res = getDownloader()->downloadCached(state.store, url, unpack, name, expectedHash).path;
|
||||
|
||||
if (state.allowedPaths)
|
||||
state.allowedPaths->insert(res);
|
||||
|
|
|
@ -87,7 +87,13 @@ static FlakeRef lookupFlake(EvalState & state, const FlakeRef & flakeRef,
|
|||
return flakeRef;
|
||||
}
|
||||
|
||||
static Path fetchFlake(EvalState & state, const FlakeRef & flakeRef)
|
||||
struct FlakeSourceInfo
|
||||
{
|
||||
Path storePath;
|
||||
std::optional<Hash> rev;
|
||||
};
|
||||
|
||||
static FlakeSourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef)
|
||||
{
|
||||
assert(flakeRef.isDirect());
|
||||
|
||||
|
@ -99,25 +105,36 @@ static Path fetchFlake(EvalState & state, const FlakeRef & flakeRef)
|
|||
|
||||
// FIXME: support passing auth tokens for private repos.
|
||||
|
||||
auto storePath = getDownloader()->downloadCached(state.store,
|
||||
fmt("https://api.github.com/repos/%s/%s/tarball/%s",
|
||||
refData->owner, refData->repo,
|
||||
refData->rev
|
||||
? refData->rev->to_string(Base16, false)
|
||||
: refData->ref
|
||||
? *refData->ref
|
||||
: "master"),
|
||||
true, "source");
|
||||
auto url = fmt("https://api.github.com/repos/%s/%s/tarball/%s",
|
||||
refData->owner, refData->repo,
|
||||
refData->rev
|
||||
? refData->rev->to_string(Base16, false)
|
||||
: refData->ref
|
||||
? *refData->ref
|
||||
: "master");
|
||||
|
||||
// FIXME: extract revision hash from ETag.
|
||||
auto result = getDownloader()->downloadCached(state.store, url, true, "source");
|
||||
|
||||
return storePath;
|
||||
if (!result.etag)
|
||||
throw Error("did not receive an ETag header from '%s'", url);
|
||||
|
||||
if (result.etag->size() != 42 || (*result.etag)[0] != '"' || (*result.etag)[41] != '"')
|
||||
throw Error("ETag header '%s' from '%s' is not a Git revision", *result.etag, url);
|
||||
|
||||
FlakeSourceInfo info;
|
||||
info.storePath = result.path;
|
||||
info.rev = Hash(std::string(*result.etag, 1, result.etag->size() - 2), htSHA1);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
else if (auto refData = std::get_if<FlakeRef::IsGit>(&flakeRef.data)) {
|
||||
auto gitInfo = exportGit(state.store, refData->uri, refData->ref,
|
||||
refData->rev ? refData->rev->to_string(Base16, false) : "", "source");
|
||||
return gitInfo.storePath;
|
||||
FlakeSourceInfo info;
|
||||
info.storePath = gitInfo.storePath;
|
||||
info.rev = Hash(gitInfo.rev, htSHA1);
|
||||
return info;
|
||||
}
|
||||
|
||||
else abort();
|
||||
|
@ -138,7 +155,11 @@ struct Flake
|
|||
|
||||
static Flake getFlake(EvalState & state, const FlakeRef & flakeRef)
|
||||
{
|
||||
auto flakePath = fetchFlake(state, flakeRef);
|
||||
auto sourceInfo = fetchFlake(state, flakeRef);
|
||||
debug("got flake source '%s' with revision %s",
|
||||
sourceInfo.storePath, sourceInfo.rev.value_or(Hash(htSHA1)).to_string(Base16, false));
|
||||
|
||||
auto flakePath = sourceInfo.storePath;
|
||||
state.store->assertStorePath(flakePath);
|
||||
|
||||
if (state.allowedPaths)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue