Skip to content

fix: prefer a reference over a commit hash as Git does - #508

Open
DeveloperC286 wants to merge 2 commits into
mainfrom
claude/short-hash-ref-precedence-kfzduc
Open

fix: prefer a reference over a commit hash as Git does#508
DeveloperC286 wants to merge 2 commits into
mainfrom
claude/short-hash-ref-precedence-kfzduc

Conversation

@DeveloperC286

Copy link
Copy Markdown
Owner

Summary

This PR improves the handling of ambiguous Git references by aligning the behavior with how Git itself resolves them. When a provided argument matches both a reference name and a commit hash, the reference is now preferred, with appropriate logging and warnings.

Key Changes

  • New resolve_to_oid() function: Replaces the previous error-chaining logic with a more explicit resolution strategy that prefers references over commit hashes, matching Git's behavior
  • Enhanced parse_to_oid() validation: Added early validation to reject non-hexadecimal strings before attempting to search the repository history, improving performance and clarity
  • Ambiguity detection and logging: When a name resolves to both a reference and a commit hash, a warning is now emitted to inform users of the ambiguity, while an info log confirms which one was used
  • End-to-end test coverage: Added comprehensive feature tests validating the ambiguous reference resolution behavior and warning messages

Implementation Details

  • The new resolution logic first attempts to resolve as a reference; if successful, it checks whether the same string is also a valid commit hash and warns if there's an ambiguity
  • If reference resolution fails, it falls back to parsing as a commit hash, preserving both error contexts for better diagnostics
  • The validation in parse_to_oid() now rejects empty strings and non-hexadecimal characters upfront, avoiding unnecessary repository history searches
  • Test scenarios use a real repository (changeloguru) with a branch named after a shortened commit hash to verify correct precedence

https://claude.ai/code/session_01Ek4L1fyrQp7kNbitNo3ysH

@DeveloperC286 DeveloperC286 changed the title Prefer Git references over commit hashes when resolving ambiguous names fix: prefer a reference over a commit hash as Git does Sep 10, 2026
The provided argument was resolved as a shortened commit hash before
being resolved as a reference, the inverse of Git's own precedence. So a
reference whose name was also the start of a commit hash, such as a
branch named after a shortened commit hash, was silently shadowed and a
different range of commits than Git itself would use was linted.

Now a reference is resolved first, only falling back to a commit hash
when no reference matches, and a warning is emitted when the provided
argument is both, so which was used is never a surprise.

Searching the history for a match is now also skipped when the provided
argument can not be a commit hash, as it contains non hexadecimal
characters.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ek4L1fyrQp7kNbitNo3ysH
@DeveloperC286
DeveloperC286 force-pushed the claude/short-hash-ref-precedence-kfzduc branch from df2aa83 to c99a0b4 Compare September 10, 2026 06:40
Resolving a reference before a commit hash matched Git for a shortened
commit hash, but not in three other ways Git resolves an ambiguous name.

A full commit hash is now resolved to the commit even when a reference
shares its name, as Git does, warning that the name is ambiguous.

A shortened commit hash of fewer than four characters is no longer
matched, as Git's own MINIMUM_ABBREV does not match one either.

References are now matched via the same expansions Git uses, in the same
order, so a name matching several references resolves to the same one
Git resolves to, warning that the name is ambiguous.

The warning for a name which is both a reference and a shortened commit
hash is also no longer conditional upon them differing, as Git warns
whenever both match.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ek4L1fyrQp7kNbitNo3ysH
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