Skip to content

Instantly share code, notes, and snippets.

@osulyanov
Forked from mpeteuil/rubocop_pre_commit_hook
Created December 3, 2017 04:57
Show Gist options
  • Save osulyanov/4b9c6e92ff3fac616219c28a5f48fce9 to your computer and use it in GitHub Desktop.
Save osulyanov/4b9c6e92ff3fac616219c28a5f48fce9 to your computer and use it in GitHub Desktop.

Revisions

  1. @mpeteuil mpeteuil created this gist Aug 3, 2013.
    21 changes: 21 additions & 0 deletions rubocop_pre_commit_hook
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #!/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