ざっくり
git-hook
の機能を使って
コミット前になんちゃらするやり方です
やり方
フックしたいプロジェクトのディレクトリまで移動して、
以下を実行してpre-commitファイルを作成
touch .git/hooks/pre-commit
そこに以下を追加
(こちらを丸コピ
http://qiita.com/quattro_4/items/f9e0884f2efe7e8c033a)
#!/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
byebug
logger
puts
binding.pry
save_and_open_page
console.log
}
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 commiting OR use --no-verify".red
exit 1
end
実行権限を付与して
おしまい。
余談
最初、もともとある.git/hooks/pre-commit.sampleをコピーして
もともとの記述をコメントアウトして
上記のコードを追加していたら、
.git/hooks/pre-commit: line 53: class: command not found
というエラーが出てはまった。。。
原因は
pre-commit.sampleの1行目が
#!/bin/sh
になっていて、
52行目に
#!/usr/bin/env ruby
この記述を書いていたのだが、
どうやらこいつは
1行目に書いていないとダメらしいです。。。
「#!」のことを「シェバング(シバン)」(shebang) というらしい。
https://ja.wikipedia.org/wiki/%E3%82%B7%E3%83%90%E3%83%B3_%28Unix%29