Posted by on Apr 10, 2016 in development, git |

Recently I had to understand when a specific feature was introduced in one of the software we manage with git: git blame came to the rescue.

git blame

It’s interesting to read, what blame really means (from thefreedictionary):

1. To consider responsible for a misdeed, failure, or undesirable outcome
2. To find fault with; criticize
3. To place responsibility for (something)

So this process is usually associated to some kind of problems, but understanding the 4 w is a common need in software development.

Let’s take as an example the Linux kernel source code: the git repository can be downloaded with the following line

git clone https://github.com/torvalds/linux.git

According to your line speed, it’s a good time for taking a coffee 🙂

Once done, you are ready for experiencing with git blame.

Suppose you would like to see details related to the cdc-ncm driver: the simplest form of the command is

git blame drivers/net/usb/cdc_ncm.c

For each line you can see the abbreviated commit hash, the author and the date of the last change related to that line, e.g:

79f422325 (Daniele Palmas 2016-03-31 15:16:47 +0200 1629) /* Telit LE910 V2 */
79f422325 (Daniele Palmas 2016-03-31 15:16:47 +0200 1630) { USB_DEVICE_AND_INTERFACE_INFO(0x1bc7, 0x0036,
79f422325 (Daniele Palmas 2016-03-31 15:16:47 +0200 1631) USB_CLASS_COMM,
79f422325 (Daniele Palmas 2016-03-31 15:16:47 +0200 1632) USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
79f422325 (Daniele Palmas 2016-03-31 15:16:47 +0200 1633) .driver_info = (unsigned long)&wwan_noarp_info,
79f422325 (Daniele Palmas 2016-03-31 15:16:47 +0200 1634) },
79f422325 (Daniele Palmas 2016-03-31 15:16:47 +0200 1635)

Sometimes it is needed to restrict the search only to a subset of lines: this can be easily done with the -L option that allows to specify the starting and ending line (or the name of the functions to be searched), e.g.:

git blame -L1640,1660 drivers/net/usb/cdc_ncm.c

or

git blame -L:cdc_ncm_setup drivers/net/usb/cdc_ncm.c

Suppose you found the change you were looking for: with the hash you can retrieve the commit to see why the patch was written, e.g.

git show 79f422325

It’s interesting to know that in github git blame can be used with a nice GUI: the output for cdc_ncm can be seen here.

For further info

man git blame