gitgobitbucketgo-modulesgo-get

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/bar.git@v1.0.0: lmod.org/bar.git@v1.0.0: reading https://sum.golang.org/lookup/lmod.org/bar.git@v1.0.0: 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.


Solution

  • 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://git@mod.org/gomod/bar.git
    

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

    # [url "http://localhost:7990/scm"]
    [url "ssh://git@mod.org"]
          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/bar.git@v1.0.0-beta.1