Created
June 13, 2016 00:27
-
-
Save amacneil/f25c7501cbc4cbf70e4b7f458a91679d to your computer and use it in GitHub Desktop.
Pre-receive hook to limit maximum git file size (attempt 1 - only compares diff)
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/bash -e | |
GITCMD="git" | |
NULLSHA="0000000000000000000000000000000000000000" | |
MAXSIZE="10" | |
MAXBYTES=$(( $MAXSIZE * 1024 * 1024 )) | |
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 MAXBYTES | |
if [ "${size}" -gt "${MAXBYTES}" ]; then | |
# Display error header if this is the first file | |
if [ "$EXIT" -eq "0" ]; then | |
EXIT=1 | |
echo "" | |
echo "---------------------------------------------------------------------------" | |
echo "Your push was rejected because it contains files larger than ${MAXSIZE} MB." | |
echo "Please rethink your actions, and consider using https://git-lfs.github.com/" | |
echo "or an external data store." | |
echo "---------------------------------------------------------------------------" | |
echo | |
echo "Offending files:" | |
fi | |
echo " - ${file}" | |
fi | |
fi | |
done | |
done | |
if [ "$EXIT" -ne "0" ]; then echo; fi | |
exit ${EXIT} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment