上下文
尝试时the answer提供于 this question关于如何使用 bash 检查 GitHub 存储库是否存在,我注意到该命令要求提供凭据,如果未找到存储库,则会抛出错误:
git ls-remote https://github.com/some_git_user/some_non_existing_repo_name -q
Username for 'https://github.com': some_git_user
Password for 'https://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93e0fcfef6ccf4fae7cce6e0f6e1d3f4fae7fbe6f1bdf0fcfe" rel="noreferrer noopener nofollow">[email protected]</a>':
remote: Repository not found.
fatal: repository 'https://github.com/some_git_user/some_non_existing_repo_name/' not found
(base) somename@somepcname:~$ echo $?
128
This answer似乎阻止了 git 询问凭据。
问题
- 在其他情况下,要求提供凭据是没问题的,因此我不想禁用
git
在系统范围内询问凭据。然而,我仍然希望该函数能够自动运行而不要求提供凭据,因为检查公共(public)存储库不需要它们。 - 如果未找到存储库,该命令会生成错误状态 128。相反,我想安全地检查是否找到存储库,而不会引发错误。例如。通过生成输出
echo "FOUND"
或echo "NOTFOUND"
。
问题
如何测试 bash 中是否存在公共(public) GitHub 存储库,而不提示输入凭据,也不引发错误?
详细信息
用户未在脚本中提供任何 ssh key 或凭据。
最佳答案
为了轻松检查 GitHub 上是否存在公共(public)存储库,您实际上不需要 git 也不需要任何凭据:您可以向 GitHub REST API 发出简单的 HTTP 请求。 .
概念验证:
#!/usr/bin/env bash
exists_public_github_repo() {
local user_slash_repo=$1
if curl -fsS "https://api.github.com/repos/${user_slash_repo}" >/dev/null; then
printf '%s\n' "The GitHub repo ${user_slash_repo} exists." >&2
return 0
else
printf '%s\n' "Error: no GitHub repo ${user_slash_repo} found." >&2
return 1
fi
}
if exists_public_github_repo "ocaml/ocaml"; then
echo "OK"
fi
有关 curl
命令本身和标志 -f
、-s
、-S
的更多详细信息我用过,你可以浏览它的man page在线。
关于bash - 无需登录即可安全检查 bash 中是否存在公共(public) GitHub 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71068558/