Custom git hunk headers for lisp files

Patches generated by git are organized in hunks whose title contains useful information about the context of the modification. For example, in C files, the hunk title contains the name of the function where the modification occurs.

Git doesn’t have any specific rule to name hunks for Common Lisp files, but we can provide a method ourselves.

First add a .gitattributes files at the root of your repository with the following content:

*.lisp diff=lisp

This tells git to look at the diff.lisp section of the configuration file to know how to handle lisp files for diffs.

Then edit your Git configuration file (for example ~/.gitconfig), and add the following section:

[diff "lisp"]
    xfuncname="^(\\((def|test).*)$"

With this regexp, the title of each hunk will indicate the corresponding top-level form, which most of the time starts with the def prefix. I added test which is used to define 5am tests, and is handy for diffs in test suites.

Max Mikhanosha pointed out that we can have a global attribute file since git 1.7.4, which is really nice to avoid modifying the attributes of each repository you work with. Just indicate the path to the attribute file in you git configuration file:

[core]
     attributesfile = ~/.gitattributes