Skip to content

Instantly share code, notes, and snippets.

@hschne
Last active January 4, 2021 14:22
Show Gist options
  • Save hschne/a292fc0e4d8c9a14bfbf2f131f4738d6 to your computer and use it in GitHub Desktop.
Save hschne/a292fc0e4d8c9a14bfbf2f131f4738d6 to your computer and use it in GitHub Desktop.
RCF

RuboCop Fix

This script runs Rubocop auto-correction on all files that were changed in the current branch.

Getting Started

Copy the script below to somewhere on your path (e.g. /usr/local/bin), then make the file executable and run it.

# In a directory in your path
wget -qO- https://gist.githubusercontent.com/hschne/a292fc0e4d8c9a14bfbf2f131f4738d6/raw/d0f3801a963365851d581cea904968f36898cff4/rcf.sh > rcf
chmod +x rcf
# In your repository
rcf
``
#!/usr/bin/env bash
main() {
local branch
local base_commit
local changed_files
branch=$(git branch --show-current)
# To reliably get the files that were changed on this branch we need to compare using some base commit. Diffing using master
# won't cut it, since there may be files that were changed on the master branch - but not by us.
#
# See also https://stackoverflow.com/a/10641810/2553104
base_commit=$(git merge-base "$branch" master)
changed_files=$(git diff --name-only "$base_commit")
if [[ -n $changed_files ]]; then
echo "$changed_files" | xargs rubocop -A --force-exclusion
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment