-
-
Save moughamir/d44695cd3992f456036e9c8619954af5 to your computer and use it in GitHub Desktop.
Git pre-commit hook for image optimization.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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