Fork 了几十个项目,想全部一次性 Download 到本地。Gitee 官方的 OpenAPI 支持获取所有仓库的信息,这样就可以通过仓库的 ssh_url 使用 Python 克隆所有的项目。
一、获取仓库信息
通过 列出授权用户的所有仓库: https://gitee.com/api/v5/swagger#/getV5UserRepos
将获取的 json 文件保存到本地。
二、Python 对 json 操作
json.loads 用于解码 JSON 数据,该函数返回 Python 字段的数据类型。
三、Python 对 git 操作
完全搬运:python操作git
0 安装 gitpython 库
pip install gitpython
1 初始化
1 2
| from git import Repo Repo.init('/data/test2')
|
2 添加与提交
1 2
| repo.index.add(['a.txt']) repo.inex.commit('update new')
|
3 回滚
1 2
| repo.index.checkout(['a.txt']) repo.index.reset(commit='486a9565e07ad291756159dd015eab6acda47e25',head=True)
|
4 分支
1
| repo.create_head('debug')
|
5 tag
6 拉取远程仓库
1 2 3 4 5 6
| clone_repo=git.Repo.clone_from('https://github.com/wangfeng7399/syncmysql.git','/data/test3') remote = repo.remote()
remote.pull('master')
remote.push('master')
|
7 使用原生命令
1 2 3 4
| repo=git.Git('/data/test4') repo.checkout('debug')
print(repo.status())
|
四、代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import json import git def git_clone(jsons, key): str_value = [jsons] key_value = '' if isinstance(jsons, dict): for json_result in jsons.values(): if key in jsons.keys(): key_value = jsons.get(key) else: git_clone(json_result, key) elif isinstance(jsons, list): for json_array in jsons: git_clone(json_array, key) if key_value != '': list_value = list(str(key_value).split()) print(list_value) for repo in list_value: repo_name1 = str(repo.replace('git@gitee.com:AshinWang/','')) repo_name2 = str(repo_name1.replace('.git','')) print(repo_name2) path = '/Users/ashin/Desktop/test_dir/' + repo_name2 git.Repo.clone_from(repo,path) if __name__ == '__main__': path = '/Users/ashin/Desktop/' with open(path + 'repos.json', 'r+') as f: json_data = json.load(f) git_clone(json_data, 'ssh_url')
|