Skip to content

Instantly share code, notes, and snippets.

@parndt
Forked from ideasasylum/pre-commit
Last active December 16, 2015 11:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save parndt/5431107 to your computer and use it in GitHub Desktop.
Save parndt/5431107 to your computer and use it in GitHub Desktop.
Place this in ~/.git_template/hooks/pre-commit and chmod to 755. Now, ensure you have git >= 1.7.1 and run: git config --global init.templatedir '~/.git_template' Back in your repository, run git init again and the hook will appear.
#!/usr/bin/env ruby
spec_hits = []
checks = {
'_spec\.rb$' => ['focus:[:space:]*true'],
'\.rb$' => ['binding\.pry', 'debugger']
}
# 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|
# Perform special checks for _spec filenames (rspec tests)
checks.each do |filename_pattern, patterns|
if filename.match filename_pattern
patterns.each do |contents_pattern|
results = `git diff --cached "#{filename}" | grep "^\+[^+]" | grep "#{contents_pattern}"`.split("\n").map { |r| r.sub(/^\+[\s\t]*/, '') }
if $? == 0
# Add the relevant change with line number to the spec_hits array
results.each do |result|
line = `grep -n '#{result}' #{filename}`.sub(/:\s+/, ' ').chomp
spec_hits.push "#{filename}:" + line
end
end
end
end
end
end
if spec_hits.any?
puts "\e[33m>>> Oops! You forgot something:\e[0m"
puts spec_hits.join("\n")
end
exit 1 if spec_hits.any?
@parndt
Copy link
Author

parndt commented May 21, 2013

$ git commit -m "Test comments.”
>>> Oops! You forgot something:
spec/features/comment_spec.rb:9 binding.pry

@twe4ked
Copy link

twe4ked commented May 21, 2013

@parndt: Any plans on moving this to a repo so it can be contributed back to?

@twe4ked
Copy link

twe4ked commented May 21, 2013

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