Skip to content

Instantly share code, notes, and snippets.

@libert-xyz
Created July 16, 2018 20:59
Show Gist options
  • Select an option

  • Save libert-xyz/9d8146da675ead0c950407c9a47fbe25 to your computer and use it in GitHub Desktop.

Select an option

Save libert-xyz/9d8146da675ead0c950407c9a47fbe25 to your computer and use it in GitHub Desktop.
pre-commit
#!/bin/sh
# to use, copy this file into your .git/hooks folder in your git repo
# Rename the file to: pre-commit
# make sure to add execute permissions using: chmod +x .git/hooks/pre-commit
command -v pngcrush >/dev/null 2>&1 || {
echo "\033[1mPlease install pngcrush to reduce images size before commit\033[0m"
echo "Install pngcrush with the following:"
echo "\t \033[1mbrew install pngcrush\033[0m"
echo "OR"
echo "\t Site: \033[1m\033[4mhttp://pmt.sourceforge.net/pngcrush/\033[0m"
echo "\t Download: \033[1m\033[4mhttp://sourceforge.net/projects/pmt/files/\033[0m"
echo "\t Installation Steps: \033[1m\033[4mhttp://www.mactricksandtips.com/2012/02/installing-and-using-pngcrush-on-your-mac.html\033[0m"
exit 1;
}
command -v jpegtran >/dev/null 2>&1 || {
echo "\033[1mPlease install jpegtran to reduce images size before commit\033[0m"
echo "Install jpegtran with the following:"
echo "\t \033[1mbrew install jpeg\033[0m"
echo "OR"
echo "\t Site: \033[1m\033[4mhttp://jpegclub.org/jpegtran/\033[0m"
echo "\t Download: \033[1m\033[4mhttp://www.ijg.org/files/\033[0m (get the latest tar.gz)"
echo "\t Installation Steps: \033[1m\033[4mhttp://www.phpied.com/installing-jpegtran-mac-unix-linux/\033[0m"
echo "\n"
echo "If the following error is produced when running the \033[1mjpegtran\033[0m commannd"
echo "\t\033[1mjpegtran: error while loading shared libraries: libjpeg.so.8: cannot open shared object file: No such file or directory\033[0m"
echo "Run the following commands:"
echo "\t\033[1msudo ranlib /usr/local/lib/libjpeg.a\033[0m"
echo "\t\033[1msudo ldconfig /usr/local/lib\033[0m"
exit 1;
}
for file in `git diff --cached --name-only | grep ".png\$"`
do
echo "Crushing $file"
pngcrush -rem alla -brute -reduce $file ${file%.png}.new | grep "filesize"
mv -f ${file%.png}.new $file
done
# jpegtran
for jpg in `git diff --cached --name-only | grep ".jpg\$"`; do
echo "crushing $jpg ..."
jpegtran -copy none -optimize "$jpg" > temp.jpg
# preserve original on error
if [ $? = 0 ]; then
mv -f temp.jpg $jpg
else
rm temp.jpg
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment