首页 文章

在个人git repo上使用“go get”

提问于
浏览
11

我在个人VPS上托管我的git存储库,我有一个我想要“获得”的软件包 . 我试图通过发出“go help importpath”找到帮助文档,但没有运气 . 无论我做什么,我都会收到以下错误:

package example.com/user/package: unrecognized import path "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向http://example.com/user/package?go-get=1发出请求,则返回的页面包含META标记 . 如果go然后向http://example.com/?go-get=1发出后续请求,则返回的页面也包含完全相同的META标记 .

是否需要在服务器上进行任何特殊配置?我不这么认为,因为go会通过http请求访问存储库 .

我的智慧结束了 . 您将提供的任何帮助将不胜感激 .

3 回答

  • 18

    这是我为nitx配置的元标记,以便为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;
      }
    

    这当然假设您正在使用git而不是ssh . 如果您使用https,则相应地重写网址 .

  • 1

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

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

    # http://stackoverflow.com/questions/26347516/using-go-get-on-a-personal-git-repo/26348986#26348986
        location ~ "(/[^/]+)(/.*)?" {
            if ($arg_go-get = "1") {
                    echo '<html><head><meta name="go-import" content="myserver.example.com$1 git https://myserver.example.com$1"/><meta name="go-source" content="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}" /></head></html>';
            }
            try_files $uri @gerrit;
        }
    
        location / {
            try_files $uri @gerrit;
        }
    
  • 2

    我刚刚为自己做了这个 - 类似的情况:一些小的回购和一个大多数私人网站 . 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 指令和out-of-tree echo 模块,并且可以轻松地调整每个存储库的元内容 .

    如果这些奇怪的文件名有误,你可以限制范围:.go文件中的 import "example.com/code/project-name" 和nginx中的 location /code { try_files ... } .

相关问题