Skip to content

Raise exception when git push fails #621

Closed as not planned
Closed as not planned

Description

@michaelkozakovsc

Having to perform bit operations on flags is very unintuitive. The command line git client fails when push fails.

Activity

  1. artgoldberg commented on May 18, 2019

    @artgoldberg

    Agree. Errors should be reported as exceptions.

  2. KalenWessel commented on Jun 10, 2019

    @KalenWessel

    Also was just bit by this issue.

  3. added a commit that references this issue on Dec 20, 2019
  4. rfrancois commented on Jul 15, 2021

    @rfrancois

    Also was just bit by this issue.

  5. Byron commented on Jul 15, 2021

    @Byron
    Member

    As a general question: What about submitting a PR with a new push_or_throw(…) method, which does as it says? There should be enough references in this thread to implement it without spending too much time on research.

  6. added 3 commits that reference this issue on Oct 1, 2021
    e1110c1
    85a2ab5
    6a0a5c7
  7. added a commit that references this issue on Nov 8, 2021
  8. added a commit that references this issue on Nov 13, 2021
  9. CyrilBrulebois commented on Apr 9, 2022

    @CyrilBrulebois

    In case someone else stumbles on this very strange behaviour, and doesn't immediately dive into Sjord's commits (mentioned in this issue, above this comment): raise_if_error() can now be called on the object returned by the push() call.

    That landed in 3.1.25, and the tutorial has an example:

    # push and pull behaves similarly to `git push|pull`
    origin.pull()
    origin.push()  # attempt push, ignore errors
    origin.push().raise_if_error()  # push and raise error if it fails
  10. CyrilBrulebois commented on Apr 9, 2022

    @CyrilBrulebois

    For some reason, the PushInfoList class has been shipped since 35f7e94 aka. 3.1.25~10 but can't be found by searching for it on https://gitpython.readthedocs.io/en/latest/ (latest or specific versions). So I suppose some extra doc metadata is needed to improve discoverability?

  11. CyrilBrulebois commented on Apr 9, 2022

    @CyrilBrulebois

    In case someone needs to work with a slightly older version (e.g. 3.1.14, available in Debian 11), here's something that seems to do the trick for me (keeping in mind raising a generic Exception works well enough for me since I'm in a retry loop doing fetch & push inside a try/except block):

    origin.pull(rebase=True)
    # Workaround for missing origin.push().raise_if_error() in 3.1.14
    # (see https://github.com/gitpython-developers/GitPython/issues/621):
    pushinfolist = origin.push()
    for pi in pushinfolist:  # pylint: disable=invalid-name
        for flag in [pi.REJECTED, pi.REMOTE_REJECTED, pi.REMOTE_FAILURE, pi.ERROR]:
            if pi.flags & flag:
                raise Exception("error flag %d set" % flag)

    Caveat: I had to hardcode which flags look like errors I should care about; not sure how stable it is going to be across releases.

  12. Byron commented on Apr 9, 2022

    @Byron
    Member

    Thanks for sharing! The flags should not change across releases as breaking changes are generally avoided.

  13. added theissue type on Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions