Skip to content

Instantly share code, notes, and snippets.

@hboon
Created May 12, 2021 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hboon/1ddbe81ae2ea8f9bf2f920184f7208b4 to your computer and use it in GitHub Desktop.
Save hboon/1ddbe81ae2ea8f9bf2f920184f7208b4 to your computer and use it in GitHub Desktop.
git pre-commit
#!/bin/sh
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
exec 1>&2
#Workaround for filenames with spaces in them (because for loop breaks use space by default)
SAVEIFS=$IFS
IFS=$(echo "\n\b")
for ENTRY in `git diff-index --cached --name-status $against -- | cut -c1,3-`; do
CHANGE=`echo "$ENTRY" | cut -c1`
FILE=`echo "$ENTRY" | cut -c2-`
if [ $CHANGE = "A" ] || [ $CHANGE = "M" ]; then
if git diff --cached --unified=0 "$FILE" | grep -q 'kkk'; then
echo "$FILE" 'contains kkk!'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q 'hhh'; then
echo "$FILE" 'contains hhh!'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q 'lll'; then
echo "$FILE" 'contains lll!'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q ' red'; then
echo "$FILE" 'contains red!'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q ' blue'; then
echo "$FILE" 'contains blue!'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q ' white'; then
echo "$FILE" 'contains white!'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q ' green'; then
echo "$FILE" 'contains green!'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q ' yellow'; then
echo "$FILE" 'contains yellow!'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q ' magenta'; then
echo "$FILE" 'contains magenta!'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q ' p '; then
echo "$FILE" 'contains p !'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q ' mlog'; then
echo "$FILE" 'contains mlog!'
IFS=$SAVEIFS
exit 1
fi
if git diff --cached --unified=0 "$FILE" | grep -q '007'; then
echo "$FILE" 'contains 007!'
IFS=$SAVEIFS
exit 1
fi
fi
done
IFS=$SAVEIFS
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment