想要检查存储库是否存在以及它是否是公共的(gitpython)

2024-04-19 16:14:35 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Python和GitPython,以及来自不同用户的git存储库的列表,我需要检查存储库是否存在以及它是否是公共的。在

考虑到这一点,考虑到GitHub,如果它要求用户名和密码,我知道存储库超出了我的标准。因此,我可以忽略它。在

import git

class User():
    def __init__(self, a, b, c):
        self.name = a
        self.git_user = b
        self.git_repos = c

user = User('John', 'john1234', 'fake_one')

try:
    git.Repo.clone_from(f'https://github.com/{user.git_user}/' + \
        f'{user.git_repos}.git', f'temp/{user.name}/')
except git.exc.GitError:
    print(f'ERROR! {user.name}: {user.git_user}/{user.git_repos} does not exist')

这段代码可以工作,到目前为止,当存储库不是公共存储库时,我使用任何用户名和相应的密码(包括空的)输入。有没有一种方法可以忽略(通过发送空字符串)或捕捉(某些异常)用户名和密码?在

请注意,如果我可以检查(公共)存储库的存在,我可以使用此信息来防止不适当的“克隆”。。。在


Tags: 用户nameimportgitselfgithub密码列表
1条回答
网友
1楼 · 发布于 2024-04-19 16:14:35

多亏了拉科托莫夫在这个问题上的回答:Git push requires username and password

具有所需行为的代码是:

import git

class User():
    def __init__(self, a, b, c):
        self.name = a
        self.git_user = b
        self.git_repos = c

user = User('John', 'john1234', 'fake_one')

try:
    git.Repo.clone_from(f'https://null:null@github.com/{user.git_user}/' + \
        f'{user.git_repos}.git', f'temp/{user.name}/')
except git.exc.GitError:
    print(f'ERROR! {user.name}: {user.git_user}/{user.git_repos} does not exist')

注意,https://null:null@github.com/{user.git_user}/中的null可能是其他字符串(但不是空的)。在

如果有人有一个更像Python的/正确的方法来做这件事,请随时更新这个答案。在

相关问题 更多 >