@@ -93,6 +93,7 @@ def clone(
9393 cwd : Optional [str ] = None ,
9494 use_http : bool = False ,
9595 add_remotes : bool = False ,
96+ cache_directory : Optional [str ] = None ,
9697 reference : Optional [str ] = None ,
9798 reference_if_able : Optional [str ] = None ,
9899 ssh_private_key_path : Optional [str ] = None ,
@@ -108,6 +109,7 @@ def clone(
108109 :param cwd: Working directory. Defaults to the current working directory.
109110 :param use_http: Whether to use``clone_url`` for cloning over http(s) instead of ``ssh_url`` for cloning over SSH.
110111 :param add_remotes: Determine and add 'parent' or 'fork' remotes to the cloned repo.
112+ :param cache_directory: Manage repo cache under ``cache_directory`` / ``conn.login.name`` / ``owner`` / ``repo`` and pass it as ``reference_if_able``.
111113 :param reference: Reuse objects from the specified local repository, error out if the repository doesn't exist.
112114 :param reference_if_able: Reuse objects from the specified local repository, only print warning if the repository doesn't exist.
113115 """
@@ -118,6 +120,11 @@ def clone(
118120 # it's perfectly fine to use os.path.join() here because git can take an absolute path
119121 directory_abspath = os .path .join (cwd , directory )
120122
123+ if cache_directory and not reference_if_able :
124+ cache_directory = os .path .join (cache_directory , conn .login .name , owner , repo )
125+ cls .clone_or_update (conn , owner , repo , directory = cache_directory )
126+ reference_if_able = cache_directory
127+
121128 repo_obj = cls .get (conn , owner , repo )
122129
123130 clone_url = repo_obj .clone_url if use_http else repo_obj .ssh_url
@@ -208,23 +215,24 @@ def clone_or_update(
208215 branch : Optional [str ] = None ,
209216 commit : Optional [str ] = None ,
210217 directory : str ,
218+ cache_directory : Optional [str ] = None ,
211219 reference : Optional [str ] = None ,
220+ reference_if_able : Optional [str ] = None ,
212221 remote : Optional [str ] = None ,
213222 ssh_private_key_path : Optional [str ] = None ,
214223 ):
215224 from osc import gitea_api
216225
217- if not pr_number and not branch :
218- raise ValueError ("Either 'pr_number' or 'branch' must be specified" )
219-
220226 if not os .path .exists (os .path .join (directory , ".git" )):
221227 gitea_api .Repo .clone (
222228 conn ,
223229 owner ,
224230 repo ,
225231 directory = directory ,
226232 add_remotes = True ,
233+ cache_directory = cache_directory ,
227234 reference = reference ,
235+ reference_if_able = reference_if_able ,
228236 ssh_private_key_path = ssh_private_key_path ,
229237 )
230238
@@ -259,7 +267,7 @@ def clone_or_update(
259267 else :
260268 git .fetch ()
261269 else :
262- raise ValueError ( "Either 'pr_number' or 'branch' must be specified" )
270+ git . fetch ( )
263271
264272 @classmethod
265273 def list_org_repos (cls , conn : Connection , owner : str ) -> List ["Repo" ]:
0 commit comments