#NEWLINE# def clone_git_repo( git_url , target_dir , commit = None ) : #NEWLINE# #INDENT# '\n    clone provided git repo to target_dir, optionally checkout provided commit\n\n    :param git_url: str, git repo to clone\n    :param target_dir: str, filesystem path where the repo should be cloned\n    :param commit: str, commit to checkout, SHA-1 or ref\n    :return: str, commit ID of HEAD\n    ' #NEWLINE# commit = ( commit or 'master' ) #NEWLINE# logger . info ( "cloning git repo '%s'" , git_url ) #NEWLINE# logger . debug ( "url = '%s', dir = '%s', commit = '%s'" , git_url , target_dir , commit ) #NEWLINE# cmd = [ 'git' , 'clone' , '-b' , commit , '--depth' , '1' , git_url , quote ( target_dir ) ] #NEWLINE# logger . debug ( "doing a shallow clone '%s'" , cmd ) #NEWLINE# try : #NEWLINE# #INDENT# subprocess . check_call ( cmd ) #NEWLINE# #UNINDENT# except subprocess . CalledProcessError as ex : #NEWLINE# #INDENT# logger . warning ( repr ( ex ) ) #NEWLINE# cmd = [ 'git' , 'clone' , '-b' , commit , '--single-branch' , git_url , quote ( target_dir ) ] #NEWLINE# logger . debug ( "cloning single branch '%s'" , cmd ) #NEWLINE# try : #NEWLINE# #INDENT# subprocess . check_call ( cmd ) #NEWLINE# #UNINDENT# except subprocess . CalledProcessError as ex : #NEWLINE# #INDENT# logger . warning ( repr ( ex ) ) #NEWLINE# cmd = [ 'git' , 'clone' , git_url , quote ( cmd ) ] #NEWLINE# logger . debug ( "cloning '%s'" , cmd ) #NEWLINE# subprocess . check_call ( cmd ) #NEWLINE# cmd = [ 'git' , 'reset' , '--hard' , commit ] #NEWLINE# logger . debug ( "checking out branch '%s'" , cmd ) #NEWLINE# subprocess . check_call ( cmd , cwd = target_dir ) #NEWLINE# #UNINDENT# #UNINDENT# cmd = [ 'git' , 'rev-parse' , 'HEAD' ] #NEWLINE# logger . debug ( "getting SHA-1 of provided ref '%s'" , cmd ) #NEWLINE# try : #NEWLINE# #INDENT# commit_id = subprocess . check_output ( cmd , cwd = target_dir ) #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# commit_id = backported_check_output ( cmd , cwd = target_dir ) #NEWLINE# #UNINDENT# commit_id = commit_id . strip ( ) #NEWLINE# logger . info ( 'commit ID = %s' , commit_id ) #NEWLINE# return commit_id