I'd like to be able to git clone a large repository using python, using some library, but importantly I'd like to be able to see the progress of the clone as it's happening. I tried pygit2 and GitPython, but they don't seem to show their progress. Is there another way?
Asked
Active
Viewed 8,646 times
11
-
How do you want to show the progress? In a GUI or on stdout? – Robᵩ Aug 05 '16 at 03:42
-
http://stackoverflow.com/questions/23155452/read-git-clones-output-in-real-time – David Neiss Aug 05 '16 at 03:53
-
@Robᵩ, I was thinking just the normal way the CLI git command shows you the progress, e.g. grabbing object 11/27... – Jonathan Aug 09 '16 at 07:19
2 Answers
12
You can use RemoteProgress
from GitPython. Here is a crude example:
import git
class Progress(git.remote.RemoteProgress):
def update(self, op_code, cur_count, max_count=None, message=''):
print 'update(%s, %s, %s, %s)'%(op_code, cur_count, max_count, message)
repo = git.Repo.clone_from(
'https://github.com/gitpython-developers/GitPython',
'./git-python',
progress=Progress())
Or use this update()
function for a slightly more refined message format:
def update(self, op_code, cur_count, max_count=None, message=''):
print self._cur_line

Robᵩ
- 163,533
- 20
- 239
- 308
2
If you simply want to get the clone information, no need to install gitpython
, you can get it directly from standard error stream through built-in subprocess
module.
import os
from subprocess import Popen, PIPE, STDOUT
os.chdir(r"C:\Users") # The repo storage directory you want
url = "https://github.com/USER/REPO.git" # Target clone repo address
proc = Popen(
["git", "clone", "--progress", url],
stdout=PIPE, stderr=STDOUT, shell=True, text=True
)
for line in proc.stdout:
if line:
print(line.strip()) # Now you get all terminal clone output text
You can see some clone command relative informations after execute the command git help clone
.
--progress
Progress status is reported on the standard error stream by default when it is attached to a terminal, unless
--quiet
is specified. This flag forces progress status even if the standard error stream is not directed to a terminal.

bruce
- 462
- 6
- 9