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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay