Skip to content

Instantly share code, notes, and snippets.

@mmuruev
Created July 1, 2016 14:11
Show Gist options
  • Save mmuruev/3513d2cb5e3cea8124d4a99f1e94a46c to your computer and use it in GitHub Desktop.
Save mmuruev/3513d2cb5e3cea8124d4a99f1e94a46c to your computer and use it in GitHub Desktop.
Limit commits in git local repo to 5mb for file. (Preventing accidental commits DB_DUMP.sql fiels and so on...)
#!/usr/bin/env bash
# make it chmod a+x ./git/hooks/pre-commit
GITCMD=$(which git) # "/usr/bin/git"
NULLSHA="0000000000000000000000000000000000000000"
MAXSIZE="5242880" # 5MB limit on file size
EXIT=0
echo 'START PRECOMMIT CHECKING...'
# Get list of files to look at using git diff
for file in $($GITCMD diff --cached --name-only --diff-filter=ACMRT); do
# Get the size of this file
size=$($GITCMD cat-file -s ${newref}:${file})
#echo "INFO: ${file} has size ${size}."
# 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 -e "\033[31m${file}\033[0m larger than ${MAXSIZE}" 1>&2
EXIT=1
fi # End size comparison
fi # End check for empty size
done # End list of files
# If we have oversized files, write more information out to the user
if [ "${EXIT}" = "1" ]; then
echo -e "ERROR: Your commit has been blocked due to certain files being oversized."
echo -e "ERROR: Check output above for more information."
fi
exit ${EXIT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment