How can I manage Go dependencies while checking the vendor directory into version control?

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

How can I manage Go dependencies while checking the vendor directory into version control?

问题

上下文

我编写了一个 Go 库,并希望锁定和打包第三方依赖项。这样,对这些第三方依赖项的任何不兼容更改都不会破坏其他用户使用我的库。有关打包的更多背景信息,请参阅Go 1.5 Vendoring Experiment的原始提案。

我正在使用 Glide 来管理依赖项并锁定特定版本。由于该项目是一个供其他人使用的库,我希望将 vendor 文件夹检入版本控制。这样,库的用户就不需要安装 Glide 来使用它。他们只需要设置环境变量 GO15VENDOREXPERIMENT=1

我以前使用过 Glide,并且对它非常熟悉。然而,我以前从未尝试过将 vendor 文件夹提交到版本控制中。这就是为什么我突然遇到问题的原因。我不认为这是 Glide 需要修复的问题,否则我会在那里提出一个问题。实际上,我认为这是 git 的问题。

问题

我正在使用 git 版本 2.5.4。当我运行 glide install 时,所有的依赖项都被克隆并存储在 vendor 文件夹中。当我尝试将 vendor 文件夹添加到 git 中时,它会混淆地尝试为它们创建子模块。(我认为这与每个依赖项都是一个克隆的仓库并且仍然在其中有一个 .git 文件有关)。这不是我想要的行为,我对 git 默认这样做感到惊讶。实际上,我花了一些时间才弄清楚实际发生了什么,以及为什么依赖项没有被正确添加。

Git 子模块很令人困惑,并且会破坏很多工具。我只想按原样将 vendored 项目添加到版本控制中。我希望所有的源代码都在那里,不会破坏任何其他工具,并且按照我希望的方式工作。

问题

有没有办法在 git 中关闭这种默认行为?理想情况下,它可以基于每个项目进行设置。我找到的唯一相关的 .gitconfig 的选项 似乎与在 git diff 中显示子模块或使用 git fetchpullclone 递归地获取子模块有关。

如果没有,有没有办法一次性添加 vendor 文件夹中的文件和文件夹而不使用子模块?我希望有类似 git add --no-submodules vendor 的东西,但我找不到类似的内容。

我意识到我可以简单地删除每个依赖项中的 .git 文件,但这个解决方案不是理想的,原因有很多。主要是,我或其他贡献者很容易忘记删除 .git 文件,结果依赖项将无法正确检入。我们必须记住在更新或添加新的依赖项时这样做。

英文:

Context

I have written a Go library and would like to lock-in and vendor the third-party dependencies. That way, any backwards-incompatible changes to these third-party dependencies will not break my library for other users. See the original proposal for the Go 1.5 Vendoring Experiment for some additional background on how vendoring works.

I am using Glide to manage dependencies and lock-in specific versions. Since the project is a library intended for other people to use, I would like to check the vendor folder into version control. That way, users of the library do not have install Glide in order to use it. All they have to do is set the environment variable GO15VENDOREXPERIMENT=1.

I have used Glide in the past and I'm very comfortable with it. However, I have never tried to submit the vendor folder to version control before. That's why I am suddenly running into problems. I don't believe this is a problem that Glide needs to fix, otherwise I would open an issue there. Really, this strikes me as a problem with git.

Problem

I'm using git version 2.5.4. When I run glide install, all the dependencies are cloned and stored in the vendor folder. When I try to add the vendor folder to git, it confusingly tries to create submodules for them. (I believe it has to do with the fact that each dependency is a cloned repo and still has a .git file in it). This is not the behavior that I want, and I was surprised that git does this by default. In fact, it took me a while to figure out what was actually happening and why the dependencies weren't being added correctly.

Git submodules are confusing and break a lot of tools. I just want to add the vendored projects to version control as they are. I want all the source code to be there, as is, so it won't mess up any other tools and will work the way I want it to.

Question

Is there a way to turn off this default behavior in git? Ideally it could be on a per-project basis. The only relevant options for .gitconfig I could find appear to do with displaying submodules in git diff or fetching submodules recursively with git fetch, pull, or clone.

If there's not, is there a way to do a one-off add the files and folders in the vendor folder without using submodules? I'm hoping for something like git add --no-submodules vendor but I could not find anything like this.

I realize that I could simply remove the .git file in each dependency, but that solution is not ideal for a number of reasons. Chiefly, I or another contributor could easily forget to remove the .git file and as a result the dependency would not be checked in correctly. We would have to remember to do this anytime we update or add a new dependency.

答案1

得分: 1

请注意,这个问题的原始标题是“如何阻止git默认使用子模块?”。我已经更新了标题,因为我提出的解决方案并不涉及这样做。据我所知,当你添加的依赖项包含一个.git目录时,没有办法阻止git使用子模块。

相反,我决定简单地让git将依赖项添加为子模块。子模块确实令人困惑,甚至有自己独特的一套命令。但我发现这并不重要。库的用户永远不必直接与子模块交互,因为go get和Go vendor实验都可以正常工作。此外,glide getglide install命令也仍然有效。

因此,总结起来,我决定继续使用子模块,但我从不直接使用子模块命令。

你可能还对查看Zoom的0.14.1版本的发布说明感兴趣,我在其中实现了这个更改并提供了一些额外的上下文。Glide的问题#112还提供了一些关于这个问题的更多信息。

更新:我说得太早了。看起来,以我使用子模块的方式(即使用默认的git行为)会导致在从头开始安装时go get出现问题。在我找出如何使其工作之前,我将取消选择此作为所选答案。

英文:

Note that the original title of this question was "How can I prevent git from using submodules by default?". I have updated the title because the solution I came up with does not involve doing that. As far as I know, there is no way to prevent git from using submodules when the dependencies you add contain a .git directory.

Instead, I've decided to simply let git add the dependencies as submodules. Submodules are admittedly confusing and even come with their own distinct set of commands. What I've discovered is that it doesn't matter. Users of the library will never have to interact with submodules directly because go get and the Go vendor experiment will work fine. Additionally, the glide get and glide install commands will also still work.

So in conclusion, I've decided to stick with submodules, but I don't ever use the submodule commands directly.

You might also be interested in seeing the release notes for version 0.14.1 of Zoom, where I implemented this change and provided some additional context. Glide issue #112 also provides some more information on the problem.

Update: I spoke too soon. It appears that using submodules the way I did (which is just using the default git behavior) causes problems with go get when installing from scratch. I'm deselecting this as the chosen answer until I can figure out how to make it work.

huangapple
  • 本文由 发表于 2015年12月15日 11:02:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/34280436.html
匿名

发表评论

匿名网友

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

确定