现在项目开发有很多私有仓库,直接git clone的方式使用,不是怎么方便。
查询go源码发现go get支持的协议除了https还支持git+ssh, bzr+ssh, svn+ssh, ssh
$GOSRC/cmd/go/internal/get/vsc.go
1var defaultSecureScheme = map[string]bool{
2 "https": true,
3 "git+ssh": true,
4 "bzr+ssh": true,
5 "svn+ssh": true,
6 "ssh": true,
7}直接在go get gitlab.com/****/****时,在后面加上.git, go会自动使用git/ssh的方式拉取git仓库.
注意: 正常的拉取方式,会生成GOPATH/git.gitlab.com/****/****目录接口, 使用.git方式拉取会生成GOPATH/gitlab.com/****/****.git的目录接口
sum校验,我们先把sum校验去除掉配置环境变量使拉取代码不走代理与sum校验
1export GOPRIVATE="gitlab.com"这个配置后, 拉取仓库,可以发现gitlab.com/user***/repo, 这种私有仓库我们能正常的拉取, 但是类似gitlab.com/gourp1/gourp2/repo不能正常拉取,
使用go get -v gitlab.com/gourp1/gourp2/repo后能发现, go认为仓库的真实地址是gitlab.com/gourp1/gourp2,并不是gitlab.com/gourp1/gourp2/repo
这个问题我们通过查看源码依旧能发现 $GOSRC/cmd/go/internal/get/vsc.go
1var vcsPaths = []*vcsPath{
2 // Github
3 {
4 prefix: "github.com/",
5 regexp: lazyregexp.New(`^(?P<root>github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[\p{L}0-9_.\-]+)*$`),
6 vcs: "git",
7 repo: "https://{root}",
8 check: noVCSSuffix,
9 },
10
11 // Bitbucket
12 {
13 prefix: "bitbucket.org/",
14 regexp: lazyregexp.New(`^(?P<root>bitbucket\.org/(?P<bitname>[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`),
15 repo: "https://{root}",
16 check: bitbucketVCS,
17 },
18 // .....
19 {
20 regexp: lazyregexp.New(`(?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_.\-]+)*$`),
21 schemelessRepo: true,
22 },
23}配置~/.netrc(window中配置~/_netrc)完成gitlab授权,获取真实的git路径
1machine gitlab.com login 账号 password 密码或者访问令牌使用访问令牌请勾选api的权限
git拉取https替换 ssh我们知道go get默认会使用https的方式拉取代码,由于git-remote-https走的验证是用户名,密码, 不怎么方便,我们来通过更改git的全局配置来使用ssh的方式拉取。 下面是配置https转换为ssh的命令
1git config --global url."git@gitlab.com:".insteadOf https://gitlab.com/