Skip to content

Commit 1321d14

Browse files
committed
fix(remote): harden push source lookup for revision sources
Three gaps in the non-ref source handling, from review: - `Reference.from_path()` accepts any `refs/` path, so a source such as `refs/heads/main~1` became a `Head` that does not exist. Sources with `~`, `^` or `@{`, which `check-ref-format` never allows in a ref name, are now taken as revisions without trying `from_path()`. - `rev-parse` prefers a ref over an object ID with the same spelling, so a branch named like the abbreviated ID in git's summary gave the wrong `local_hash`. Expand it with `rev-parse --disambiguate`, which only looks at object IDs. - A lookup error other than `BadName` escaped the stdout handler and dropped the push result. Keep the result with `local_hash=None`. `test_push_revision_sources` covers each case, including a failing lookup on both paths; each new assertion fails with only its part of the change reverted, under `cli` and `gix`. Agent-Model: claude-opus-5-5
1 parent c7e4f81 commit 1321d14

2 files changed

Lines changed: 51 additions & 7 deletions

File tree

‎git/remote.py‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from git.cmd import Git, handle_process_output
3333
from git.compat import force_text
3434
from git.config import GitConfigParser, SectionConstraint, cp
35-
from git.exc import BadName
35+
from git.exc import GitCommandError, ODBError
3636
from git.refs import Reference, RemoteReference, SymbolicReference, TagReference
3737
from git.types import AnyGitObject, Literal, PathLike
3838
from git.util import (
@@ -171,6 +171,9 @@ class PushInfo(IterableObj):
171171
"!": ERROR,
172172
}
173173

174+
# git check-ref-format rejects these in ref names; revisions use them.
175+
_re_revision_syntax = re.compile(r"[~^]|@\{")
176+
174177
def __init__(
175178
self,
176179
flags: int,
@@ -237,11 +240,15 @@ def _from_line(cls, remote: "Remote", line: str) -> "PushInfo":
237240
from_ref: Union[SymbolicReference, None] = None
238241
is_revision = False
239242
if not flags & cls.DELETED and from_ref_string != "(delete)":
240-
try:
241-
from_ref = Reference.from_path(remote.repo, from_ref_string)
242-
except ValueError:
243-
# Git prints the source as it was given, which can be any revision.
243+
# Git prints the source as it was given, which can be any revision.
244+
# from_path() would accept one under refs/, like refs/heads/main~1.
245+
if cls._re_revision_syntax.search(from_ref_string):
244246
is_revision = True
247+
else:
248+
try:
249+
from_ref = Reference.from_path(remote.repo, from_ref_string)
250+
except ValueError:
251+
is_revision = True
245252

246253
# Commit handling, could be message or commit info
247254
old_commit: Optional[str] = None
@@ -276,8 +283,13 @@ def _from_line(cls, remote: "Remote", line: str) -> "PushInfo":
276283
# The push may have moved a ref the revision is relative to, like
277284
# origin/main~1, so prefer the hash git reports as pushed.
278285
try:
279-
local_hash = remote.repo.rev_parse(new_sha or from_ref_string).hexsha
280-
except BadName:
286+
if new_sha:
287+
# rev-parse would prefer a ref spelled like the abbreviated ID.
288+
oids = remote.repo.git._call_process_safe("rev_parse", "--disambiguate=%s" % new_sha).split()
289+
local_hash = oids[0] if len(oids) == 1 else None
290+
else:
291+
local_hash = remote.repo.rev_parse(from_ref_string).hexsha
292+
except (ODBError, GitCommandError, ValueError):
281293
pass
282294

283295
return PushInfo(flags, from_ref, to_ref_string, remote, old_commit, summary, local_hash)

‎test/test_remote.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,38 @@ def test_push_revision_sources(self, rw_repo):
581581
self.assertIsNone(info.local_ref)
582582
self.assertEqual(info.local_hash, parent.hexsha)
583583

584+
# A ref spelled like the abbreviated ID git prints must not win over the object.
585+
rw_repo.create_head(rw_repo.git.rev_parse("--short", parent.hexsha), rw_repo.head.commit)
586+
remote.push("HEAD:refs/heads/pushed_ref")
587+
remote.fetch()
588+
(info,) = remote.push("self/pushed_ref~1:refs/heads/pushed_ref", force=True)
589+
self.assertEqual(info.local_hash, parent.hexsha)
590+
591+
# A fully qualified revision is not a reference either.
592+
source = rw_repo.create_head("push_source", rw_repo.head.commit)
593+
(info,) = remote.push("%s~1:refs/heads/pushed_full" % source.path)
594+
self.assertIsNone(info.local_ref)
595+
self.assertEqual(info.local_hash, parent.hexsha)
596+
597+
# A failed lookup keeps the result without a hash.
598+
with mock.patch.object(rw_repo, "rev_parse", side_effect=GitCommandError("rev-parse", 128)):
599+
(info,) = remote.push("HEAD~1:refs/heads/pushed_lookup")
600+
self.assertTrue(info.flags & PushInfo.NEW_HEAD)
601+
self.assertIsNone(info.local_hash)
602+
603+
run = Git._call_process_safe
604+
605+
def fail_disambiguate(git, method, *args, **kwargs):
606+
if method == "rev_parse" and args and str(args[0]).startswith("--disambiguate="):
607+
raise GitCommandError("rev-parse", 128)
608+
return run(git, method, *args, **kwargs)
609+
610+
remote.push("HEAD:refs/heads/pushed_ref")
611+
with mock.patch.object(Git, "_call_process_safe", fail_disambiguate):
612+
(info,) = remote.push("HEAD~1:refs/heads/pushed_ref", force=True)
613+
self.assertTrue(info.flags & PushInfo.FORCED_UPDATE)
614+
self.assertIsNone(info.local_hash)
615+
584616
@with_rw_repo("HEAD", bare=True)
585617
def test_creation_and_removal(self, bare_rw_repo):
586618
new_name = "test_new_one"

0 commit comments

Comments
 (0)