为什么 go module ssh 自定义私有仓库(非 github)配置仍然请求 https 获取?

sgo*_*n00 1 go go-modules

我正在使用 Go 模块。

为了使用模块版本,我无法使用本地模块。例如:

replace locakpkg => ../localpkg v0.1.0
Run Code Online (Sandbox Code Playgroud)

上面的操作将会失败,因为替换本地路径到目前为止还没有版本(转到 1.15)。

因此,为了使模块版本正常工作,我决定使用私有 ssh 存储库。

我花了两天时间搜索如何使私人 ssh 存储库工作。

通过参考很多网上的文章,我做到了

git config --global url.user@private.com:.insteadOf https://private.com/
go env -w GOPRIVATE=private.com
Run Code Online (Sandbox Code Playgroud)

我发现 go get 总是会执行 https fetch 来检查 ssl 凭证。所以我也正确配置了一个https服务器。

但最终我仍然收到错误消息:

unrecognized import path "private.com/foo": reading https://private.com/foo?go-get=1: 404 Not Found
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索了这个错误,发现了这个规范https://golang.org/ref/mod#vcs-find,它说我必须让服务器回复<meta name="go-import" content="root-path vcs repo-url">https 获取请求。

  • 如果有办法在本地模块包中使用 git 标签版本控制,我可以在 go.mod 中使用本地替换,而不是配置私有 ssh 存储库。

  • 如果上述一点不可能,当我配置私有 ssh 存储库时如何避免 https 获取?我认为 ssh repo 与 https 协议无关。

sgo*_*n00 6

(我在linux上使用go 1.15。发布这个答案时最新的稳定版本)

我解决了这个问题并在这里发布,希望有一天这会对其他人有所帮助。我在网上搜索没有找到正确的答案。

简而言之,答案是.git在所有地方都使用后缀。没有.git后缀,go mod tidygo get使用https而不是ssh(git)。

在客户处:

~/.gitconfig如果您使用服务器上的路径,则该文件(在 Linux 上) /repopath/foo.git

[url "ssh://user@private.com"]
    insteadOf = https://private.com
Run Code Online (Sandbox Code Playgroud)

~/.gitconfig如果您使用服务器上的路径,则该文件(在 Linux 上) ~/repopath/foo.git

[url "user@private.com:"]
    insteadOf = https://private.com/
Run Code Online (Sandbox Code Playgroud)

在linux上执行以下命令进行更新~/.config/go/env

go env -w GOPRIVATE=private.com
Run Code Online (Sandbox Code Playgroud)

在 中go.mod,应该使用

require private.com/repopath/foo.git v0.1.0
Run Code Online (Sandbox Code Playgroud)

在 中file.go,应该是

import private.com/repopath/foo.git
Run Code Online (Sandbox Code Playgroud)

在 SSH 服务器上

foo.git/go.mod私人服务器应该有:

module private.com/repopath/foo.git
Run Code Online (Sandbox Code Playgroud)

并确保服务器上的 git repo 有 tag version v0.1.0。不要忘记git push --tags在客户端使用将标签版本更新到服务器。如果没有--tags,则不会推送标签版本。

.git在所有需要的地方添加后缀后,go mod tidygo get不再发送https请求。