We always want to speed up our workflows, right? Of course, automation is the best way to do that.
Git hooks are scripts that Git executes before or after events such as: commit, push, and receive.
In my words, Hooks is like an event trigger, After you did some git operation it'll trigger some events.
There are two types of git Hooks.
1.) Client side (Local PC)
2.) Server side (Remote Server)
#!/bin/sh => For shell Scripts
#!/usr/bin/env python => For Python
#!/usr/bin/env ruby => For Ruby
applypatch-msg
pre-applypatch
post-applypatch
pre-commit
prepare-commit-msg
commit-msg
post-commit
pre-rebase
post-receive
pre-commit
prepare-commit-msg
commit-msg
post-commit
diff_files = %x(git diff --cached --name-only)
rails_result = `grep -rlns "binding.pry" --exclude-dir=".git" #{diff_files}`
if rails_result != ""
puts 'You have added binding.pry in your files'.colorize(:red)
puts rails_result
exit 1
end
failures = %x(rspec spec/ |grep "examples"| grep "failure"| awk {'print $3'}).to_i
if failures!=0
puts 'Your modified files doesnot passed the test'.colorize(:red)
exit 1
else
puts 'All test passed'.colorize(:green)
exit 0
end