我正在寻找获得$ 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不太好,有没有其他方法?


当前回答

我偶然发现了.netrc,并发现它与此相关。

创建一个包含以下内容的文件~/.netrc:

machine github.com
    login <github username>
    password <github password or Personal access tokens >

完成了!

此外,对于最新的GO版本,您可能需要将其添加到环境变量GOPRIVATE=github.com (我已经把它添加到我的。zshrc)

netrc还使我的开发环境设置更好,因为我个人的github访问HTTPS现在已配置为跨机器使用(就像我的SSH配置)。

生成GitHub个人访问令牌:https://github.com/settings/tokens

请参阅这个答案,了解它在Windows上与Git的具体使用

参考:netrc手册页


如果您希望坚持使用SSH身份验证,那么可以屏蔽请求以强制使用SSH

git配置——全局url."git@github.com:". insteadof "https://github.com/"


更多设置git访问的方法:https://gist.github.com/technoweenie/1072829#gistcomment-2979908

其他回答

在尝试了多种解决方案后,我的问题仍然存在。设置~/.netrc和SSH配置文件后的最终解决方案是在~/.bash_profile中添加以下行

出口GOPRIVATE = " github.com/(组织)”

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

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

在设置GOPRIVATE和git配置后…

人们在获取私有资源时仍然会遇到这样的问题:

https fetch: Get "https://private/user/repo?go-get=1": EOF

他们不能使用私人回购没有。git扩展名。

原因是go工具不知道这个repo的VCS协议,git或svn或其他任何协议,不像github.com或golang.org,它们是硬编码到go的源代码中。

然后go工具将在获取你的私有repo之前执行一个https查询:

https://private/user/repo?go-get=1

如果你的私人回购不支持https请求,请使用replace直接告诉它:

require private/user/repo v1.0.0

...

replace private/user/repo => private.server/user/repo.git v1.0.0

https://golang.org/cmd/go/#hdr-Remote_import_paths

这是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。

在这里生成一个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/"

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