-
-
Save wzxjohn/0732511c172357d44e6c to your computer and use it in GitHub Desktop.
commit size pre-receive hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
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.