In the global file call the local file to prevent overriding

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the architecture category.

Last Updated: 2024-11-23

I came across a useful general pattern when getting global githooks working in a way that did not shadow local githooks (FYI: the git API at the time had the limitation that only one hook path could be given - either the global or the local hooks path)

The trick is to check for a corresponding local githook, and run it if it happens to be available. For example:

if some_condition
then
  echo "Trying to commit non-committable code."
  exit 1
else
  # Run local pre-commit hook if exists
  if [ -e ./.git/hooks/pre-commit ]; then
    ./.git/hooks/pre-commit "$@"
  else
    exit 0
  fi
fi

References: