git 如何将 go.mod 中的 Go 模块依赖指向 repo 中的最新提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53682247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:21:16  来源:igfitidea点击:

How to point Go module dependency in go.mod to a latest commit in a repo?

gitgomodule

提问by dimus

Starting with v1.11 Go added support for modules. Commands

从 v1.11 Go 开始增加了对模块的支持。命令

go mod init <package name>
go build

would generate go.modand go.sumfiles that contain all found versions for the package dependencies.

将生成go.modgo.sum包含找到的所有版本的软件包的依赖文件。

If a module does not have any releases, the latest commit of that module is used. If a module does have releases, the latest one is picked as a dependency.

如果模块没有任何版本,则使用该模块的最新提交。如果模块确实有版本,则选择最新的作为依赖项。

However sometimes I would need functionality that is not in a published release yet, but from a commit made after that release. How do I set go.modto point not to a release of a module, but to a specific commit in the module's repository?

但是,有时我需要的功能尚未发布在已发布的版本中,而是来自该版本之后的提交。如何设置go.mod不指向模块的发布,而是指向模块存储库中的特定提交?

It looks like I can do it by hand in go.mod with

看起来我可以在 go.mod 中手动完成

module /my/module

require (
...
github.com/someone/some_module v0.0.0-20181121201909-af044c0995fe
...
)

where v0.0.0does not correspond to the last published release tag, 20181121201909would be a commit timestamp and af044c0995fewould be the commit hash? Should such information to be found and entered by hand, or there is a better way?

哪里v0.0.0不对应于上次发布的发布标签,20181121201909是提交时间戳,af044c0995fe还是提交哈希?应该手动查找和输入此类信息,还是有更好的方法?

回答by Everton

Just 'go get' at the commit hash you want:

只需在您想要的提交哈希上“去获取”:

go get github.com/someone/some_module@af044c0995fe

'go get' will correctly update the dependency files (go.mod, go.sum).

'go get' 将正确更新依赖文件(go.mod、go.sum)。

More information: https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies

更多信息:https: //github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies

回答by typical182

In addition the answerfrom Everton on using go get github.com/someone/some_module@af044c0995feto get a specific commit, you can also use branch names such as:

除了埃弗顿关于使用获取特定提交的答案之外go get github.com/someone/some_module@af044c0995fe,您还可以使用分支名称,例如:

  • go get github.com/someone/some_module@master
  • go get github.com/someone/some_module@dev_branch
  • go get github.com/someone/some_module@master
  • go get github.com/someone/some_module@dev_branch

Those examples get the latest commit on the corresponding branch.

这些示例获取相应分支上的最新提交。

It will still be recorded as a pseudo-versionin your go.modfile, such as v0.0.0-20171006230638-a6e239ea1c69. (This helps provide a simple total ordering across all versions based on standard semverordering).

它仍然会在您的文件中记录为伪版本go.mod,例如v0.0.0-20171006230638-a6e239ea1c69. (这有助于提供基于标准semver排序的所有版本的简单总排序)。

回答by Chaitanya Gadkari

I have been banging my head for some time that how it works for everyone and I am not able to run it. For me, I had to commit to master branch then only I was able to get it.

一段时间以来,我一直在思考它如何适用于每个人,但我无法运行它。对我来说,我必须提交到 master 分支然后才能够得到它。

For go get to work with specific branch, commit id or tag, you need to enable a flag for go module by running below command

要使用特定的分支、提交 ID 或标签,您需要通过运行以下命令为 go 模块启用标志

go env -w GO111MODULE=on

去 env -w GO111MODULE=on

after this we will be able to do

在此之后,我们将能够做到

go get repo@branchname
go get repo@tag
go get repo@commithash

回答by JFW

Also if you put the word latest in place of the tag in the go.mod file it will get changed to the latest tag the modules.

此外,如果您在 go.mod 文件中将单词 latest 替换为标签,它将被更改为模块的最新标签。

For example:

例如:

module /my/module

require (
...
github.com/someone/some_module latest
...
)

will become

会变成

module /my/module

require (
...
github.com/someone/some_module v2.0.39
...
)

after running go mod tidy

跑完后 go mod tidy