Skip to content

Commit 49fce2a

Browse files
committed
Let remote.push return a PushInfoList
List-like, so that it's backward compatible. But it has a new method raise_on_error, that throws an exception if anything failed to push. Related to gitpython-developers#621
1 parent ea43def commit 49fce2a

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

Diff for: git/remote.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
log.addHandler(logging.NullHandler())
4848

4949

50-
__all__ = ('RemoteProgress', 'PushInfo', 'FetchInfo', 'Remote')
50+
__all__ = ('RemoteProgress', 'PushInfo', 'PushInfoList', 'FetchInfo', 'Remote')
5151

5252
#{ Utilities
5353

@@ -84,6 +84,19 @@ def to_progress_instance(progress):
8484
return progress
8585

8686

87+
class PushInfoList(list):
88+
def __init__(self):
89+
super().__init__()
90+
self.exception = None
91+
92+
def raise_on_error(self):
93+
"""
94+
Raise an exception if any ref failed to push.
95+
"""
96+
if self.exception:
97+
raise self.exception
98+
99+
87100
class PushInfo(object):
88101
"""
89102
Carries information about the result of a push operation of a single head::
@@ -728,7 +741,7 @@ def _get_push_info(self, proc, progress):
728741
# read the lines manually as it will use carriage returns between the messages
729742
# to override the previous one. This is why we read the bytes manually
730743
progress_handler = progress.new_message_handler()
731-
output = []
744+
output = PushInfoList()
732745

733746
def stdout_handler(line):
734747
try:
@@ -741,11 +754,12 @@ def stdout_handler(line):
741754
stderr_text = progress.error_lines and '\n'.join(progress.error_lines) or ''
742755
try:
743756
proc.wait(stderr=stderr_text)
744-
except Exception:
757+
except Exception as e:
745758
if not output:
746759
raise
747760
elif stderr_text:
748-
log.warning("Error lines received while fetching: %s", stderr_text)
761+
log.warning("Error lines received while pushing: %s", stderr_text)
762+
output.exception = e
749763

750764
return output
751765

@@ -843,7 +857,7 @@ def push(self, refspec=None, progress=None, **kwargs):
843857
:note: No further progress information is returned after push returns.
844858
:param kwargs: Additional arguments to be passed to git-push
845859
:return:
846-
list(PushInfo, ...) list of PushInfo instances, each
860+
PushInfoList list-like of PushInfo instances, each
847861
one informing about an individual head which had been updated on the remote
848862
side.
849863
If the push contains rejected heads, these will have the PushInfo.ERROR bit set

0 commit comments

Comments
 (0)