Skip to content

Commit 98f31ca

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 98f31ca

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/libfetchers/git.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,13 @@ 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 : !origRev ? getDefaultRef(repoInfo, shallow) : std::string{};
866+
if (!ref.empty())
867+
input.attrs.insert_or_assign("ref", ref);
863868

864869
std::filesystem::path repoDir;
865870

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

0 commit comments

Comments
 (0)