我正在寻找获得$ go与私人存储库工作的方法,经过许多谷歌尝试。

第一次尝试:

$ go get -v gitlab.com/secmask/awserver-go
Fetching https://gitlab.com/secmask/awserver-go?go-get=1
https fetch failed.
Fetching http://gitlab.com/secmask/awserver-go?go-get=1
Parsing meta tags from http://gitlab.com/secmask/awserver-go?go-get=1 (status code 200)
import "gitlab.com/secmask/awserver-go": parse http://gitlab.com/secmask/awserver-go?go-get=1: no go-import meta tags
package gitlab.com/secmask/awserver-go: unrecognized import path "gitlab.com/secmask/awserver-go

是的,它没有看到元标签,因为我不知道如何提供登录信息。

第二次尝试:

按照https://gist.github.com/shurcooL/6927554。将config添加到.gitconfig。

[url "ssh://git@gitlab.com/"]
    insteadOf = https://gitlab.com/
$ go get -v gitlab.com/secmask/awserver-go --> not work
$ go get -v gitlab.com/secmask/awserver-go.git --> work but I got src/gitlab.com/secmask/awserer-go.git

是的,它的工作,但与。git扩展与我的项目名称,我可以重命名为原来的,但每次做$ go get不太好,有没有其他方法?


当前回答

我有一个问题,从我们公司的gitlab使用私人存储库。 我花了几分钟试图找到解决办法。我找到了这个:

你需要获得一个私人令牌: https://gitlab.mycompany.com/profile/account 配置你的git添加额外的头与你的私有令牌: $ git配置——global http。"PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN" 配置你的git将请求从http转换为ssh: $ git配置——全局url."git@gitlab.mycompany.com:". insteadof "https://gitlab.mycompany.com/" 最后你可以正常使用你的go get: $ go get gitlab.com/company/private_repo

其他回答

在设置GOPRIVATE和git配置后…

人们在获取私有资源时仍然会遇到这样的问题:

https fetch: Get "https://private/user/repo?go-get=1": EOF

他们不能使用私人回购没有。git扩展名。

原因是go工具不知道这个repo的VCS协议,git或svn或其他任何协议,不像github.com或golang.org,它们是硬编码到go的源代码中。

然后go工具将在获取你的私有repo之前执行一个https查询:

https://private/user/repo?go-get=1

如果你的私人回购不支持https请求,请使用replace直接告诉它:

require private/user/repo v1.0.0

...

replace private/user/repo => private.server/user/repo.git v1.0.0

https://golang.org/cmd/go/#hdr-Remote_import_paths

你有一个东西要配置。这个例子是基于GitHub的,但这不应该改变这个过程:

$ git config --global url.git@github.com:.insteadOf https://github.com/
$ cat ~/.gitconfig
[url "git@github.com:"]
    insteadOf = https://github.com/
$ go get github.com/private/repo

为了让Go模块工作(使用Go 1.11或更新版本),你还需要设置GOPRIVATE变量,以避免使用公共服务器获取代码:

export GOPRIVATE=github.com/private/repo

这是Go Get中的硬代码。不是Git的原因。修改Go Source。 原因: reportforimportdynamic将请求:https://....go-get

// RepoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
func RepoRootForImportPath(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
    rr, err := repoRootFromVCSPaths(importPath, security, vcsPaths)
    if err == errUnknownSite {
        rr, err = repoRootForImportDynamic(importPath, mod, security)
        if err != nil {
            err = importErrorf(importPath, "unrecognized import path %q: %v", importPath, err)
        }
    }
    if err != nil {
        rr1, err1 := repoRootFromVCSPaths(importPath, security, vcsPathsAfterDynamic)
        if err1 == nil {
            rr = rr1
            err = nil
        }
    }

所以添加gitlab域到vcsPaths就可以了。 下载go源代码:

vi ./src/cmd/go/internal/vcs/vcs.go    

查看下面的代码:

var vcsPaths = []*vcsPath{
    // GitHub
    {
        pathPrefix: "github.com",
        regexp:     lazyregexp.New(`^(?P<root>github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
        vcs:        "git",
        repo:       "https://{root}",
        check:      noVCSSuffix,
    },

添加代码如下,XXXX Is Your Domain:

    // GitLab
    {
        pathPrefix: "gitlab.xxxx.com",
        regexp:     lazyregexp.New(`^(?P<root>gitlab.xxxx\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
        vcs:        "git",
        repo:       "https://{root}",
        check:      noVCSSuffix,
    },

编译和替换go。

对于我来说,其他人提供的解决方案在go get时仍然给出了以下错误

git@gl.nimi24.com:拒绝权限(publickey)。 无法从远程存储库读取。 请确保您拥有正确的访问权限 并且存储库存在。

这个解决方案需要什么

如其他人所述: git配置——全局url."git@github.com:". insteadof "https://github.com/" 从我的。/ssh/id_rsa密钥中删除密码短语,该密钥用于验证到存储库的连接。这可以通过在提示时输入空密码来完成: ssh - keygen - p

为什么会这样

这不是一个很好的解决办法,因为在您的私钥上有一个密码短语总是更好的,但它在OpenSSH内部的某个地方引起了问题。

go get uses internally git, which uses openssh to open the connection. OpenSSH takes the certs necessary for authentication from .ssh/id_rsa. When executing git commands from the command line an agent can take care of opening the id_rsa file for you so that you do not have to specify the passphrase every time, but when executed in the belly of go get, this did not work somewhy in my case. OpenSSH wants to prompt you then for a password but since it is not possible due to how it was called, it prints to its debug log:

read_passphrase:不能打开/dev/tty:没有这样的设备或地址

然后就失败了。如果您从密钥文件中删除了密码短语,OpenSSH将在没有提示的情况下获取您的密钥,并且可以正常工作

这可能是由于Go同时获取模块并同时打开到Github的多个SSH连接造成的(如本文所述)。OpenSSH调试日志显示到存储库的初始连接成功,但后来由于某种原因再次尝试,这一次选择了请求密码短语,这在一定程度上支持了这一点。

然而,上述文章中提出的使用SSH连接多路复用的解决方案对我来说并不适用。为了记录,作者建议在受影响主机的ssh配置文件中添加以下conf:

  ControlMaster auto
  ControlPersist 3600
  ControlPath ~/.ssh/%r@%h:%p

但就像我说的,对我来说这行不通,也许我做错了

我第一次尝试

[url "ssh://git@github.com/"]
    insteadOf = https://github.com/

但在我的本地车里,它不管用。

我试着

SSH -t git@github.com

它显示我的SSH是好的。

最后,我解决了这个问题,告诉go get考虑所有的私有和使用ssh而不是HTTPS。

添加export GOPRIVATE=*