我正在寻找获得$ go与私人存储库工作的方法,经过许多谷歌尝试。

第一次尝试:

$ go get -v gitlab.com/secmask/awserver-go
Fetching https://gitlab.com/secmask/awserver-go?go-get=1
https fetch failed.
Fetching http://gitlab.com/secmask/awserver-go?go-get=1
Parsing meta tags from http://gitlab.com/secmask/awserver-go?go-get=1 (status code 200)
import "gitlab.com/secmask/awserver-go": parse http://gitlab.com/secmask/awserver-go?go-get=1: no go-import meta tags
package gitlab.com/secmask/awserver-go: unrecognized import path "gitlab.com/secmask/awserver-go

是的,它没有看到元标签,因为我不知道如何提供登录信息。

第二次尝试:

按照https://gist.github.com/shurcooL/6927554。将config添加到.gitconfig。

[url "ssh://git@gitlab.com/"]
    insteadOf = https://gitlab.com/
$ go get -v gitlab.com/secmask/awserver-go --> not work
$ go get -v gitlab.com/secmask/awserver-go.git --> work but I got src/gitlab.com/secmask/awserer-go.git

是的,它的工作,但与。git扩展与我的项目名称,我可以重命名为原来的,但每次做$ go get不太好,有没有其他方法?


当前回答

这是Go Get中的硬代码。不是Git的原因。修改Go Source。 原因: reportforimportdynamic将请求:https://....go-get

// RepoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
func RepoRootForImportPath(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
    rr, err := repoRootFromVCSPaths(importPath, security, vcsPaths)
    if err == errUnknownSite {
        rr, err = repoRootForImportDynamic(importPath, mod, security)
        if err != nil {
            err = importErrorf(importPath, "unrecognized import path %q: %v", importPath, err)
        }
    }
    if err != nil {
        rr1, err1 := repoRootFromVCSPaths(importPath, security, vcsPathsAfterDynamic)
        if err1 == nil {
            rr = rr1
            err = nil
        }
    }

所以添加gitlab域到vcsPaths就可以了。 下载go源代码:

vi ./src/cmd/go/internal/vcs/vcs.go    

查看下面的代码:

var vcsPaths = []*vcsPath{
    // GitHub
    {
        pathPrefix: "github.com",
        regexp:     lazyregexp.New(`^(?P<root>github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
        vcs:        "git",
        repo:       "https://{root}",
        check:      noVCSSuffix,
    },

添加代码如下,XXXX Is Your Domain:

    // GitLab
    {
        pathPrefix: "gitlab.xxxx.com",
        regexp:     lazyregexp.New(`^(?P<root>gitlab.xxxx\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
        vcs:        "git",
        repo:       "https://{root}",
        check:      noVCSSuffix,
    },

编译和替换go。

其他回答

对于独立/最终回购,作为快速修复,为什么不直接在go中命名模块呢?Mod作为一个包使用贵公司的域名…?

module go.yourcompany.tld/the_repo

Go.yourcompany.tld甚至不必作为有效(子)域名存在…

同样,在同一个地方。mod你可以使用替换块/行来使用之前以同样的方式克隆的私有回购(在$GOPATH/src/go.yourcompany.tld中克隆的各自文件夹中)(为什么我们必须在GitHub中依赖太多?)

Edit

不用说,私人回购通常应该是私人回购,通常是标准的git回购,对吧?有了这些,为什么不直接克隆git,然后进入克隆文件夹?

确保你删除了你之前的gitconfig,我也有同样的问题。

之前我执行了令牌过期的gitconfig,当你下次使用新令牌执行命令时,一定要删除之前的令牌。

我第一次尝试

[url "ssh://git@github.com/"]
    insteadOf = https://github.com/

但在我的本地车里,它不管用。

我试着

SSH -t git@github.com

它显示我的SSH是好的。

最后,我解决了这个问题,告诉go get考虑所有的私有和使用ssh而不是HTTPS。

添加export GOPRIVATE=*

正确的方法是手动将存储库放在正确的位置。一旦有了存储库,您可以使用go get -u来更新包,然后使用install来安装它。一个名为

github.com/secmask/awserver-go

进入

$GOPATH/src/github.com/secmask/awserver-go

您键入的命令是:

cd $GOPATH/src/github.com/secmask
git clone git@github.com:secmask/awserver-go.git

在这里生成一个github oauth令牌,并将您的github令牌作为环境变量导出:

export GITHUB_TOKEN=123

设置git配置为使用基本认证url:

git config --global url."https://$GITHUB_TOKEN:x-oauth-basic@github.com/".insteadOf "https://github.com/"

现在你可以去拿你的私人回购了。