我正在寻找获得$ 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

其他回答

正确的方法是手动将存储库放在正确的位置。一旦有了存储库,您可以使用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

我第一次尝试

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

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

我试着

SSH -t git@github.com

它显示我的SSH是好的。

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

添加export GOPRIVATE=*

在设置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的,但这不应该改变这个过程:

$ git config --global url.git@github.com:.insteadOf https://github.com/
$ cat ~/.gitconfig
[url "git@github.com:"]
    insteadOf = https://github.com/
$ go get github.com/private/repo

为了让Go模块工作(使用Go 1.11或更新版本),你还需要设置GOPRIVATE变量,以避免使用公共服务器获取代码:

export GOPRIVATE=github.com/private/repo