Skip to content

Instantly share code, notes, and snippets.

@amacneil
Created June 13, 2016 00:28
Show Gist options
  • Save amacneil/c572c7560399da26eb7c5c99bdb83fc6 to your computer and use it in GitHub Desktop.
Save amacneil/c572c7560399da26eb7c5c99bdb83fc6 to your computer and use it in GitHub Desktop.
Pre-receive hook to limit maximum git file size (attempt 2 - checks all objects but is slow)
#!/bin/bash -e
nullsha="0000000000000000000000000000000000000000"
maxsize=5
maxbytes=$(( $maxsize * 1024 * 1024 ))
status=0
# Read stdin for ref information
while read oldref newref refname; do
echo ref: $oldref $newref $refname
# Skip branch deletions
if [ "$newref" = "$nullsha" ]; then
continue
fi
# Set oldref to HEAD if this is branch creation
if [ "$oldref" = "$nullsha" ]; then
oldref="HEAD"
fi
# Loop through commits
for commit in $(git log --format=%H ${oldref}..${newref}); do
echo commit: $commit
# Inspect objects in commit
for file in $(git ls-tree -l -r $commit | awk -v maxbytes="$maxbytes" '$4 > maxbytes { print $5 }'); do
# Display error header if this is the first offending file
if [ "$status" -eq "0" ]; then
status=1
echo ""
echo "---------------------------------------------------------------------------"
echo "Your push was rejected because it contains files larger than $maxsize MB."
echo "Please discuss with the infra team the best place to store these files."
echo "You might want to consider using https://git-lfs.github.com/ instead."
echo "---------------------------------------------------------------------------"
echo
echo "Offending files:"
fi
echo " - $file"
done
done
done
if [ "$status" -ne "0" ]; then echo; fi
exit $status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment