go get获取gitlab私有仓库的代码

go get获取gitlab私有仓库的代码

目录

[toc]

1、Gitlab的搭建

  在上一篇文章中,已经介绍了如何搭建Gitlab Https服务<Nginx设置Https反向代理,指向Docker Gitlab11.3.9 Https服务>

2、如何通过go get,获取Gitlab的代码

  在$GOROOT/src/cmd/go/internal/get/vcs.go的源码中可以看到,大概在997行左右 ,go get还是使用GIT获取代码的。

// vcsPaths defines the meaning of import paths referring to
// commonly-used VCS hosting sites (github.com/user/dir)
// and import paths referring to a fully-qualified importPath
// containing a VCS type (foo.com/repo.git/dir)
var vcsPaths = []*vcsPath{
    // Github
    {
        prefix: "github.com/",
        re:     `^(?P<root>github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[\p{L}0-9_.\-]+)*$`,
        vcs:    "git",
        repo:   "https://{root}",
        check:  noVCSSuffix,
    },

    // Bitbucket
    {
        prefix: "bitbucket.org/",
        re:     `^(?P<root>bitbucket\.org/(?P<bitname>[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`,
        repo:   "https://{root}",
        check:  bitbucketVCS,
    },

    // IBM DevOps Services (JazzHub)
    {
        prefix: "hub.jazz.net/git/",
        re:     `^(?P<root>hub\.jazz\.net/git/[a-z0-9]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`,
        vcs:    "git",
        repo:   "https://{root}",
        check:  noVCSSuffix,
    },

    // Git at Apache
    {
        prefix: "git.apache.org/",
        re:     `^(?P<root>git\.apache\.org/[a-z0-9_.\-]+\.git)(/[A-Za-z0-9_.\-]+)*$`,
        vcs:    "git",
        repo:   "https://{root}",
    },

    // Git at OpenStack
    {
        prefix: "git.openstack.org/",
        re:     `^(?P<root>git\.openstack\.org/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(\.git)?(/[A-Za-z0-9_.\-]+)*$`,
        vcs:    "git",
        repo:   "https://{root}",
    },

    // chiselapp.com for fossil
    {
        prefix: "chiselapp.com/",
        re:     `^(?P<root>chiselapp\.com/user/[A-Za-z0-9]+/repository/[A-Za-z0-9_.\-]+)$`,
        vcs:    "fossil",
        repo:   "https://{root}",
    },

    // General syntax for any server.
    // Must be last.
    {
        re:   `^(?P<root>(?P<repo>([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?(/~?[A-Za-z0-9_.\-]+)+?)\.(?P<vcs>bzr|fossil|git|hg|svn))(/~?[A-Za-z0-9_.\-]+)*$`,
        ping: true,
    },
}

  如果是 http 服务,可以通过使用-insecure选项获取:

> go get -insecure  mygitlab.com/user/repo

  但每次要输入密码比较麻烦,不过go提供了https->ssh的映射,执行如下命令即可,其中lion是你的帐号:

> git config --global url."git@gitlab-ce.mshk.top:".insteadOf "https://lion@gitlab-ce.mshk.top/"

  配置好的文件在~/.gitconfig文件中,执行cat ~/.gitconfig可以 看到以下内容:

[url "git@gitlab-ce.mshk.too:"]
    insteadOf = https://lion@gitlab-ce.mshk.top/

  获取后的效果如下:
mshk.top
  

常见问题

1、遇到 terminal prompts disabled 的提示

可以按以下命令操作,然后重新登录https需要使用的用户名和密码,就可以完成正常的项目获取

➜ ~/_golang >env GIT_TERMINAL_PROMPT=1 go get gitlab-ce.xxx.xxx/Micro-Service/gomicro
Username for 'https://gitlab-ce.xxx.xxx': lion
Password for 'https://lion@gitlab-ce.xxx.xxx':
package gitlab-ce.xxx.xxx/Micro-Service/gomicro: no Go files in ~/_golang/src/gitlab-ce.xxx.xxx/Micro-Service/gomicro

博文作者:迦壹
博客地址:go get获取gitlab私有仓库的代码
转载声明:可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明,谢谢合作!


发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注