Skip to content
Open
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
11 changes: 8 additions & 3 deletions src/libfetchers/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,13 @@ struct GitInputScheme : InputScheme

auto originalRef = input.getRef();
bool shallow = canDoShallow(input);
auto ref = originalRef ? *originalRef : getDefaultRef(repoInfo, shallow);
input.attrs.insert_or_assign("ref", ref);

/* When a specific rev is requested without an explicit ref, don't
resolve the default ref (which would contact the remote). The
rev can be fetched directly by its hash. */
auto ref = originalRef ? *originalRef : !origRev ? getDefaultRef(repoInfo, shallow) : std::string{};
Copy link
Member

Choose a reason for hiding this comment

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

I think the std::string{} will break things:

The ref is used in a call to resolveRev below, which basically asks git to resolve the string $REF^{commit} (to "peel the ref ultimately down to the underlying commit"). ^{commit} (REF="") will fail to parse, so I think we need to figure something out there.

if (!ref.empty())
input.attrs.insert_or_assign("ref", ref);

std::filesystem::path repoDir;

Expand Down Expand Up @@ -941,7 +946,7 @@ struct GitInputScheme : InputScheme
} catch (Error & e) {
warn("could not update mtime for file %s: %s", localRefFile, e.info().msg);
}
if (!originalRef && !storeCachedHead(repoUrl.to_string(), shallow, ref))
if (!originalRef && !ref.empty() && !storeCachedHead(repoUrl.to_string(), shallow, ref))
warn("could not update cached head '%s' for '%s'", ref, repoInfo.locationToArg());
}

Expand Down
13 changes: 13 additions & 0 deletions tests/functional/fetchGit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ git -C "$repo" commit -m 'Bla4'
rev3=$(git -C "$repo" rev-parse HEAD)
nix eval --tarball-ttl 3600 --expr "builtins.fetchGit { url = $repo; rev = \"$rev3\"; }" >/dev/null

# Fetching a rev that is already cached should not hit the network,
# even when the cached HEAD ref has expired (NixOS/nix#10773).
export _NIX_FORCE_HTTP=1
nix eval --expr "builtins.fetchGit { url = file://$repo; rev = \"$rev3\"; }" >/dev/null
# Make the repo unavailable so any network access triggers a warning.
mv "$repo" "${repo}"-tmp
path6=$(nix eval --tarball-ttl 0 --raw --expr "(builtins.fetchGit { url = file://$repo; rev = \"$rev3\"; }).outPath" 2>"$TEST_ROOT/stderr-10773")
[[ $(cat "$path6"/hello) = delft ]]
# The old code would try to resolve HEAD here and emit this warning.
(! grep -q "could not get HEAD ref" < "$TEST_ROOT/stderr-10773")
mv "${repo}"-tmp "$repo"
unset _NIX_FORCE_HTTP

# Update 'path' to reflect latest master
path=$(nix eval --impure --raw --expr "(builtins.fetchGit file://$repo).outPath")

Expand Down