Skip to content

Commit cfb222f

Browse files
authored
Merge pull request #2316 from gitpython-developers/2313-supersede
fix(refs): keep missing references from resolving to lookalike tags
2 parents 75e2cb9 + d400a1d commit cfb222f

11 files changed

Lines changed: 192 additions & 15 deletions

File tree

‎AGENTS.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ when communicating through a person's account.
99
issue with a fix attached. Do not create an issue solely to follow it immediately
1010
with a PR that fixes it; describe the problem in the PR instead.
1111

12+
Agents creating a PR must state the underlying motivation in its description, such
13+
as the project that will benefit or where the bug was first discovered. Any
14+
motivation is valid; explain why the work was pursued rather than restating what
15+
the PR does. "Want to fix a bug" is insufficient because that is already evident
16+
from the PR itself.
17+
1218
# Gix backend implementations
1319

1420
Gix backend operations must use Gix through the supported GixPython (`gix`)

‎doc/gix-backend.md‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,20 @@ and predate the current fallback; they do not establish native coverage.
392392
| `Repo.untracked_files` | Native directory walk | Extra status options and non-root command directories |
393393
| `Repo.ignored` | Native excludes with tracked-file suppression | Symlink/submodule traversal, unsupported path normalization, and unavailable path metadata |
394394

