简体   繁体   English

Go mod replace 和 github /vN 存储库的分支

[英]Go mod replace and github forks of /vN repositories

I've just sent a pull request to http://periph.io/x/devices/v3;我刚刚向http://periph.io/x/devices/v3发送了一个拉取请求 it's already been merged, but something about the process was suboptimal:它已经被合并了,但是关于这个过程的一些事情是次优的:

First I cloned the repository (which is actually http://github.com/periph/devices; the module is named periph.io/x/devices/v3 though), so my fork is automatically called http://github.com/lutzky/devices .首先我克隆了存储库(实际上是http://github.com/periph/devices;该模块名为periph.io/x/devices/v3 ),所以我的 fork 自动称为http://github.com /lutzky/设备 I wanted to test a separate piece of code I'm working on with the modified library, call it testclient ;我想测试一段我正在使用修改后的库的单独代码,称之为testclient adding this to testclient/go.mod works:将此添加到testclient/go.mod作品:

replace periph.io/x/devices/v3 v3.6.9 => ../devices

However, this should also work (and would be useful if the pull request took longer to be accepted), and doesn't:但是,这也应该有效(如果拉取请求需要更长的时间才能被接受,这将很有用),并且不会:

replace periph.io/x/devices/v3 v3.6.9 => github.com/lutzky/devices main

That gives this error:这给出了这个错误:

testclient/go.mod:15: replace github.com/lutzky/devices:
  version "v0.0.0-20210508194004-cae0146d8900" invalid:
  go.mod has post-v0 module path "periph.io/x/devices/v3" at revision cae0146d8900

On a hunch, I figured I'd create a tag v3.6.9-newfeature and push that to my fork, and point the replace command at that.凭直觉,我想我会创建一个标签v3.6.9-newfeature并将其推送到我的 fork,然后将replace命令指向那里。 That doesn't work either:这也不起作用:

testclient/go.mod:15: replace github.com/lutzky/devices:
version "v3.6.9-newfeature" invalid:
module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v3

So I can't use a v0 tag because go.mod says it's v3 , but I can't use a v3 tag because the URL doesn't have /v3 .所以我不能使用 v0 标签,因为go.mod说它是v3 ,但我不能使用 v3 标签,因为 URL 没有/v3 I don't think I should modify go.mod in my forked repository (for one thing, that'd make the pull request silly).认为我不应该在我的分叉存储库中修改go.mod (一方面,这会使拉取请求变得愚蠢)。 What's the intended way to go about this? go 的预期方法是什么?

Here's the fix that you are looking for, I tested the following go.mod file and it works,这是您正在寻找的修复程序,我测试了以下go.mod文件,它可以工作,

module test

go 1.14

replace periph.io/x/devices/v3 => github.com/lutzky/devices/v3 v3.6.9-newfeature

require (
    periph.io/x/devices/v3 v3.6.9 // indirect
)

I first installed periph.io/x/devices/v3 ,我首先安装了periph.io/x/devices/v3
go get periph.io/x/devices/v3

Then I inserted the replace in go.mod ,然后我在go.mod中插入了替换,
replace periph.io/x/devices/v3 => github.com/lutzky/devices/v3 main
Format: replace <original_module> => <forked_repo> <branch>格式: replace <original_module> => <forked_repo> <branch>

Then I ran just go get .然后我只跑了go get After that, the main in the replace instruction got swapped with v3.6.9-newfeature which is the latest tag on the forked repo's main branch, thus giving you the content that you see above.之后,replace 指令中的main被替换为v3.6.9-newfeature ,这是 fork repo main分支上的最新标签,从而为您提供了您在上面看到的内容。

Thanks to ramaraja-ramanujan@'s answer and a bit more debugging, I found the answer.感谢 ramaraja-ramanujan@ 的回答和更多的调试,我找到了答案。

TL;DR - the correct answer is: TL;DR - 正确答案是:

replace periph.io/x/devices/v3 v3.6.9 => github.com/lutzky/devices/v3 main
                                         // this part was missing ^^^

This will work immediately when building, and go mod tidy (or go get ) will change main to a specific tag.这将在构建时立即起作用,并且go mod tidy (或go get )会将main更改为特定标签。 There are a couple of interesting side-notes to this:对此有几个有趣的旁注:

The go proxy go 代理

The v3.6.9-newfeature tag was my own; v3.6.9-newfeature标签是我自己的; I had created it as part of trying to get this to work.我创建它是为了让它发挥作用。 I've deleted it, and yet go mod tidy kept replacing main with v3.6.9-newfeature - no matter how many caches I cleared!我已经删除了它,但是go mod tidy不断用v3.6.9-newfeature替换main - 无论我清除了多少缓存! Running strace it turned out that go was contacting http://proxy.golang.org .运行strace发现go正在联系http://proxy.golang.org To disable this, I ran GOPROXY=direct go get .要禁用此功能,我运行了GOPROXY=direct go get This replaced the line with the pseudo version v3.6.10-0.20210508194004-cae0146d8900 .这用伪版本v3.6.10-0.20210508194004-cae0146d8900替换了该行。 Note that this matches the current main commit in my fork, and neither my fork nor the original repo have v3.6.10 - it appears to have started with the existing v3.6.9 and incremented it.请注意,这与我的 fork 中的当前main提交匹配,并且我的 fork 和原始 repo 都没有v3.6.10 - 它似乎从现有的v3.6.9开始并增加了它。

The /v3 is not a subdirectory /v3不是子目录

Indeed, the source repo ( https://github.com/periph/devices ) does not have a v3 subdirectory.实际上,源代码库 ( https://github.com/periph/devices ) 没有v3子目录。 I thought what was going on is that the periph.io domain is doing some trickery - indeed going to http://periph.io/x/devices/v3 with a browser does work.认为发生的事情是periph.io域正在做一些诡计 - 确实使用浏览器访问http://periph.io/x/devices/v3确实有效。 However, it would appear that Go's resolution takes the final /v3 in the module path not to indicate, in this case, a subdirectory (but indeed a restriction on tag names).但是,Go 的解析似乎采用模块路径中的最终/v3来指示,在这种情况下,不指示子目录(但实际上是对标签名称的限制)。 This is apparently allowed, and is discussed in https://golang.org/ref/mod , but it isn't entirely straightforward.这显然是允许的,并在https://golang.org/ref/mod中进行了讨论,但这并不完全简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM