为什么 go module ssh custom private repo (non-github) config 仍然请求 https fetch?

Why does go module ssh custom private repo (non-github) config still request https fetch?

我正在使用 Go 模块。

为了使用模块版本,我不能使用本地模块。例如:

replace locakpkg => ../localpkg v0.1.0

以上将失败,因为替换本地路径目前不能有版本(转到 1.15)。

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

我确实搜索了两天如何使私有 ssh 存储库工作。

通过关注许多在线文章,我做到了

git config --global url.user@private.com:.insteadOf https://private.com/
go env -w GOPRIVATE=private.com

我发现 go get 将始终执行 https 提取以检查 ssl 凭据。所以我也正确配置了一个https服务器。

但最后还是报错:

unrecognized import path "private.com/foo": reading https://private.com/foo?go-get=1: 404 Not Found

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

(我在 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

文件 ~/.gitconfig(位于 linux)如果您使用 ~/repopath/foo.git 服务器的路径:

[url "user@private.com:"]
    insteadOf = https://private.com/

执行以下操作以在 linux 处更新 ~/.config/go/env

go env -w GOPRIVATE=private.com

go.mod中应该使用

require private.com/repopath/foo.git v0.1.0

file.go中,应该是

import private.com/repopath/foo.git

在 SSH 服务器上

in foo.git/go.mod at private server 应该有:

module private.com/repopath/foo.git

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

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