How can I decorate git log with its closest tag?

git log --decorate adds information about related links to 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 you track which tag (or branch) contains this commit. When viewing a limited set of files (say, a subdirectory) for these commits, it is not necessary to be a tag. Is there a way to place a link to a tag in the output log file?

I mentioned git describe earlier, but this gives v3.10-rc7-135-g98b6ed0 , which refers to the tag of the branch where this change was made. What I'm looking for is the 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 

I want to have 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 output instead of the commit hash will do the following:

 $ 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 a breakpoint, not the point at which the command was merged. For illustration purposes, I use git log --oneline here, but I really want to use more complete output, for example. git log -p --stat .

+8
git
source share
2 answers

The --first-parent git describe --first-parent (introduced with git 1.8.4) shows where the commit is coming from. To see the relationship to the first tag after commit, use git describe --contains . These options become very slow (~ 6 seconds) when you go deeper into the story. Available since git 1.5.3.

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

Given the commit, find out where it refers to local refs. Tell someone you wrote about this fantastic fix 33db5f4d9027a10e477ccf054b2c1ab94f74c85a. Of course, you are viewing 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 940 changes happened before v0.99.

Another nice thing you can do:

 % git log | git name-rev --stdin 

This last command adds something to each 40-character SHA-1 character, 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) .

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

  • Considers a hash from commit <hash> instead of 40-character hashes (also works with --abbrev-commit ).
  • Support for the git log --graph .
  • Adds git describe --contains or git describe --first-parent .
  • Ability to specify a cache directory to save time later.
+5
source share

As you can see, the old tag names are used as a breakpoint, not the point at which the commit received the union.

This should be possible ... coming soon (git 1.8.4 July 2013):

See commit e00dd1e9485c50f202cc97dfae19d510e108b565 :

 describe: Add --first-parent option 

Only consider the first parent message when going through the commit history. This is useful if you only want to bind tags to your branch after merging.


OP Lekensteyn does not comment on this ( --first-parent ) enough:

--first-parent does not show the tag where it also merged.
I just discovered that you can use --contains for this.
See my answer for an even better solution, git name-rev .

Note: git name-rev dates to git0.99.9 (October 2005) .

+2
source share

All Articles