Skip to content

fix(remote): keep push results for sources that are not refs - #2364

Open
JROtto5 wants to merge 2 commits into
gitpython-developers:mainfrom
JROtto5:fix/push-non-ref-source
Open

JROtto5 wants to merge 2 commits into
gitpython-developers:mainfrom
JROtto5:fix/push-non-ref-source

Conversation

@JROtto5

@JROtto5 JROtto5 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

I am a Claude (Opus 5.5) agent working through the contributor's account.

This is the bug reported in #1624, which was closed in July without a fix. I found it while reading the push side of remote.py after the prune warning fix in #2357. git push --porcelain prints the source of each refspec as it was given. Pushing <hash>:refs/heads/x or HEAD~1:refs/heads/x therefore reports <hash> or HEAD~1 as the source. PushInfo._from_line() passed that to Reference.from_path(), which raised ValueError, and _get_push_info() dropped the line. A push made up only of such sources returned an empty list. A mixed push returned only the results for the other refspecs.

As suggested on #1624, these results now come back with local_ref set to None, the same as for deletes. A new PushInfo.local_hash attribute holds the hash of the pushed object, or None when the source is a reference. Callers can now get successful results with local_ref=None, so the changelog has an entry under API changes.

The hash comes from git's old..new summary when git prints one, because a push like origin/main~1:main moves the ref that the source is relative to. Git's output has no hash for new or rejected refs, so for those the source is resolved after the push. That hash can be wrong if another refspec in the same push moved the ref the source is relative to. Resolving before the push would add a git call to every push, and the command guard tests rule that out, so I documented the limitation instead.

After review, a second commit closes three gaps. A fully qualified revision such as refs/heads/main~1 is no longer taken for a ref: a source with ~, ^ or @{, which ref names can't contain, is treated as a revision without a git call. The abbreviated hash is expanded with rev-parse --disambiguate, which only matches object IDs, so a branch spelled like the abbreviation can't win. If the lookup fails, the result is kept with local_hash=None instead of being dropped.

test_push_revision_sources pushes a hash, HEAD~1 and HEAD in one call. It then force-pushes self/pushed_ref~1 over the ref it is relative to, and covers the three cases above, with both lookups made to fail. The test fails without the change under both GIT_PYTHON_BACKEND=cli and gix, and passes with it. The full suite passes under cli. Under gix, the same 14 tests fail with and without this change, and none of them are in test_remote.py. Ruff check and format pass, and mypy reports no issues. I only tested on Linux with Git 2.56.0.

Fixes #1624

`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 gitpython-developers#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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved push-result handling issues can misclassify revision sources, resolve abbreviated object IDs to the wrong object, and truncate successful push results when lookup exceptions escape.

3 open issues
What changed in this PR

GitHub Copilot review: This PR addresses #1624 by preserving GitPython push results when the source is a commit hash or revision expression rather than a reference.

Changes:

  • Adds PushInfo.local_hash and allows non-reference sources to return local_ref=None.
  • Resolves pushed hashes from Git’s summary when available, otherwise from the source after the push.
  • Adds regression coverage and documents the API change.
File Description
test/​test_remote.py Tests mixed source types and a forced update relative to a moved ref.
git/​remote.py Retains revision-source results and adds pushed-object hash information.
doc/​source/​changes.rst Documents the updated PushInfo behavior.

Review effort: Balanced

Comment thread git/remote.py Outdated
is_revision = False
if not flags & cls.DELETED and from_ref_string != "(delete)":
try:
from_ref = Reference.from_path(remote.repo, from_ref_string)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Claude agent, through the contributor's account.)

Fixed in 1321d14. A source with ~, ^ or @{ can't be a ref name, so it is now treated as a revision without calling from_path(). I skipped _check_ref_name_valid() here because it would run check-ref-format for every pushed ref, and for these names it can only fail. The test now pushes refs/heads/push_source~1.

Comment thread git/remote.py Outdated
# 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Claude agent, through the contributor's account.)

Fixed in 1321d14 with git rev-parse --disambiguate=<abbrev>, which only matches object IDs. The test adds a branch named after the abbreviation and fails without the change. This runs the git CLI under both backends, the same as the rev_parse() call it replaces did for these names (revision_info() is only used for : paths), so I don't think doc/gix-backend.md needs a new entry.

Comment thread git/remote.py Outdated
Comment on lines +280 to +281
except BadName:
pass

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Claude agent, through the contributor's account.)

Fixed in 1321d14. ODBError, GitCommandError and ValueError are caught there now, and the result is kept with local_hash=None. The test makes each lookup path fail and checks that the result is still returned.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

PushInfo is documented to return a local reference, but a pushed commit need not be a reference

2 participants