Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch Git submodules lazily #11425

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/libfetchers/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "processes.hh"
#include "git.hh"
#include "mounted-source-accessor.hh"
#include "lazy-source-accessor.hh"
#include "git-utils.hh"
#include "logging.hh"
#include "finally.hh"
Expand Down Expand Up @@ -645,22 +646,27 @@ struct GitInputScheme : InputScheme
std::map<CanonPath, nix::ref<SourceAccessor>> mounts;

for (auto & [submodule, submoduleRev] : repo->getSubmodules(rev, exportIgnore)) {
auto resolved = repo->resolveSubmoduleUrl(submodule.url);
debug("Git submodule %s: %s %s %s -> %s",
submodule.path, submodule.url, submodule.branch, submoduleRev.gitRev(), resolved);
fetchers::Attrs attrs;
attrs.insert_or_assign("type", "git");
attrs.insert_or_assign("url", resolved);
if (submodule.branch != "")
attrs.insert_or_assign("ref", submodule.branch);
attrs.insert_or_assign("rev", submoduleRev.gitRev());
attrs.insert_or_assign("exportIgnore", Explicit<bool>{ exportIgnore });
attrs.insert_or_assign("submodules", Explicit<bool>{ true });
attrs.insert_or_assign("allRefs", Explicit<bool>{ true });
auto submoduleInput = fetchers::Input::fromAttrs(*input.settings, std::move(attrs));
auto [submoduleAccessor, submoduleInput2] =
submoduleInput.getAccessor(store);
submoduleAccessor->setPathDisplay("«" + submoduleInput.to_string() + "»");
auto submoduleAccessor = make_ref<LazySourceAccessor>([repoDir, submodule, submoduleRev, exportIgnore, store, settings(input.settings)]()
{
auto repo = GitRepo::openRepo(repoDir);
auto resolved = repo->resolveSubmoduleUrl(submodule.url);
debug("Git submodule %s: %s %s %s -> %s",
submodule.path, submodule.url, submodule.branch, submoduleRev.gitRev(), resolved);
fetchers::Attrs attrs;
attrs.insert_or_assign("type", "git");
attrs.insert_or_assign("url", resolved);
if (submodule.branch != "")
attrs.insert_or_assign("ref", submodule.branch);
attrs.insert_or_assign("rev", submoduleRev.gitRev());
attrs.insert_or_assign("exportIgnore", Explicit<bool>{ exportIgnore });
attrs.insert_or_assign("submodules", Explicit<bool>{ true });
attrs.insert_or_assign("allRefs", Explicit<bool>{ true });
auto submoduleInput = fetchers::Input::fromAttrs(*settings, std::move(attrs));
auto [submoduleAccessor, submoduleInput2] =
submoduleInput.getAccessor(store);
submoduleAccessor->setPathDisplay("«" + submoduleInput.to_string() + "»");
return submoduleAccessor;
});
mounts.insert_or_assign(submodule.path, submoduleAccessor);
}

Expand Down
69 changes: 69 additions & 0 deletions src/libutil/lazy-source-accessor.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once

#include "source-accessor.hh"
#include "sync.hh"

#include <variant>

namespace nix {

/**
* A wrapper `SourceAccessor` that lazily constructs an underlying
* `SourceAccessor`.
*/
struct LazySourceAccessor : SourceAccessor
{
using Fun = std::function<ref<SourceAccessor>()>;

Sync<std::variant<ref<SourceAccessor>, Fun>> next_;

LazySourceAccessor(Fun next)
: next_{{std::move(next)}}
{
}

ref<SourceAccessor> getNext()
{
auto next(next_.lock());
if (auto accessor = std::get_if<ref<SourceAccessor>>(&*next))
return *accessor;
else {
auto fun = std::get<Fun>(*next);
auto acc = fun();
*next = acc;
return acc;
}
}

std::string readFile(const CanonPath & path) override
{
return getNext()->readFile(path);
}

void readFile(const CanonPath & path, Sink & sink, std::function<void(uint64_t)> sizeCallback) override
{
return getNext()->readFile(path, sink, sizeCallback);
}

bool pathExists(const CanonPath & path) override
{
return getNext()->pathExists(path);
}

std::optional<Stat> maybeLstat(const CanonPath & path) override
{
return getNext()->maybeLstat(path);
}

DirEntries readDirectory(const CanonPath & path) override
{
return getNext()->readDirectory(path);
}

std::string readLink(const CanonPath & path) override
{
return getNext()->readLink(path);
}
};

}
1 change: 1 addition & 0 deletions src/libutil/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ headers = [config_h] + files(
'hilite.hh',
'json-impls.hh',
'json-utils.hh',
'lazy-source-accessor.hh',
'logging.hh',
'lru-cache.hh',
'memory-source-accessor.hh',
Expand Down
Loading