Skip to content

Instantly share code, notes, and snippets.

@ajfarkas
Last active December 20, 2021 19:49
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 ajfarkas/331c51a5c2f13a780c95a50aaa2f6ebf to your computer and use it in GitHub Desktop.
Save ajfarkas/331c51a5c2f13a780c95a50aaa2f6ebf to your computer and use it in GitHub Desktop.
Git pre-commit hook to limit number of changes
#!bin/zsh
maxlines=100
lines=0
# read shortened commit data
git diff --cached --oneline --numstat | while read commit; do
# use tr to break words into newlines
echo $commit | tr -s '[[:space:]]' '\n' | while read word; do
# match numbers only
if [[ $word =~ ^[0-9]+$ ]]
then
# add number of changes to total lines changed
((lines=$lines+$word))
fi
done
done
echo $lines' lines changed.'
if [[ $lines -le $maxlines ]]
then
exit 0
else
echo 'Too many changes are included. the max is '$maxlines
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment