从当前提交中获取最新的git标签 [英] Get latest git tag from the current commit

查看:68
本文介绍了从当前提交中获取最新的git标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从树中的某个位置获取最新的git标签.

I am trying to get the latest git tag from a certain point in my tree.

树看起来如下:

* 334322c|2016-12-06| (tag: 0.1265, tag: 0.1264) (18 hours ago)
* 739392e|2016-12-06| (HEAD -> testbranch, tag: 0.1263, tag: 0.1262) (19 hours ago)
* 8ec1add|2016-12-06| (tag: 0.1261, tag: 0.1260, tag: 0.1259) (20 hours ago)
* 5b2667b|2016-12-06| (tag: 0.1258) (21 hours ago)
* c7ff4bc|2016-12-06| (tag: 0.1257, tag: 0.1256) (22 hours ago)

0.1263是我正在寻找的git标签.

0.1263 is the git tag I am looking for.

739392e上使用git describe --tags时,仅返回0.1262,这是该提交中的第一个标签,而我没有得到所有的候选人.

When on 739392e using git describe --tags returns only 0.1262 which is the first tag on that commit, and I'm not getting all the candidates.

739392e上使用git describe --tags $(git rev-list --tags --max-count=1)返回0.1265,即树上的最新标签(无论HEAD在何处).

When on 739392e using git describe --tags $(git rev-list --tags --max-count=1) returns 0.1265, the latest tag on the tree (regardless of where the HEAD is).

推荐答案

如果您可以按名称对标签进行排序(这看起来合理吗?),则可以使用以下命令:

If you can sort tags on name (which seems reasonable?) you can use the following command:

git tag --points-at HEAD --sort -version:refname | head -1

如果不首先检查是否在标签上设置了committerdate或authordate:

If not first check if committerdate or authordate is set on your tags:

git tag --points-at HEAD --format='%(*committerdate:iso) %(*authordate:iso) %(refname) %(*objectname) %(objectname)'

这将输出如下内容:

2018-05-28 09:58:06 +0200 2018-05-28 09:58:06 +0200 refs/tags/3.11.47 55a4f6de2b1466d1a2ee60acc53aa12fd5ad07b3 914ac376102a6c7f189453fbcd8737db32b90693
2018-05-28 09:58:06 +0200 2018-05-28 09:58:06 +0200 refs/tags/3.11.46 55a4f6de2b1466d1a2ee60acc53aa12fd5ad07b3 e0d37427b89fbd6c3baa898a1264a9ba3e3ff7f

从此示例中可以看到,两个标签的committerdate和authordate相同,这意味着我们无法在这些字段上进行排序.但是,如果设置正确,则可以使用以下命令:

As you can see from this example, both tags have identical committerdate and authordate, meaning we cannot sort on those fields. However if they are set correctly you can use this command:

git tag --points-at HEAD --sort -version:creatordate | head -1

来自git help tag:

--sort=<key> 根据给定的键排序. 前缀-以值的降序排列.您可以多次使用--sort=<key>选项,在这种情况下 最后一个键成为主键.还支持版本:refname"或"v:refname"(标记名称被视为版本). 版本:refname"排序 顺序也可能会受到"versionsort.suffix"配置变量的影响. 支持的键与git for-each-ref 中的键相同.种类 order默认为为tag.sort变量配置的值(如果存在),否则为字典顺序.参见git-config(1).

--sort=<key> Sort based on the key given. Prefix - to sort in descending order of the value. You may use the --sort=<key> option multiple times, in which case the last key becomes the primary key. Also supports "version:refname" or "v:refname" (tag names are treated as versions). The "version:refname" sort order can also be affected by the "versionsort.suffix" configuration variable. The keys supported are the same as those in git for-each-ref. Sort order defaults to the value configured for the tag.sort variable if it exists, or lexicographic order otherwise. See git-config(1).

并且来自git help for-each-ref:

对于提交和标记对象,特殊的 creatordate 和创建者字段将对应于来自提交者或标记者字段的适当日期或名称-电子邮件日期元组,具体取决于对象类型.这些旨在用于带注释的标签和轻量级标签的混合使用.

For commit and tag objects, the special creatordate and creator fields will correspond to the appropriate date or name-email-date tuple from the committer or tagger fields depending on the object type. These are intended for working on a mix of annotated and lightweight tags.

这篇关于从当前提交中获取最新的git标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