如何将 Go 模块替换为 fork @ master 的主要版本

Dan*_*tte 7 git fork replace go go-modules

我无法通过使用具有 / 主要版本映射的项目的 go.mod 来通过任何固定分支映射分叉/v2

我有以下 go.mod:

go 1.18

require (
    github.com/versent/saml2aws/v2 v2.35.0
)
Run Code Online (Sandbox Code Playgroud)

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

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

我正在使用以下指令,但没有一个有效:

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
Run Code Online (Sandbox Code Playgroud)

我也尝试修改我的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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我在这里很迷路。

Dan*_*tte 9

经过大量挖掘后,我发现以下步骤似乎有效:

  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
Run Code Online (Sandbox Code Playgroud)

请注意,如果您仅单独工作,则可以使用替换指令来映射驱动器上的本地文件夹。但这往往不适用于队友,因为他们在拉取代码时还必须检查相同的精确分叉路径。这是一个例子:

module <yourname>

go 1.18

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

replace github.com/versent/saml2aws/v2 => /Users/dmarcotte/git/saml2aws/
Run Code Online (Sandbox Code Playgroud)

  • 我在 go get 上收到错误:“版本无效:版本“my/branch”无效:不允许的版本字符串” (3认同)