DEV Community

Jayesh Mann
Jayesh Mann

Posted on

Pre-Commit hooks guide for Ruby

Note: This guide assumes you are using rubocop for linting and RSpec for testing

Git hook scripts are useful for identifying simple issues before submission to code review. We run our hooks on every commit to automatically point out issues in code such as missing semicolons, trailing whitespace, and debug statements. By pointing these issues out before code review, this allows a code reviewer to focus on the architecture of a change while not wasting time with trivial style nitpicks.

Installation

Using homebrew:

brew install pre-commit
Enter fullscreen mode Exit fullscreen mode

Configuration

create a file named .pre-commit-config.yaml in your project root.

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.3.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
  - repo: https://github.com/mattlqx/pre-commit-ruby
    rev: v1.3.5
    hooks:
      - id: rubocop
      - id: rspec
Enter fullscreen mode Exit fullscreen mode

Updated configuration & discussion thread is maintained on GitHub.

Install git hooks

pre-commit install
Enter fullscreen mode Exit fullscreen mode

now pre-commit will run automatically on git commit

Run against all files

Pre-Commit will only run on changed files during git commit.

In case you would like to run it on all the files:

pre-commit run --all-files
Enter fullscreen mode Exit fullscreen mode

Feel free to config pre-commit according to your requirements.

Supported hooks

Official Website

Official Demo


License: AGPL-3.0-or-later

Top comments (0)