Skip to content

Instantly share code, notes, and snippets.

@johncarney
Last active March 3, 2020 20:04
Show Gist options
  • Save johncarney/22d1c9ea806824bf7f7f9d4323a389ac to your computer and use it in GitHub Desktop.
Save johncarney/22d1c9ea806824bf7f7f9d4323a389ac to your computer and use it in GitHub Desktop.
Git pre-push hook that runs Rubocop on files that have been added or modified on the current branch
#!/usr/bin/env sh
# If you don't already have a pre-push hook in your working copy, simply copy
# this into .git/hooks/pre-push in your working copy. If you already have a
# pre-push hook, you'll have to integrate it with your existing hook.
function current_branch {
git rev-parse --abbrev-ref HEAD
}
# The point at which the current branch diverges from master
function merge_base {
git merge-base $(current_branch) master
}
function files_added_or_modified_on_current_branch {
git diff --diff-filter=AM --name-only $(merge_base)
}
# Run rubocop on .rb files that have been added or modified on the current branch
rubocop --force-exclusion $(files_added_or_modified_on_current_branch | grep -E '\.(rb|rake)$|^Gemfile')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment