How to git diff FLAC files

If you're as stubborn as me, and use Git to manage everything, even FLAC files, then here's a way to tell Git how to perform a diff on their metadata.

First, add the following line to your repo's .gitattributes file:

*.flac binary diff=flac

This tells Git that FLAC files are binary, and that it should perform a diff on them in a unique way we've arbitrarily labelled "flac".

Next, we need to tell Git how to do that, by very carefully adding these lines to your repo's .git/config file:

[diff "flac"]
    binary = true
    textconv = tidymetaflac.sh

Finally, we need to make a short script, ~/bin/tidymetaflac.sh, that actually extracts the plain text from the FLAC file:

#!/bin/sh

metaflac --list --block-type=STREAMINFO,VORBIS_COMMENT "$1" | sed 's/comment\[[0-9]*\]:/comment:/;/length:/d;/comments:/d' | sort

This does a few things. First, it lists just the plain text metadata in the FLAC file; next, it discards each comment's number, as these might move around arbitrarily, along with unnecessary derived information; finally, it alphabetises what's left. As a result, Git will ignore metadata that hasn't really changed, focusing only on what has. Remember to chmod 755 the script, so Git can execute it.

Now when you use git-diff on a commit which changes a FLAC file's metadata, you should see those changes in a nicely formatted summary.