【问题标题】:Right way to get dependencies from private repository从私有存储库获取依赖项的正确方法
【发布时间】:2021-03-16 14:16:59
【问题描述】:

我在http://localhost:7990 上有私人bitbucket 回购 ???????? 克隆链接 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.0GOPATH = /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 中使用一个作为依赖项。

【问题讨论】:

  • 为什么不在你的 go.mod 中使用替换指令?
  • 为什么不简单的把依赖的代码放到$GOPATH/src中呢?
  • @Markus W Mahlberg - 我尝试使用本地 bitbucket repo 作为自我(私有)依赖项的持有者。我还使用go modules 来自动化获取和控制公共/私有依赖项的过程。
  • @Volker - 我想使用私有 repo 是公开的,但 replace 会在配置中产生噪音。对于我所有的私人依赖,我必须写replace lmod.org/xxx => lmod.org/xxx.git v1.0.0
  • 您的本地 bitbucket 有一个主机名。所以你的导入路径是your.host.name/owner/repo。您可以在 $GOPATH 中使用相同的路径

标签: git go bitbucket go-modules go-get


【解决方案1】:

https://mod.org/bar 的服务器需要按照https://golang.org/cmd/go/#hdr-Remote_import_paths 中描述的协议返回go-import 元数据。

存在几个开源实现,例如:

您可以将 HTTPS 服务器和底层存储库的凭据(或访问令牌)存储在 .netrc 文件中,并使用 GOPRIVATE 环境变量告诉 go 命令不要查找您的私人仓库在公共代理中。

【讨论】:

    【解决方案2】:

    你不能使用没有 .git 扩展的私有 repo,因为 go 工具不知道你的私有 repo 的版本控制协议,git 或 svn 或任何其他。

    对于 github.comgolang.org,它们被硬编码到 go 的源代码中。

    go 工具将在获取您的私人仓库之前执行https 查询以了解这一点:

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

    如果你的私有仓库不支持https,请使用go模块的replace语法直接告诉go工具:

    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

    【讨论】:

      【解决方案3】:

      解决问题的步骤

      1️⃣ 将go.mod 中的模块声明更改为

      module mod.org/gomod/bar
      
      go 1.16
      

      bitbucket存储库结构

      repo 对克隆的引用:

      http://localhost:7990/scm/gomod/bar.git
      ssh://git@mod.org/gomod/bar.git
      

      2️⃣更改.gitconfig:添加insteadOfsshhttps

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

      3️⃣ 将https://mod.org 添加到私有仓库

      go env -w GOPRIVATE="mod.org"
      

      ❗在所有准备工作之后,go mod download 可以通过 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
      ) 
      

      或手动

      go get -u mod.org/gomod/bar.git
      go get mod.org/gomod/bar.git@v1.0.0-beta.1
      

      【讨论】:

        猜你喜欢
        • 2020-06-20
        • 2020-02-21
        • 1970-01-01
        • 2021-12-07
        • 2012-04-03
        相关资源
        最近更新 更多