Skip to content

Commit

Permalink
Support substituting in fetchGit
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewbauer committed Jun 22, 2020
1 parent 62d6ee0 commit 5fa33d7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/libfetchers/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ struct GitInput : Input

auto ingestionMethod = treeHash ? FileIngestionMethod::Git : FileIngestionMethod::Recursive;

// try to substitute
if (settings.useSubstitutes && treeHash && !submodules) {
auto storePath = fetchers::trySubstitute(store, ingestionMethod, *treeHash, name);
if (storePath) {
return {
Tree {
.actualPath = store->toRealPath(*storePath),
.storePath = std::move(*storePath),
.info = TreeInfo {
.revCount = std::nullopt,
.lastModified = 0,
},
},
input
};
}
}

std::string cacheType = "git";
if (shallow) cacheType += "-shallow";
if (submodules) cacheType += "-submodules";
Expand Down Expand Up @@ -385,7 +403,7 @@ struct GitInput : Input
unpackTarfile(*source, tmpDir);
}

auto storePath = store->addToStore(name, tmpDir, FileIngestionMethod::Recursive, ingestionMethod == FileIngestionMethod::Git ? htSHA1 : htSHA256, filter);
auto storePath = store->addToStore(name, tmpDir, ingestionMethod, ingestionMethod == FileIngestionMethod::Git ? htSHA1 : htSHA256, filter);

// verify treeHash is what we actually obtained in the nix store
if (input->treeHash) {
Expand Down
9 changes: 7 additions & 2 deletions tests/git.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
source common.sh

clearStore
clearCache

try () {
hash=$(nix hash-git --base16 --type sha1 $TEST_ROOT/hash-path)
Expand Down Expand Up @@ -40,8 +41,6 @@ test "$hash3" = "sha256:1i2x80840igikhbyy7nqf08ymx3a6n83x1fzyrxvddf0sdl5nqvp"
if [[ -n $(type -p git) ]]; then
repo=$TEST_ROOT/git

export _NIX_FORCE_HTTP=1

rm -rf $repo $TEST_HOME/.cache/nix

git init $repo
Expand All @@ -64,6 +63,12 @@ if [[ -n $(type -p git) ]]; then

# Submodules cause error.
(! nix eval --raw "(builtins.fetchTree { type = \"git\"; url = file://$repo; treeHash = \"$treeHash\"; submodules = true; }).outPath")

# Check that we can substitute it from other places.
nix copy --to file://$cacheDir $path
nix-store --delete $path
path2=$(nix eval --raw "(builtins.fetchTree { type = \"git\"; url = file:///no-such-repo; treeHash = \"$treeHash\"; }).outPath" --substituters file://$cacheDir)
[ $path2 = $path ]
else
echo "Git not installed; skipping Git tests"
fi

0 comments on commit 5fa33d7

Please sign in to comment.