Skip to content

Instantly share code, notes, and snippets.

@patrickvossler18
Created August 2, 2018 02:22
Show Gist options
  • Save patrickvossler18/2ff326362ed1e5d10f8fd6268a50db52 to your computer and use it in GitHub Desktop.
Save patrickvossler18/2ff326362ed1e5d10f8fd6268a50db52 to your computer and use it in GitHub Desktop.
#!/bin/bash -eu
# Taken from https://github.com/amacneil/git-banish-large-files/blob/master/pre-receive.sh
# The difference here is that we are doing a pre-push hook on the local side instead of a pre-receive hook like in the above link.
# As a result, we want to read different variables from stdin than in the other script.
set -o pipefail
maxsize=100
maxbytes=$(( $maxsize * 1024 * 1024 ))
status=0
# Read stdin for ref information
while read local_ref local_sha remote_ref remote_sha;do
# Find large objects in our local commit. Not pretty but it works.
for file in $(git rev-list --objects ${local_sha} | \
git cat-file --batch-check='%(objectname) %(objecttype) %(objectsize) %(rest)' | \
awk -v maxbytes="$maxbytes" '$3 > maxbytes { print $4 }'); 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 "-------------------------------------------------------------------------"
echo
echo "Offending files:"
fi
echo " - $file"
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