In this article, we will set up GitHub pre-commit hook using the pre-commit gem in Ruby On Rails, and we will use Git Hooks to run Rubocop, Brakeman, and Rspec.
Pre-commit hook runs before typing the commit message. It’s primarily being used for linting or running tests to make sure everything is working. Pre-commit hooks are very helpful in maintaining the coding standards and are productive also as many of the suggested lint changes can be fixed automatically. It plays an essential role when multiple people are working on the same repo. Everyone will have their own way of writing the code and formatting it. But as a project, you can’t afford to have too many different formattings. By using pre-commit hooks, a team can make sure everything goes to the repo passed the checks and balances that are put in place.
For the pre-commit hook, we will be using the pre-commit gem
Install the pre-commit gem ( https://github.com/jish/pre-commit )
$ gem install pre-commit
Use the pre-commit command to generate a stub pre-commit hook
# In your git repo $ pre-commit install
This creates a .git/hooks/pre-commit script which will check your git config and run checks that are enabled.
If you want to use Bundler to specify a version of RuboCop, add the following to
group :development do gem "pre-commit", require: false gem "rubocop", require: false end
And run the following to run
pre-commit via Bundler:
$ git config pre-commit.ruby "bundle exec ruby"
If you are using rvm you need to install pre-commit into the
default gemset, because it does not use the
$ rvm default do gem install pre-commit
Alternatively, you can configure pre-commit to use the
current rvm gemset
$ git config pre-commit.ruby "rvm `rvm current` do ruby" # OR: $ git config pre-commit.ruby `rvm wrapper current show ruby` # available in RVM 1.26.12
Here we are creating a config file to manage our pre-commit checks.
pre-commit comes with 4 configuration providers:
default- basic settings, read-only
git- reads configuration from
git config pre-commit.*, allow local update
yaml- reads configuration from
env- reads configuration from environment variables
default available checks list. you can add according to your requirement.
before_all, ci, coffeelint, common, console_log, csslint, debugger, gemfile_path, go, go_build, go_fmt, jshint, jslint, json, local, merge_conflict, migration, nb_space, pry, rails, rspec_focus, rubocop, ruby, ruby_symbol_hashrockets, scss_lint, tabs, whitespace, yaml
Use pre-commit list to see the list of default and enabled checks and warnings.
If we are trying to commit with incorrect format code or debugger present in code then it will stop commit and display something like the below image.
For more information please check gem.
We will create a custom script according to the requirements. Here we are creating for RSpec and Brakeman.
Let’s start by creating a
scripts directory in your app’s root folder:
$ cd rails-app $ mkdir scripts
Now we will create three different bash files for each command we want to run, in our case, it's Brakeman and Rspec. Below are files you can use and put inside the scripts folder:
Now, let’s create or edit
pre-commit bash files which we will symlink into the
.git folder where the hooks reside:
Next, we want to write a bash script to create the symlink between these files and the hooks:
All the files which we need are created, but before we run the install-hooks bash file to install our hooks, we need to make these files executables. This is done with the following command:
$ chmod +x scripts/*.bash
Now we are ready to run
$ ./scripts/install-hooks.bash Installing hooks... Done!
And you’re all set! Try committing and pushing your code. Before committing, Brakeman and Rspec will run.
If this guide has been helpful to you and your team please share it with others!