Skip to content

fix(gix-ref): read loose refs that vanish while iterating from a fresh packed-refs - #3249

Open
Oleg Stotsky (OlegStotsky) wants to merge 1 commit into
GitoxideLabs:mainfrom
OlegStotsky:fix-vanished-loose-refs
Open

Oleg Stotsky (OlegStotsky) wants to merge 1 commit into
GitoxideLabs:mainfrom
OlegStotsky:fix-vanished-loose-refs

Conversation

@OlegStotsky

Copy link
Copy Markdown

This PR was written by Claude Code, an AI agent, working through Oleg Stotsky (@OlegStotsky)'s account.

Motivation

A service runs gix inside a server-side pre-receive hook, and the hook lists refs/heads/ of the repository being pushed to. Other pushes land on the same repository at the same time, and receive-pack starts git gc --auto after them. One night the hook refused an unrelated push with:

Could not read reference, path=…/refs/heads/<another branch>

The branch was never deleted. pack-refs --prune had moved it into packed-refs between the iterator listing refs/heads/ and opening the file.

The change

LooseThenPacked reads a directory's entries and opens each loose file later. When that file is missing, iterators from file::Store::iter() (all() and prefixed()) now open packed-refs afresh and look the ref up there:

  • found: the packed value is returned. pack-refs --prune writes packed-refs before it unlinks loose files, so a ref that was packed is always found.
  • not found: the ref was deleted in between, and it is skipped.

Other read errors are still returned. loose_iter(), loose_iter_prefixed() and pseudo() keep reporting a missing file, since they don't list packed refs. Each missing file costs one read of packed-refs, which only happens while refs are packed or deleted under the iterator.

The new test overlay_iter_survives_refs_packed_and_deleted_while_iterating reads the first ref of refs/heads/, runs git pack-refs --all --prune and deletes another branch, then reads the rest. Without the fix it fails with Could not read reference, path=…/refs/heads/d1.

Not covered: refs packed before their directory is read

Platform takes its packed-refs snapshot when it is created, and the loose directories are read later. If pack-refs --prune finishes in between, a ref that existed the whole time is in neither the stale snapshot nor the directory, and the iterator silently leaves it out. Git avoids this by reading the loose refs under the prefix before it checks packed-refs (the comment in files_ref_iterator_begin() in refs/files-backend.c). Doing the same here would mean taking the snapshot after the loose walk instead of in Platform, which changes the API, so it is left out of this PR. A follow-up can do it if you want that change.

Checks run

By the agent, locally: cargo test -p gix-ref with the default and GIX_TEST_FIXTURE_HASH=sha256 fixtures, cargo fmt --check, and cargo clippy -p gix-ref --all-targets, which adds no new warnings. just test was not run.

…h `packed-refs`

`LooseThenPacked` lists a directory of loose refs and opens each file later.
If `git pack-refs --prune` runs in between, which `git gc --auto` does after
pushes, a listed file is gone by the time it is opened. The ref still exists
in `packed-refs`, but the iterator returned `Could not read reference` for it.
A ref deleted in between failed the same way.

Iterators from `file::Store::iter()` now read `packed-refs` afresh when a
listed loose file is missing. Git writes `packed-refs` before it removes loose
files, so the fresh file has the ref unless it was deleted, and a deleted ref
is skipped. Loose-only iteration still reports the missing file.

This was found in a server-side pre-receive hook that lists `refs/heads/`
with `gix` while other pushes and `git gc --auto` run on the same repository.
An unrelated push was refused because one branch was packed mid-listing.

Assisted-by: Claude Code (claude-opus-5-5)
@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T14:27:38.173014Z 54cdd85 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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.

1 participant