Git Metadata In-Band

Moving configuration and hooks into the tree.

When writing tools for git, or while working on more complex projects, I often need to work with the git configuration or hooks.

Unfortunately (or so I sometimes feel), the configuration and hooks are not stored in the tree (the version controlled part of repo), and must be installed out of band. This easily leads to a non-uniform configuration across clones.

Let's get that information in band!

I tend to place any configuration I want to persist into a .gitconfig file, and take advantage of the include.path variable. From the docs:

You can include one config file from another by setting the special include.path variable to the name of the file to be included. [...] If the value of the include.path variable is a relative path, the path is considered to be relative to the configuration file in which the include directive was found.

The key is that the include.path is relative; a simple leading .. will let you refer to the working directory.

Next, any hooks that I want to persist go into a .githooks directory.

Finally, a script will install them both:

# Install local config.
if [[ -f .gitconfig ]]; then
    git config --add include.path ../.gitconfig

# Install local hooks.
if [[ -d .githooks ]]; then
    chmod +x .githooks/*
    for hook in .githooks/*; do
        ln -sf ../../$hook .git/hooks/

Now one only needs to run . once after cloning the repository to set up a repeatable environment. There is still a manual element there, but at least it is now very consistent.

Posted . Categories: .