Skip to content

Instantly share code, notes, and snippets.

@libo
Created October 18, 2012 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save libo/3910539 to your computer and use it in GitHub Desktop.
Save libo/3910539 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 = []
HASROCKET_PATTERN = ':.*=>'
# 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`.split("\n")
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
@bquorning
Copy link

It's ok to use hashrockets for a symbolized string, i.e. :'foo-bar' => 42. You can fix the pattern to:

HASHROCKET_PATTERN = ":[@$_A-Za-z][_A-Za-z0-9]*[=!?]? *=> *"

Symbols can generally contain letters and digits, except: They can start with one of @$ but not digits, and they can end with one of =?!.

To make the grep work now, it need the -E flag, which works on both 10.7 and 10.8.

In line 11, you don't need the argument to split.

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