Skip to content

Commit ece57af

Browse files
committed
Merge pull request #450 from barry-scott/master
The progress arg to push, pull, fetch and clone is now a python calla…
2 parents c5077da + d255f4c commit ece57af

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

‎git/remote.py

+42-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ def add_progress(kwargs, git, progress):
5858

5959
#} END utilities
6060

61+
def progress_object(progress):
62+
"""Given the 'progress' return a suitable object derived from
63+
RemoteProgress().
64+
"""
65+
# new API only needs progress as a function
66+
if callable(progress):
67+
return RemoteProgress(progress)
68+
69+
# where None is passed create a parser that eats the progress
70+
elif progress is None:
71+
return RemoteProgress()
72+
73+
# assume its the old API with an instance of RemoteProgress.
74+
else:
75+
return progress
76+
6177

6278
class PushInfo(object):
6379

@@ -536,7 +552,10 @@ def update(self, **kwargs):
536552
self.repo.git.remote(scmd, self.name, **kwargs)
537553
return self
538554

555+
539556
def _get_fetch_info_from_stderr(self, proc, progress):
557+
progress = progress_object(progress)
558+
540559
# skip first line as it is some remote info we are not interested in
541560
output = IterableList('name')
542561

@@ -591,6 +610,8 @@ def _get_fetch_info_from_stderr(self, proc, progress):
591610
return output
592611

593612
def _get_push_info(self, proc, progress):
613+
progress = progress_object(progress)
614+
594615
# read progress information from stderr
595616
# we hope stdout can hold all the data, it should ...
596617
# read the lines manually as it will use carriage returns between the messages
@@ -665,7 +686,7 @@ def fetch(self, refspec=None, progress=None, **kwargs):
665686

666687
proc = self.repo.git.fetch(self, *args, as_process=True, with_stdout=False,
667688
universal_newlines=True, v=True, **kwargs)
668-
res = self._get_fetch_info_from_stderr(proc, progress or RemoteProgress())
689+
res = self._get_fetch_info_from_stderr(proc, progress)
669690
if hasattr(self.repo.odb, 'update_cache'):
670691
self.repo.odb.update_cache()
671692
return res
@@ -684,7 +705,7 @@ def pull(self, refspec=None, progress=None, **kwargs):
684705
kwargs = add_progress(kwargs, self.repo.git, progress)
685706
proc = self.repo.git.pull(self, refspec, with_stdout=False, as_process=True,
686707
universal_newlines=True, v=True, **kwargs)
687-
res = self._get_fetch_info_from_stderr(proc, progress or RemoteProgress())
708+
res = self._get_fetch_info_from_stderr(proc, progress)
688709
if hasattr(self.repo.odb, 'update_cache'):
689710
self.repo.odb.update_cache()
690711
return res
@@ -694,10 +715,26 @@ def push(self, refspec=None, progress=None, **kwargs):
694715
695716
:param refspec: see 'fetch' method
696717
:param progress:
697-
Instance of type RemoteProgress allowing the caller to receive
698-
progress information until the method returns.
699718
If None, progress information will be discarded
700719
720+
No further progress information is returned after push returns.
721+
722+
A function (callable) that is called with the progress infomation:
723+
724+
progress( op_code, cur_count, max_count=None, message='' )
725+
726+
op_code is a bit mask of values defined in git.RemoteProgress
727+
728+
cur_count and max_count are float values.
729+
730+
max_count is None if there is no max_count
731+
732+
messages is '' if there is no additon message.
733+
734+
Deprecated: Pass in a class derived from git.RemoteProgres that
735+
overrides the update() function.
736+
737+
701738
:param kwargs: Additional arguments to be passed to git-push
702739
:return:
703740
IterableList(PushInfo, ...) iterable list of PushInfo instances, each
@@ -710,7 +747,7 @@ def push(self, refspec=None, progress=None, **kwargs):
710747
kwargs = add_progress(kwargs, self.repo.git, progress)
711748
proc = self.repo.git.push(self, refspec, porcelain=True, as_process=True,
712749
universal_newlines=True, **kwargs)
713-
return self._get_push_info(proc, progress or RemoteProgress())
750+
return self._get_push_info(proc, progress)
714751

715752
@property
716753
def config_reader(self):

‎git/repo/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
from git.config import GitConfigParser
3333
from git.remote import (
3434
Remote,
35-
add_progress
35+
add_progress,
36+
progress_object
3637
)
3738

3839
from git.db import GitCmdObjectDB
@@ -872,6 +873,8 @@ def init(cls, path=None, mkdir=True, odbt=DefaultDBType, **kwargs):
872873

873874
@classmethod
874875
def _clone(cls, git, url, path, odb_default_type, progress, **kwargs):
876+
progress = progress_object(progress)
877+
875878
# special handling for windows for path at which the clone should be
876879
# created.
877880
# tilde '~' will be expanded to the HOME no matter where the ~ occours. Hence

‎git/util.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,16 @@ class RemoteProgress(object):
174174
DONE_TOKEN = 'done.'
175175
TOKEN_SEPARATOR = ', '
176176

177-
__slots__ = ("_cur_line", "_seen_ops")
177+
__slots__ = ("_cur_line", "_seen_ops", "__progress_function")
178178
re_op_absolute = re.compile(r"(remote: )?([\w\s]+):\s+()(\d+)()(.*)")
179179
re_op_relative = re.compile(r"(remote: )?([\w\s]+):\s+(\d+)% \((\d+)/(\d+)\)(.*)")
180180

181-
def __init__(self):
181+
def __init__(self, progress_function=None):
182+
if progress_function is not None:
183+
self.__progress_function = progress_function
184+
else:
185+
self.__progress_function = self.update
186+
182187
self._seen_ops = list()
183188
self._cur_line = None
184189

@@ -267,7 +272,7 @@ def _parse_progress_line(self, line):
267272
# END end message handling
268273
message = message.strip(self.TOKEN_SEPARATOR)
269274

270-
self.update(op_code,
275+
self.__progress_function(op_code,
271276
cur_count and float(cur_count),
272277
max_count and float(max_count),
273278
message)
@@ -314,7 +319,6 @@ def update(self, op_code, cur_count, max_count=None, message=''):
314319
You may read the contents of the current line in self._cur_line"""
315320
pass
316321

317-
318322
class Actor(object):
319323

320324
"""Actors hold information about a person acting on the repository. They

0 commit comments

Comments
 (0)