Last active
December 20, 2021 19:49
-
-
Save ajfarkas/331c51a5c2f13a780c95a50aaa2f6ebf to your computer and use it in GitHub Desktop.
Git pre-commit hook to limit number of changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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