Skip to content

Instantly share code, notes, and snippets.

@jespr
Forked from libo/hashrocket
Created October 19, 2012 08:04
Show Gist options
  • Save jespr/3916854 to your computer and use it in GitHub Desktop.
Save jespr/3916854 to your computer and use it in GitHub Desktop.
No hashrocket pre-commit
#!/usr/bin/env ruby
# Inspiration from:
# http://jish.github.com/pre-commit/
# https://gist.github.com/2711625
messages = []
HASHROCKET_PATTERN = ":[@$_A-Za-z][_A-Za-z0-9]*[=!?]? *=> *"
# Find the names of all the filenames that have been (A)dded (C)opied or (M)odified
filenames = `git diff --cached --name-only --diff-filter=ACM`
filenames.each do |filename|
# Filter all the additions to this file, find if they contain `:foo => bar
violations = `git diff --cached #{filename} | grep "^\+[^+]" | grep "#{HASROCKET_PATTERN}"`
# and store these lines without the initial `+` and spaces
violations = violations.split("\n").map { |r| r.sub(/^\+[\s\t]*/, '') }
violations.each do |v|
messages.push "#{filename}: " + `grep -n "#{HASROCKET_PATTERN}" #{filename}`.sub(/:\s+/, ' ').chomp
end
end
if messages.any?
puts "\e[33m>>> Please remove hashrockets before committing from:\e[0m"
puts messages.join("\n")
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment