git - 为什么 `go get` 尝试访问 http 而不是 https?

标签 git go

我现在正在为我的协作项目使用私有(private)存储库。
我设置了我的个人 GitLab,例如 gitlab.xity.dev/QuietJoon/testgoget ,
我试图为我的团队提供它。
(存储库 https://gitlab.xity.dev/QuietJoon/testgoget 对于这个问题是公开的)

问题

但是,当我尝试 go get -v gitlab.xity.dev/QuietJoon/testgoget ,
它说

get "gitlab.xity.dev/QuietJoon/testgoget": found meta tag get.metaImport{Prefix:"gitlab.xity.dev/QuietJoon/testgoget", VCS:"git", RepoRoot:"http://gitlab.xity.dev/QuietJoon/testgoget.git"} at //gitlab.xity.dev/QuietJoon/testgoget?go-get=1
package gitlab.xity.dev/QuietJoon/testgoget: cannot download, http://gitlab.xity.dev/QuietJoon/testgoget.git uses insecure protocol

另外,我试过go get -v gitlab.xity.dev/QuietJoon/testgoget.git显式 .git后缀
我得到
# cd .; git ls-remote https://gitlab.xity.dev/QuietJoon/testgoget
fatal: repository 'https://gitlab.xity.dev/QuietJoon/testgoget/' not found
# cd .; git ls-remote git+ssh://gitlab.xity.dev/QuietJoon/testgoget

你可以看到go get尝试https第一的,
但是 go get.git 的后缀因此它说找不到存储库,并前进到下一个 git+ssh协议(protocol)。
我可以得到 git clone gitlab.xity.dev/QuietJoon/testgoget.git无需任何认证过程,
git ls-remote https://gitlab.xity.dev/QuietJoon/testgoget.git
c238e4b9818eabe6309a2c5cf274808ec39bf7d5        HEAD
c238e4b9818eabe6309a2c5cf274808ec39bf7d5        refs/heads/master

我的不完整解决方案

在我设置全局 git 重定向配置后(如 git config --global url."https://gitlab.xity.dev".insteadOf "http://gitlab.xity.dev" )
我可以通过 -insecure 获得包裹旗帜
$ go get -v -insecure gitlab.xity.dev/QuietJoon/testgoget
get "gitlab.xity.dev/QuietJoon/testgoget": found meta tag get.metaImport{Prefix:"gitlab.xity.dev/QuietJoon/testgoget", VCS:"git", RepoRoot:"http://gitlab.xity.dev/QuietJoon/testgoget.git"} at //gitlab.xity.dev/QuietJoon/testgoget?go-get=1
gitlab.xity.dev/QuietJoon/testgoget (download)
gitlab.xity.dev/QuietJoon/testgoget

当我不提供 http://gitlab.xity.dev/QuietJoon/testgoget
$ curl -I http://gitlab.xity.dev/QuietJoon/testgoget
HTTP/1.1 404 Not Found
Server: nginx/1.16.0
Date: Tue, 09 Jun 2020 06:16:25 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
$ curl -I http://gitlab.xity.dev/QuietJoon/testgoget.git
HTTP/1.1 404 Not Found
Server: nginx/1.16.0
Date: Tue, 09 Jun 2020 06:17:30 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
$ git clone http://gitlab.xity.dev/QuietJoon/testgoget
Cloning into 'testgoget'...
fatal: repository 'http://gitlab.xity.dev/QuietJoon/testgoget/' not found
$ git clone http://gitlab.xity.dev/QuietJoon/testgoget.git
Cloning into 'testgoget'...
fatal: repository 'http://gitlab.xity.dev/QuietJoon/testgoget.git/' not found

是的,这可能会起作用,因为 go get使用 git clone , 和 go get添加 .git协议(protocol)不安全时给定 url 的后缀。

其他解决方案

我可以通过使用 git clone 来解决这个问题它在 %GOPATH%/src手动。

然而,这不是我个人的项目,而是合作项目。
因此,我不能告诉每个人都这样做。 (因为我和大家都是 Golang 初学者)
我只想以常规的 Golang 方式使用它
* 仅适用于 import "gitlab.xity.dev/XXX/YYY" . (import "gitlab.xity.dev/XXX/YYY.git" 对 Golang 来说似乎也是不好的方式),
* 不是手动 git 重定向配置和 go get -insecure独自

问题

1. 有什么可以帮忙的go get访问 https而不是 http ?

我调查了许多帮助,例如 this 等问答。 ,并添加全局 git 配置,如
[url "https://gitlab.xity.dev"]
        insteadOf = http://gitlab.xity.dev

但这无济于事go get正确地没有 -insecure旗帜。
我了解 go get从来没有意识到有这样的 git 配置。

那么,我该如何帮助go get更喜欢尝试https而不是 http ?

2. 为什么go get喜欢访问http而不是 https即使它说它不喜欢访问不安全的协议(protocol)?

go get提示不安全的协议(protocol),为什么它不尝试访问 https起初而不是 http ?
我不确定这是 go get 的错误或不。

3. 为什么go get去掉后缀.git即使我明确给出?

当我给 .git后缀明确,go get删除给定的后缀并说它无法正确找到存储库。
$ go get -v gitlab.xity.dev/QuietJoon/testgoget.git
# cd .; git ls-remote https://gitlab.xity.dev/QuietJoon/testgoget
fatal: repository 'https://gitlab.xity.dev/QuietJoon/testgoget/' not found

但是你可以看到go get不需要显式 .git当它尝试不安全的协议(protocol)时
$ go get -v gitlab.xity.dev/QuietJoon/testgoget
get "gitlab.xity.dev/QuietJoon/testgoget": found meta tag get.metaImport{Prefix:"gitlab.xity.dev/QuietJoon/testgoget", VCS:"git", RepoRoot:"http://gitlab.xity.dev/QuietJoon/testgoget.git"} at //gitlab.xity.dev/QuietJoon/testgoget?go-get=1
package gitlab.xity.dev/QuietJoon/testgoget: cannot download, http://gitlab.xity.dev/QuietJoon/testgoget.git uses insecure protocol

有什么区别?

条件与环境

我试过这个
* go 版本 go1.14.4 windows/amd64
* 去版本 go1.14.3 darwin/amd64
* 去版本 go1.12.7 linux/amd64
* 去版本 go1.14.4 linux/amd64

我设置了GOPRIVATE对于 gitlab.xity.dev
go env
...
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=gitlab.xity.dev
set GONOSUMDB=gitlab.xity.dev
set GOOS=windows
set GOPATH=t:\go
set GOPRIVATE=gitlab.xity.dev
set GOPROXY=https://proxy.golang.org,direct
set GOSUMDB=sum.golang.org
...

我认为 GOPRIVATE在这种情况下尚未使用。

最佳答案

问题出在你的 git 服务器上。 Go get 将获取所需的模块,并尝试使用 https 来感染它。如果你试试:curl https://gitlab.xity.dev/QuietJoon/testgoget.git .你会得到一个 302:<html><body>You are being <a href="http://gitlab.xity.dev/QuietJoon/testgoget">redirected</a>.</body></html>
如果你不能用 go get insecure 改变你的服务器 gitconfig 是你最好的选择。

关于git - 为什么 `go get` 尝试访问 http 而不是 https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62276763/

相关文章:

dictionary - 从标准输入中读取 map key

go - 使用mapstructure处理接口(interface)数据

git - VS 2017 团队资源管理器 - 没有可用的存储库

git - 如何让 Git 忘记已跟踪但现在位于 .gitignore 中的文件?

Git:将分支 merge 到master,或将master merge 到分支

go - YAML MapSlice 在编码或解码时保留序列

go - 选择默认 channel 竞争条件

design-patterns - 可能有数百个小类的工厂模式的替代方案

git - 如何为特定应用程序运行 rake?

git - 为 Dokku 使用远程 git 存储库