Created
October 18, 2012 08:53
-
-
Save libo/3910539 to your computer and use it in GitHub Desktop.
No hashrocket pre-commit
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 | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's ok to use hashrockets for a symbolized string, i.e.
:'foo-bar' => 42
. You can fix the pattern to: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
.