395+
Reference reads require an exact name, unlike revision APIs that intentionally
396+
apply Git's name guessing. `reference_info()` uses
397+
`Repository.try_find_reference()` and verifies the returned name for `HEAD` and
398+
fully qualified refs. Other root names, including `REBASE_HEAD`, retain the CLI
399+
fallback: GixPython's lookup accepts partial names, including symbolic lookalike
400+
tags for missing root names (GIX-33). Native `symbolic_ref` dispatch verifies the
401+
returned name and falls back for a lookalike. A native root-reference conversion
402+
needs verified existence and error behavior, including Git's distinction between
403+
an absent and an invalid reference. `Repo.currently_rebasing_on()` probes
404+
`git show-ref --exists` and then uses the shared exact reference reader; the
405+
existence probe remains CLI on both backends. `exact-refs.sh` tests missing names beside fetched tags with both object
406+
and reference formats. Reftable still uses GIX-1; this change claims no new native
407+
coverage. The symbolic lookalike mismatch remains open upstream as GIX-33.
408+
395409
Reference names and revisions are encoded with `surrogateescape` before calling
396410
GixPython's byte-capable `Target.Symbolic`, `Repository.try_find_reference`,
397411
`Repository.rev_parse_single`, `Repository.rev_parse` and `RefEdit` APIs.
@@ -935,7 +949,7 @@ identify the historical fixture where needed.
935949
| GIX-14 | Bug; missing API | For a linked worktree of a bare main repository, `is_bare()` returns true while `git rev-parse --is-bare-repository` returns false; `workdir()` does expose the worktree. Initial gitfile opening can retain a noncanonical target and use an arbitrary gitfile's location as the worktree, unlike Git. Discovery accepts undecodable HEADs until explicit `head()` decoding and ignores dangling `commondir` symlinks that Git rejects. Bindings flatten failure kinds into `gix.Error`. Windows also exposes adapter path-formatting differences and a worker panic on an undecodable `commondir`; see the Windows reproduction below. | Provide Git-compatible classification, canonical gitfile locations and layout validation, plus typed errors instead of panics. Format native locations compatibly with Git's command output. The adapter combines Gix metadata, reopens through Gix and forces HEAD decoding, retaining CLI for remaining validation/diagnostic gaps. Those workarounds do not close these compatibility bugs. A Git-strict mode must cover these cases; strict config and trust settings alone do not. |
936950
| GIX-15 | Bug | Native blame disagrees with Git even with Myers and rewrite tracking selected. In the fixture's `README.md`, lines 150 and 158 are attributed to the opposite commits. Incremental order also differs. | Attribution and incremental-output parity before replacing `Repo.blame` / `blame_incremental`. |
937951
| GIX-16 | Bug; missing API | Native archive streaming takes a tree rather than a commit. Its TAR omits Git's global PAX commit comment, leaves `export-subst` placeholders literal, and writes ordinary modes as `0644` where Git uses `0664`. | Commit-aware export substitution, metadata and permission parity. Both engines respected `export-ignore` in the probe. |
938-
| GIX-17 | Bug | Revision parsing accepts abbreviated IDs with `-dirty`, prefers the OID suffix over an exact describe-shaped tag, and treats escaped braces in message searches differently. | Git-compatible revision grammar and regex semantics. Existing `test_rev_parse.py` cases reproduce all three. |
952+
| GIX-17 | Bug (open) | Revision parsing accepts abbreviated IDs with `-dirty`, prefers the OID suffix over an exact describe-shaped tag, and treats escaped braces in message searches differently. With GixPython 0.3.0 and Git 2.54.0, `2313-supersede` resolves to blob `23136a1d1dd1f02de7cde0f7b55f49d76ac77d55` through Gix while Git resolves the existing branch's commit. The deterministic `rev-parse.sh hex-prefix-collision` fixture creates a branch named `<blob-prefix>-topic`: Gix returns the blob and also accepts `<blob-prefix>-missing`, which Git rejects. | Provide Git-compatible revision grammar and regex semantics. `_check_revision_grammar()` retains CLI for hexadecimal prefixes followed by a dash, as well as the existing guarded forms; this adapter workaround does not close the upstream bug. `test_rev_parse.py` reproduces these differences, including `test_rev_parse_hex_prefix_with_arbitrary_suffix`. |
939953
| GIX-18 | Bug; missing API | The tree-diff resource cache reads attributes from the index; Git also consults worktree attributes. Native binary classification with textconv configured can also differ even in `to_git` mode. | Independent attribute-source control and `--no-textconv` parity. Statistics currently fall back for any effective `diff` attribute. |
940954
| GIX-19 | Unverified | GixPython opens repositories with `extensions.compatObjectFormat`, but translation and object-write compatibility have not been validated. The installed Git reports `compatibility hash algorithm support requires Rust` when asked to translate an ID. This is an unvalidated capability guard, not evidence of missing native object mappings. | Verify compatibility-format reads, writes and OID translation against a Git build that supports them. All operations use CLI in the meantime. |
941955
| GIX-20 | Bug; missing API | `Target.Symbolic(name)` validates full names but rejects standalone names accepted by `git check-ref-format --allow-onelevel`. Four of 430 audited calls differed: `refs` twice, `hellothere`, and `valid_one_level_refname`. Further probes include digits, punctuation and Unicode (`1`, `A1`, `HEAD_1`, `A-B`, `A.B`, `Ä`). | Expose Git-compatible partial/one-level name validation, or correct the native restriction where appropriate. This is a binding/behavior shortcoming to revisit upstream; standalone names rejected natively retain CLI validation. |
@@ -951,6 +965,7 @@ identify the historical fixture where needed.
951965
| GIX-30 | Bug (open) | GixPython 0.2.0 `Commit.decode()` reports `encoding=None` when `encoding ISO-8859-1` follows `x-extra value`; Git 2.54.0 selects `ISO-8859-1` and renders Latin-1 identity/message bytes correctly. Gix retains `(b"encoding", b"ISO-8859-1")` in `extra_headers`, but its encoding field disagrees with Git. The `late-latin1` read-only `commit.sh` fixture in `test_deserialization_declared_encoding` reproduces this and asserts the fallback. | Recognize encoding headers in Git-compatible order. `object_metadata()` conservatively retains CLI when an encoding appears in `extra_headers`; it does not select the header in Python. Keep this bug open until an upstream fix or compatible mode is verified. |
952966
| GIX-31 | Bug (open) | GixPython 0.2.0 `Commit.decode()` rejects commit headers containing a CR-only or tab-only line with `object parsing failed, at gix-object/src/commit/decode.rs:20`. Git 2.54.0 accepts both objects with `fsck --strict`, continues through their header block and renders the later Latin-1 declaration and message correctly. A space-only continuation line decodes natively but retains the late encoding declaration in `extra_headers` (GIX-30). `test_deserialization_headers_end_at_empty_line` reproduces all three using the `commit.sh whitespace-headers` fixtures and checks Git's projection. | Accept Git-compatible header lines and end headers only at an empty line. The existing `object_metadata()` native decoder diagnostics fallback retains Git for CR/tab inputs; the existing GIX-30 fallback covers the space input. Neither fallback closes these bugs. |
953967
| GIX-32 | Missing capability (open); no demonstrated Git compatibility mismatch | Windows CI on GixPython 0.3.0, Git 2.56.0.windows.2 and CPython 3.12.10 enumerates the non-UTF-8 packed names in `legacy-refs.sh`, but exact byte-name lookup selects `Reference.read: CLI: native read diagnostics`; the no-CLI guard fails for SHA-1 and SHA-256. POSIX lookup succeeds. Git's Windows CLI likewise cannot address those original bytes through UTF-16 arguments, so there is no demonstrated equivalent Windows CLI operand. Git enumeration does preserve both names. | Provide native Windows packed-reference lookup for byte names without first converting the name to a loose-reference filesystem path. Keep `reference_info()`'s existing diagnostic fallback; `test_non_utf8_tag_names_preserve_bytes` checks it on Windows. Resolution coverage and native claims remain POSIX-only. The upstream lookup source attempts loose-path conversion before consulting packed refs; CI demonstrates the fallback, but does not capture the underlying Gix error text. |
968+
| GIX-33 | Bug (open); missing exact-name mode | With Git 2.54.0 and GixPython 0.3.0, `exact-refs.sh sha1 files symbolic` makes `refs/tags/REBASE_HEAD` a symbolic ref to another tag while the root `REBASE_HEAD` is absent. `Repository.try_find_reference("REBASE_HEAD")` returns that tag; native `_symbolic_ref` formerly returned its target, whereas `git symbolic-ref --quiet --no-recurse -- REBASE_HEAD` exits 1. GitPython's root-reference object read consequently returned a commit through Gix and raised `ValueError` through CLI. SHA-256 reproduces the same mismatch. | Expose an exact-name lookup mode, including root references, whose absence and error behavior matches Git. `_symbolic_ref()` compares the native reference's name with the requested bytes and selects CLI for a lookalike. `test_missing_root_reference_does_not_follow_symbolic_lookalike_tag` verifies parity and forced CLI fallback. This adapter guard does not close the upstream compatibility bug. |
954969

