Search code examples
gitgitlabhttp-headershttprequest

Check if git repo is public with HTTP request


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.


Solution

  • 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