Skip to content

Instantly share code, notes, and snippets.

@1000ch
Forked from arnaud-lb/pre-commit.sh
Last active October 25, 2016 06:38
Show Gist options
  • Save 1000ch/f4eb3f1325de2609fe154ff4f19f5b3f to your computer and use it in GitHub Desktop.
Save 1000ch/f4eb3f1325de2609fe154ff4f19f5b3f 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 imgo >/dev/null 2>&1 || {
echo "\033[1mPlease install imgo to reduce images size before commit\033[0m"
echo "Install imgo with the following:"
echo "\t \033[1mnpm install -g imgo\033[0m"
exit 1;
}
for file in `git diff --cached --name-status|awk '$1 ~ /[AM]/ && tolower($2) ~ /\.png$/ {print $2}'`
do
echo "Optimizing $file"
cat $file | imgo --pngquant --zopflipng > ${file%.png}.new
mv -f ${file%.png}.new $file
git add $file
done
for file in `git diff --cached --name-status|awk '$1 ~ /[AM]/ && tolower($2) ~ /\.jpe?g$/ {print $2}'`
do
echo "Optimizing $file"
cat $file | imgo --jpegoptim --jpegRecompress --mozjpeg > ${file%.jpg}.new
mv -f ${file%.jpg}.new $file
git add $file
done
for file in `git diff --cached --name-status|awk '$1 ~ /[AM]/ && tolower($2) ~ /\.gif$/ {print $2}'`
do
echo "Optimizing $file"
cat $file | imgo --gifsicle > ${file%.gif}.new
mv -f ${file%.gif}.new $file
git add $file
done
for file in `git diff --cached --name-status|awk '$1 ~ /[AM]/ && tolower($2) ~ /\.svg$/ {print $2}'`
do
echo "Optimizing $file"
cat $file | imgo --svgo > ${file%.svg}.new
mv -f ${file%.svg}.new $file
git add $file
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment