How to replace a Go Module with a major version to a fork @ master

huangapple go评论72阅读模式
英文:

How to replace a Go Module with a major version to a fork @ master

问题

我在使用 go.mod 时遇到了问题,无法通过任何固定的分支映射到一个使用 /v2 或主要版本映射的项目的 fork。

我有以下的 go.mod 文件:

go 1.18

require (
	github.com/versent/saml2aws/v2 v2.35.0
)

请注意,该模块需要 /v2,否则它将获取 v2.17.0+incompatible

  1. 我在这里 fork 了该项目:https://github.com/marcottedan/saml2aws
  2. 我在这里创建了一个分支 add-aad-entropy-PhoneAppNotificationhttps://github.com/marcottedan/saml2aws/tree/add-aad-entropy-PhoneAppNotification
  3. 我尝试修改我的 fork 的 go.mod 文件的第一行,将 module github.com/versent/saml2aws/v2 改为 module github.com/marcottedan/saml2aws/v2

我尝试了以下指令,但都没有起作用:

这个命令下载了我 fork 的标签 2.35.0,尽管我要求它获取 master

dmarcotte@dmarcottes% go get -d github.com/marcottedan/saml2aws/v2@master
go: downloading github.com/marcottedan/saml2aws/v2 v2.35.0
go: github.com/marcottedan/saml2aws/v2@v2.35.0: parsing go.mod:
        module declares its path as: github.com/versent/saml2aws/v2
                but was required as: github.com/marcottedan/saml2aws/v2

我还尝试修改我的 go.mod 文件:

replace github.com/versent/saml2aws/v2 => github.com/marcottedan/saml2aws/v2 v2.35.0

-> 无法找到通过 /v2 模式来定位 master 的方法

如果我去掉 /v2,只使用 @master,它不会关心,而是获取 v1 中的最新标签(在 saml2aws 迁移到 go mod 之前,它被命名为 2.17.0+incompatible):

dmarcotte@dmarcottes % go get -d github.com/marcottedan/saml2aws@master   
go: github.com/marcottedan/saml2aws@master: github.com/marcottedan/saml2aws@v1.8.5-0.20220520001825-f05a14a2b3f2: invalid version: go.mod has post-v1 module path "github.com/marcottedan/saml2aws/v2" at revision f05a14a2b3f2

我在这里感到相当困惑。

英文:

I have trouble mapping a fork through any pinned branch with go.mod that use a project with the /v2 / major version mapping.

I have the following go.mod:

go 1.18

require (
	github.com/versent/saml2aws/v2 v2.35.0
)

Notice the module requires the /v2, otherwise it would get v2.17.0+incompatible.

  1. I forked the project here: https://github.com/marcottedan/saml2aws
  2. I created a branch add-aad-entropy-PhoneAppNotification here: https://github.com/marcottedan/saml2aws/tree/add-aad-entropy-PhoneAppNotification
  3. I tried to modify, or not, the first line of go.mod in my fork go mod from module github.com/versent/saml2aws/v2 to module github.com/marcottedan/saml2aws/v2

I'm using the following directives and none are working:

This downloads the tag 2.35.0 of my fork even though I ask it to get master

dmarcotte@dmarcottes% go get -d github.com/marcottedan/saml2aws/v2@master
go: downloading github.com/marcottedan/saml2aws/v2 v2.35.0
go: github.com/marcottedan/saml2aws/v2@v2.35.0: parsing go.mod:
        module declares its path as: github.com/versent/saml2aws/v2
                but was required as: github.com/marcottedan/saml2aws/v2

I also tried modifying my go.mod:

replace github.com/versent/saml2aws/v2 => github.com/marcottedan/saml2aws/v2 v2.35.0

-> Can't find a way to target master with the /v2 pattern

If I drop the /v2 and just go with @master, it doesn't care and get the latest tag in v1 (which was named 2.17.0+incompatible before saml2aws migrated to go mod)

dmarcotte@dmarcottes % go get -d github.com/marcottedan/saml2aws@master   
go: github.com/marcottedan/saml2aws@master: github.com/marcottedan/saml2aws@v1.8.5-0.20220520001825-f05a14a2b3f2: invalid version: go.mod has post-v1 module path "github.com/marcottedan/saml2aws/v2" at revision f05a14a2b3f2

I'm quite lost here.

答案1

得分: 8

经过一番搜索,我找到了一些看起来有效的步骤:

  1. 创建一个项目的分支
  2. go.mod文件的第一行改为你的新分支名称,提交并推送
  3. 在分支或主分支上提交和推送任何需要的更改
  4. 在原始项目中执行以下命令:go get -d -u github.com/marcottedan/saml2aws/v2@master,其中@version是你的分支名称
  5. 在你的go.mod文件中添加以下替换指令:replace github.com/versent/saml2aws/v2 v2.35.0 => github.com/marcottedan/saml2aws/v2 master
  6. 每次在你的分支上提交代码后,重复第4步

最后,你的go.mod文件应该如下所示:

module <yourname>

go 1.18

require (
	github.com/versent/saml2aws/v2 v2.35.0
)

replace github.com/versent/saml2aws/v2 v2.35.0 => github.com/marcottedan/saml2aws/v2 master

请注意,如果你是独自工作,你可以使用替换指令将本地文件夹映射到你的驱动器上。但是,这种方法在团队合作中可能不太适用,因为其他成员在拉取代码时必须也要检出相同的分支路径。以下是一个示例:

module <yourname>

go 1.18

require (
	github.com/versent/saml2aws/v2 v2.35.0
)

replace github.com/versent/saml2aws/v2 => /Users/dmarcotte/git/saml2aws/
英文:

After a lot of digging, here are the steps I found that seem to be working:

  1. Create a fork of any project
  2. Change the go.mod first line to your new fork name, commit & push
  3. Commit&Push any change you need, in a branch or in master
  4. In your original project, do the following command: go get -d -u github.com/marcottedan/saml2aws/v2@master where the @version is your branch name.
  5. In your go.mod, add the following replace directive: replace github.com/versent/saml2aws/v2 v2.35.0 =&gt; github.com/marcottedan/saml2aws/v2 master
  6. Every time you push a commit in your fork, repeat step 4.

At the end your go.mod should look like this:

module &lt;yourname&gt;

go 1.18

require (
	github.com/versent/saml2aws/v2 v2.35.0
)

replace github.com/versent/saml2aws/v2 v2.35.0 =&gt; github.com/marcottedan/saml2aws/v2 master

Note that if you're only working in solo, you can use the replace directive to map a local folder on your drive. But this tend to not work well with teammates since they must also have the same exact fork path checked out while pulling the code. Here's an example:

module &lt;yourname&gt;

go 1.18

require (
	github.com/versent/saml2aws/v2 v2.35.0
)

replace github.com/versent/saml2aws/v2 =&gt; /Users/dmarcotte/git/saml2aws/

huangapple
  • 本文由 发表于 2022年5月20日 08:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/72312460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定