Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby style guide git pre-commit hook using Rubocop as the style guide checker. Only runs on staged ruby files that have been added and/or modified.
#!/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.to_s[-1].to_i
@mkllnk

This comment has been minimized.

Copy link

commented Mar 24, 2016

You can use git diff --diff-filter AM --name-only to list the modified files.
But I would like to show only issues of changed lines, not files.

@danielpowell4

This comment has been minimized.

Copy link

commented Nov 1, 2016

It will go in .git/pre-hooks/pre-commit

Remove .sample from the name

@d-line

This comment has been minimized.

Copy link

commented Jan 20, 2017

Do not work correctly if filenames contains spaces. Another problem: git status --porcelain wraps filename with ".

my fixed version is https://gist.github.com/d-line/0f0d51a60eaa6ff08c46dbbced79a381

@horaciob

This comment has been minimized.

Copy link

commented Jul 5, 2017

You may avoid to take care of extension, rubocop allready do that

https://rubocop.readthedocs.io/en/latest/configuration/#includingexcluding-files

@grosser

This comment has been minimized.

Copy link

commented Feb 9, 2018

Simpified / improved version grosser/dotfiles#7

  • symlink instead of copy-paste with install helper
  • less funky string logic
  • use --force-exclusion so all files work not just .rb
  • only print when things fail
@jagdeepsingh

This comment has been minimized.

Copy link

commented Jun 7, 2018

my fixed version is https://gist.github.com/d-line/0f0d51a60eaa6ff08c46dbbced79a381

@d-line the link returns 404 now.

@jagdeepsingh

This comment has been minimized.

Copy link

commented Jun 7, 2018

It will go in .git/pre-hooks/pre-commit

Above code goes into .git/hooks/pre-commit as per new structure. You may also have to run this in command line to make this file executable:

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

This comment has been minimized.

Copy link

commented Oct 10, 2018

You can use git diff --diff-filter AM --name-only to list the modified files.
But I would like to show only issues of changed lines, not files.

@mklink to only show changed lines, you'll need to write something up yourself, I haven't found anything out there I'm happy with.

E.g., something that:

  1. Collects all the changed ruby file names in diff
  2. Collects all the changed lines in the diff. E.g., for @@ -98,20 +98,20 @@ it would be lines 98..107.
  3. Maps the changed lines to their file name
  4. Runs rubocop on those changed file names
  5. Filters the rubocop output to only show offenses that match a file/line combo from step 3
@cdmo

This comment has been minimized.

Copy link

commented Oct 29, 2018

fwiw $CHILD_STATUS.exitstatus seems to work just as well, see https://ruby-doc.org/core-2.2.3/Process/Status.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.