Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I git clone a repository with python, and get the progress of the clone process?

Tags:

git

python

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?

like image 785
Jonathan Avatar asked Aug 05 '16 03:08

Jonathan


2 Answers

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
like image 63
Robᵩ Avatar answered Oct 14 '22 09:10

Robᵩ


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.

like image 38
bruce Avatar answered Oct 14 '22 09:10

bruce