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

Experimentally return treeHash on git and github fetchers #10344

Draft
wants to merge 1 commit 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
32 changes: 23 additions & 9 deletions src/libfetchers/git-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool operator == (const git_oid & oid1, const git_oid & oid2)

namespace nix {

struct GitInputAccessor;
struct GitInputAccessorImpl;

// Some wrapper types that ensure that the git_*_free functions get called.
template<auto del>
Expand Down Expand Up @@ -334,9 +334,11 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
}

/**
* A 'GitInputAccessor' with no regard for export-ignore or any other transformations.
* A 'GitInputAccessorImpl' with no regard for export-ignore or any other transformations.
*/
ref<GitInputAccessor> getRawAccessor(const Hash & rev);
ref<GitInputAccessorImpl> getRawAccessor(const Hash & rev);

ref<GitInputAccessor> getPlainAccessor(const Hash & rev) override;

ref<InputAccessor> getAccessor(const Hash & rev, bool exportIgnore) override;

Expand Down Expand Up @@ -477,17 +479,24 @@ ref<GitRepo> GitRepo::openRepo(const std::filesystem::path & path, bool create,
/**
* Raw git tree input accessor.
*/
struct GitInputAccessor : InputAccessor
struct GitInputAccessorImpl : GitInputAccessor
{
ref<GitRepoImpl> repo;
Tree root;

GitInputAccessor(ref<GitRepoImpl> repo_, const Hash & rev)
GitInputAccessorImpl(ref<GitRepoImpl> repo_, const Hash & rev)
: repo(repo_)
, root(peelObject<Tree>(*repo, lookupObject(*repo, hashToOID(rev)).get(), GIT_OBJECT_TREE))
{
}

Hash getTreeHash() override
{
auto * oid = git_tree_id(root.get());
assert(oid);
return toHash(*oid);
}

std::string readBlob(const CanonPath & path, bool symlink)
{
auto blob = getBlob(path, symlink);
Expand Down Expand Up @@ -922,17 +931,22 @@ struct GitFileSystemObjectSinkImpl : GitFileSystemObjectSink
}
};

ref<GitInputAccessor> GitRepoImpl::getRawAccessor(const Hash & rev)
ref<GitInputAccessorImpl> GitRepoImpl::getRawAccessor(const Hash & rev)
{
auto self = ref<GitRepoImpl>(shared_from_this());
return make_ref<GitInputAccessor>(self, rev);
return make_ref<GitInputAccessorImpl>(self, rev);
}

ref<GitInputAccessor> GitRepoImpl::getPlainAccessor(const Hash & rev)
{
return getRawAccessor(rev);
}

ref<InputAccessor> GitRepoImpl::getAccessor(const Hash & rev, bool exportIgnore)
{
auto self = ref<GitRepoImpl>(shared_from_this());
ref<GitInputAccessor> rawGitAccessor = getRawAccessor(rev);
ref<GitInputAccessorImpl> rawGitAccessor = getRawAccessor(rev);
if (exportIgnore) {
auto self = ref<GitRepoImpl>(shared_from_this());
return make_ref<GitExportIgnoreInputAccessor>(self, rawGitAccessor, rev);
}
else {
Expand Down
12 changes: 12 additions & 0 deletions src/libfetchers/git-utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ struct GitFileSystemObjectSink : FileSystemObjectSink
virtual Hash sync() = 0;
};

/**
* Git Input Accessor
*
* Created from `GitRepo`. Support some additional operations.
*/
struct GitInputAccessor : InputAccessor
{
virtual Hash getTreeHash() = 0;
};

struct GitRepo
{
virtual ~GitRepo()
Expand Down Expand Up @@ -75,6 +85,8 @@ struct GitRepo

virtual bool hasObject(const Hash & oid) = 0;

virtual ref<GitInputAccessor> getPlainAccessor(const Hash & rev) = 0;

virtual ref<InputAccessor> getAccessor(const Hash & rev, bool exportIgnore) = 0;

virtual ref<InputAccessor> getAccessor(const WorkdirInfo & wd, bool exportIgnore, MakeNotAllowedError makeNotAllowedError) = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/libfetchers/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ struct GitInputScheme : InputScheme
"shallow",
"submodules",
"exportIgnore",
"treeHash",
"lastModified",
"revCount",
"narHash",
Expand Down Expand Up @@ -601,6 +602,8 @@ struct GitInputScheme : InputScheme

auto rev = *input.getRev();

auto gotTreeHash = repo->getPlainAccessor(rev)->getTreeHash();

Attrs infoAttrs({
{"rev", rev.gitRev()},
{"lastModified", getLastModified(repoInfo, repoDir, rev)},
Expand Down Expand Up @@ -646,6 +649,11 @@ struct GitInputScheme : InputScheme
mounts.insert_or_assign(CanonPath::root, accessor);
accessor = makeMountedInputAccessor(std::move(mounts));
}
} else {
/* If we don't have submodules and aren't doing export
ignore, then the tree hash is useful info to provide. */
Comment on lines +653 to +654
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • How is it useful?
  • getSubmodulesAttr(input) doesn't tell us anything about whether the repo has submodules. false doesn't imply anything, and true only correlates.

if (experimentalFeatureSettings.isEnabled(Xp::GitHashing) && !exportIgnore)
input.attrs.insert_or_assign("treeHash", gotTreeHash.gitRev());
}

assert(!origRev || origRev == rev);
Expand Down
5 changes: 2 additions & 3 deletions src/libfetchers/github.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,8 @@ struct GitArchiveInputScheme : InputScheme
{
auto [input, tarballInfo] = downloadArchive(store, _input);

#if 0
input.attrs.insert_or_assign("treeHash", tarballInfo.treeHash.gitRev());
#endif
if (experimentalFeatureSettings.isEnabled(Xp::GitHashing))
input.attrs.insert_or_assign("treeHash", tarballInfo.treeHash.gitRev());
input.attrs.insert_or_assign("lastModified", uint64_t(tarballInfo.lastModified));

auto accessor = getTarballCache()->getAccessor(tarballInfo.treeHash, false);
Expand Down
Loading