Skip to content

Cache getLegacyGitAccessor()#396

Merged
edolstra merged 3 commits into
mainfrom
eelcodolstra/nix-349
Mar 24, 2026
Merged

Cache getLegacyGitAccessor()#396
edolstra merged 3 commits into
mainfrom
eelcodolstra/nix-349

Conversation

@edolstra
Copy link
Copy Markdown
Collaborator

@edolstra edolstra commented Mar 24, 2026

Motivation

This prevents repeated calls to git checkout / git archive when nix-219-compat is enabled.

Context

Summary by CodeRabbit

  • Optimizations
    • Improved Git caching so repository HEADs and legacy accesses are reused more broadly, reducing redundant downloads and exports for repeat operations.
  • Bug Fixes
    • More robust handling of submodule exports and quieter fetch/checkout steps, improving reliability and reducing noisy output.

This prevents repeated calls to git checkout / git archive.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 24, 2026

📝 Walkthrough

Walkthrough

Updated GitInputScheme::getLegacyGitAccessor to accept const Settings & settings and use settings.getCache() for cache lookup/upsert around the legacy git export flow; call sites were updated to pass settings. Also adjusted some git commands and export-ignore behavior for submodules.

Changes

Cohort / File(s) Summary
Git legacy accessor
src/libfetchers/git.cc
Added const Settings & settings param to getLegacyGitAccessor; compute legacy fingerprint and cache key; lookup via settings.getCache() and on hit parse stored SRI to construct fixed-output path and validate via maybeQueryPathInfo; on miss run legacy export and upsert cache with {"hash": <narHash SRI>}. Forced exportIgnore when !options.submodules. Adjusted git init to -b master and added --quiet to git fetch/git checkout. Updated call sites in getAccessorFromCommit to pass settings in both nix219Compat and NAR-hash-mismatch paths.

Sequence Diagram

sequenceDiagram
    participant Caller as getAccessorFromCommit
    participant Accessor as getLegacyGitAccessor
    participant Cache as settings.getCache()
    participant Export as Legacy Git Export
    participant Store as Fixed-output Store

    Caller->>Accessor: call(settings, repo, options)
    Accessor->>Accessor: compute fingerprint & cacheKey
    Accessor->>Cache: lookup(cacheKey)
    alt cache hit
        Cache-->>Accessor: return stored SRI
        Accessor->>Accessor: parse SRI -> nar path
        Accessor->>Store: maybeQueryPathInfo(path)
        Store-->>Accessor: path info
        Accessor-->>Caller: return accessor (cached)
    else cache miss
        Cache-->>Accessor: not found
        Accessor->>Export: run legacy git export (git init/fetch/checkout)
        Export-->>Accessor: narHash SRI
        Accessor->>Cache: upsert(cacheKey, {"hash": SRI})
        Accessor-->>Caller: return accessor (new, fingerprint set)
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • grahamc
  • cole-h

Poem

🐰 I hopped through git and cached the trail,
Fingerprints tucked in a cozy pail.
Lookup, fetch, or upsert delight—
Nar hash snug in burrowed light.
🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately captures the main objective of the changeset: adding caching for getLegacyGitAccessor() to improve performance.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch eelcodolstra/nix-349

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

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 24, 2026

@github-actions github-actions Bot temporarily deployed to pull request March 24, 2026 14:34 Inactive
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/libfetchers/git.cc`:
- Around line 843-847: The git init call uses the unsupported "-b master" form;
replace that argument list in the runProgram invocation that calls git init (the
runProgram entry that currently uses {"init", tmpDir, "-b", "master"}) with the
documented compatibility pattern using "-c",
"init.defaultBranch=<gitInitialBranch>", "init", tmpDir and reuse the existing
gitInitialBranch variable so the init honors the configured default branch
consistently with the other git invocations.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6a9c6054-b0eb-4089-8833-90b27dca1163

📥 Commits

Reviewing files that changed from the base of the PR and between 8483ea6 and 84f79c0.

📒 Files selected for processing (1)
  • src/libfetchers/git.cc

Comment thread src/libfetchers/git.cc
Copy link
Copy Markdown
Member

@cole-h cole-h left a comment

Choose a reason for hiding this comment

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

LGTM

Comment thread src/libfetchers/git.cc
? [&]() {
// Nix < 2.20 used `git checkout` for repos with submodules.
runProgram({.program = "git", .args = {"init", tmpDir}});
runProgram({.program = "git", .args = {"init", tmpDir, "-b", "master"}});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is the -b master required now? Just to make it "reproducible" (more easily correctly cache-able)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also related (I think): the LLM comment above.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It's to shut up a harmless warning about not having a default branch.

@edolstra edolstra enabled auto-merge March 24, 2026 17:30
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/libfetchers/git.cc (1)

802-805: Consider accepting options by value to avoid mutating caller's state.

The function modifies options.exportIgnore on line 805, but options is passed by mutable reference. While the current control flow doesn't cause issues (the submodules block is guarded by if (options.submodules)), mutating input parameters can lead to subtle bugs during future refactoring.

♻️ Suggested fix
     ref<SourceAccessor> getLegacyGitAccessor(
         const Settings & settings,
         Store & store,
         RepoInfo & repoInfo,
         const std::filesystem::path & repoDir,
         const Hash & rev,
-        GitAccessorOptions & options) const
+        GitAccessorOptions options) const
     {
-        if (!options.submodules)
-            options.exportIgnore = true;
+        options.exportIgnore = options.exportIgnore || !options.submodules;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/libfetchers/git.cc` around lines 802 - 805, This function currently takes
GitAccessorOptions &options and mutates options.exportIgnore when
!options.submodules; change the parameter to take GitAccessorOptions options
(pass by value) so mutations affect only the local copy, update the function
signature accordingly, and ensure all internal uses refer to the local options
(including the existing check of options.submodules and assignment to
options.exportIgnore) to avoid mutating the caller's state.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/libfetchers/git.cc`:
- Around line 802-805: This function currently takes GitAccessorOptions &options
and mutates options.exportIgnore when !options.submodules; change the parameter
to take GitAccessorOptions options (pass by value) so mutations affect only the
local copy, update the function signature accordingly, and ensure all internal
uses refer to the local options (including the existing check of
options.submodules and assignment to options.exportIgnore) to avoid mutating the
caller's state.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5b059abc-0581-49d7-9b2c-c5087c309ecc

📥 Commits

Reviewing files that changed from the base of the PR and between 84f79c0 and 39a204a.

📒 Files selected for processing (1)
  • src/libfetchers/git.cc

@github-actions github-actions Bot temporarily deployed to pull request March 24, 2026 17:38 Inactive
@edolstra edolstra added this pull request to the merge queue Mar 24, 2026
Merged via the queue into main with commit 73cc313 Mar 24, 2026
28 checks passed
@edolstra edolstra deleted the eelcodolstra/nix-349 branch March 24, 2026 18:11
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.

2 participants