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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With