Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wzxjohn
Forked from gmaccarone/pre-receive
Last active July 11, 2017 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wzxjohn/0732511c172357d44e6c to your computer and use it in GitHub Desktop.
Save wzxjohn/0732511c172357d44e6c 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."
@stroucki
Copy link

stroucki commented Oct 5, 2016

The script is missing a fi for grammar, and at the end it should exit with $EXIT.
It doesn't prevent huge files ending in the repository if the sender has removed them on his copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment