Running rubocop on a local git branches’ diff

You can run rubocop on just a local branches’ changes like this:

git fetch && git diff-tree -r --no-commit-id --name-only master@\{u\} head | xargs ls -1 2>/dev/null | xargs rubocop --force-exclusion

Then you can create 2 corresponding aliases in your terminal .rc file to make this easy to remember:

# Alias to run rubocop on the current branch's diff from master
alias rubocop-branch="git fetch && git diff-tree -r --no-commit-id --name-only master@\{u\} head | xargs ls -1 2>/dev/null | xargs rubocop --force-exclusion"

# Alias to run rubocop autocorrect on the current branch's diff from master
alias rubocop-branch-autocorrect="git fetch && git diff-tree -r --no-commit-id --name-only master@\{u\} head | xargs ls -1 2>/dev/null | xargs rubocop -A --force-exclusion"
Back