git has many useful features in git log(1) and git blame(1) to display the history of a file, or who contributed each line in said file, respectively. However, it might be useful to get the full history not of a file or a line but, say, a function (that is, more than one line in a coherent structure). This can be interesting for things such as displaying all the authors of a given function.

Of course, git can do this. Both commands cited above have an almost similar -L option which allows to work only on a subset of lines. An interesting way of expressing this subset is with the :<regexp> construct. It instructs git to find funcnames matching that regular expression, and consider the lines following until a new funcname is found (I haven’t found much documentation on what funcname is, but presumably, this is what git uses to identify functions in a particulary source language to give context in diff outputs). git log has an additional expectation after the regexp: the name of a file in which to find matching funcnames, separated from the regexp by a colon.

I initially thought that git blame would be the tool, but it turns out that git log is better armed. Unfortunately, it also outputs the diff with every commit, which is more than we need in that case, even with custom formats, so we need to do some plumbing.

shtrom@lxiv:~/nicta/OML/oml (staging)$ git log   -L :omlc_inject:lib/client/api.c   --format="Author: %aN" | sed -n "s/^Author: //p" | sort | uniq
Jolyon White
Olivier Mehani

The format simply asks each author’s name, preceded by a tag for easier extraction. We could have used git log‘- porcelain output, but it doesn’t apply the mappings in mailmap (documented in git shortlog(1)), and might result in different versions of authors’ names, which would remain duplicated after the sort | uniq gymnastics.

A word of caution, though: as this relies on funcname to identify the beginning and the end of a function, the selection might extend pass the actual end of the function, and include whitespaces and top-comments for the next function. Some authors might end up being credited for working on functions they never looked at!

Leave a Reply