Оvercommit - convenient Git hook manager

Voldemar Duletskiy, Ruby developer, Evrone, Moscow Ruby Meet-up 6 report
01 August 2017   1129
Ruby

Dynamic, open source programming language with a focus on simplicity and productivity, it has an elegant syntax that is natural to read and easy to write.

Hello Ruby fans! Today we will talk about Overcommit - the convenient management of git-hooks.

Imagine that you have a small project with a micro-team of three or four people, including the manager. Deadline is near, but you cannot lower the quality of the code. You don't wanna use CI and there are no extra money for it.

What is hooks and where are they located?

It is the scripts that are executed at certain event. You can view all existing hooks by running the following command:

ls -la .git/hooks

You will see this listing:

-rwxr-xr-x   1 voldemar  staff  3755 28 Jun 16:39 commit-msg
-rwxr-xr-x   1 voldemar  staff  3755 28 июн 16:39 overcommit-hook
-rwxr-xr-x   1 voldemar  staff  3755 28 Jun 16:39 post-checkout
-rwxr-xr-x   1 voldemar  staff  3755 28 Jun 16:39 post-commit
-rwxr-xr-x   1 voldemar  staff  3755 28 Jun 16:39 post-merge
-rwxr-xr-x   1 voldemar  staff  3755 28 Jun 16:39 post-rewrite
-rwxr-xr-x   1 voldemar  staff  3755 28 Jun 16:39 pre-commit
-rwxr-xr-x   1 voldemar  staff   673  2 Jun 14:37 pre-commit.sh
-rwxr-xr-x   1 voldemar  staff  3755 28 Jun 16:39 pre-push
-rwxr-xr-x   1 voldemar  staff  3755 28 Jun 16:39 pre-rebase

Hook is an executable file, which can content anything, including the Ruby code.

Why it is cool?

  • You can put linters like rubocop to the pre-commit hooks. It will not allow you to commit substandard code
  • You can hang up rspec to the pre-push hooks, and if the tests are dropped - cancel code sending
  • If you are already actively using hooks - you don't need to drag them from one repository to another, they all lie in .ovecommit.yml in a convenient format
RuboCop

Ruby static code analyzer, based on the community Ruby style guide

If you suffer from dispersion and you are sick of messages from your CI that tests have dropped again, or the ruby-cop found a million syntactic violations (or your colleagues in the code-review process). If you are an experienced developer, a good set of hooks seriously discourages young developers and reduces your code-review time. 

Intallation

Gemfile:

gem 'overcommit'

Execude from the consol:

bundle exec overcommit --install

 Now let's edit .overcommit.yml file

PreCommit:
  RuboCop:
    enabled: true
    command: ['bin/bundle', 'exec', 'rubocop', '-R']
    on_warn: fail
  HamlLint:
    enabled: true
    command: ['bin/bundle', 'exec', 'haml-lint', 'app/views/']
    on_warn: fail
  ScssLint:
    enabled: true
    command: ['bin/bundle', 'exec', 'scss-lint']
    include: 'app/assets/**/*.scss'
    on_warn: fail

PrePush:
  RSpec:
    enabled: true

We can see here that the launch of the rubocop scripts is describled in nice forman. haml-linter, sccs-linter and tests runs just before the commit.

In order to enforce hook running this should be executed:

bundle exec overcommit -R

Now, with every attempt to commit something, checks will be performed firstly.

 

Underwater rocks

If there are a lot of tests or they are dropping randomly - if you already have a habit to brew coffee while test runs, it's better to chop the hook responsible for running the tests. If they fall randomly - fix the tests at last, damn it.

Integration with Rubymine - I have serious problems with the integration of Overcommit and Rubymine

Continuous Integration

The practice of merging all developer working copies to a shared mainline several times a day

Overcommit and CI integration - if you still decide to connect CI, then most likely it already provides validation for the code. Often, their rules don't match and you have to adjust the settings, for example Rubocop under CI, or vice versa.

You can skip hooks by running git commit --no-verify

Conclusion

Overcommit is an excellent utility for maintaining a project in good shape for small teams. It will allow you not to grab your head every time after creating a pool of the requester with exclamations "Damn, I forgot to use rubocop again!" or "Damn, all the tests fell!".

Ruby Meditation 21 in Kharkiv

Ruby Meditation 21 will take place in Kharkiv on Sat, April 14, 2018 at 11:00 AM EEST
02 April 2018   1302

At first in history, Ruby Meditation goes to Kharkiv! It will be hot. Join us in the delightful city Kharkiv, April 14!

Interesting talks, live discussion and awesome speakers. Friendly atmosphere, charming professionals and tasty lunch ;)

Planned talks

Tetiana Chupryna

Topic: "We're rewriting everything"

Tetiana Chupryna
Tetiana Chupryna

"Rewrite from scratch” is considered by many developers as an ultimate tool to fix problems with programs. However, what will happens if a team of a booming startup decide to put aside their code base and start with a fresh beginning? This talk is inspired by a true story. A story of success or failure, it’s up to you to decide.
 

Tetiana Chupryna

Ruby developer

Andrey Blazhey

Topic: "Peatio"

Andrey Blazhey
Andrey Blazhey

Create your platform for crypto trading. Features of the development and support of the platform. Connecting popular crypto currency.
 

Andrey Blazhey

Ruby/Node.js Developer

Victor Shepelev

Topic: "A tale of query languages. Is GraphQL The Chosen One?"

Victor Shepelev
Victor Shepelev

Should you switch to GraphQL today or avoid it as long as you can? How is GraphQL similar to guinea pig? Would it become even scarier than SOAP once was? What query languages prefer really huge data sources like OpenStreetMap? Answers to those and other questions, contrived examples, and shameless rants. Be prepared.
 

Victor Shepelev

Ruby developer

Maxim Goncharov

Topic: "Compromise Driven Development: The right way."

Maxim Goncharov
Maxim Goncharov

Every team often get some problems with implementation of selected solutions. In discussions, developers could find some compromise decision. But will this way be right?
 

Maxim Goncharov
Full-stack developer

Sergii Koba

Topic: "Ruby microservices with Docker"

Sergii Koba
Sergii Koba

Docker Compose infrastructure for Microservices. Multistage Docker images build for minimalistic Ruby images. Requests routing and interservice communication. ELK stack for logging microservices with ease.
 

Sergii Koba
Web Team Lead

Sponsors: Svitla, Aejis.

Share your positive experience and best practices on this Ruby Meditation #21. If you have any work issues you cannot solve, our community will gladly help you to find the best way out in live discussion at Lightning talks session. You may also try yourself as a speaker with a short 5-10 mins talk. Please, fill in the form.

Students. who interested in ruby and have a willingness to visit Ruby Meditation will get a discount 50% with promo code 'student'.

If you are a parent of a small baby (0-3) on maternity leave and you want to learn more about ruby development you can get a special discount for a ticket with promo code 'GrowWithYourKid'.

If you have any questions or suggestions, don't hesitate to contact us via cell phone: 099 202 63 08 or by email rubymeditation@gmail.com

Buy a ticket