Skip to content

Commit 617fe32

Browse files
committed
fetchGit: don't resolve HEAD ref when a specific rev is requested
When fetchGit is called with a rev but no explicit ref, the code unconditionally called getDefaultRef() which contacts the remote to resolve HEAD. This caused an unnecessary network round-trip (~800ms) even when the requested rev was already in the local cache. Skip resolving the default ref when a rev is specified, since the rev can be fetched directly by its hash. Fixes NixOS#10773.
1 parent a7a6c4f commit 617fe32

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/libfetchers/git.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,15 @@ struct GitInputScheme : InputScheme
858858

859859
auto originalRef = input.getRef();
860860
bool shallow = canDoShallow(input);
861-
auto ref = originalRef ? *originalRef : getDefaultRef(repoInfo, shallow);
862-
input.attrs.insert_or_assign("ref", ref);
861+
862+
/* When a specific rev is requested without an explicit ref, don't
863+
resolve the default ref (which would contact the remote). The
864+
rev can be fetched directly by its hash. */
865+
auto ref = originalRef ? *originalRef
866+
: !origRev ? getDefaultRef(repoInfo, shallow)
867+
: std::string{};
868+
if (!ref.empty())
869+
input.attrs.insert_or_assign("ref", ref);
863870

864871
std::filesystem::path repoDir;
865872

@@ -941,7 +948,7 @@ struct GitInputScheme : InputScheme
941948
} catch (Error & e) {
942949
warn("could not update mtime for file %s: %s", localRefFile, e.info().msg);
943950
}
944-
if (!originalRef && !storeCachedHead(repoUrl.to_string(), shallow, ref))
951+
if (!originalRef && !ref.empty() && !storeCachedHead(repoUrl.to_string(), shallow, ref))
945952
warn("could not update cached head '%s' for '%s'", ref, repoInfo.locationToArg());
946953
}
947954

0 commit comments

Comments
 (0)