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

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.

Tags:

Python

Git