Skip to content

Instantly share code, notes, and snippets.

@handleman
Created June 8, 2020 08:37
Show Gist options
  • Save handleman/afe370c4abe33f20da8df57f4b3ffadc to your computer and use it in GitHub Desktop.
Save handleman/afe370c4abe33f20da8df57f4b3ffadc to your computer and use it in GitHub Desktop.
pre-commit hook to check size of the image, to show list of it and prompt user to continue
#!/usr/bin/env bash
declare -r MAX_IMAGE_SIZE_MB=1
declare -r CURRENT_DIR="$(pwd)"
declare -r IMAGES_MASK=".*\.(webp|png|jpg|jpeg|gif)$"
HAS_ERROR=""
OIFS="$IFS"
IFS=$'\n'
for file in $(git diff --cached --name-only | uniq | grep -E $IMAGES_MASK); do
file_size=$(du -m $CURRENT_DIR/$file | awk '{print $1}')
if [ "$file_size" -gt $MAX_IMAGE_SIZE_MB ]; then
echo "WARNING: $file is over $MAX_IMAGE_SIZE_MB MB."
HAS_ERROR="1"
fi
done
IFS="$OIFS"
if [ "$HAS_ERROR" != "" ]; then
exec < /dev/tty
while true; do
read -p "[PRE-COMMIT HOOK]: WARNING: some files exceeds file size limit.\n Do you want to proceed? (Y/n) " yn
if [ "$yn" = "" ]; then
yn='Y'
fi
case $yn in
[Yy] ) exit 0;;
[Nn] ) exit 1;;
esac
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment