Skip to content

fix(gitclone): include HEAD in local refs - #452

Merged
alecthomas merged 1 commit into
mainfrom
aat/evaluate-reported-bug
Nov 17, 2026
Merged

alecthomas merged 1 commit into
mainfrom
aat/evaluate-reported-bug

Conversation

@alecthomas

@alecthomas alecthomas commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

An ensure-refs request for HEAD could never be satisfied. GetLocalRefs used git for-each-ref, which only lists refs/... and never reports HEAD. Every such request fell through to a full upstream fetch, and the response still had no SHA for HEAD. Clients that ask for HEAD on each clone paid for that fetch every time. For large repositories that was several seconds per clone, plus an unnecessary fetch against upstream.

GetLocalRefs now lists refs with git show-ref --head, so local refs include HEAD, as the upstream ls-remote listing already does. show-ref exits 1 with no output when a repository has no refs, so that case returns an empty map. A HEAD request with an empty SHA now follows the documented contract: the ref exists, at any SHA, so no fetch is needed.

TestRepository_EnsureRefs now asserts that a HEAD request on an up-to-date mirror returns fetched=false with the resolved SHA.

Other requests that the mirror cannot satisfy even after fetching, such as a missing ref or commit, still fetch upstream on every call. That will be a separate fix.

🤖 Generated with Claude Code

An ensure-refs request for HEAD could never be satisfied, because
GetLocalRefs used `git for-each-ref`, which only lists refs/... and so
never reports HEAD. Every such request fell through to a full upstream
fetch, and the response still had no SHA for HEAD. Clients that ask for
HEAD on each clone paid for that fetch every time.

List refs with `git show-ref --head` instead, so local refs include HEAD
as the upstream ls-remote listing already does.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@alecthomas
alecthomas marked this pull request as ready for review November 17, 2026 02:30
@alecthomas
alecthomas requested a review from a team as a code owner November 17, 2026 02:30
@alecthomas
alecthomas requested review from joshfriend and removed request for a team November 17, 2026 02:30
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
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-17T02:31:40.574608Z cac5c96 Draft marked ready
ℹ️ About Codex in GitHub

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

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cac5c96816

ℹ️ About Codex in GitHub

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

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

output, err = cmd.CombinedOutput()
r.mu.RUnlock()

// show-ref exits 1 with no output when the repository has no refs.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove the new short-block explanatory comments

The new inline comment describes a three-line conditional, contrary to the root AGENTS.md requirement that non-documentation comments only accompany non-obvious blocks of at least 20 lines. The new comment above the five-line HEAD test has the same issue. Remove these two inline comments while retaining the required exported-method documentation.

AGENTS.md reference: AGENTS.md:L19-L22

Useful? React with 👍 / 👎.

@alecthomas
alecthomas merged commit d208d4c into main Nov 17, 2026
8 checks passed
@alecthomas
alecthomas deleted the aat/evaluate-reported-bug branch November 17, 2026 04:33
alecthomas added a commit that referenced this pull request Nov 17, 2026
`EnsureRefs` fetched from upstream whenever a request was unsatisfied,
and it kept no record that a fetch had already failed to help. Any
request the mirror could not satisfy even after fetching therefore cost
a full upstream fetch on every call. #452 fixed one instance of this,
`HEAD`. These cases remained:

- A ref pinned to a SHA the mirror has moved past, because the check was
exact equality.
- An annotated tag pinned to its commit, because the mirror records the
tag object's SHA.
- A ref or commit that upstream does not have, such as a deleted branch,
a misspelled name, or a commit removed by a force push.

Separately, a ref requested with an empty SHA was satisfied as soon as
it existed locally, so it never refreshed a stale mirror. That is not
what callers asking for `HEAD` expect.

The approach is to fetch only when upstream has something the mirror
lacks:

- A pinned ref is satisfied when its SHA is reachable from the ref.
- An unsatisfied request compares the mirror's refs with `git ls-remote`
and fetches only if they differ. This bypasses `RefCheckInterval`, so a
commit pushed a moment ago is still found.
- An entry upstream could not satisfy is remembered for
`RefCheckInterval`, so a caller repeating a hopeless request makes no
upstream call.
- An empty SHA now also requires the ref to be current. For `HEAD` and
branches this uses the existing cached check in `EnsureRefsUpToDate`, so
frequent `HEAD` requests cost at most one `git ls-remote` per
`RefCheckInterval`. That check compares only branches, so other refs,
such as tags, are compared directly with upstream instead.

Two behaviours change for callers. A request for a ref or commit that
upstream lacks now returns `fetched=false`, with missing commits still
listed. An empty-SHA request now fails if upstream cannot be reached,
where before it succeeded from the mirror alone.

The upstream comparison is shared with `EnsureRefsUpToDate` through a
new `behindUpstream` method, which takes a filter for the refs to
compare. Tests cover each case above, plus the memory of unsatisfiable
entries.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
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