python 从 github 代码组织中克隆所有代码库的脚本

python script to clone all the code repos from github code organisation

有很多方法可以从组织中克隆 GitHub 代码库。现在考虑到有一些私有和 public 的回购协议,我正在寻找一种解决方案,其中自动化脚本或代码逻辑将解决我克隆代码回购协议的目的,而不管回购协议类型如何。我尝试了各种措施,但无法采用简单的自动化解决方案。我尝试使用 curl 命令,但速度很慢,因为要克隆的代码库密度超过 200+ 个代码库。

你能试试下面的 python 代码片段来克隆所有 repos:

这里我们使用 git ssh 以安全的方式克隆每个存储库以容纳两个 public/private 代码。

有时由于网络问题,它可能会显示远程 hung/packet-loss 消息,从而导致回购 partial/no 克隆。为避免这种情况,请在 shell.

中设置以下参数
git config --global http.postBuffer 157286400

假设您的 git 全局凭据已更新。

代码:

import os
# Define your repo list
list = ["repo1","repo2"]

#Loop it through repo's list
for repo in list:
    #Clone the each repo using ssh git clone command in most secure way
    cmd = "git clone git@<git ssh url>/{}".format(repo)
    print("Starting to clone {}".format(repo))
    os.system(cmd)
    print("Finshed cloning {}".format(repo))
    print("#####################################")
    print("")

将来自两个资源的信息放在一起以克隆所有组织的私有存储库:

# https://www.thepythoncode.com/article/using-github-api-in-python
# https://github.com/libgit2/pygit2/issues/554

# pip3 install PyGithub pygit2

from github import Github
import pygit2

# using an access token
g = Github('TOKEN')
org = g.get_organization('ORG NAME')

callbacks = pygit2.RemoteCallbacks(pygit2.UserPass('TOKEN', 'x-oauth-basic'))
# Clone repo
for repo in org.get_repos():
    pygit2.clone_repository(
        url=repo.clone_url,
        path=f'/home/<Username>/PycharmProjects/Backup/{repo.name}',
        callbacks=callbacks)