Sunday, September 15, 2019

Git pre-commit hook to disallow a substring

This took way longer than it should have to figure out, so I wanted to share in case others are trying to do the same thing.

Add this snippet to your Git pre-commit hook to prevent (in this case) the substring FIXME from making it into a committed file. Since the hook includes the substring 'FIXME' you'll need to commit like this:
git add git-hooks/pre-commit
allowfixme=true git commit -m 'Disallow FIXME in committed files.'
Here's the code. Public domain, unless you really need a license, in which case use the MIT license. If you can, I'd appreciate a link, but no worries if not.
# Disallow 'FIXME' from being committed. 'TODO' is for cross-commit notes of
# pending work, but 'FIXME' is for things that need to be updated before
# committing.
# Set allowfixme=true to allow commit anyway, such as if the file contains
# 'FIXME' for a good reason (like this file), or you have some other reason
# to leave it in the repo for now.
if [ "$allowfixme" != "true" ] ; then
  found_fixme=
  for F in $(git diff --cached --name-only) ; do
    C=$(git ls-files -s "$F" | awk '{print $2}')
    if git cat-file blob "$C" | grep -q 'FIXME' ; then
      if [ -z "$found_fixme" ] ; then
        echo "COMMIT REJECTED: Found 'FIXME' in:"
        found_fixme=1
      fi
      echo "  $F"
    fi
  done
  if [ -n "$found_fixme" ] ; then
    echo "Please remove before committing."
    exit 1
  fi
fi

No comments: