Last active
August 29, 2015 14:26
-
-
Save gcfunk/3471e945584548803b5c to your computer and use it in GitHub Desktop.
pre commit hook - save to .git/hooks/pre-commit
This file contains hidden or 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 | |
| class String | |
| # colorization | |
| # from http://stackoverflow.com/questions/1489183/colorized-ruby-output | |
| def colorize(color_code) | |
| "\e[#{color_code}m#{self}\e[0m" | |
| end | |
| def red ; colorize(31) ; end | |
| def green ; colorize(32) ; end | |
| def yellow ; colorize(33) ; end | |
| def pink ; colorize(35) ; end | |
| end | |
| checks = %w{ | |
| debugger | |
| logger | |
| puts | |
| binding.pry | |
| save_and_open_page | |
| console.log | |
| iit | |
| ddescribe | |
| } | |
| errors = [] | |
| files_changed = `git diff --cached --name-only HEAD` | |
| files_changed.each_line do |filename| | |
| filename.chomp! | |
| changes = `git diff --cached -U0 HEAD -- "#{filename}"` | |
| checks.each do |check| | |
| result = changes.split(/\n/).grep(/^\+.*\b#{check}\b/) | |
| unless result.empty? | |
| errors << {:name => check, :file => filename, :matches => result} | |
| end | |
| end | |
| end | |
| unless errors.empty? | |
| errors.each do |error| | |
| puts "'#{error[:name]}' found in file: #{error[:file]}".yellow | |
| error[:matches].each {|m| puts " -> #{m}" } | |
| end | |
| puts "COMMIT REJECTED. Please remove them before committing OR use --no-verify".red | |
| exit 1 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment