问题
Im using GitPython to clone a repo within my program. I figured how to display the the status of the clone with the clone_from command but I want the status to look more like a tqdm progress bar. I tried using the requests library to get the size of the file but I'm still unsure how to implement it. Tried doing something like this below but it's not working. Any help appreciated, thanks.
url = 'git@github.com:somegithubrepo/repo.git'
r = requests.get(url, stream=True)
total_length = r.headers.get('content-length')
for i in tqdm(range(len(total_length??))):
git.Git(pathName).clone(url)
回答1:
You can try something like:
import git
from git import RemoteProgress
from tqdm import tqdm
class CloneProgress(RemoteProgress):
def update(self, op_code, cur_count, max_count=None, message=''):
pbar = tqdm(total=max_count)
pbar.update(cur_count)
git.Repo.clone_from(project_url, repo_dir, branch='master', progress=CloneProgress()
回答2:
This is an improved version of the other answer. The bar is created only once when the CloneProgress
class is initialized. And when updating, it sets the bar at the correct amount.
import git
from git import RemoteProgress
from tqdm import tqdm
class CloneProgress(RemoteProgress):
def __init__(self):
super().__init__()
self.pbar = tqdm()
def update(self, op_code, cur_count, max_count=None, message=''):
self.pbar.total = max_count
self.pbar.n = cur_count
self.pbar.refresh()
git.Repo.clone_from(project_url, repo_dir, branch='master', progress=CloneProgress()
来源:https://stackoverflow.com/questions/51045540/python-progress-bar-for-git-clone