duanlu1908 2018-08-08 16:50
浏览 227
已采纳

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

I'm trying to use go-git to clone a repository from GitHub Enterprise. To do that, I'm using the HTTPS protocol with an access token with appropriate permissions for my repos (verified on the command line). go-git fails when making the git-upload-pack RPC call because the server responds with a 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

The request it makes is equivalent to this:

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

Without the token in the request header, I get the expected 401 (Anonymous access denied) response from the repository. With the token, it responds with a 400.

I have found the same to be true for public repositories on non-Enterprise GitHub; with the difference that it (expectedly) works without an Authorization header since none is necessary. If I include a valid token, GitHub just like its Enterprise version responds with a 400.

Below is a minimal example. Is there a way to use go-git with GitHub Enterprise that requires authentication? Ideally using an auth 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)
}
  • 写回答

2条回答 默认 最新

  • douyabu1528 2018-09-07 09:46
    关注

    Turns out that Github uses the token as a password for your user, so it requires a basic authentication rather than a token in the header:

    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,
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图