955970
The PR #2306 regression exposed an adapter ownership-check bypass in both
956971
backends: `Repo`'s automatic `GIT_DIR` made Git 2.54.0 treat hook execution as

‎doc/source/changes.rst‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
Changelog
33
=========
44

5-
Unreleased: Git CLI migration
6-
=============================
5+
Unreleased: Git CLI migration - back to the roots
6+
=================================================
77

88
GitPython now delegates repository discovery, references, configuration, object
9-
storage, tree construction, index operations, and revision parsing to Git. The
9+
storage, tree construction, index operations, and revision parsing to Git.The
1010
default backend supports SHA-1 and SHA-256 repositories with either files or
1111
reftable reference storage. Repository object IDs must no longer be assumed to
1212
contain 20 binary bytes or 40 hexadecimal characters.
@@ -54,6 +54,10 @@ Git executable and safety
5454
API changes
5555
-----------
5656

57+
* Reference reads require the exact name. A missing branch no longer resolves to
58+
a similarly named tag, so an unborn branch's initial commit has no parent.
59+
A tag named ``REBASE_HEAD`` no longer reports a rebase in progress. Revision
60+
APIs such as ``Repo.commit(rev)`` retain Git's name guessing.
5761
* Packed tag names containing non-UTF-8 bytes are represented losslessly as
5862
surrogate-escaped strings. ``safe_encode()`` recovers their original bytes,
5963
and Gix reference validation and resolution receive those bytes directly.

‎git/_backend.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def _canonical_repository(repo: Any) -> Any:
166166

167167

168168
def _check_revision_grammar(ref: str) -> None:
169-
if ref.startswith(":/") or "^{/" in ref or "-dirty" in ref or re.search(r"-\d+-g[0-9a-fA-F]+", ref):
169+
if ref.startswith(":/") or "^{/" in ref or "-dirty" in ref or re.search(r"-\d+-g[0-9a-fA-F]+|^[0-9a-fA-F]+-", ref):
170170
raise _Unsupported("revision grammar differences (GIX-17)")
171171

