Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python progress bar for git clone

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 = '[email protected]: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)
like image 549
Xavier Salazar Avatar asked Aug 27 '26 18:08

Xavier Salazar


1 Answers

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()
like image 53
Cosmos Zhu Avatar answered Aug 30 '26 08:08

Cosmos Zhu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!