How does "go get" get to know what version control system to use with remote repos?

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

How does "go get" get to know what version control system to use with remote repos?

问题

我是新手学习golang。我了解到go get作为一个包管理器,可以与各种版本控制系统的远程仓库进行通信。

那么,给定一个包的导入路径,go get是如何决定使用哪个版本控制系统的呢?我经常在请求中看到下面的查询字符串:

?go-get=1

所以这里似乎有一些协议/约定?

英文:

I am new to golang. I learned that go get serves as a package manager and contact remote repos with various version control systems.

So, given a package import path, how does go get decide which VCS to use? I always see below query string in the reqeust:

?go-get=1

So seems some protocol/convention here?

答案1

得分: 6

Go的一个好处是,所有东西都是用Go编写的,包括几乎所有的标准库和工具。

cmd/go/vcs.go中,我们可以看到是如何实现的,在cmd/go/alldocs.go中有相关文档。

具体来说,看一下repoRootForImportPath函数:

// repoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
func repoRootForImportPath(importPath string, security securityMode) (*repoRoot, error) {

这个函数的作用是:

  1. vcsPaths保存了一个静态的“已知主机”列表。对于一些主机(比如GitHub),这很容易,我们可以直接设置版本控制系统(VCS)。对于其他主机(比如BitBucket),使用一个回调函数来检查URL,以确定可以使用哪个代码仓库。

  2. 如果第一步失败,它会尝试查看路径中的“VCS扩展”,比如example.com/foo.gitexample.com/foo.git/dir

  3. 最后,go get会查找go-import的元标签,格式如下:

     <meta name="go-import" content="example.com/path git https://github.com/Example/repo.git">
    

还可以参考parseMetaGoImports()函数。Vanity Imports with Hugo是一个关于如何使用这个功能的好介绍。

没有真正的“自动检测”机制。所以如果你的代码仓库位于https://example.com/stuff,那么go get example.com/stuff不会起作用。你需要使用扩展或元标签。

添加go-get=1参数是为了让网站构建者知道这是一个来自go get的请求,这在某些情况下可能很有用。

英文:

One of the nice things about Go is that everything is in, well, Go How does "go get" get to know what version control system to use with remote repos? Including almost all of the standard library and tools.

In cmd/go/vcs.go we can see how this is done, and in cmd/go/alldocs.go it is documented.

Specifically, look at repoRootForImportPath:

// repoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
func repoRootForImportPath(importPath string, security securityMode) (*repoRoot, error) {

What this does is:

  1. vcsPaths holds a static lists of "known hosts". For some (e.g. GitHub) this is easy and we can just set the VCS. For others (e.g. BitBucket) a callback function is used which examines the URL to see which repo can be used.

  2. If that fails, it tries to look at a "VCS extension" in the path, such as example.com/foo.git or example.com/foo.git/dir.

  3. And finally go get will look for a go-import meta tag, which looks like:

     &lt;meta name=&quot;go-import&quot; content=&quot;example.com/path git https://github.com/Example/repo.git&quot;&gt;
    

Also see parseMetaGoImports(). Vanity Imports with Hugo is a nice introduction on how to use this.

There is no real "auto-detection" mechanism. So if you repo lives at https://example.com/stuff then go get example.com/stuff will not work. You need the extension or meta tags.

The go-get=1 parameter is added so that it's easy for website builders to see that this is a request from go get, which may be useful in some cases.

huangapple
  • 本文由 发表于 2016年11月28日 14:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/40838321.html
匿名

发表评论

匿名网友

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

确定