172172

@@ -363,7 +363,7 @@ def _symbolic_ref(repo: Any, args: List[str], kwargs: Dict[str, Any]) -> bytes:
363363
if len(args) != 4 or args[:3] != ["--quiet", "--no-recurse", "--"]:
364364
raise _Unsupported("reference mutation or options")
365365
reference = repo.try_find_reference(safe_encode(args[-1]))
366-
if reference is None:
366+
if reference is None or reference.name() != safe_encode(args[-1]):
367367
raise _Unsupported("missing reference diagnostics")
368368
target = reference.target().try_name()
369369
if target is None:

‎git/refs/symbolic.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ def _get_ref_info_helper(
198198
if exc.status != 1:
199199
raise ValueError("Invalid symbolic reference %r" % path) from exc
200200
try:
201-
oid = repo.git._call_process_safe("rev_parse", "--verify", "--end-of-options", path)
201+
# Revision parsing can guess a tag when the exact reference is absent.
202+
oid = repo.git._call_process_safe("show_ref", "--verify", "--hash", "--", path)
202203
except GitCommandError as exc:
203204
raise ValueError("Reference at %r does not exist or is invalid" % path) from exc
204205
if not repo.re_hexsha_only.fullmatch(oid):

‎git/repo/base.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
)
3636
from git.index import IndexFile
3737
from git.objects import Submodule, RootModule, Commit
38-
from git.refs import HEAD, Head, Reference, TagReference
38+
from git.refs import HEAD, Head, Reference, SymbolicReference, TagReference
3939
from git.remote import Remote, _T_RemoteName, add_progress, to_progress_instance
4040
from git.util import (
4141
Actor,
@@ -88,7 +88,6 @@
8888
if TYPE_CHECKING:
8989
from git.objects import Tree
9090
from git.objects.submodule.base import UpdateProgress
91-
from git.refs.symbolic import SymbolicReference
9291
from git.remote import RemoteProgress
9392
from git.util import IterableList
9493

@@ -1969,11 +1968,11 @@ def currently_rebasing_on(self) -> Commit | None:
19691968
"""
19701969
if not self.git_dir:
19711970
return None
1972-
status, oid, stderr = self.git._call_process_safe(
1973-
"rev_parse", "--verify", "--quiet", "REBASE_HEAD", with_extended_output=True, with_exceptions=False
1971+
status, stdout, stderr = self.git._call_process_safe(
1972+
"show_ref", "--exists", "--", "REBASE_HEAD", with_extended_output=True, with_exceptions=False
19741973
)
1975-
if status == 1:
1974+
if status == 2:
19761975
return None
19771976
if status:
1978-
raise GitCommandError("git rev-parse", status, stderr, oid)
1979-
return self.commit(oid)
1977+
raise GitCommandError("git show-ref", status, stderr, stdout)
1978+
return self.commit(SymbolicReference.dereference_recursive(self, "REBASE_HEAD"))

‎test/test_git.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def test_it_redacts_authorization_extra_header_from_error(self):
518518
def test_clone_redacts_config_urls_from_trace_and_error(self, option, expected):
519519
for trace in (True, "full"):
520520
with self.subTest(trace=trace), writable_fixture("repo.sh", "empty") as temporary:
521-
directory = temporary.path()
521+
directory = temporary.path() / "alice-bob-user-token"
522522
with mock.patch.object(Git, "GIT_PYTHON_TRACE", trace):
523523
with self.assertLogs("git.cmd", level="INFO") as logs:
524524
with self.assertRaises(GitCommandError) as context:
@@ -532,6 +532,9 @@ def test_clone_redacts_config_urls_from_trace_and_error(self, option, expected):
532532
message = str(context.exception)
533533
assert logs is not None
534534
log_output = " ".join(logs.output)
535+
for path in (str(directory), directory.as_posix()):
536+
message = message.replace(path, "<fixture>")
537+
log_output = log_output.replace(path, "<fixture>")
535538
self.assertIn(expected, log_output)
536539
for credential in ("alice", "bob", "user", "token"):
537540
self.assertNotIn(credential, message)

0 commit comments

Comments
 (0)