在个人的git仓库上使用”go get”命令

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

Using "go get" on a personal git repo

问题

我在个人VPS上托管我的git仓库,并且有一个包我想要使其可以通过"go get"命令获取。我已经按照"go help importpath"中的帮助文档进行了设置,但是没有成功。无论我做什么,都会得到以下错误信息:

package example.com/user/package: 无法识别的导入路径"example.com/user/package"

我尝试了所有提到的META标签的组合,但结果都一样。

<meta name="go-import" content="example.com git http://example.com/user/package">

实际的git仓库可以通过http://example.com/user/package.git访问。我可以直接克隆它,但我希望使用go来正确地下载和安装它。

根据帮助文档,如果go向http://example.com/user/package?go-get=1发出请求,返回的页面应包含META标签。然后,如果go向http://example.com/?go-get=1发出后续请求,返回的页面也应包含完全相同的META标签。

服务器上需要进行特殊配置吗?我认为不需要,因为go将通过http请求访问仓库。

我已经束手无策了。如果您能提供任何帮助,我将非常感激。

英文:

I host my git repositories on a personal VPS and I have one package that I want to make "go get"-able. I have tried to set everything up per the help document found by issuing "go help importpath" with no luck. No matter what I do I get the following error:

package example.com/user/package: unrecognized import path &quot;example.com/user/package&quot;

I've tried every combination of the mentioned META tag with the same results.

&lt;meta name=&quot;go-import&quot; content=&quot;example.com git http://example.com/user/package&quot;&gt;

The actual git repository is accessible via http://example.com/user/package.git. I am able to clone it directly but I want go to download and install it properly.

Per the help document, if go makes a request to http://example.com/user/package?go-get=1 the page returned contains the META tag. If go then makes a subsequent request to http://example.com/?go-get=1 the page returned also contains the exact same META tag.

Does any special configuration need to be done on the server? I wouldn't think so since go would be accessing the repository via an http request.

I'm at my wits end. Any help you can provide would be greatly appreciated.

答案1

得分: 19

这是我配置nginx为gitlab服务器返回的元标记:
如果你请求 http://mygit.server/group/project?go-get=1
你会得到:

<meta content='mygit.server/group/project git git+ssh://git@mygit.server/group/project.git' name='go-import'>

它非常有效。

以下是实现此功能的nginx重写规则:

location ~ "(/[^/]+/[^/]+)(/.*)?" {
    if ($arg_go-get = "1") {
            echo '<html><head><meta name="go-import" content="my.domain.com$1 git git+ssh://git@my.domain.com$1"/></head></html>';
    }
    try_files $uri $uri/index.html $uri.html @gitlab;
  }

当然,这假设你正在使用SSH进行git操作。如果你使用HTTPS,请相应地重写URL。

英文:

This is the meta tag I've configured nginx to return for a gitlab server:
if you request http://mygit.server/group/project?go-get=1

you get:

&lt;meta content=&#39;mygit.server/group/project git git+ssh://git@mygit.server/group/project.git&#39; name=&#39;go-import&#39;&gt;

And it works like a charm.

Here is the nginx rewrite rule that does this:

location ~ &quot;(/[^/]+/[^/]+)(/.*)?&quot; {
    if ($arg_go-get = &quot;1&quot;) {
            echo &#39;&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;go-import&quot; content=&quot;my.domain.com$1 git git+ssh://git@my.domain.com$1&quot;/&gt;&lt;/head&gt;&lt;/html&gt;&#39;;
    }
    try_files $uri $uri/index.html $uri.html @gitlab;
  }

This of course assumes you're working with git over ssh. If you're using https rewrite the url accordingly.

答案2

得分: 2

我刚刚为自己完成了类似的情况 - 一些小的Go仓库和一个主要私有的网站。Nginx配置如下:

root /var/www;

location / {
    try_files $uri?$args $uri $uri/ @my-proxy;
}

然后运行以下命令:

