This PR got superseded, see my new code at https://github.com/libgit2/libgit2/blob/development/src/refdb_fs.c#L1026