我正在寻找获得$ 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不太好,有没有其他方法?


当前回答

在设置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

其他回答

我有一个问题,从我们公司的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

如果你已经使用SSH得到git, Ammar Bandukwala的回答是一个简单的解决方案:


$ go get内部使用git。下面的一行代码将使git和$ go get通过SSH克隆你的包。

Github:

$ git config --global url."git@github.com:".insteadOf "https://github.com/"

BitBucket都:

$ git config --global url."git@bitbucket.org:".insteadOf "https://bitbucket.org/"

我偶然发现了.netrc,并发现它与此相关。

创建一个包含以下内容的文件~/.netrc:

machine github.com
    login <github username>
    password <github password or Personal access tokens >

完成了!

此外,对于最新的GO版本,您可能需要将其添加到环境变量GOPRIVATE=github.com (我已经把它添加到我的。zshrc)

netrc还使我的开发环境设置更好,因为我个人的github访问HTTPS现在已配置为跨机器使用(就像我的SSH配置)。

生成GitHub个人访问令牌:https://github.com/settings/tokens

请参阅这个答案,了解它在Windows上与Git的具体使用

参考:netrc手册页


如果您希望坚持使用SSH身份验证,那么可以屏蔽请求以强制使用SSH

git配置——全局url."git@github.com:". insteadof "https://github.com/"


更多设置git访问的方法:https://gist.github.com/technoweenie/1072829#gistcomment-2979908

对于我来说,其他人提供的解决方案在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

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

你有一个东西要配置。这个例子是基于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