我怎样才能计算出 git 标签和原始提交之间的时间差

How can I work out time difference between git tag and the original commit

我想计算出提交时间和标记提交时间之间的时间差。

这是我目前所拥有的,但是我认为这是正确字段的提交日期为空。

git for-each-ref --format='%(taggerdate) : %(committerdate) :  %(refname)' --sort=-taggerdate --count=10 refs/tags

我猜我只是在看标签,虽然我没看对。在一个理想的世界中,它会显示时间之间的差异,但是如果不可能的话,我可以在之后这样做。

The git for-each-ref documentation 有点误导:

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.

这确实按照记录工作:%(creatordate) 从这些带注释的标签的 tagger 字段中获取日期。但是,似乎 暗示 对于带注释的标记,Git 可能会检查相应的提交:如果不是,它如何获得 committer?

事实上,for-each-ref 从不直接 看注释标签的目标对象。 committer 字段的提及特定于 lightweight 标签,它指向提交,1 而不是带注释的标签对象。幸运的是,有一个格式指令对 "look indirectly" 说:只需在指令前加上 *.2

因此答案是:

git for-each-ref \
    --format='%(taggerdate) : %(*committerdate) :  %(refname)' \
    --sort=-taggerdate --count=10 refs/tags

1轻量级标签可以直接指向树或blob;目前尚不清楚在这种情况下会发生什么。可能展开是空的。

2注意这只会间接一次,所以如果标注标签的目标是另一个标签(或者是一棵树或者blob),将没有提交者日期字段。要真正使其可靠,您可能应该使用一些 shell 脚本并使用 ^{}^{commit} 后缀解析标记名称。 (这两者之间的区别在于,如果最终目标对象是树或 blob,^{commit} 失败 ,而 ^{} 将成功,找到该树或 blob对象。)