Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I decorate a git log with its nearest tag?

Tags:

git

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

commit 9e895ace5d82df8929b16f58e9f515f6d54ab82d (tag: v3.10-rc7)
Author: Linus Torvalds <[email protected]>
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.

like image 982
Lekensteyn Avatar asked Jun 29 '13 10:06

Lekensteyn


1 Answers

The --first-parent parameter of git describe (introduced with git 1.8.4) shows where a commit is derived from. In order to see a relation to the first tag following the commit, use git describe --contains. This options gets very slow (~6 seconds) when you delve deeper in the history though. Available since git 1.5.3.

The command git name-rev can be used to annotate git rev-name and works with --graph and --color too! From its manual page:

Given a commit, find out where it is relative to the local refs. Say somebody wrote you about that fantastic commit 33db5f4d9027a10e477ccf054b2c1ab94f74c85a. Of course, you look into the commit, but that only tells you what happened, but not the context.

Enter git name-rev:

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

Now you are wiser, because you know that it happened 940 revisions before v0.99.

Another nice thing you can do is:

% git log | git name-rev --stdin

This last command appends something to every 40-character SHA-1 hash as shown below (the highlighted part is added by 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).

An awk script for post-processing git log output is available at https://git.lekensteyn.nl/scripts/tree/git-log-describe.awk (written before I knew of git rev-name). Features:

  • Considers the hash from commit <hash> instead of 40-character hashes (works with --abbrev-commit too).
  • Support for git log --graph format.
  • Adds git describe --contains or git describe --first-parent output.
  • Possibility to specify a cache directory to save time later.
like image 114
Lekensteyn Avatar answered Sep 27 '22 20:09

Lekensteyn