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

标签 python github gitpython

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

考虑到这一点并考虑 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')

此代码有效,到目前为止,当未找到公共(public)存储库时,我可以使用任何用户名和相应密码(包括空密码)输入。有没有办法忽略(通过发送空字符串)或捕获(某些异常(exception)?)用户名/密码内容?

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

最佳答案

感谢 Lacho Tomov 在这个问题中的回答,我已经弄清楚了: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 可能是其他字符串(但不是空的) .

如果有人有更Pythonic/正确的方法来做到这一点,请随意更新这个答案。

关于python - 想要检查存储库是否存在以及是否是公共(public)的(gitpython),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55347799/

相关文章:

python - 关于 model.load 的 Tflearn 问题

python - 在pycassa中使用column_validators

javascript - 单击 Javascript 警报时如何保留我的 Firefox 首选项?

python - 'git stash' 可能会在哪些情况下失败?

python - 如何更改ttk.Notebook的标签

git - PhpStorm 和 Git - 推送失败 - 失败,错误为 :

android - 如何将github源代码插入到我的项目中

git - 如何强制 merge 分支到master?

python - 如何检查用户对给定存储库拥有哪些权限

python - GitPython 中当前日期和前一段时间之间的 git diff