【问题标题】:Why does go module ssh custom private repo (non-github) config still request https fetch?为什么 go module ssh custom private repo (non-github) config 仍然请求 https fetch?
【发布时间】:2021-05-01 10:49:39
【问题描述】:

我正在使用 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 fetch 来检查 ssl 凭证。所以我也正确配置了一个 https 服务器。

但最后,我还是得到了一个错误信息:

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

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

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

  • 如果以上几点都不可能,我配置私有ssh repo时如何避免https fetch?我认为 ssh repo 与 https 协议无关。

【问题讨论】:

    标签: go go-modules


    【解决方案1】:

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

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

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

    在客户处:

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

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

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

    [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 服务器上

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

    module private.com/repopath/foo.git
    

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

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

    【讨论】:

      猜你喜欢
      • 2021-02-07
      • 2015-11-20
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 2011-10-30
      • 2017-01-09
      • 2017-07-13
      相关资源
      最近更新 更多