Skip to content

Instantly share code, notes, and snippets.

@waffleboot
Last active November 5, 2021 12:05
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 waffleboot/a3c79617cd3c0447cdf955e54550907f to your computer and use it in GitHub Desktop.
Save waffleboot/a3c79617cd3c0447cdf955e54550907f to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
set -e
git diff --cached | while IFS= read -r line
do
read -r -a WORDS <<< "$line"
diff=${line:0:1}
case ${WORDS[0]} in
diff)
file=${WORDS[2]:2}
;;
new | index | deleted | similarity | rename | --- | +++)
;;
@@)
state=0
;;
*)
case ${diff} in
-)
case ${state} in
0 | 1)
state=1
;;
2)
state=4
;;
*)
echo >& too many changes in ${file}
exit 1
;;
esac
;;
+)
case ${state} in
0 | 2)
state=2
;;
1 | 3)
state=3
;;
*)
echo >&2 too many changes in ${file}
exit 1
;;
esac
;;
' ')
# unchanged line
state=0
;;
*)
echo >&2 "parse error on '${line}' in ${file}"
exit 2
;;
esac
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment