将 Git 仓库中的 Git 仓库(由 go get 命令创建)转换为子模块。

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

Git repo inside git repo, created by go get - convert to submodule

问题

我有一个go / golang项目的完整源代码树,存储在一个git仓库中。这样我就可以轻松地将整个项目及其依赖项克隆到另一台计算机上。

当使用go get从github.com等地方获取代码时,go会将github项目克隆到我的源代码树中。

是否有一种简单/标准的方法将这个内部git仓库转换为子模块?类似于git submodule add <path>这样的操作?

还是我必须手动或自动地找到内部仓库的URL,并使用git submodule add <url> <path>

有没有不使用这种方法的理由?据我所见,优点是我可以完全控制源代码的版本管理,轻松克隆包括依赖项的完整项目,方便更新我依赖的第三方代码/使用go get获取的代码。

结论
谢谢Will C和VonC。最终我使用了带有govendor工具的供应商系统。我喜欢这个工具,因为它透明且简单易用,而且它不是必需的工具。此外,Go的供应商系统满足了我的主要目标-轻松地将我的完整项目及其依赖项克隆到另一台计算机上。

英文:

I have the complete src-tree of a go / golang project in a git repository. This so I can easily clone a complete project and its dependencies to a different computer.

When using go get from e.g., github.com, go clones the github project inside my src-tree.

Is there an easy / standard approach for converting this inner git repo into a submodule? Something similar to git submodule add &lt;path&gt;?

Or do I have to, manually or automatically, find the url to the inner repository and use git submodule add &lt;url&gt; &lt;path&gt;

Any arguments for NOT using this approach? As far as I can see, the advantages are that I have complete control of versioning of the sources, easy cloning of the complete project including dependencies, easy way to update third-part code I depend on / retrieved using go get.

Conclusion
Thank you Will C and VonC. I ended up using the vendor system with the tool govendor. I like the tool because of its transparancy and simplicity - it is never a required tool. Also, the Go vendoring system satisifies my main objective - to make it easy to clone my complete project and its dependencies to a different computer.

答案1

得分: 2

你描述的方法似乎会给维护工作带来更多麻烦。想象一下,每次进行更改时都要记得执行 git submodule update,然后还可能出现向后兼容性问题。

相反,我建议你看一下 Godep,它是一个依赖管理工具。此外,在 Go 1.5+ 中,他们已经在尝试使用 vendor 文件夹来管理依赖项,你可以在这里找到更多信息。

从高层次上看,Godep 在版本控制方面有一些细微差别,但它允许你通过 SHA 来有效地检出特定的提交。SHA 是根据你的 GOPATH 中实际依赖项所在的提交来确定的。这也有效地包括了你提到的优点,比如有一个集中的目录来存放依赖项及其依赖项,并且可以控制你想要使用的依赖项的版本。相比于在 git submodule 中管理它,这样做会少很多麻烦。

英文:

The approach you described seems to cause more headaches for maintenance purposes. Imagine whenever making a change and having to remember to do a git submodule update and then having backwards compatibility issues.

Instead, I would advise taking a look at Godep as a dependency tool. Also, in Go 1.5+, they are already experimenting with a vendor folder for managing dependencies which you can find more information about here.

From a high level, there are some nuances in versioning with Godep but it allows you to effectively check out at a specific commit via its SHA. The SHA is determined by the commit you're on in the actual dependency located in your GOPATH. This effectively also includes the advantages you mentioned such as having one centrally located directory with the dependencies and their dependencies and having the ability to control the version of the dependencies you'd want to use. It comes with much less headache of attempting to manage it in git submodules.

答案2

得分: 1

我完全同意Will C答案,但是以防万一你仍然想将你的github仓库作为子模块引用(在一个vendor子文件夹中),这里有一个我制作的脚本可以快速完成这个操作:

它将会:

  • vendor/src目录下使用go get获取github仓库(通过临时重置GOPATH到vendor)
  • 识别所有被go get导入的仓库(意味着你的github仓库以及它的依赖),使用go list -f命令
  • 将这些仓库作为主仓库的子模块添加到vendor子文件夹中
  • 清理(删除)刚刚用来获取仓库的vendor/src文件夹

请参考vendor.bat

使用方法:

cd /path/to/your/repo
vendor github.com/jroimartin/gocui

这将会创建:

 /path/to/your/repo/vendor/github.com/jroimartin/gocui
 /path/to/your/repo/vendor/github.com/<other/dependent/repos>
英文:

I fully agree with Will C's answer, but just in case you still want to reference your github repo as a submodule (in a vendor subfolder), here is a script I made to quickly do just that:

It will:

  • go get the github repo within vendor/src (by resetting temporarily GOPATH to vendor)
  • identify all the repos imported by go get (meaning your github repo and its dependencies), using go list -f
  • add those repos as submodule of your main repo, in a vendor subfolder
  • clean (rm) the vendor/src folder, which was just there to get the repos.

See vendor.bat.

Usage:

cd /path/to/your/repo
vendor github.com/jroimartin/gocui

That will create:

 /path/to/your/repo/vendor/github.com/jroimartin/gocui
 /path/to/your/repo/vendor/github.com/&lt;other/dependent/repos&gt;

huangapple
  • 本文由 发表于 2016年4月8日 20:08:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/36499180.html
匿名

发表评论

匿名网友

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

确定