A framework to manage git hooks with your repository. Codes in *_githooks.rb will be executed by the framework.
Githooks will look for files named as filename_githooks.rb under githooks folder in your repository. The typical directory structure with Githooks will look like this,
/Your repository
/lib
/app
...
/githooks
example_githooks.rb
example2_githooks.rb
Add this line to your application's Gemfile:
gem 'githooks'
And then execute:
$ bundle
Or install it yourself (recommended if you use bundle install --path vendor/bundle
):
$ gem install githooks
After the gem is installed, you need to initialize githooks in your repository.
$ githooks --init
Following key words can be used in your filename_githooks.rb files
- applypatch_msg
- pre_applypatch
- post_applypatch
- pre_commit
- prepare_commit_msg
- commit_msg
- post_commit
- pre_rebase
- post_checkout
- post_merge
- pre_receive
- post_receive
- update
- post_update
- pre_auto_gc
- post_rewrite
The framework enable you to group different git hooks in one file and manage hooks based on their functionalities. Arguments for a specific hook is yielded to its corresponding marco. For example, you can access commit file through commit_msg { |args| p args[0] }
# dummy_githooks.rb
pre_commit do
puts "executed in pre commit hook"
end
commit_msg do |args|
git_root = `git rev-parse --show-toplevel`.chop
message_file = args[0]
message = File.read(git_root+'/'+message_file)
$regex = /\[ref: (\d+)\]/
if !$regex.match(message)
puts "[POLICY] Your message is not formatted correctly"
exit 1
end
end
The example hook checks whether the commit message follows the provided pattern.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
- Provide git related information in the environment
- Share context across hooks