Check if git repo is public with HTTP request

为君一笑 提交于 2020-12-05 12:10:20

问题


For my project, I need to know if a git repo is public or not to do some actions. I first thought to use HTTP request so I'm using postman and I tried with multiple URLs with HEAD method.

When I'm sending a request, I can't see what in the header can indicate me when it failed or not.

When I send a request to github for a private project, I'm receiving a the status 404 so it's perfect for me cause I know that is a private project.

BUT when I do this for gitlab, I'm receiving the status 200 then I'm redirected.

I would like to know if there is a solution for that.


回答1:


Generally, the easiest way to do this is to perform a GET request after appending /info/refs?service=git-upload-pack to the URL. That's the endpoint which Git uses to get reference information.

If the repo is public, you'll get a 200. You'll likely get a 401 if the repository is private or doesn't exist. Most major hosting providers don't provide information about whether a private repository exists or not until after you've authenticated, so you'll get a 401 either way.

Note that you can't use a HEAD request; GitHub returns a 405 Method Not Allowed in that case.

Example: Open Source Repo: https://gitlab.com/gitlab-org/gitlab-ce.git

{GET}: https://gitlab.com/gitlab-org/gitlab-ce.git/info/refs?service=git-upload-pack

Example 401:

wget $URL
Connecting to gitlab.com (35.231.145.151:80)
Connecting to gitlab.com (35.231.145.151:443)
wget: server returned error: HTTP/1.1 401 Unauthorized

echo $?
1


来源:https://stackoverflow.com/questions/54959589/check-if-git-repo-is-public-with-http-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!