Skip to content

Instantly share code, notes, and snippets.

@gmaccarone
Created June 20, 2013 22:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gmaccarone/5827183 to your computer and use it in GitHub Desktop.
Save gmaccarone/5827183 to your computer and use it in GitHub Desktop.
commit size pre-receive hook
#!/bin/sh
GITCMD="/usr/local/bin/git"
NULLSHA="0000000000000000000000000000000000000000"
MAXSIZE="5242880" # 5MB limit on file size
EXIT=0
# Read stdin for ref information
while read oldref newref refname; do
# Skip branch deletions
if [ "${newref}" = "${NULLSHA}" ]; then
continue;
fi
# Set oldref properly if this is branch creation.
if [ "${oldref}" = "${NULLSHA}" ]; then
oldref="HEAD"
fi
# Get list of files to look at using git diff
for file in $($GITCMD diff --stat --name-only --diff-filter=ACMRT ${oldref}..${newref}); do
# Get the size of this file
size=$($GITCMD cat-file -s ${newref}:${file})
# Check to see if for some reason we didn't get a size
if [ ! -z ${size} ]; then
# Compare filesize to MAXSIZE
if [ "${size}" -gt "${MAXSIZE}" ]; then
# Send output back to the user about oversized files.
echo "ERROR: ${file} larger than ${MAXSIZE}."
EXIT=1
fi # End size comparison
fi # End check for empty size
done # End list of files
done # End reading stdin
# If we have oversized files, write more information out to the user
if [ "${EXIT}" = "1" ]; then
echo "ERROR: Your commit has been blocked due to certain files being oversized."
echo "ERROR: Check output above for more information."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment