2020"""
2121
2222import os
23+ import shutil
2324from typing import Optional , Union
2425
25- from git import GitCommandError , RemoteProgress , Repo
26- from tqdm import tqdm
26+ import tqdm
27+ from git import (
28+ GitCommandError ,
29+ InvalidGitRepositoryError ,
30+ RemoteProgress ,
31+ Repo ,
32+ )
2733
2834from ._repositories import REPOSITORIES
2935
@@ -39,7 +45,7 @@ def __init__(self):
3945 Initialize progress bar.
4046 """
4147 super ().__init__ ()
42- self .progress = tqdm ()
48+ self .progress = tqdm . tqdm ()
4349
4450 def update (
4551 self ,
@@ -64,7 +70,9 @@ def update(
6470
6571
6672def clone_to_directory (
67- repo_url : str , target_dir : str , commit : Optional [str ] = None
73+ repo_url : str ,
74+ target_dir : str ,
75+ commit : Optional [str ] = None ,
6876) -> Repo :
6977 """
7078 Clone a git repository to a designated directory.
@@ -77,9 +85,16 @@ def clone_to_directory(
7785 Returns:
7886 Cloned git repository.
7987 """
88+ clone = None
8089 if os .path .exists (target_dir ):
81- clone = Repo (target_dir )
82- else :
90+ try :
91+ clone = Repo (target_dir )
92+ except InvalidGitRepositoryError :
93+ print (f"Repository at { target_dir } is invalid, recreating it..." )
94+ shutil .rmtree (target_dir )
95+ clone = None
96+
97+ if clone is None :
8398 print (f"Cloning { repo_url } ..." )
8499 os .makedirs (target_dir )
85100 progress_bar = CloneProgressBar ()
@@ -88,6 +103,7 @@ def clone_to_directory(
88103 target_dir ,
89104 progress = progress_bar .update ,
90105 )
106+
91107 if commit is not None :
92108 try :
93109 clone .git .checkout (commit )
@@ -99,6 +115,7 @@ def clone_to_directory(
99115 clone .git .fetch ("origin" )
100116 clone .git .checkout (commit )
101117 print (f"Found commit { commit } successfully!" )
118+
102119 return clone
103120
104121
0 commit comments