When using go-git's Plainclone feature, the git repo url will have /git-upload-pack appended

WBOY
Release: 2024-02-12 08:06:08
forward
424 people have browsed it

使用 go-git 的 Plainclone 功能时,git repo url 会附加 /git-upload-pack

#php editor Baicao introduced that when using the Plainclone function of go-git, when you provide the git repo url, /git-upload-pack will be automatically appended. This is because git-upload-pack is the command used by the git protocol to clone and pull remote repositories. With this feature, you can easily clone and pull remote repositories without adding /git-upload-pack manually. In this way, you can use go-git to perform git operations more conveniently and improve work efficiency.

Question content

Try to clone the repository from Azure Devops.

func (gitopt *GitOptions) clone() (*git.Repository, error) {
    r, err := git.PlainClone(gitopt.RepoDir, false, &git.CloneOptions{
        Progress: os.Stdout,
        URL:      "https://<path to repo>",
        Auth: &http.BasicAuth{
            Username: "gituser",
            Password: gitopt.PAT,
        },
    })
    if err != nil {
        log.Info(err.Error())
        return nil, err
    }

    return r, nil
}
Copy after login

Running this code adds /git-upload-pack ("https:///git-upload-pack") at the end of the repo url, so the clone fails with status code 400. Can't understand why this is appended.

Solution

The HTTP-based Git protocol consists of two steps, depending on the protocol version used. In v0 and v1, the first request is to /info/refs and reads the refs in use, then the second request is to /git-upload-pack (for get and clone) or /git-receive-pack (for pushing). In v2, the endpoints are the same, but the first is the feature request, and then the ref request and data transfer to the second endpoint.

In all these cases, the URL you provide is just the basis for appending the path. Different paths make it easier to control access to a simple Git server behind something like nginx or Apache, which is why there isn't just a single URL component.

So the generated URL is actually correct. The reason you're seeing a 400 is because There's a problem Azure DevOps requires the client to support the multi_ack feature, which go-git doesn't support. While technically servers don't have to provide support for any clients they don't want to, the Git Smart HTTP protocol is generally designed to degrade gracefully, so it's not a safe assumption that clients necessarily support any particular set. functionality, and Azure DevOps should avoid making this assumption.

Linked issues have a link to a pull request that fixes the issue in some (but not all) cases. However, you may need to update to a higher version to take advantage of this.

The above is the detailed content of When using go-git's Plainclone feature, the git repo url will have /git-upload-pack appended. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!