11

git log --decorate adds information about related refs to the log output:

commit 9e895ace5d82df8929b16f58e9f515f6d54ab82d (tag: v3.10-rc7)
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Sat Jun 22 09:47:31 2013 -1000

    Linux 3.10-rc7

This information helps tracking which tag (or branch) contains this commit. When viewing a restricted set of files (say, a subdirectory), there does not have to be a tag for those commits. Is there a way to put a reference to a tag in the log output?

I previously mentioned git describe, but that yields v3.10-rc7-135-g98b6ed0 which is relative to a tag of branch where this change was committed. What I am looking for is a tag name between commits.

For clarity, this is the current situation:

$ git log --decorate --oneline
98b6ed0 (HEAD, origin/master, master) Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
1a506e4 Merge branch 'drm-fixes' of git://people.freedesktop.org/~airlied/linux
578a131 dlci: validate the net device in dlci_del()
11eb264 dlci: acquire rtnl_lock before calling __dev_get_by_name()
...
9e895ac (tag: v3.10-rc7) Linux 3.10-rc7

What I want to have is something like:

98b6ed0 (v3.10-rc7+, HEAD, origin/master, master) Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
1a506e4 (v3.10-rc7+) Merge branch 'drm-fixes' of git://people.freedesktop.org/~airlied/linux
578a131 (v3.10-rc7+) dlci: validate the net device in dlci_del()
11eb264 (v3.10-rc7+) dlci: acquire rtnl_lock before calling __dev_get_by_name()
...
9e895ac (tag: v3.10-rc7) Linux 3.10-rc7

Using git describe's output instead of the commit hash would show something like:

$ git log --decorate --oneline -n4 | awk '{system("git describe " $1 " |tr -d '\''\n'\''");$1="";print}'
v3.10-rc7-135-g98b6ed0 (HEAD, origin/master, master) Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
v3.10-rc7-54-g1a506e4 Merge branch 'drm-fixes' of git://people.freedesktop.org/~airlied/linux
v3.10-rc6-81-g578a131 dlci: validate the net device in dlci_del()
v3.10-rc6-80-g11eb264 dlci: acquire rtnl_lock before calling __dev_get_by_name()
...
v3.10-rc7 (tag: v3.10-rc7) Linux 3.10-rc7

As you can see, older tag names are used as reference point rather than the point where the commit got merged. For illustation purposes, I am using git log --oneline here, but I actually want to use fuller output, e.g. git log -p --stat.

4

2 回答 2

9

(在 git 1.8.4 中引入)的--first-parent参数git describe显示了提交的来源。为了查看与提交后第一个标签的关系,请使用git describe --contains. 但是,当您深入研究历史时,此选项会变得非常慢(约 6 秒)。从 git 1.5.3 开始可用。

该命令也git name-rev可用于注释git rev-name和使用!从其手册页:--graph--color

给定一个提交,找出它相对于本地参考的位置。假设有人给你写了关于那个奇妙的提交 33db5f4d9027a10e477ccf054b2c1ab94f74c85a。当然,您会查看提交,但这只会告诉您发生了什么,而不是上下文。

输入git name-rev

% git name-rev 33db5f4d9027a10e477ccf054b2c1ab94f74c85a
33db5f4d9027a10e477ccf054b2c1ab94f74c85a tags/v0.99~940

现在你更聪明了,因为你知道它在 v0.99 之前发生了 940 次修订。

您可以做的另一件好事是:

% git log | git name-rev --stdin

最后一个命令将一些内容附加到每个 40 个字符的 SHA-1 散列中,如下所示(突出显示的部分由添加git name-rev)。

提交 1ee2dcc2245340cf4ac94b99c4d00efbeba61824 (tags/v3.13-rc1~33)
合并:4457e6f 091e066
作者:莱纳斯·托瓦兹

    合并 git://git.kernel.org/pub/scm/linux/kernel/git/davem/net

提交 b4089d6d8e71a7293e2192025dfa507a04f661c4 (tags/v3.13-rc1~7^2~6^2^2~8)
作者:费利克斯·菲特考

    rt2x00:修复 HT 描述符处理中的崩溃错误修复
...
提交 dfb6b7c109a7f98d324a759599d1b4616f02c79f (tags/v3.12-rc7~20^2~20^2^2~11)
作者:斯坦尼斯瓦夫·格鲁兹卡
日期:2013 年 9 月 23 日星期一 04:08:13 +0200

    还原“rt2x00pci:尽可能使用 PCI MSI”

    这将恢复提交 9483f40d8d01918b399b4e24d0c1111db0afffeb (tags/v3.11-rc1~16^2~103^2^2~111)

git log用于后处理输出的 awk 脚本可在https://git.lekensteyn.nl/scripts/tree/git-log-describe.awk获得 (在我知道之前编写git rev-name)。特征:

  • 考虑哈希commit <hash>而不是 40 个字符的哈希(--abbrev-commit也适用)。
  • 支持git log --graph格式。
  • 添加git describe --containsgit describe --first-parent输出。
  • 可以指定缓存目录以节省以后的时间。
于 2013-11-23T11:34:40.137 回答
2

如您所见,较旧的标记名称用作参考点,而不是提交合并的点。

这应该是可能的......很快(git 1.8.4 July 2013):

请参阅提交 e00dd1e9485c50f202cc97dfae19d510e108b565

describe: Add --first-parent option

遍历提交历史时仅考虑第一个父提交。
如果您只想在合并后匹配分支上的标签,这很有用。


OP Lekensteyn评论它 ( --first-parent) 还不够:

--first-parent也不显示它被合并的标签。
我刚刚发现--contains可以用于此。
请参阅我的答案以获得更好的解决方案,git name-rev.

注意:git name-rev可以追溯到git0.99.9 (Oct. 2005!)

于 2013-06-29T12:22:45.390 回答