如何用最近的标签装饰git日志?

Lek*_*eyn 10 git

git log --decorate 将有关refs的信息添加到日志输出中:

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
Run Code Online (Sandbox Code Playgroud)

此信息有助于跟踪哪个标记(或分支)包含此提交.查看受限制的文件集(例如,子目录)时,不必为这些提交标记.有没有办法在日志输出中引用标记?

我之前提到过git describe,但是这个产生的v3.10-rc7-135-g98b6ed0结果是相对于提交此更改的分支标记.我要找的是提交之间的标签名称.

为清楚起见,这是目前的情况:

$ 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
Run Code Online (Sandbox Code Playgroud)

我想拥有的是:

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
Run Code Online (Sandbox Code Playgroud)

使用git describe输出而不是提交哈希将显示如下内容:

$ 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
Run Code Online (Sandbox Code Playgroud)

如您所见,较旧的标记名称用作参考点,而不是提交合并的点.为了说明目的,我在git log --oneline这里使用,但我实际上想要使用更全面的输出,例如git log -p --stat.

Lek*_*eyn 6

所述--first-parent的参数git describe(引入与GIT 1.8.4)示出了在提交时来源于.为了查看提交后第一个标记的关系,请使用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
Run Code Online (Sandbox Code Playgroud)

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

你能做的另一件好事是:

% git log | git name-rev --stdin
Run Code Online (Sandbox Code Playgroud)

最后一个命令会为每个40个字符的SHA-1哈希添加一些内容,如下所示(突出显示的部分被添加git name-rev).

commit 1ee2dcc2245340cf4ac94b99c4d00efbeba61824 (tags/v3.13-rc1~33)
Merge: 4457e6f 091e066
Author: Linus Torvalds

    Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net

commit b4089d6d8e71a7293e2192025dfa507a04f661c4 (tags/v3.13-rc1~7^2~6^2^2~8)
Author: Felix Fietkau

    rt2x00: fix a crash bug in the HT descriptor handling fix
...
commit dfb6b7c109a7f98d324a759599d1b4616f02c79f (tags/v3.12-rc7~20^2~20^2^2~11)
Author: Stanislaw Gruszka
Date:   Mon Sep 23 04:08:13 2013 +0200

    Revert "rt2x00pci: Use PCI MSIs whenever possible"

    This reverts commit 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输出.
  • 可以指定缓存目录以便以后节省时间.