gpt4 book ai didi

git - 使用 go-git 从 GitHub Enterprise 克隆存储库

转载 作者:IT王子 更新时间:2023-10-29 01:14:29 24 4
gpt4 key购买 nike

我正在尝试使用 go-git 从 GitHub Enterprise 克隆存储库。为此,我使用带有访问 token 的 HTTPS 协议(protocol),该 token 对我的存储库具有适当的权限(在命令行上验证)。 go-git 在进行 git-upload-pack RPC 调用时失败,因为服务器以 400 响应:

$ go run main.go
unexpected client error: unexpected requesting "https://github.mycompany.net/my-org/myrepo.git/info/refs?service=git-upload-pack" status code: 400

它发出的请求等同于:

GET /my-org/myrepo.git/info/refs?service=git-upload-pack HTTP/1.1
Host: github.mycompany.net
User-Agent: git/1.0
Accept: */*
Authorization: Bearer atokenthatisdefinitelyvalid

如果请求 header 中没有 token ,我会从存储库中得到预期的 401(Anonymous access denied)响应。使用 token ,它以 400 响应。

我发现非企业 GitHub 上的公共(public)存储库也是如此;不同之处在于它(预期)在没有 Authorization header 的情况下工作,因为没有必要。如果我包含一个有效的 token ,GitHub 就像它的企业版一样以 400 响应。

下面是一个最小的例子。有没有办法将 go-git 与需要身份验证的 GitHub Enterprise 一起使用?理想情况下使用身份验证 token ?

package main

import (
"fmt"
"io/ioutil"

git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)

const (
repoURL = "https://github.mycompany.net/my-org/myrepo.git"
githubAccessToken = "atokenthatisdefinitelyvalid"
)

func main() {
dir, _ := ioutil.TempDir("", "temp_dir")

options := &git.CloneOptions{
Auth: &http.TokenAuth{Token: githubAccessToken},
URL: repoURL,
Depth: 500,
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
SingleBranch: true,
Tags: git.NoTags,
}

_, err := git.PlainClone(dir, false, options)
fmt.Println(err)
}

最佳答案

事实证明,Github 使用 token 作为您用户的密码,因此它需要基本身份验证而不是 header 中的 token :

options := &git.CloneOptions{
Auth: &http.BasicAuth{Username: "myusername", Token: "mytoken"},
URL: repoURL,
Depth: 500,
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
SingleBranch: true,
Tags: git.NoTags,
}

关于git - 使用 go-git 从 GitHub Enterprise 克隆存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51751828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com