从本地裸git仓库获取'go get'项目

wgxvkvu9  于 2023-04-27  发布在  Go
关注(0)|答案(1)|浏览(156)

我尝试将Go依赖的远程仓库模拟为本地git仓库。我知道go mod edit -replace等,但对于我的用例,我需要Go命令从本地git仓库获取依赖。
为了用最少的可重复示例来模拟它,我创建了bare git repo:

# in /tmp/git/repo1
git init --bare

然后克隆repo,创建go项目,并将其推回:

# in /tmp/projects/
git clone file:///tmp/git/repo1
cd repo1
go mod init git.localhost/repo1
git add go.mod && git commit -m "init: go mod init localhost" && git push origin

以防万一,已检查的裸存储库已成功更新:

# in /tmp/git/repo1
git cat-file -p $(cat refs/heads/master) | grep init
# init: go mod init localhost

然后我创建了“client”go项目,更新了git配置,并尝试使用go get获取它:

# in /tmp/go-proj
go mod init example.com
git config --global url."file:///tmp/git/".insteadOf "https://git.localhost/"
go get git.localhost/repo1

但得到了这个错误:

go: unrecognized import path "git.localhost/repo1": https fetch: Get "https://git.localhost/repo1?go-get=1": dial tcp: lookup git.localhost on 127.0.0.1:53: no such host

另外,我尝试更新GOPRIVATE env并设置git config allow=always for file protocol:

export GOPRIVATE="git.localhost/*"
git config --global protocol.file.allow always

git可以通过使用以下命令克隆这个repo:

git clone https://git.localhost/repo1
# successfully cloned from bare repo

是否可以使用go get从本地git bare repo中获取依赖项?
更新:添加了GOPROXY env,所以现在我的go env有:

GOPRIVATE="git.localhost/*"
GOPROXY="direct"

git config --global有:

url.file:///tmp/git/.insteadof=https://git.localhost/
protocol.file.allow=always

更新2:
go get命令的详细输出:

GOFLAGS="-v -x" go get git.localhost/repo1
# get https://git.localhost/?go-get=1
# get https://git.localhost/repo1?go-get=1
# get https://git.localhost/?go-get=1: Get "https://git.localhost/?go-get=1": dial tcp: lookup git.localhost on 127.0.0.1:53: no such host
# get https://git.localhost/repo1?go-get=1: Get "https://git.localhost/repo1?go-get=1": dial tcp: lookup git.localhost on 127.0.0.1:53: no such host
go: unrecognized import path "git.localhost/repo1": https fetch: Get "https://git.localhost/repo1?go-get=1": dial tcp: lookup git.localhost on 127.0.0.1:53: no such host
xwbd5t1u

xwbd5t1u1#

为了防止出现这种问题,请首先使用以下命令测试go get命令:

export GOPROXY=direct

这应该允许go get从本地Git存储库中获取依赖项,而不会出现任何问题。
这应该在你的其他设置之外设置,比如你的export GOPRIVATE="git.localhost/*"和你的Git insteadOf指令。
OP在评论中补充说:
看起来go get试图通过http从依赖主机(我的例子是git.localhost)获取一些 meta数据,然后在go/pkg/mod/cache/vcs/{hash}中创建包git repo,然后使用git获取依赖代码,其中git应用isnteadOf替换。
因此,我将尝试创建简单的http服务器,以使用VCS,Prefix和RepoRoot URL响应http://git.localhost/?go-get=1 URL的 meta信息进行响应
这确实是在“How can I make go get work with a repo on a local server”中说明的。

相关问题