1010import logging
1111import argparse
1212import shutil
13- import pygit2 as git
13+ from git import Repo
1414import bz2
1515import contextlib
1616from datetime import datetime
@@ -230,14 +230,9 @@ def get_github_token(git_url):
230230
231231
232232def download_git_clone (git_url , target_dir , checkout_to = "" , tag = "" , branch = "" ):
233- ref_to_checkout = decide_checkout (checkout_to , tag , branch )
234- msg = ""
235233 oss_name = get_github_ossname (git_url )
236- oss_version = ""
237- github_token = get_github_token (git_url )
238- callbacks = None
239- if github_token != "" :
240- callbacks = git .RemoteCallbacks (credentials = git .UserPass ("foo" , github_token )) # username is not used, so set to dummy
234+ refs_to_checkout = decide_checkout (checkout_to , tag , branch )
235+ msg = ""
241236
242237 try :
243238 if platform .system () != "Windows" :
@@ -248,30 +243,27 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch=""):
248243 alarm .start ()
249244
250245 Path (target_dir ).mkdir (parents = True , exist_ok = True )
251- repo = git .clone_repository (git_url , target_dir ,
252- bare = False , repository = None ,
253- remote = None , callbacks = callbacks )
246+ if refs_to_checkout != "" :
247+ # gitPython uses the branch argument the same whether you check out to a branch or a tag.
248+ repo = Repo .clone_from (git_url , target_dir , branch = refs_to_checkout )
249+ logger .info (f"git checkout: { refs_to_checkout } " )
250+ else :
251+ repo = Repo .clone_from (git_url , target_dir )
252+
253+ if refs_to_checkout != tag :
254+ oss_version = repo .active_branch .name
255+ else :
256+ oss_version = repo .git .describe ('--tags' )
257+
254258 if platform .system () != "Windows" :
255259 signal .alarm (0 )
256260 else :
257261 del alarm
258262 except Exception as error :
259263 logger .warning (f"git clone - failed: { error } " )
260264 msg = str (error )
261- return False , msg , oss_name , oss_version
262- try :
263- if ref_to_checkout != "" :
264- ref_list = [x for x in repo .references ]
265- ref_to_checkout = get_ref_to_checkout (ref_to_checkout , ref_list )
266- logger .info (f"git checkout: { ref_to_checkout } " )
267- repo .checkout (ref_to_checkout )
265+ return False , msg , oss_name , refs_to_checkout
268266
269- for prefix_ref in prefix_refs :
270- if ref_to_checkout .startswith (prefix_ref ):
271- oss_version = ref_to_checkout [len (prefix_ref ):]
272-
273- except Exception as error :
274- logger .warning (f"git checkout to { ref_to_checkout } - failed: { error } " )
275267 return True , msg , oss_name , oss_version
276268
277269
0 commit comments