检查 git 存储库是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23914896/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:08:27  来源:igfitidea点击:

Check that git repository exists

gitgithub

提问by Jan Stanicek

Is there any way how to check whether git repository exist just before perform git cloneaction? Something like git clone $REMOTE_URL --dry-run?

有什么方法可以在执行git clone操作之前检查git存储库是否存在?像git clone $REMOTE_URL --dry-run什么?

I would like to implement fast checking method which will check existence before the clone action will be performed. I expect that connection might be slow and repository might be big even more than 1GB, so I don't want to rely on time extensive operation due to simple validation. The clone will be performed asynchronously in the background when validation passes.

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

I found this https://superuser.com/questions/227509/git-ping-check-if-remote-repository-existsbut it works from git repository context after initial clone action is done.

我发现这个https://superuser.com/questions/227509/git-ping-check-if-remote-repository-exists但它在初始克隆操作完成后从 git 存储库上下文工作。

Maybe there is some git call which works without git repository context and return "some data" if repository on destination exist or error if doesn't.

也许有一些 git 调用在没有 git 存储库上下文的情况下工作,如果目标上的存储库存在或错误,则返回“一些数据”。

回答by Leigh

The answer that you linked does what you want it to do.

您链接的答案会执行您希望它执行的操作。

Try running this in a folder without a repository in it:

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

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

It should show you the references on the remote, even though you haven't added a local repository with git initor done a git clone.

它应该向您显示远程上的引用,即使您没有添加本地存储库git init或完成git clone.

See more: https://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html

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

回答by Jan Stanicek

You can easily do this using GitHub's API(make sure to leave out .gitfrom the repo name):

您可以使用GitHub 的 API轻松完成此操作(确保.git从 repo 名称中省略):

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

If the repository is not found:

如果未找到存储库:

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

Otherwise:

除此以外:

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...
}

回答by Chris

You can use ls-remote:

您可以使用ls-remote

$ git ls-remote [email protected]: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 [email protected]: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.

Note that ls-remoteexits 128 when this occurs, so you could redirect the command output if you don't need it and just check the command's return value.

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

Note that in some cases you may get prompted for input, e.g. if you try to access a GitHub https://...repository. This is because there may be a matching private repository.

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

回答by Ravi Prajapati

Try the following:

请尝试以下操作:

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

If the Git repository does not exist, the output will be:

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

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

If the Git repository exists, the output will be:

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

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