Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

VISHAL DEEPAK
VISHAL DEEPAK

Posted on

     

Run Rubcop on changed files with Git Status

Here me out before you think this is just another post which runs rubocop only on changed files. There's four things different here

  1. We run rubocop on newly added and renamed files as well. No need to stage them.(I found a few tutorials online that don't consider this)
  2. Skip deleted files
  3. Works on both staged and unstaged files with same code
  4. We use Git Status instead, for the sake of first point.

First let me give the command to you straight

git status --porcelain | grep -E -v '^(D| D|RD)' | awk '{ print $NF }' | xargs rubocop
Enter fullscreen modeExit fullscreen mode

Lets break this down

git status --porcelain

This gets all the files that have been changed, added or deleted.--porcelain is a easy to parse output format

grep -E -v '^(D| D|RD)'

We remove all deleted files and renamed+deleted files if any. We dont want to run rubocop on our deleted files

awk '{ print $NF }'

This one is useful for one edge case. When we rename a staged file. For example if we rename a controller , the output ofgit status --porcelain is as follows

R  app/controllers/home_controller.rb -> app/controllers/house_controller.rb
Enter fullscreen modeExit fullscreen mode

Theawk command withNF fetches only the last column

xargs rubocop

xargs run the rubocop command on each file

Alias it

Adding alias requires a small modification to the script. This is because we dont want $NF to be expanded on declaration.

alias rubogit="git status --porcelain |  grep -E -v '^(D| D|RD)' | awk '{ print \$NF }' | xargs rubocop "
Enter fullscreen modeExit fullscreen mode

Let me know if you had any edge case that didn't work with this command

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromVISHAL DEEPAK

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp