为什么go module ssh自定义私有仓库(非github)配置仍然请求https提取? [英] Why does go module ssh custom private repo (non-github) config still request https fetch?

查看:299
本文介绍了为什么go module ssh自定义私有仓库(非github)配置仍然请求https提取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Go模块.

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

In order to use module version, I cannot use local module. For example:

replace locakpkg => ../localpkg v0.1.0

上述操作将失败,因为到目前为止,替换本地路径尚无法提供版本(执行1.15).

The above will fail because replacement local path cannot have version so far (go 1.15).

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

Thus, to make the module version work, I decided to use a private ssh repo.

我确实搜索了如何使私有ssh回购工作两天.

I did search how to make private ssh repo work for two days.

通过阅读许多在线文章,我做到了

By following many online articles, I did

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

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

I found out go get will always do https fetch to check ssl credential. So I configured a https server properly too.

但是最后,我仍然收到错误消息:

But in the end, I still get an error message:

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 ,它表示我必须让服务器使用<元名称="go-import"内容=用于https提取请求的根路径vcs repo-url"" .

I did google this error and found out this spec https://golang.org/ref/mod#vcs-find which says I have to let the server reply with <meta name="go-import" content="root-path vcs repo-url"> for https fetch request.

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

  • If there is a way to use git tag versioning in local module packages, I am OK to use local replace in go.mod instead of configuring a private ssh repo.

如果无法实现上述几点,那么在配置私有ssh存储库时如何避免https抓取?我认为ssh repo与https协议无关.

If the above point is not possible, how to avoid https fetch when I configure a private ssh repo? I think ssh repo has nothing to do with https protocol.

推荐答案

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

(I am using go 1.15 at linux. The latest stable version while posting this answer)

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

I solved the problem and posting here, hopefully, this will help other people one day. I don't find any correct answer by my search online.

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

In short, the answer is to use .git suffix in all places. Without .git suffix, go mod tidy and go get will use https instead of ssh (git).

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

The file ~/.gitconfig (at linux) if you use /repopath/foo.git path at server:

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

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

The file ~/.gitconfig (at linux) if you use ~/repopath/foo.git path at server:

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

执行以下命令以在Linux上更新〜/.config/go/env :

Execute the following to update ~/.config/go/env at linux:

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 中的

应该具有:

At SSH Server

in foo.git/go.mod at private server should have:

module private.com/repopath/foo.git

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

And make sure the git repo at server has tag version v0.1.0. Don't forget to use git push --tags at client to update the tag version to the server. Without --tags, tag version will not be pushed.

.git 后缀添加到所有必需的位置后, go mod tidy go get 将不再发送https请求.

After adding .git suffix to all the required places, go mod tidy and go get will no longer send https request.

这篇关于为什么go module ssh自定义私有仓库(非github)配置仍然请求https提取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