Skip to content

Instantly share code, notes, and snippets.

@ajfarkas
Last active Dec 20, 2021
Embed
What would you like to do?
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