From c7e4f81b90266b5e912ac53a6400a50096bfbef7 Mon Sep 17 00:00:00 2001 From: JROtto5 <222316562+JROtto5@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:15:23 -0500 Subject: [PATCH 1/2] fix(remote): keep push results for sources that are not refs `git push --porcelain` prints the source side of each refspec as it was given, so `git push origin :refs/heads/x` or `HEAD~1:refs/heads/x` reports `` or `HEAD~1` there. `PushInfo._from_line()` passed it to `Reference.from_path()`, which raised `ValueError`, and `_get_push_info()` dropped the line. A push of only such sources returned an empty list, and a mixed push only the results for the other refspecs. When the source is not a reference, set `local_ref` to `None`, as it already is for deletes, and record the pushed object's hash in a new `local_hash` attribute. This is the shape suggested in #1624. The hash comes from git's `old..new` summary when there is one, because the push can move a ref the source is relative to, such as `origin/main~1`; only new refs and rejected lines resolve the source. `test_push_revision_sources` pushes a hash, `HEAD~1` and `HEAD` in one call, then force-pushes `self/pushed_ref~1` over the ref it is relative to. It fails without this change under the `cli` and `gix` backends, and passes with it. Agent-Model: claude-opus-5-5 --- doc/source/changes.rst | 3 +++ git/remote.py | 37 ++++++++++++++++++++++++++++--------- test/test_remote.py | 27 +++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 3f087b8bd..851a567b4 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -114,6 +114,9 @@ API changes current repository (``branch..remote=.``) as the upstream, instead of constructing an invalid ``refs/remotes/./...`` reference. Git resolves the upstream location; remote tracking branches retain their existing behavior. +* ``Remote.push()`` no longer drops results whose source is not a reference, + such as a commit hash or ``HEAD~1``. Their ``PushInfo.local_ref`` is ``None``, + and the new ``PushInfo.local_hash`` holds the hash of the pushed object. * ``GitCmdObjectDB`` no longer inherits ``LooseObjectDB``. Its object reads, writes, existence checks, and enumeration use Git, including packed objects. Precompressed input streams and custom object-output writers are unsupported. diff --git a/git/remote.py b/git/remote.py index 4b470b5f0..7a43fe563 100644 --- a/git/remote.py +++ b/git/remote.py @@ -32,6 +32,7 @@ from git.cmd import Git, handle_process_output from git.compat import force_text from git.config import GitConfigParser, SectionConstraint, cp +from git.exc import BadName from git.refs import Reference, RemoteReference, SymbolicReference, TagReference from git.types import AnyGitObject, Literal, PathLike from git.util import ( @@ -121,7 +122,11 @@ class PushInfo(IterableObj): info = remote.push()[0] info.flags # bitflags providing more information about the result info.local_ref # Reference pointing to the local reference that was pushed - # It is None if the ref was deleted. + # It is None if the ref was deleted, or if the pushed source + # was not a reference, like a commit hash or HEAD~1. + info.local_hash # Hash of the pushed object if the source was not a + # reference, otherwise None. For new and rejected refs git + # prints no hash, so the source is resolved after the push. info.remote_ref_string # path to the remote reference located on the remote side info.remote_ref # Remote Reference on the local side corresponding to # the remote_ref_string. It can be a TagReference as well. @@ -132,6 +137,7 @@ class PushInfo(IterableObj): __slots__ = ( "local_ref", + "local_hash", "remote_ref_string", "flags", "_old_commit_sha", @@ -173,6 +179,7 @@ def __init__( remote: "Remote", old_commit: Optional[str] = None, summary: str = "", + local_hash: Optional[str] = None, ) -> None: """Initialize a new instance. @@ -180,6 +187,7 @@ def __init__( """ self.flags = flags self.local_ref = local_ref + self.local_hash = local_hash self.remote_ref_string = remote_ref_string self._remote = remote self._old_commit_sha = old_commit @@ -226,16 +234,18 @@ def _from_line(cls, remote: "Remote", line: str) -> "PushInfo": # from_to handling from_ref_string, to_ref_string = from_to.split(":") - if flags & cls.DELETED: - from_ref: Union[SymbolicReference, None] = None - else: - if from_ref_string == "(delete)": - from_ref = None - else: + from_ref: Union[SymbolicReference, None] = None + is_revision = False + if not flags & cls.DELETED and from_ref_string != "(delete)": + try: from_ref = Reference.from_path(remote.repo, from_ref_string) + except ValueError: + # Git prints the source as it was given, which can be any revision. + is_revision = True # Commit handling, could be message or commit info old_commit: Optional[str] = None + new_sha: Optional[str] = None if summary.startswith("["): if "[rejected]" in summary: flags |= cls.REJECTED @@ -256,12 +266,21 @@ def _from_line(cls, remote: "Remote", line: str) -> "PushInfo": split_token = "..." if control_character == " ": split_token = ".." - old_sha, _new_sha = summary.split(" ")[0].split(split_token) + old_sha, new_sha = summary.split(" ")[0].split(split_token) # Have to use constructor here as the sha usually is abbreviated. old_commit = old_sha # END message handling - return PushInfo(flags, from_ref, to_ref_string, remote, old_commit, summary) + local_hash: Optional[str] = None + if is_revision: + # The push may have moved a ref the revision is relative to, like + # origin/main~1, so prefer the hash git reports as pushed. + try: + local_hash = remote.repo.rev_parse(new_sha or from_ref_string).hexsha + except BadName: + pass + + return PushInfo(flags, from_ref, to_ref_string, remote, old_commit, summary, local_hash) @classmethod def iter_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> NoReturn: # -> Iterator['PushInfo']: diff --git a/test/test_remote.py b/test/test_remote.py index ba18dce3a..e1c6c9c83 100644 --- a/test/test_remote.py +++ b/test/test_remote.py @@ -554,6 +554,33 @@ def test_fetch_prune_skips_deleted_refs(self, rw_repo): "pruned refs are skipped without a warning", ) + @with_rw_repo("HEAD", bare=False) + def test_push_revision_sources(self, rw_repo): + remote = rw_repo.create_remote("self", rw_repo.git_dir) + parent = rw_repo.head.commit.parents[0] + res = remote.push( + [ + "%s:refs/heads/pushed_hash" % parent.hexsha, + "HEAD~1:refs/heads/pushed_rev", + "HEAD:refs/heads/pushed_ref", + ] + ) + infos = {info.remote_ref_string: info for info in res} + self.assertEqual(set(infos), {"refs/heads/pushed_hash", "refs/heads/pushed_rev", "refs/heads/pushed_ref"}) + for name in ("refs/heads/pushed_hash", "refs/heads/pushed_rev"): + self.assertTrue(infos[name].flags & PushInfo.NEW_HEAD) + self.assertIsNone(infos[name].local_ref) + self.assertEqual(infos[name].local_hash, parent.hexsha) + self.assertEqual(infos["refs/heads/pushed_ref"].local_ref, rw_repo.head) + self.assertIsNone(infos["refs/heads/pushed_ref"].local_hash) + + # The push moves self/pushed_ref, which the source is relative to. + remote.fetch() + (info,) = remote.push("self/pushed_ref~1:refs/heads/pushed_ref", force=True) + self.assertTrue(info.flags & PushInfo.FORCED_UPDATE) + self.assertIsNone(info.local_ref) + self.assertEqual(info.local_hash, parent.hexsha) + @with_rw_repo("HEAD", bare=True) def test_creation_and_removal(self, bare_rw_repo): new_name = "test_new_one" From 1321d14fc9874b96e8fca1e3415a500dcffb00a4 Mon Sep 17 00:00:00 2001 From: JROtto5 <222316562+JROtto5@users.noreply.github.com> Date: Wed, 25 Nov 2026 02:39:14 -0500 Subject: [PATCH 2/2] 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 --- git/remote.py | 26 +++++++++++++++++++------- test/test_remote.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/git/remote.py b/git/remote.py index 7a43fe563..0eba7d093 100644 --- a/git/remote.py +++ b/git/remote.py @@ -32,7 +32,7 @@ from git.cmd import Git, handle_process_output from git.compat import force_text from git.config import GitConfigParser, SectionConstraint, cp -from git.exc import BadName +from git.exc import GitCommandError, ODBError from git.refs import Reference, RemoteReference, SymbolicReference, TagReference from git.types import AnyGitObject, Literal, PathLike from git.util import ( @@ -171,6 +171,9 @@ class PushInfo(IterableObj): "!": ERROR, } + # git check-ref-format rejects these in ref names; revisions use them. + _re_revision_syntax = re.compile(r"[~^]|@\{") + def __init__( self, flags: int, @@ -237,11 +240,15 @@ def _from_line(cls, remote: "Remote", line: str) -> "PushInfo": from_ref: Union[SymbolicReference, None] = None is_revision = False if not flags & cls.DELETED and from_ref_string != "(delete)": - try: - from_ref = Reference.from_path(remote.repo, from_ref_string) - except ValueError: - # Git prints the source as it was given, which can be any revision. + # Git prints the source as it was given, which can be any revision. + # from_path() would accept one under refs/, like refs/heads/main~1. + if cls._re_revision_syntax.search(from_ref_string): is_revision = True + else: + try: + from_ref = Reference.from_path(remote.repo, from_ref_string) + except ValueError: + is_revision = True # Commit handling, could be message or commit info old_commit: Optional[str] = None @@ -276,8 +283,13 @@ def _from_line(cls, remote: "Remote", line: str) -> "PushInfo": # The push may have moved a ref the revision is relative to, like # origin/main~1, so prefer the hash git reports as pushed. try: - local_hash = remote.repo.rev_parse(new_sha or from_ref_string).hexsha - except BadName: + if new_sha: + # rev-parse would prefer a ref spelled like the abbreviated ID. + oids = remote.repo.git._call_process_safe("rev_parse", "--disambiguate=%s" % new_sha).split() + local_hash = oids[0] if len(oids) == 1 else None + else: + local_hash = remote.repo.rev_parse(from_ref_string).hexsha + except (ODBError, GitCommandError, ValueError): pass return PushInfo(flags, from_ref, to_ref_string, remote, old_commit, summary, local_hash) diff --git a/test/test_remote.py b/test/test_remote.py index e1c6c9c83..3c390d445 100644 --- a/test/test_remote.py +++ b/test/test_remote.py @@ -581,6 +581,38 @@ def test_push_revision_sources(self, rw_repo): self.assertIsNone(info.local_ref) self.assertEqual(info.local_hash, parent.hexsha) + # A ref spelled like the abbreviated ID git prints must not win over the object. + rw_repo.create_head(rw_repo.git.rev_parse("--short", parent.hexsha), rw_repo.head.commit) + remote.push("HEAD:refs/heads/pushed_ref") + remote.fetch() + (info,) = remote.push("self/pushed_ref~1:refs/heads/pushed_ref", force=True) + self.assertEqual(info.local_hash, parent.hexsha) + + # A fully qualified revision is not a reference either. + source = rw_repo.create_head("push_source", rw_repo.head.commit) + (info,) = remote.push("%s~1:refs/heads/pushed_full" % source.path) + self.assertIsNone(info.local_ref) + self.assertEqual(info.local_hash, parent.hexsha) + + # A failed lookup keeps the result without a hash. + with mock.patch.object(rw_repo, "rev_parse", side_effect=GitCommandError("rev-parse", 128)): + (info,) = remote.push("HEAD~1:refs/heads/pushed_lookup") + self.assertTrue(info.flags & PushInfo.NEW_HEAD) + self.assertIsNone(info.local_hash) + + run = Git._call_process_safe + + def fail_disambiguate(git, method, *args, **kwargs): + if method == "rev_parse" and args and str(args[0]).startswith("--disambiguate="): + raise GitCommandError("rev-parse", 128) + return run(git, method, *args, **kwargs) + + remote.push("HEAD:refs/heads/pushed_ref") + with mock.patch.object(Git, "_call_process_safe", fail_disambiguate): + (info,) = remote.push("HEAD~1:refs/heads/pushed_ref", force=True) + self.assertTrue(info.flags & PushInfo.FORCED_UPDATE) + self.assertIsNone(info.local_hash) + @with_rw_repo("HEAD", bare=True) def test_creation_and_removal(self, bare_rw_repo): new_name = "test_new_one"