Skip to content

Instantly share code, notes, and snippets.

@oren-l
Last active November 17, 2021 13:58
Show Gist options
  • Save oren-l/31bf15dccac97a35a0bf9e4d14469fce to your computer and use it in GitHub Desktop.
Save oren-l/31bf15dccac97a35a0bf9e4d14469fce to your computer and use it in GitHub Desktop.
pre-commit hook that limits number of characters in a commit (including binary files)
#!/bin/sh
# meant to be used with husky: https://github.com/typicode/husky/blob/main/README.md
# tested on macos and ubuntu
. "$(dirname "$0")/_/husky.sh"
max_size=500000
echo "committed files:"
git diff --staged --stat HEAD # show staged diff stat
echo "" # adds \n
git diff --staged HEAD -p --text `# get staged files diff and count binary files content as text` \
| wc -c `# count number of characters` \
| awk -v max_size=$max_size '$1 > max_size { printf "changes (%d chars) exceeded limit of %d chars\n", $1, max_size; exit 1 }' `# fail if number of chars exceeds max_size` \
>&2 # redirect output to stderr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment