Skip to content

Instantly share code, notes, and snippets.

@nickjacob
Last active April 11, 2022 22:06
Show Gist options
  • Save nickjacob/5a0c56ba451a6892ff14 to your computer and use it in GitHub Desktop.
Save nickjacob/5a0c56ba451a6892ff14 to your computer and use it in GitHub Desktop.
Pre-commit hook to check for extra-large files that you probably shouldn't have in version control
#!/usr/bin/env bash
echo "================================================================================"
echo " PRE-COMMIT: CHECKING FILE SIZES"
echo "================================================================================"
# constant that you want to check sizes against
SIZE_LIMIT=$(( 1048576 * 64 ))
# $1 - filetype
function invalid_filetype() {
local filetype="$1"
local filename="$2"
echo -e "[NO!]"
echo -e "\t.$filetype files are not allowed."
echo -e "\tplease git reset HEAD $filename and try again"
exit 69;
}
invalid_filetypes="pyc|zip|jpg|png"
for file in $(git status --porcelain -uno); do
if [ ${#file} -gt 1 -a -f "$file" ]; then
__filename=$(basename $file)
__extension="${__filename##*.}"
echo -en "- checking $file "
if [[ $__extension =~ $invalid_filetypes ]]; then
invalid_filetype "$__extension" "$file"
fi
__ln=( $( ls -Lon "$file" ) )
__size=${__ln[3]}
if [ $__size -lt $SIZE_LIMIT ]; then
echo -e " [OK!]"
else
echo -e " [NO!]"
echo -e "$file is larger than 64mb. please don't add to repo"
exit 2;
fi
fi
done
echo "================================================================================"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment