kozmo
kozmo

Reputation: 4471

Right way to get dependencies from private repository

I have private bitbucket repo on http://localhost:7990 πŸ‘‰πŸ» clone link http://localhost:7990/scm/gom/bar.git

go.mod looks like:

module mod.org/bar

go 1.13

References available in a remote repository:

git ls-remote  http://localhost:7990/scm/gom/bar.git 

From http://localhost:7990/scm/gom/bar.git
d456de4f12785b26ac27ba08cffb76687d1287c8        HEAD
d456de4f12785b26ac27ba08cffb76687d1287c8        refs/heads/master
f948bd47a22c5fb9abed5bff468a10fc24f67483        refs/tags/v1.0.0

I changed .gitconfig to

[url "http://localhost:7990/scm/gom"]
      insteadOf = https://mod.org

and tried to get module by name, get no such host error:

go get -v mod.org/bar

go get lmod.org/bar: unrecognized import path "lmod.org/bar" (https fetch: Get https://lmod.org/bar?go-get=1: dial tcp: lookup lmod.org: no such host)

When I add extension .git

go get -v mod.org/bar.git 

go: finding lmod.org/bar.git v1.0.0
go: downloading lmod.org/bar.git v1.0.0
verifying lmod.org/[email protected]: lmod.org/[email protected]: reading https://sum.golang.org/lookup/lmod.org/[email protected]: 410 Gone

go download version with tag v1.0.0 to GOPATH = /Users/user/go":

$GOPATH
└── go
     └── pkg
         └── mod
             └── cache
                 └── download
                     └── mod.org
                         └── bar.git
                             └── @v
                                 β”œβ”€β”€ v1.0.0.info
                                 β”œβ”€β”€ v1.0.0.lock
                                 └── v1.0.0.zip.tmp882433775

, but I still can't use one as dependency in other go-project.

Upvotes: 0

Views: 9659

Answers (3)

kozmo
kozmo

Reputation: 4471

Steps to solve the problem:

1️⃣ changed module declaration in go.mod to

module mod.org/gomod/bar

go 1.16

the same as bitbucket repositories structure

enter image description here

repo's references to cloning:

http://localhost:7990/scm/gomod/bar.git
ssh://[email protected]/gomod/bar.git

2️⃣ change .gitconfig: add insteadOf (ssh or https)

# [url "http://localhost:7990/scm"]
[url "ssh://[email protected]"]
      insteadOf = https://mod.org

3️⃣ add https://mod.org to private repository

go env -w GOPRIVATE="mod.org"

❗After all preparations the module will be accessible to go mod download from other module by version tags

module mod.org/gomod/foo

go 1.16

require (
   mod.org/gomod/bar v1.0.0-beta.1
)

replace (
   mod.org/gomod/bar => mod.org/gomod/bar.git v1.0.0-beta.1
) 

or manually

go get -u mod.org/gomod/bar.git
go get mod.org/gomod/[email protected]

Upvotes: 1

igonejack
igonejack

Reputation: 2532

You can't use private repo without .git extension because of go tools don't know the version control protocol of your private repo, git or svn or any other.

For github.com or golang.org they are hardcoded into go's source.

The go tool will do a https query to know that before fetching your private repo:

https://private/user/repo?go-get=1

If your private repo didn't support https, please use replace syntax of go module to tell go tools directly:

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

Upvotes: 2

bcmills
bcmills

Reputation: 5187

The server for https://mod.org/bar needs to return go-import metadata following the protocol described in https://golang.org/cmd/go/#hdr-Remote_import_paths.

Several open-source implementations exist, such as:

You can store credentials (or access tokens) for both the HTTPS server and the underlying repository in a .netrc file, and use the GOPRIVATE environment variable to tell the go command not to look for your private repo in the public proxy.

Upvotes: 3

Related Questions