$ echo '<html><head><meta name="go-import" content="example.com/project-name git git+ssh://example.com/~/code/magic/blah/project-name.git" /></head></html>' > /var/www/project-name?go-get=1
  • 查询字符串嵌入到文件名中,如果存在,Nginx会提供该文件。
  • 我们为每个项目创建一个具有适当(稍微奇怪)名称的文件。
  • 每个文件包含一个带有适当<meta />标签的最小HTML文档。

这样做的代价是每次访问位置块时会多出一个stat()调用,但是你可以避免可怕的Nginx if指令和不在树上的echo模块,并且可以轻松地针对每个仓库调整元数据内容。

如果你不喜欢这些奇怪的文件名,你可以限制范围:在你的.go文件中使用import "example.com/code/project-name",并在nginx中使用location /code { try_files ... }

英文:

I've just done this for myself - similar scenario: a few small go repos and a mostly-private website. Nginx configuration:

root /var/www;

location / {
    try_files $uri?$args $uri $uri/ @my-proxy;
}

I then run:

$ echo &#39;&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;go-import&quot; content=&quot;example.com/project-name git git+ssh://example.com/~/code/magic/blah/project-name.git&quot; /&gt;&lt;/head&gt;&lt;/html&gt;&#39; &gt; /var/www/project-name\?go-get\=1
  • The query-string is embedded into a filename, which nginx serves if it exists.
  • We create a file of the appropriate (slightly weird) name per project.
  • Each file contains a minimal HTML document with the appropriate &lt;meta /&gt; tag

The cost is an extra stat() call per hit to the location block, but you avoid the dreaded nginx if directive and the out-of-tree echo module, and can trivially tweak the meta content per-repository.

If those weird filenames bug you, you could restrict the scope: import &quot;example.com/code/project-name&quot; in your .go files, andlocation /code { try_files ... } in nginx.

答案3

得分: 2

只是为了扩展@Not_a_Golfer的答案,非常有帮助。

我在我的Gerrit安装中使用Gitiles来浏览源代码,所以现在godoc可以工作了(它将文档链接到正确的代码行):

# http://stackoverflow.com/questions/26347516/using-go-get-on-a-personal-git-repo/26348986#26348986
location ~ &quot;(/[^/]+)(/.*)?&quot; {
    if ($arg_go-get = &quot;1&quot;) {
        echo '&#39;&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;go-import&quot; content=&quot;myserver.example.com$1 git https://myserver.example.com$1&quot;/&gt;&lt;meta name=&quot;go-source&quot; content=&quot;myserver.example.com$1 https://myserver.example.com/plugins/gitiles$1 https://myserver.example.com/plugins/gitiles$1/+/master/{dir} https://myserver.example.com/plugins/gitiles$1/+/master/{dir}/{file}#{line}&quot; /&gt;&lt;/head&gt;&lt;/html&gt;&#39;';
    }
    try_files $uri @gerrit;
}

location / {
    try_files $uri @gerrit;
}
英文:

Just to expand on @Not_a_Golfer answer, which was very helpful.

I have that for my Gerrit installation using Gitiles for browsing source code, so now godoc works (It links documentation to the right line of code):

    # http://stackoverflow.com/questions/26347516/using-go-get-on-a-personal-git-repo/26348986#26348986
    location ~ &quot;(/[^/]+)(/.*)?&quot; {
        if ($arg_go-get = &quot;1&quot;) {
                echo &#39;&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;go-import&quot; content=&quot;myserver.example.com$1 git https://myserver.example.com$1&quot;/&gt;&lt;meta name=&quot;go-source&quot; content=&quot;myserver.example.com$1 https://myserver.example.com/plugins/gitiles$1 https://myserver.example.com/plugins/gitiles$1/+/master/{dir} https://myserver.example.com/plugins/gitiles$1/+/master/{dir}/{file}#{line}&quot; /&gt;&lt;/head&gt;&lt;/html&gt;&#39;;
        }
        try_files $uri @gerrit;
    }

    location / {
        try_files $uri @gerrit;
    }

huangapple
  • 本文由 发表于 2014年10月14日 03:31:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/26347516.html
匿名

发表评论

匿名网友

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

确定