Progress parsing by int3 · Pull Request #20 · gitpython-developers/GitPython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion git/cmd.py
55 changes: 13 additions & 42 deletions git/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
TagReference
)

from git.util import join_path
from git.util import (
join_path,
_digest_process_messages,
_finalize_proc
)
from gitdb.util import join

import re
Expand Down Expand Up @@ -432,42 +436,6 @@ def update(self, **kwargs):
self.repo.git.remote("update", self.name)
return self

def _digest_process_messages(self, fh, progress):
"""Read progress messages from file-like object fh, supplying the respective
progress messages to the progress instance.

:return: list(line, ...) list of lines without linebreaks that did
not contain progress information"""
line_so_far = ''
dropped_lines = list()
while True:
char = fh.read(1)
if not char:
break

if char in ('\r', '\n'):
dropped_lines.extend(progress._parse_progress_line(line_so_far))
line_so_far = ''
else:
line_so_far += char
# END process parsed line
# END while file is not done reading
return dropped_lines


def _finalize_proc(self, proc):
"""Wait for the process (fetch, pull or push) and handle its errors accordingly"""
try:
proc.wait()
except GitCommandError,e:
# if a push has rejected items, the command has non-zero return status
# a return status of 128 indicates a connection error - reraise the previous one
if proc.poll() == 128:
raise
pass
# END exception handling


def _get_fetch_info_from_stderr(self, proc, progress):
# skip first line as it is some remote info we are not interested in
output = IterableList('name')
Expand All @@ -477,7 +445,7 @@ def _get_fetch_info_from_stderr(self, proc, progress):
# this also waits for the command to finish
# Skip some progress lines that don't provide relevant information
fetch_info_lines = list()
for line in self._digest_process_messages(proc.stderr, progress):
for line in _digest_process_messages(proc.stderr, progress):
if line.startswith('From') or line.startswith('remote: Total'):
continue
elif line.startswith('warning:'):
Expand All @@ -499,15 +467,15 @@ def _get_fetch_info_from_stderr(self, proc, progress):
output.extend(FetchInfo._from_line(self.repo, err_line, fetch_line)
for err_line,fetch_line in zip(fetch_info_lines, fetch_head_info))

self._finalize_proc(proc)
_finalize_proc(proc)
return output

def _get_push_info(self, proc, progress):
# read progress information from stderr
# we hope stdout can hold all the data, it should ...
# read the lines manually as it will use carriage returns between the messages
# to override the previous one. This is why we read the bytes manually
self._digest_process_messages(proc.stderr, progress)
_digest_process_messages(proc.stderr, progress)

output = IterableList('name')
for line in proc.stdout.readlines():
Expand All @@ -519,7 +487,7 @@ def _get_push_info(self, proc, progress):
# END exception handling
# END for each line

self._finalize_proc(proc)
_finalize_proc(proc)
return output


Expand All @@ -546,6 +514,7 @@ def fetch(self, refspec=None, progress=None, **kwargs):
:note:
As fetch does not provide progress information to non-ttys, we cannot make
it available here unfortunately as in the 'push' method."""
if self.repo.git.version_info >= (1, 7, 0, 0): kwargs['progress'] = True
proc = self.repo.git.fetch(self, refspec, with_extended_output=True, as_process=True, v=True, **kwargs)
return self._get_fetch_info_from_stderr(proc, progress or RemoteProgress())

Expand All @@ -557,6 +526,7 @@ def pull(self, refspec=None, progress=None, **kwargs):
:param progress: see 'push' method
:param kwargs: Additional arguments to be passed to git-pull
:return: Please see 'fetch' method """
if self.repo.git.version_info >= (1, 7, 0, 0): kwargs['progress'] = True
proc = self.repo.git.pull(self, refspec, with_extended_output=True, as_process=True, v=True, **kwargs)
return self._get_fetch_info_from_stderr(proc, progress or RemoteProgress())

Expand All @@ -578,7 +548,8 @@ def push(self, refspec=None, progress=None, **kwargs):
in their flags.
If the operation fails completely, the length of the returned IterableList will
be null."""
proc = self.repo.git.push(self, refspec, porcelain=True, as_process=True, **kwargs)
if self.repo.git.version_info >= (1, 7, 0, 0): kwargs['progress'] = True
proc = self.repo.git.push(self, refspec, porcelain=True, as_process=True, progress=True, v=True, **kwargs)
return self._get_push_info(proc, progress or RemoteProgress())

@property
Expand Down
24 changes: 18 additions & 6 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
)


from git.util import (
_digest_process_messages,
_finalize_proc
)

from gitdb.util import (
join,
isfile,
Expand Down Expand Up @@ -652,7 +657,7 @@ def init(cls, path=None, mkdir=True, **kwargs):
return Repo(path)

@classmethod
def _clone(cls, git, url, path, odb_default_type, **kwargs):
def _clone(cls, git, url, path, odb_default_type, progress, **kwargs):
# special handling for windows for path at which the clone should be
# created.
# tilde '~' will be expanded to the HOME no matter where the ~ occours. Hence
Expand All @@ -679,7 +684,11 @@ def _clone(cls, git, url, path, odb_default_type, **kwargs):
# END windows handling

try:
git.clone(url, path, **kwargs)
if git.version_info >= (1, 7, 0, 0): kwargs['progress'] = True
proc = git.clone(url, path, with_extended_output=True, as_process=True, v=True, **kwargs)
if progress:
_digest_process_messages(proc.stderr, progress)
_finalize_proc(proc)
finally:
if prev_cwd is not None:
os.chdir(prev_cwd)
Expand All @@ -703,28 +712,31 @@ def _clone(cls, git, url, path, odb_default_type, **kwargs):
# END handle remote repo
return repo

def clone(self, path, **kwargs):
def clone(self, path, progress=None, **kwargs):
"""Create a clone from this repository.
:param path:
is the full path of the new repo (traditionally ends with ./<name>.git).

:param progress: See 'git.remote.Remote.push'.

:param kwargs:
odbt = ObjectDatabase Type, allowing to determine the object database
implementation used by the returned Repo instance

All remaining keyword arguments are given to the git-clone command

:return: ``git.Repo`` (the newly cloned repo)"""
return self._clone(self.git, self.git_dir, path, type(self.odb), **kwargs)
return self._clone(self.git, self.git_dir, path, type(self.odb), progress, **kwargs)

@classmethod
def clone_from(cls, url, to_path, **kwargs):
def clone_from(cls, url, to_path, progress=None, **kwargs):
"""Create a clone from the given URL
:param url: valid git url, see http://www.kernel.org/pub/software/scm/git/docs/git-clone.html#URLS
:param to_path: Path to which the repository should be cloned to
:param progress: See 'git.remote.Remote.push'.
:param kwargs: see the ``clone`` method
:return: Repo instance pointing to the cloned directory"""
return cls._clone(Git(os.getcwd()), url, to_path, GitCmdObjectDB, **kwargs)
return cls._clone(Git(os.getcwd()), url, to_path, GitCmdObjectDB, progress, **kwargs)

def archive(self, ostream, treeish=None, prefix=None, **kwargs):
"""Archive the tree at the given revision.
Expand Down
43 changes: 41 additions & 2 deletions git/util.py