无需登录即可安全检查 public GitHub 存储库是否存在于 bash 中?
Safely check if public GitHub repo exists in bash without login?
上下文
在尝试 provided in 如何使用 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://some_git_user@github.com':
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"
.
问题
如何测试 public GitHub 存储库是否存在于 bash 中,而不提示输入凭据,而不引发错误?
详情
用户未提供任何 ssh 密钥或凭据 to/in 脚本。
为了轻松检查 public 存储库是否存在于 GitHub,您实际上不需要 git
或任何凭据:您可以向 GitHub REST API.
Proof-of-concept:
#!/usr/bin/env bash
exists_public_github_repo() {
local user_slash_repo=
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 .
上下文
在尝试
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://some_git_user@github.com':
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"
.
问题
如何测试 public GitHub 存储库是否存在于 bash 中,而不提示输入凭据,而不引发错误?
详情
用户未提供任何 ssh 密钥或凭据 to/in 脚本。
为了轻松检查 public 存储库是否存在于 GitHub,您实际上不需要 git
或任何凭据:您可以向 GitHub REST API.
Proof-of-concept:
#!/usr/bin/env bash
exists_public_github_repo() {
local user_slash_repo=
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 .