Last active
June 30, 2021 21:16
-
-
Save drmartell/a45a0d277921943aab91c6b593956de4 to your computer and use it in GitHub Desktop.
run RuboCop on staged files with pre-commit hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen-string-literal: true | |
# SEE: https://gist.github.com/mpeteuil/6147292 | |
require 'rubocop' | |
VALID_FILE_CODES = %w[A M].freeze | |
# staged files have both a valid code and a `git status --porcelain` line that doesn't start with a space | |
staged_files = `git status --porcelain` | |
.split(/\n/) # one file per line | |
.filter { |status_and_file| status_and_file[0] != ' ' } | |
.map { |status_and_file| status_and_file.split(' ', 2) } # each line has a status and a filename | |
.select { |status, filename| VALID_FILE_CODES.include?(status[0]) && File.extname(filename) == '.rb' } | |
.map { |_status, filename| filename.delete('"') } # --porcelain may add " | |
system("bundle exec rubocop #{staged_files.shelljoin}") unless staged_files.empty? | |
exit $CHILD_STATUS.to_s[-1].to_i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Never mind, it looks like
git diff-index --name-status --relative --cached @
from within the pre-commit hook (not sure why).But since I'm using https://github.com/rycus86/githooks, I can just get the list from: