Skip to content

Instantly share code, notes, and snippets.

@kdonovan
Created November 2, 2010 22:45
Show Gist options
  • Save kdonovan/660448 to your computer and use it in GitHub Desktop.
Save kdonovan/660448 to your computer and use it in GitHub Desktop.
Git pre-commit hook to automatically run Jammit when assets are committed and add the processed files to the commit as well.
#!/usr/bin/env ruby
# This pre-commit hook automatically processes changed assets with Jammit
# and adds the changed files to the commit.
RAILS_ROOT = File.expand_path('.')
ASSET_DIR = File.join(RAILS_ROOT, 'public', 'assets')
status_lines = `git diff-index --cached HEAD`
files_committing = status_lines.map{|l| l.split(/\t/).last.strip }
unless files_committing.any?{|f| f['public/images/embed'] || f['public/stylesheets'] || f['public/javascripts'] || f['public/assets']}
puts "\t* None of the changes being committed are asset files"
else
puts "\t* About to commit changes to asset files."
puts "\t* Processing with Jammit and adding output to commit before continuing..."
# Spinner - based on http://snippets.dzone.com/posts/show/3760
cr = "\r" # move cursor to beginning of line
clear = "\e[0K" # ANSI escape code to clear line from cursor to end of line
reset = cr + clear # reset lines
chars = [ "|", "/", "-", "\\" ]
color = 32 # green
str = "#{reset}\e[#{color};1m"
worker = Thread.new do
`cd #{RAILS_ROOT} && jammit\n`
`git add #{ASSET_DIR}`
end
i = 0
while worker.alive? do
i += 1
char = chars[ i % chars.length ]
print "#{str}\t#{char}"
sleep(0.2)
$stdout.flush
end
print reset
puts "\tDone!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment