Last Updated: February 25, 2016
·
718
· hauleth

Simplify your configurable hooks

Sometimes we want to have option to enable/disable some git hooks. We can create if for each, but we should be DRY even in hooks. So my solution is:

#!/bin/sh

ROOT=$(git rev-parse --show-toplevel)

function hook {
  value=$(git config hooks.$1)
  if [ "$value" = "true" ]; then
    shift 1
    eval $* || exit 1
  fi
}

hook rubocop rubocop -R
hook tests $ROOT/bin/rake test:all # check https://github.com/rails/rails/pull/12958

IMHO it is simply enough but powerful as well. Enjoy yourselves.