参考
关于git:如何浅深度克隆深度为1的特定提交? | 码农家园
1 常规方式
import git
git.Repo.clone_from(url, to_path=path, branch=branch, multi_options=[
'--single-branch','-c http.sslCAinfo=' + SSL_VERIFY])
repo = git.Repo(path)
repo.git.reset('--hard', commit_id)
2 查询commit所在位置,再行下载
url_p = '/'.join(url.split('/')[0:3])
gl = gitlab.Gitlab(url_p, private_token=GIT_TOKEN, ssl_verify=False)
path_with_namespace = '/'.join(url.split('/blob/')[0].split('/')[3:])
project = gl.projects.get(path_with_namespace)
git.Repo.clone_from(url, to_path=path, branch=branch, multi_options=[
'--single-branch', '--depth=1', '-c http.sslCAinfo=' + SSL_VERIFY])
repo = git.Repo(path)
repo.git.reset('--hard', commit_id)
3 快速下载(无法根据 .git 查询到当前准确的分支信息)
3.1 linux shell 方式
mkdir z_xxxx_1
cd z_xxxx_1
git init
git remote add origin https://gitlab.xxx.com/xxx/xxx
git fetch --depth 1 origin commitid
git checkout FETCH_HEAD
3.2 python 方式
import git
import datetime
datetime.datetime.now()
# 在文件夹里新建一个仓库,如果已存在git仓库也不报错不覆盖没问题
repo = git.Repo.init('/root/repo')
# 创建remote:
remote = repo.create_remote(name='origin', url='https://oauth:GIT_TOKEN@gitlab.xxx.com/xxx/xxx')
# fetch
repo.git.fetch('--depth', '1', 'origin', 'commitid')
# checkout
repo.git.checkout('FETCH_HEAD')
# 修改分支名 git symbolic-ref -q HEAD
repo.git.switch('-c', 'master')
# remote set-url
repo.remotes.origin.set_url(new_url='https://gitlab.xxx.com/xxx/xxx')
datetime.datetime.now()