Skip to content

Commit c7e4f81

Browse files
committed
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 <hash>:refs/heads/x` or `HEAD~1:refs/heads/x` reports `<hash>` 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
1 parent 0816ef5 commit c7e4f81

3 files changed

Lines changed: 58 additions & 9 deletions

File tree

‎doc/source/changes.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ API changes
114114
current repository (``branch.<name>.remote=.``) as the upstream, instead of
115115
constructing an invalid ``refs/remotes/./...`` reference. Git resolves the
116116
upstream location; remote tracking branches retain their existing behavior.
117+
* ``Remote.push()`` no longer drops results whose source is not a reference,
118+
such as a commit hash or ``HEAD~1``. Their ``PushInfo.local_ref`` is ``None``,
119+
and the new ``PushInfo.local_hash`` holds the hash of the pushed object.
117120
* ``GitCmdObjectDB`` no longer inherits ``LooseObjectDB``. Its object reads,
118121
writes, existence checks, and enumeration use Git, including packed objects.
119122
Precompressed input streams and custom object-output writers are unsupported.

‎git/remote.py‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +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
3536
from git.refs import Reference, RemoteReference, SymbolicReference, TagReference
3637
from git.types import AnyGitObject, Literal, PathLike
3738
from git.util import (
@@ -121,7 +122,11 @@ class PushInfo(IterableObj):
121122
info = remote.push()[0]
122123
info.flags # bitflags providing more information about the result
123124
info.local_ref # Reference pointing to the local reference that was pushed
124-
# It is None if the ref was deleted.
125+
# It is None if the ref was deleted, or if the pushed source
126+
# was not a reference, like a commit hash or HEAD~1.
127+
info.local_hash # Hash of the pushed object if the source was not a
128+
# reference, otherwise None. For new and rejected refs git
129+
# prints no hash, so the source is resolved after the push.
125130
info.remote_ref_string # path to the remote reference located on the remote side
126131
info.remote_ref # Remote Reference on the local side corresponding to
127132
# the remote_ref_string. It can be a TagReference as well.
@@ -132,6 +137,7 @@ class PushInfo(IterableObj):
132137

133138
__slots__ = (
134139
"local_ref",
140+
"local_hash",
135141
"remote_ref_string",
136142
"flags",
137143
"_old_commit_sha",
@@ -173,13 +179,15 @@ def __init__(
173179
remote: "Remote",
174180
old_commit: Optional[str] = None,
175181
summary: str = "",
182+
local_hash: Optional[str] = None,
176183
) -> None:
177184
"""Initialize a new instance.
178185
179186
local_ref: HEAD | Head | RemoteReference | TagReference | Reference | SymbolicReference | None
180187
"""
181188
self.flags = flags
182189
self.local_ref = local_ref
190+
self.local_hash = local_hash
183191
self.remote_ref_string = remote_ref_string
184192
self._remote = remote
185193
self._old_commit_sha = old_commit
@@ -226,16 +234,18 @@ def _from_line(cls, remote: "Remote", line: str) -> "PushInfo":
226234

227235
# from_to handling
228236
from_ref_string, to_ref_string = from_to.split(":")
229-
if flags & cls.DELETED:
230-
from_ref: Union[SymbolicReference, None] = None
231-
else:
232-
if from_ref_string == "(delete)":
233-
from_ref = None
234-
else:
237+
from_ref: Union[SymbolicReference, None] = None
238+
is_revision = False
239+
if not flags & cls.DELETED and from_ref_string != "(delete)":
240+
try:
235241
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.
244+
is_revision = True
236245

237246
# Commit handling, could be message or commit info
238247
old_commit: Optional[str] = None
248+
new_sha: Optional[str] = None
239249
if summary.startswith("["):
240250
if "[rejected]" in summary:
241251
flags |= cls.REJECTED
@@ -256,12 +266,21 @@ def _from_line(cls, remote: "Remote", line: str) -> "PushInfo":
256266
split_token = "..."
257267
if control_character == " ":
258268
split_token = ".."
259-
old_sha, _new_sha = summary.split(" ")[0].split(split_token)
269+
old_sha, new_sha = summary.split(" ")[0].split(split_token)
260270
# Have to use constructor here as the sha usually is abbreviated.
261271
old_commit = old_sha
262272
# END message handling
263273

264-
return PushInfo(flags, from_ref, to_ref_string, remote, old_commit, summary)
274+
local_hash: Optional[str] = None
275+
if is_revision:
276+
# The push may have moved a ref the revision is relative to, like
277+
# origin/main~1, so prefer the hash git reports as pushed.
278+
try:
279+
local_hash = remote.repo.rev_parse(new_sha or from_ref_string).hexsha
280+
except BadName:
281+
pass
282+
283+
return PushInfo(flags, from_ref, to_ref_string, remote, old_commit, summary, local_hash)
265284

266285
@classmethod
267286
def iter_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> NoReturn: # -> Iterator['PushInfo']:

‎test/test_remote.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,33 @@ def test_fetch_prune_skips_deleted_refs(self, rw_repo):
554554
"pruned refs are skipped without a warning",
555555
)
556556

557+
@with_rw_repo("HEAD", bare=False)
558+
def test_push_revision_sources(self, rw_repo):
559+
remote = rw_repo.create_remote("self", rw_repo.git_dir)
560+
parent = rw_repo.head.commit.parents[0]
561+
res = remote.push(
562+
[
563+
"%s:refs/heads/pushed_hash" % parent.hexsha,
564+
"HEAD~1:refs/heads/pushed_rev",
565+
"HEAD:refs/heads/pushed_ref",
566+
]
567+
)
568+
infos = {info.remote_ref_string: info for info in res}
569+
self.assertEqual(set(infos), {"refs/heads/pushed_hash", "refs/heads/pushed_rev", "refs/heads/pushed_ref"})
570+
for name in ("refs/heads/pushed_hash", "refs/heads/pushed_rev"):
571+
self.assertTrue(infos[name].flags & PushInfo.NEW_HEAD)
572+
self.assertIsNone(infos[name].local_ref)
573+
self.assertEqual(infos[name].local_hash, parent.hexsha)
574+
self.assertEqual(infos["refs/heads/pushed_ref"].local_ref, rw_repo.head)
575+
self.assertIsNone(infos["refs/heads/pushed_ref"].local_hash)
576+
577+
# The push moves self/pushed_ref, which the source is relative to.
578+
remote.fetch()
579+
(info,) = remote.push("self/pushed_ref~1:refs/heads/pushed_ref", force=True)
580+
self.assertTrue(info.flags & PushInfo.FORCED_UPDATE)
581+
self.assertIsNone(info.local_ref)
582+
self.assertEqual(info.local_hash, parent.hexsha)
583+
557584
@with_rw_repo("HEAD", bare=True)
558585
def test_creation_and_removal(self, bare_rw_repo):
559586
new_name = "test_new_one"

0 commit comments

Comments
 (0)