检查git存储库是否存在

时间:2014-05-28 14:48:36

标签: git github

有没有办法在执行git clone操作之前检查git存储库是否存在?像git clone $REMOTE_URL --dry-run

这样的东西

我想实现快速检查方法,它将在执行克隆操作之前检查是否存在。我希望连接速度可能很慢,而且存储库可能会超过1GB,因此我不希望由于简单的验证而依赖于大量的时间操作。验证通过后,克隆将在后台异步执行。

我发现了这个https://superuser.com/questions/227509/git-ping-check-if-remote-repository-exists,但它在初始克隆操作完成后从git repository context起作用。

也许有一些git调用可以在没有git存储库上下文的情况下工作并返回"一些数据"如果存在目的地的存储库,或者如果没有存在错误。

4 个答案:

答案 0 :(得分:23)

您链接的答案是您希望它做的。

尝试在没有存储库的文件夹中运行它:

git ls-remote https://github.com/git/git

即使您没有添加git initgit clone的本地存储库,它也应该显示远程参考。

查看更多:https://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html

答案 1 :(得分:6)

您可以使用GitHub's API轻松完成此操作(请确保从回购名称中删除.git):

curl https://api.github.com/repos/<user>/<repo>

如果找不到存储库:

curl https://api.github.com/repos/coldhawaiian/blarblar
{
  "message": "Not Found",
  "documentation_url": "https://developer.github.com/v3"
}

否则:

curl https://api.github.com/repos/coldhawaiian/git-ninja-toolkit
{
  "id": 11881218,
  "name": "git-ninja-toolkit",
  "full_name": "coldhawaiian/git-ninja-toolkit",
  "owner": {
    "login": "coldhawaiian",
    "id": 463580,
    "avatar_url": "https://avatars.githubusercontent.com/u/463580?",
    "gravatar_id": "f4de866459391939cd2eaf8b369d4d09",
    "url": "https://api.github.com/users/coldhawaiian",
    "html_url": "https://github.com/coldhawaiian",
    "followers_url": "https://api.github.com/users/coldhawaiian/followers",
    // etc...
    "type": "User",
    "site_admin": false
  },
  // etc...
}

答案 2 :(得分:3)

您可以使用ls-remote

$ git ls-remote git@github.com:github/markup.git
794d5d36dae7c1a9a0ed3d452ad0ffa5ab2cc074    HEAD
d27b5b1c5ae84617d4a9356eaf565c7b555c4d1d    refs/heads/can_render_regex
c03cce8f271d683c9cdeb5253c9cb21b5f0e65a0    refs/heads/fix-tests-part-deux
# Snip many more lines

$ git ls-remote git@github.com:nothing/here.git
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

请注意ls-remote在发生这种情况时退出128,因此如果您不需要它,可以重定向命令输出,只需检查命令的返回值。

请注意,在某些情况下,系统可能会提示您输入内容,例如:如果您尝试访问GitHub https://...存储库。这是因为可能存在匹配的私有存储库。

答案 3 :(得分:1)

尝试以下方法:

curl -u "$username:$token" https://api.github.com/repos/user/repository-name

如果Git存储库不存在,输出将为:

{
    "message": "Not Found",
    "documentation_url": "https://developer.github.com/v3"
}

如果Git存储库存在,输出将为:

{
    "id": 123456,
    "name": "repository-name",
    "full_name": "user/repository-name",
    "owner": { ... } 
    ............
}