Skip to content

Instantly share code, notes, and snippets.

@uri
Last active December 14, 2015 19:29
Show Gist options
  • Save uri/a1ea4511da44bad452e6 to your computer and use it in GitHub Desktop.
Save uri/a1ea4511da44bad452e6 to your computer and use it in GitHub Desktop.
A post-commit hook that will check for changed assets and ask if they should be compiled.
#!/bin/bash
ASSET_FILES="$(git diff-index --name-only HEAD~ | egrep '.+\.(?:js|css|jpg|jpeg|png|gif)' | grep -v 'public/')"
if [[ ! -z $ASSET_FILES ]] ; then
echo 'You have changed the following assets that need to be compiled:'
echo $ASSET_FILES
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
read -p "Are you sure? " -r
echo
if [[ $REPLY =~ ^[Yy]$ ]] ; then
echo "Starting compilation..."
RAILS_ENVIRONMENT=production bundle exec rake assets:precompile
RAILS_ENVIRONMENT=production bundle exec rake assets:clean
git add -A
git commit --no-verify -m "Precompiled assets [ci skip]"
fi
fi
@uri
Copy link
Author

uri commented Nov 17, 2015

Install

WARNING: This will overwrite your exist post-commit if you had one.

Go into a project that is using git.

curl  https://gist.githubusercontent.com/uri/a1ea4511da44bad452e6/raw/99e356726626496f6f71805f65f88fcedaea0898/post-commit > .git/hooks/post-commit
chmod +x .git/hooks/post-commit

Uninstall

rm .git/hooks/post-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment