简体   繁体   English

如何避免使用`go get`更新自己

[英]How to avoid updating myself with `go get`

I have a package structure that looks like this: 我有一个看起来像这样的包结构:

GOPATH
   src
      github.com/myname
         mymainproject
            something.go //imports things from github
            mysubproject
               main.go //imports "github.com/myname/mymainproject" + others

As part of m build I run, go get -u -t ./.. from the main directory. 作为我运行的m build的一部分,从主目录中go get -u -t ./.. This works great if building from master, but I have jenkins configured to build from any branch. 如果从master构建,这很有用,但是我将jenkins配置为从任何分支构建。

When building from another branch though, when it updates dependencies of mysubproject it updates the full working directory to master, which is very much not what I want. 从另一个分支构建时,当它更新mysubproject依赖关系时,它mysubproject完整的工作目录更新为master,这根本不是我想要的。

I only have two real ideas that might work: 我只有两个可能有用的真实想法:

  1. After I run go get , check out the original branch that I want to build. 在我运行go get ,查看我想要构建的原始分支。 I still worry that switching branches mid go-get could cause it to skip some dependencies that exist only in the branch, or fetch unnecessarily ones that exist only in master, so I'm not sure this is a good solution. 我仍然担心在go-get中切换分支可能会导致它跳过仅存在于分支中的某些依赖项,或者只获取仅存在于master中的不必要的依赖项,所以我不确定这是一个很好的解决方案。
  2. Tag the current commit on the branch with the magic go1 tag. 使用magic go1标记标记分支上的当前提交。 I believe this would have to be done on the remote as well, which makes it much less appealing. 我相信这也必须在遥控器上完成,这使它不那么吸引人了。

Is there any way to prevent go-get from touching my main repository? 有没有办法防止go-get触及我的主存储库?

While I don't recommend go get -u as part of a production build (like Google, I recommend vendoring your packages so you can reproduce old builds and manage package versioning on branches), it's not that hard to get what you want with a small script using list and some filtering. 虽然我不建议go get -u作为生产构建的一部分(比如Google,我建议您销售您的软件包,以便您可以重现旧版本并管理分支机构上的软件包版本控制),但要获得所需的内容并不难。小脚本使用list和一些过滤。 Something like this should work: 这样的事情应该有效:

go get -u -t `go list ./... | grep -v mysubproject`

That would exclude mysubproject . 这将排除mysubproject Or you could exclude mymainproject , or otherwise filter to get rid of whatever you don't want to update. 或者您可以排除mymainproject ,或以其他方式过滤以删除您不想更新的内容。

You should also look at godep to help you manage your dependencies and update them when you mean to. 您还应该查看godep来帮助您管理依赖项并在需要时更新它们。


UPDATE: You can take this to another level and explicitly list all the packages that you import: 更新:您可以将此更改为另一个级别并明确列出您导入的所有包:

go list -f '{{join .Imports "\n"}}' github.com/mymainproject/mysubproject | sort -u

This lists everything that mysubproject directly imports. 这列出了mysubproject直接导入的所有内容。 You can filter out mymainproject and then update that list (which won't include mysubproject or mymainproject". This includes system packages, too, but those won't update, so it's ok (though you could filter them if you wanted). 您可以过滤掉mymainproject,然后更新该列表(不包括mysubproject或mymainproject“。这也包括系统包,但是那些不会更新,所以没关系(尽管你可以根据需要过滤它们)。

Godep is currently adding a -r option that will rewrite imports, so you can vendor your dependencies into your tree, and import them as github.com/mymainproject/mysubproject/Godeps/_workspace/src/github.com/<package> . Godep目前正在添加一个将重写导入的-r选项,因此您可以将依赖项提供给树,并将它们导入为github.com/mymainproject/mysubproject/Godeps/_workspace/src/github.com/<package> This is on the rewrite branch. 这是在rewrite分支上。 The advantage of this approach is that it means your library uses the version you tested with rather than whatever version happens to be on the final consumer's machine. 这种方法的优点在于它意味着您的库使用您测试的版本,而不是最终消费者机器上的任何版本。 The downside is that the final consumer may wind up with multiple copies of the same package. 缺点是最终消费者最终可能会使用相同包装的多个副本。

You should always take times like these to ask yourself if you really need the dependency in your library. 您应该总是花些时间来问自己是否真的需要库中的依赖项。 Transitive dependencies have seemingly never-ending problems like these. 传递依赖性似乎永无止境的问题就像这些。 You can work around them, and you should for a dependency that is really important to you, but I've more than once picked up the two functions I actually needed and copied them into my package to avoid a dependency. 你可以解决它们,你应该找一个对你来说非常重要的依赖,但是我不止一次拿起我实际需要的两个函数并将它们复制到我的包中以避免依赖。 I'm not saying reusing code is bad; 我不是说重用代码很糟糕; just make sure the cost is worth it in each case. 在每种情况下确保成本都是值得的。

What appears to work for me is to not use go get -u as Rob suggests. 似乎对我有用的是不要像Rob建议的那样使用go get -u Instead I make sure I fully clean my gopath before a build., then I can simply do a go get -t ./... to fetch all dependencies not already on disk. 相反,我确保在构建之前完全清理了我的gopath。然后我可以简单地执行go get -t ./... -t。/ go get -t ./...来获取磁盘上尚未存在的所有依赖项。

This takes lokger to fetch all dependencies, but it works well. 这需要lokger来获取所有依赖项,但它运行良好。

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

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