Skip to content

Instantly share code, notes, and snippets.

@samhemelryk
Last active May 27, 2021 05:41
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save samhemelryk/8857553 to your computer and use it in GitHub Desktop.
Save samhemelryk/8857553 to your computer and use it in GitHub Desktop.
A git pre-commit hook example.
#!/bin/bash
#
# This pre-commit hook checks that you havn't left and DONOTCOMMIT tokens in
# your code when you go to commit.
#
# To use this script copy it to .git/hooks/pre-commit and make it executable.
#
# This is provided just as an example of how to use a pre-commit hook to
# catch nasties in your code.
# Work out what to diff against, really HEAD will work for any established repository.
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
diffstr=`git diff --cached $against | grep -e '^\+.*DONOTCOMMIT.*$'`
if [[ -n "$diffstr" ]] ; then
echo "You have left DONOCOMMIT in your changes, you can't commit until it has been removed."
exit 1
fi
@wkoszycki
Copy link

Cool ! :-)

@afroewis
Copy link

afroewis commented May 9, 2018

Great! BTW, there is a typo in line 22: DONOCOMMIT instead of DONOTCOMMIT

@kokamvd
Copy link

kokamvd commented Aug 22, 2018

How to restrict token verification only in files in the specified directory?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment