Automating code checks with overcommit
Tags: git, hooks, overcommit, ruby
Published: 2018-12-30 12:22:21

---
title: Automating code checks with overcommit
published: true
description: Learn how to easily automate code checks using overcommit
tags: git, hooks, overcommit, ruby
cover_image: https://camo.githubusercontent.com/1968863cd4be5843e9e9b2ba33f0e54059035958/68747470733a2f2f627269676164652e6769746875622e696f2f6f766572636f6d6d69742f6f766572636f6d6d69742e676966
---

A few months ago, I was looking for something to prevent me to commit/push code with a debugger call, `byebug` for example. Then I found something that I didn't know at the moment: [Git Hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). Git Hooks allow running custom scripts when certain actions occur, for example: commit, push, merge, checkout, etc.

You can put whatever you want in these custom scripts, including to check if you did something *not so clever*, for example, push code with a `byebug` call. And if you search in Google, you'll find a couple of Git hook scripts to prevent you from doing that.

But then, as a Ruby developer, I thought: *Hey, there's always a gem, right!?*. Right! And it's called [overcommit](https://github.com/brigade/overcommit). Overcommit is a Ruby gem which allows you to easily manage and configure your Git hooks.

To start using overcommit is very simple:

```
$ gem install overcommit
$ overcommit --install
```

The `overcommit --install` command will create a `.overcommit.yml` settings file in the current folder and also back up existing hooks. The [default settings file](https://github.com/brigade/overcommit/blob/master/config/default.yml) includes the following hooks:

- **CapitalizedSubject**: Ensures commit message subject lines start with a capital letter
- **EmptyMessage**: Checks that the commit message is not empty
- **SingleLineSubject**: Ensures commit message subject lines are followed by a blank line
- **TextWidth**: Ensures the number of columns the subject and commit message lines occupy is under the preferred limits
- **TrailingPeriod**: Ensures commit message subject lines do not have a trailing period
- **AuthorEmail**: Checks the format of an author's email address
- **AuthorName**: Ensures that a commit author has a name with at least first and last names
- **BrokenSymlinks**: Checks for broken symlinks
- **CaseConflicts**: Checks for files that would conflict in case-insensitive filesystems

Of course, you can easily disable any of these hooks:

```
  CapitalizedSubject:
    enabled: false
```

Every time you perform changes in the settings file, you also need to update the signature running `overcommit --sign`. You can also disable signature checking by setting `verify_signatures: false` in your `.overcommit.yml` file. However, please [read the security implications](https://github.com/brigade/overcommit#security) before doing that.

# Recommended built-in hooks

The hooks described in this section are the ones I'm currently using and recommend. You can also find, between parenthesis in the hook title, in which action the hooks are being executed.

## [RuboCop](https://docs.rubocop.org/) (PreCommit)

Runs RuboCop against any modified Ruby files.

```
  RuboCop:
    enabled: true
    on_warn: fail
    problem_on_unmodified_line: ignore
    command: ['bundle', 'exec', 'rubocop']
```

- `on_warn: fail`: blocks the commit in case of warnings
- `problem_on_unmodified_line: ignore`: ignore problems in lines of code you did not add/change

## [BundleAudit](https://github.com/rubysec/bundler-audit) (PreCommit)

Checks for vulnerable versions of gems in Gemfile.lock.

```
  BundleAudit:
    enabled: true
    flags: ['--update']
```

- `flags: ['--update']`: force BundleAudit to update before checking

## [BundleCheck](https://github.com/brigade/overcommit/blob/master/lib/overcommit/hook/pre_commit/bundle_check.rb) (PreCommit)

Check if local Gemfile.lock matches Gemfile when either changes, unless `Gemfile.lock` is ignored by git.

```
  BundleCheck:
    enabled: true
```

## [ForbiddenBranches](https://github.com/brigade/overcommit/blob/master/lib/overcommit/hook/pre_commit/forbidden_branches.rb) (PreCommit and PrePush)

Prevents commits to branches matching one of the configured patterns.

```
  ForbiddenBranches:
    enabled: true
    branch_patterns: ['master']
```

## [Fasterer](https://github.com/DamirSvrtan/fasterer) (PreCommit)

Runs `fasterer` against any modified Ruby files.

```
  Fasterer:
    enabled: true
    exclude:
      - 'vendor/**/*.rb'
      - 'db/schema.rb'
```

## CustomScript (PreCommit)

Runs a custom command/script before commit.

```
  CustomScript:
    quiet: true
    enabled: true
    command: ['i18n-tasks', 'normalize']
```

The previous hook will run `i18n-tasks normalize` before commit. `normalize` is a task of the [i18n-tasks](https://github.com/glebm/i18n-tasks) gem.

## [BundleInstall](https://github.com/brigade/overcommit/blob/master/lib/overcommit/hook/post_checkout/bundle_install.rb) (PostCheckout, PostMerge and PostRewrite)

Runs `bundle install` when a change is detected in the repository's dependencies.

```
  BundleInstall:
    enabled: true
```

# Other (probably) useful built-in hooks

These are the hooks I'm not currently using but I'm willing to give a try.

- [Foodcritic](http://www.foodcritic.io): linting tool for writing better and safer code
- [RailsBestPractices](https://github.com/railsbp/rails_best_practices): code metric tool to check the quality of Rails code
- [RailsSchemaUpToDate](https://github.com/brigade/overcommit/blob/master/lib/overcommit/hook/pre_commit/rails_schema_up_to_date.rb): Check to see whether the schema file is in line with the migrations
- [Reek](https://github.com/troessner/reek): Code smell detector for Ruby
- [Brakeman](http://brakemanscanner.org/): Vulnerability scanner designed for Rails applications. It statically analyzes code to find security issues
- [FixMe](https://github.com/brigade/overcommit/blob/master/lib/overcommit/hook/pre_commit/fix_me.rb): Check for "token" (`TODO`, `FIXME`, etc.) strings before commit
- [BundleOutdated](http://bundler.io/bundle_outdated.html): List installed gems with newer versions available

# Looking for a job?

If you're looking for a job and you live around Frankfurt am Main or you're willing to move, we have positions available at [creditshelf](https://creditshelf.com). **All positions available for English speakers**.

- [Ruby on Rails Developer](https://creditshelf-jobs.personio.de/job/26809)
- [DevOps Engineer](https://creditshelf-jobs.personio.de/job/83994)
- [Frontend Developer](https://creditshelf-jobs.personio.de/job/99614)
- [Software Quality Engineer](https://creditshelf-jobs.personio.de/job/99754)
- [Python Developer](https://creditshelf-jobs.personio.de/job/79886)
- [Fullstack Java/Python Developer](https://creditshelf-jobs.personio.de/job/79886)