How does go mod tidy work generate psuedoversions for local directories?

huangapple go评论78阅读模式
英文:

How does go mod tidy work generate psuedoversions for local directories?

问题

问题

go mod tidy能否为本地git仓库生成有意义的提交版本?

背景

最近我注意到,当从本地目录运行go mod tidy时,它会使用Golang的“零时间”值生成一个版本,如下所示:

require example.com/db_api v0.0.0-00010101000000-000000000000

对于没有提交和版本标签的本地仓库来说,这似乎是合理的...然而,当我尝试为本地目录添加提交和标签时,我注意到go mod tidy仍然依赖于零时间戳作为require语句的标识符。

示例

让我们从一个依赖项开始:

➜  db_api git:(heads/v1.0.0) tree
.
├── go.mod
└── vars
    └── constants.go

和一个主要的仓库:

➜  webapp git:(master) ✗ tree
.
├── go.mod
└── main.go

现在,如果我添加一个替换指令,如下所示(使得webapp/main.go依赖于db_api目录中的constants.go文件...

replace example.com/db_api => ../db_api

那么go mod tidy的结果将始终是上述的“零时间戳”,而不是v1.0.0

相比之下,对一个通用的“GitHub”仓库(例如cobra)运行go mod tidy会生成基于“标签”的版本锁定:

require (
        example.com/db_api v0.0.0-00010101000000-000000000000
        github.com/spf13/cobra v1.2.1
)

在我看来,与远程git仓库相关的逻辑(获取最新的语义化版本标签)应该对远程仓库和本地仓库是相同的。然而,根据我在这里的实验,似乎不会检查本地仓库的标签。

因此,原始问题是... go mod tidy能否像对待远程仓库一样,从本地仓库中猜测版本化数据,或者本地git仓库在go模块中的固定方式是否根本不同?

英文:

Question

Can go mod tidy generate meaningful commit versions for local git repositories?

Background

I recently noticed that go mod tidy, when run from a local directory, generates a version using the "zero time" value from Golang, like so:

require example.com/db_api v0.0.0-00010101000000-000000000000

This seems reasonable for a local repository with no commits and no version tags... however, when I tried to add commits and tags to a local directory, I noticed that go mod tidy still relied on the zero timestamp as the identifier for the require statement.

Example

Let's start with a dependency:

➜  db_api git:(heads/v1.0.0) tree
.
├── go.mod
└── vars
    └── constants.go

And a main repo:

➜  webapp git:(master) ✗ tree
.
├── go.mod
└── main.go

Now, if I add a create replace directive like so (such that the webapp/main.go depends on the constants.go file in the db_api directory...

replace example.com/db_api => ../db_api

Then the result of go mod tidy will always be the above "zero timestamp", as opposed to v1.0.0.

In contrast, running go mod tidy against a generic "GitHub" repo (i.e. cobra), results in a "tag" based version pin:

require (
        example.com/db_api v0.0.0-00010101000000-000000000000
        github.com/spf13/cobra v1.2.1
)

It seems to me like, the logic associated with remote git repositories (to get the latest semver tag), should be identical even for remote repositories. However, based on my experiments in here, it appears like local repositories aren't inspected for tags.

Hence, the original question... Can go mod tidy guess versioned data from a local repo in the same manner as it would for a remote repository, or is a local git repo fundamentally different in the way it is pinned in a go module?

答案1

得分: 1

> go mod tidy能为本地git仓库生成有意义的提交版本吗?

不可以。

> 因此,原始问题是... go mod tidy能否像对待远程仓库一样猜测本地仓库的版本化数据,

不可以。

> 或者说,本地git仓库在固定在go模块中的方式上有根本的不同吗?

是的。

go.mod中的replace意味着“使用我在那里放置的那些文件作为模块”。你可能使用git、rsync或其他工具来管理这些文件,但这并不重要。

英文:

> Can go mod tidy generate meaningful commit versions for local git repositories?

No

> Hence, the original question... Can go mod tidy guess versioned data from a local repo in the same manner as it would for a remote repository,

No

> or is a local git repo fundamentally different in the way it is pinned in a go module?

Yes.

replace in go.mod means "use those files I dumped there as the module". The fact that you might manage these files with git or rsync or whatnot doesn't matter the slightest.

huangapple
  • 本文由 发表于 2021年9月27日 04:37:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/69338773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定