Skip to content

Instantly share code, notes, and snippets.

@gsmendoza
Created October 5, 2013 07:13
Show Gist options
  • Save gsmendoza/6837718 to your computer and use it in GitHub Desktop.
Save gsmendoza/6837718 to your computer and use it in GitHub Desktop.
Git pre-commit hook for image optimization.
#!/usr/bin/env ruby
require 'image_optim'
staged_files = `git diff --cached --name-only --diff-filter=ACM`.split("\n")
staged_files.select! { |f| f =~ %r{/images/} }
if staged_files.any?
image_optim = ImageOptim.new(pngout: false)
begin
optimized_images =
image_optim.optimize_images!(staged_files) do |filename, was_optimized|
filename if was_optimized
end
optimized_images.compact!
rescue ImageOptim::BinNotFoundError => e
puts "#{e.message}.
This binary is a dependency of image_optim.
See https://github.com/toy/image_optim for more info.".gsub(/\s+/, " ")
exit 1
end
if optimized_images.any?
puts "Optimized #{optimized_images.map(&:to_s).inspect}.
Please add them to your commit.".gsub(/\s+/, " ")
exit 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment