OOZOU
與我們聯繫
Back to TIL
ruby

Run rubocop before commit

October 13, 2020

Introduction

Git hooks are super powerful for automating tasks and enforcing coding standards in a Git repository. One common use case for Git hooks is to run automated tests and code analysis tools before allowing a commit to proceed. In this article we’ll cover pre-commit hooks, how to skip them and how to set up a pre-commit hook to run Rubocop, a Ruby code analysis tool.

What is Rubocop?

Rubocop is a static code analysis tool for Ruby that checks code for style and security issues. It’s a popular tool among Ruby developers to ensure their code follows the Ruby style guide and best practices. Rubocop can be run manually but integrating it with a pre-commit hook ensures code is checked automatically before each commit.

Copy this into .git/hooks/pre-commit (remove .sample)

#!/usr/bin/env ruby

require 'english'
require 'rubocop'

ADDED_OR_MODIFIED = /A|AM|^M/.freeze

changed_files = `git status --porcelain`.split(/\n/).
    select { |file_name_with_status|
      file_name_with_status =~ ADDED_OR_MODIFIED
    }.
    map { |file_name_with_status|
      file_name_with_status.split(' ')[1]
    }.
    select { |file_name|
      File.extname(file_name) == '.rb'
    }.join(' ')

system("rubocop #{changed_files}") unless changed_files.empty?

exit $CHILD_STATUS.exitstatus

Make it executable

$ chmod +x .git/hooks/pre-commit

You're good to go, from now it'd run rubocop against your changes and prevents from commiting unless you fix the offences.

How to skip pre-commit? just pass -n to git commit

$ git commit -n -m "[hotfix] blah blah"

What is a Pre-Commit Hook?

A pre-commit hook is a client-side Git hook that runs before a commit is created. It’s a script that can do various things like run automated tests, check code style or enforce coding standards. If the pre-commit hook fails the commit is aborted and you have to fix the issues before committing again.

Why Run Rubocop Before Commit

Running Rubocop before commit has several advantages:

  • Code quality: Rubocop checks code for style and security issues so the codebase is consistent and secure.
  • Time saver: By running Rubocop automatically before commit you can catch issues early and avoid wasting time debugging later.
  • Collaboration: By enforcing coding standards pre-commit hooks promote collaboration and consistency among team members.

How to set up a pre-commit hook

  1. Install Rubocop and the pre-commit hook tool of your choice (e.g., Husky or Pre-commit).
  2. Create a new file in the .git/hooks directory called pre-commit.
  3. Add the Rubocop command to the pre-commit file, e.g., rubocop -a.
  4. Make the pre-commit file executable by running chmod +x .git/hooks/pre-commit.
  5. Test the pre-commit hook by running git commit -m “Test commit”

Note: If you want to skip the pre-commit hook temporarily you can use the –no-verify option with the git commit command, e.g., git commit -m “Test commit” –no-verify.

More on ruby

rubyApril 30, 2021

Allow pasting multiline ruby blocks in Pry when using leading dot notation

We use leading dot notation a lot now e.g.

rubyOctober 14, 2020

Heredoc without interpolation

Sometimes you want to use Ruby's Heredoc without interpolation ('#{....}'). You can do this by adding single dashes around your keyword:

rubyOctober 13, 2020

Automagical Intermediate-level Hashtables in Ruby

All intermediate-level hashtables will be created automagically

有專案構想嗎?

我們很樂意聆聽您正在打造的產品。

開始對話hello@oozou.com
OOZOU Logo

曼谷 · 新加坡 · 香港

X
Facebook
Instagram
LinkedIn

服務

  • Web 開發
  • AI 與生成式 AI 解決方案
  • 行動應用程式開發
  • 資料分析與工程
  • UI/UX 設計與產品設計
  • 數位轉型

公司

  • 關於我們
  • 職涯
  • 部落格
  • 白皮書
  • 案例研究
  • Today I Learned
  • 聯繫我們

更多

  • 產業
  • 索取報價
  • 合作夥伴網絡
© 2026 OOZOU. 版權所有。
隱私政策行為準則反賄賂反貪腐政策