从私有存储库获取依赖项的正确方法

我在👉🏻克隆链接上有私人bitbucket仓库http://localhost:7990 http://localhost:7990/scm/gom/bar.git


go.mod好像:


module mod.org/bar


go 1.13

远程存储库中可用的参考:


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

我.gitconfig改为


[url "http://localhost:7990/scm/gom"]

      insteadOf = https://mod.org

并试图通过 获取模块name,得到 no such host错误:


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)

当我添加扩展名时.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下载带有标签v1.0.0的版本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

,但我仍然无法在其他 go-project 中使用它作为依赖项。


qq_花开花谢_0
浏览 122回答 3
3回答

ABOUTYOU

服务器需要按照https://golang.org/cmd/go/#hdr-Remote_import_paths中描述的协议https://mod.org/bar返回元数据。go-import存在多种开源实现,例如:rsc.io/go-import-redirectorgithub.com/GoogleCloudPlatform/govanityurls您可以将 HTTPS 服务器和底层存储库的凭据(或访问令牌)存储在文件中.netrc,并使用GOPRIVATE环境变量告诉go命令不要在公共代理中查找您的私有存储库。

达令说

你不能使用没有.git扩展的私有仓库,因为 go 工具不知道你的私有仓库、git 或 svn 或任何其他的版本控制协议。或者github.com它们golang.org被硬编码到 go 的源代码中。go 工具将https在获取您的私人存储库之前进行查询以了解:https://private/user/repo?go-get=1如果你的私有仓库不支持https,请使用replacego模块的语法直接告诉go工具:require private/user/repo v1.0.0...replace private/user/repo => private.server/user/repo.git v1.0.0https://golang.org/cmd/go/#hdr-Remote_import_paths

陪伴而非守候

解决问题的步骤:1️⃣ 将模块声明更改go.mod为module mod.org/gomod/bar go 1.16bitbucket与存储库结构相同存储库对克隆的引用:http://localhost:7990/scm/gomod/bar.gitssh://git@mod.org/gomod/bar.git2️⃣ 更改.gitconfig:添加insteadOf(ssh或https)# [url "http://localhost:7990/scm"][url "ssh://git@mod.org"]      insteadOf = https://mod.org3️⃣ 添加https://mod.org到私有存储库go env -w GOPRIVATE="mod.org"go mod download❗完成所有准备工作后,可以通过以下方式从其他模块访问该模块version tagsmodule mod.org/gomod/foogo 1.16require (   mod.org/gomod/bar v1.0.0-beta.1)replace (   mod.org/gomod/bar => mod.org/gomod/bar.git v1.0.0-beta.1) 或手动go get -u mod.org/gomod/bar.gitgo get mod.org/gomod/bar.git@v1.0.0-beta.1
打开App,查看更多内容
随时随地看视频慕课网APP