Skip to content

Instantly share code, notes, and snippets.

@handleman
Forked from jiskanulo/pre-commit
Created June 4, 2020 08:21
Show Gist options
  • Save handleman/b6efe3b24fd97efeba43db6699a8d5a6 to your computer and use it in GitHub Desktop.
Save handleman/b6efe3b24fd97efeba43db6699a8d5a6 to your computer and use it in GitHub Desktop.
Git pre-commit hook: detect file size over 100MB
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# 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.
#
# To enable this hook, rename this file to "pre-commit".
# Redirect output to stderr.
exec 1>&2
CURRENT_DIR="$(pwd)"
HAS_ERROR=""
for file in $(git diff --cached --name-only | sort | uniq); do
file_size=$(du -m $CURRENT_DIR/$file | awk '{print $1}')
if [ "$file_size" -ge 100 ]; then
echo "$file is over 100MB."
HAS_ERROR="1"
fi
done
if [ "$HAS_ERROR" != "" ]; then
echo "Can't commit, fix errors first." >&2
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment