gem 'pronto' and git hooks

I submitted a pull request and walked away thinking I was done. The continuous integration server failed it within two seconds; RuboCop had been added to the project.

There were a few problems.

  1. It told me too late.

  2. RuboCop was added to just this project. What about all the others I work on?

  3. Since it was a large older project most of the cops had been turned off. I could be producing code that could fail the build later on.

So I went looking for a solution that would tell me earlier if the build was going to fail, work with all my projects and cover more 'better' practices without slowing me down.

Pronto

Pronto was almost exactly what I was searching for.

gem install pronto pronto-rubocop

then, checkout and work on a feature branch. When you are ready

pronto run

Pronto will run RuboCop against only the changes that are different to master.

As a bonus it also comes with bunch of other runners making it easy to pick up on other things too.

These are the ones I am currently trialling.

gem install pronto pronto-brakeman \
pronto-jshint pronto-haml pronto-poper \
pronto-rails_best_practices pronto-rails_schema \
pronto-reek pronto-rubocop pronto-scss

Git hooks

Git hooks are executable shell scripts that run at some stage during your git workflow.

They live in your project folder at ./.git/hooks/. For example, ./.git/hooks/pre-commit, runs before a commit, ./.git/hooks/pre-push, runs before a push.

If a git hook exits with a non-zero status the action will be stopped.

Combining both

I wrote a pre-push git hook to run Pronto. It doesn't get in the way when Pronto is not installed (it just reminds you).

#!/bin/sh

# To reinstall this script in the same or another git repo run:
# curl -sSL https://gist.githubusercontent.com/elliotthilaire/cef91754bb296d67e776/raw/pre-push > .git/hooks/pre-push; chmod +x .git/hooks/pre-push

# check that Pronto is installed
hash pronto 2>/dev/null || {
  echo >&2 "Pronto is not installed. Install with 'gem install pronto pronto-rubocop'";
  echo >&2 "Find other Pronto runners at https://github.com/mmozuras/pronto#runners";
  exit 0;
}

pronto run --exit-code

Ignoring a git-hook

So what if Pronto and the hook are preventing you from pushing some thing but you want to make an exception?

git push --no-verify

Using it on multiple projects

I made a one liner to install the hook in existing repositories.

curl -sSL https://gist.githubusercontent.com/elliotthilaire/cef91754bb296d67e776/raw/pre-push > .git/hooks/pre-push; chmod +x .git/hooks/pre-push

There's a git template directory in my dotfiles to add it to any new initialised or cloned repositories.

Why?

There are services out there that will provide feedback on pull requests. I wanted something that provided feedback earlier, was flexible for me, and not tied to a project or a service.

Using Pronto and git hooks help improve the code I am producing across all projects without having to worry about per project setup.

I am informed about better practices that I hadn't considered before, at a time when it is most relevant to me and more likely to stick in my mind.