diff --git a/src/libfetchers/git.cc b/src/libfetchers/git.cc index 85902126dbf..49587cf461b 100644 --- a/src/libfetchers/git.cc +++ b/src/libfetchers/git.cc @@ -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"; @@ -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) { diff --git a/tests/git.sh b/tests/git.sh index bdc98844a0d..cd9979ada29 100644 --- a/tests/git.sh +++ b/tests/git.sh @@ -1,6 +1,7 @@ source common.sh clearStore +clearCache try () { hash=$(nix hash-git --base16 --type sha1 $TEST_ROOT/hash-path) @@ -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 @@ -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