Skip to content

Instantly share code, notes, and snippets.

@yangsu
Created August 2, 2012 18:20
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yangsu/3239343 to your computer and use it in GitHub Desktop.
Save yangsu/3239343 to your computer and use it in GitHub Desktop.
pre-commit git hook for crushing images
#!/bin/sh
# to use, save this file as .git/hooks/pre-commit in your git repo
# 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
for file in `git diff --cached --name-only | grep ".jpg\$"`
do
echo "Crushing $file"
jpegtran -copy none -optimize -perfect -outfile $file ${file%.png}.new | grep "filesize"
mv -f ${file%.png}.new $file
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment