GITLAB 下载指定 commit 的代码仓库

参考

关于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()
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值