bash 如何检查远程 git 存储库 URL 的有效性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9610131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check the validity of a remote git repository URL?
提问by Highway of Life
Within a bash script, what would be the simplest way to verify that a git URL points to a valid git repo and that the script has access to read from it?
在 bash 脚本中,验证 git URL 是否指向有效的 git repo 并且脚本有权从中读取的最简单方法是什么?
Protocols that should be supported are git@
, https://
, and git://
. Curl fails on the git://
protocol.
应该支持的协议是git@
,https://
和git://
。卷曲在git://
协议上失败。
[email protected]:UserName/Example.git
https://[email protected]/UserName/Example.git
git://github.com/UserName/Example.git
Note: I'm not asking to check to see if a URL is syntactically correct, I need to verify that a repo exists at the URL location entered from within a bash script.
注意:我不是要检查 URL 在语法上是否正确,我需要验证从 bash 脚本中输入的 URL 位置是否存在 repo。
采纳答案by VonC
As seen in this issue, you can use git ls-remoteto test your address.
如本期所示,您可以使用git ls-remote来测试您的地址。
If you need to debug the git calls
set GIT_TRACE=1
. eg:
如果您需要调试 git 调用
set GIT_TRACE=1
. 例如:
env GIT_PROXY_COMMAND=myproxy.sh GIT_TRACE=1 git ls-remote https://...
"
git ls-remote
" is the quickest way I know to test communications with a remote repository without actually cloning it. Hence its utility as a test for this issue.
“
git ls-remote
”是我所知道的测试与远程存储库通信而不实际克隆它的最快方法。因此,它可用作此问题的测试。
You can see it used for detecting an address issue in "git ls-remote
returns 128 on any repo".
您可以看到它用于检测“git ls-remote
在任何回购上返回 128”中的地址问题。
回答by Highway of Life
Although VonC's answer is correct, here's what I ended up using:
尽管 VonC 的答案是正确的,但我最终使用的是以下内容:
git ls-remotewill return information about a repository, by default this is HEAD, all branches and tags, along with the commit ID for each entry. e.g.:
git ls-remote将返回有关存储库的信息,默认情况下这是 HEAD、所有分支和标签,以及每个条目的提交 ID。例如:
$ git ls-remote git://github.com/user/repo.git
<commit id> HEAD
<commit id> refs/heads/example_branch
<commit id> refs/heads/master
<commit id> refs/tags/v1.0.2
<commit id> refs/tags/v1.0.0
git ls-remote
returns code 0 on success, error code 128 on failure.
git ls-remote
成功时返回代码 0,失败时返回错误代码 128。
If the repo is unreachable, for example, if you don't have permission to view the repository, or if a repository doesn't exist at that location, git ls-remote
will return:
如果存储库无法访问,例如,如果您无权查看存储库,或者该位置不存在存储库,git ls-remote
则将返回:
fatal: The remote end hung up unexpectedly
To use this in a bash script, the following will work...
要在 bash 脚本中使用它,以下将起作用...
git ls-remote "$SITE_REPO_URL" > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo "[ERROR] Unable to read from '$SITE_REPO_URL'"
exit 1;
fi
(Note:The > /dev/null 2>&1
silences stderr and stdout, so the command won't output anything)
(注:在> /dev/null 2>&1
沉默标准错误和标准输出,因此命令将不会输出任何东西)