检查git repo是否通过HTTP请求公开 [英] Check if git repo is public with HTTP request

查看:154
本文介绍了检查git repo是否通过HTTP请求公开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的项目,我需要知道git repo是否公开. 我首先想到要使用HTTP请求,所以我使用的是邮递员,并且我尝试使用HEAD方法处理多个URL.

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.

当我向github发送一个私有项目的请求时,我收到状态404,因此对我来说是完美的,因为我知道这是一个私有项目.

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.

但是当我对gitlab执行此操作时,我会收到状态200,然后将我重定向.

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.

推荐答案

通常,最简单的方法是将/info/refs?service=git-upload-pack附加到URL之后执行GET请求.这就是Git用来获取参考信息的端点.

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.

如果回购是公开的,则将获得200.如果存储库是私有的或不存在,则可能会收到401.大多数主要的托管服务提供商只有在通过身份验证后才提供有关是否存在私有存储库的信息,因此您将以两种方式获得401.

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.

请注意,您不能使用HEAD请求;在这种情况下,GitHub返回不允许的405方法.

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

示例: 开源存储库: https://gitlab.com/gitlab-org/gitlab -ce.git

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

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

示例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

这篇关于检查git repo是否通过HTTP请求公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