I'm trying to simulate remote repository for Go dependency as local git repo. I'm aware of go mod edit -replace
etc., but for my use case I need Go command to fetch dependency from git repo locally.
To simulate it with minimal reproducible example, I created bare git repo:
# in /tmp/git/repo1
git init --bare
Then cloned the repo, created go project, and pushed it back:
# 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
Just in case, checked bare repo updated successfully:
# in /tmp/git/repo1
git cat-file -p $(cat refs/heads/master) | grep init
# init: go mod init localhost
Then I created "client" go project, updated git config and tried to fetch it using 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
But got this error:
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
Also, I tried to update GOPRIVATE
env and set git config allow=always for file
protocol:
export GOPRIVATE="git.localhost/*"
git config --global protocol.file.allow always
And checked git can clone this repo by using:
git clone https://git.localhost/repo1
# successfully cloned from bare repo
Is it possible to get dependency from local git bare repo using go get
?
Update:
Added GOPROXY
env, so now my go env
has:
GOPRIVATE="git.localhost/*"
GOPROXY="direct"
and git config --global
has:
url.file:///tmp/git/.insteadof=https://git.localhost/
protocol.file.allow=always
Update2:
Verbose output for go get
command:
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