Skip to content

fetchGit: don't resolve HEAD ref when a specific rev is requested#346

Open
grahamc wants to merge 2 commits intomainfrom
push-qsrpowwzxmwy
Open

fetchGit: don't resolve HEAD ref when a specific rev is requested#346
grahamc wants to merge 2 commits intomainfrom
push-qsrpowwzxmwy

Conversation

@grahamc
Copy link
Member

@grahamc grahamc commented Feb 11, 2026

Motivation

Fixes NixOS#10773.

Note that I used AI as part of making this PR. It looks sensible to me? But I didn't think as hard about this one as I usually do.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved Git revision fetching to avoid unnecessary remote lookups when explicit revisions are specified.
    • Enhanced cache handling for Git HEAD references to prevent storing invalid references.
  • Tests

    • Added validation test for Git revision fetching from cache without triggering network access.

@coderabbitai
Copy link

coderabbitai bot commented Feb 11, 2026

Caution

Review failed

An error occurred during the review process. Please try again later.

📝 Walkthrough

Walkthrough

The PR refines Git fetching logic to prevent unnecessary network hits when a specific revision is requested without an explicit ref. The code now omits ref resolution against the remote for explicit rev paths, storing the ref in input attributes only when non-empty. A test is added to verify that cached revisions can be fetched without network access.

Changes

Cohort / File(s) Summary
Git Fetcher Logic
src/libfetchers/git.cc
Refined ref evaluation to avoid resolving default ref against remote when explicit rev is provided; ref is only inserted into input.attrs when non-empty; cache update condition tightened to only store cached HEAD when non-empty ref is present; same logic applied consistently across commit and non-commit accessor paths.
Git Fetching Tests
tests/functional/fetchGit.sh
Added test scenario to verify that fetching a cached Git revision does not trigger network access even with expired cached HEAD reference, toggling network access via _NIX_FORCE_HTTP and asserting correct behavior without network hits.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • Avoid unnecessary Git refetches #270 — Modifies git.cc's ref determination and cache-update behavior for Git fetches with similar logic changes to ref handling and shallow/non-shallow reuse.

Suggested reviewers

  • cole-h

Poem

🐰 A speedy rabbit hops with glee,
No network calls when rev's set free!
The cache remembers, swift and true,
No HEAD resolutions—just the rev we knew! 🚀

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: preventing default HEAD ref resolution when a specific rev is requested, which directly addresses the linked issue.
Linked Issues check ✅ Passed The PR successfully addresses the core requirement from issue #10773: eliminating unnecessary network requests when fetching a fixed rev by skipping default ref resolution.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the linked issue: modifications to git.cc refine ref resolution logic, and the test verifies the fix works as intended.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch push-qsrpowwzxmwy

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

github-actions bot commented Feb 11, 2026

@github-actions github-actions bot temporarily deployed to pull request February 11, 2026 15:47 Inactive
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.
@github-actions github-actions bot temporarily deployed to pull request February 11, 2026 16:38 Inactive
/* 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fetchGit with a fixed rev and no ref repeatedly hits the network

2 participants