Skip to content

Instantly share code, notes, and snippets.

@alexbevi
Created August 23, 2012 12:05
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save alexbevi/3436040 to your computer and use it in GitHub Desktop.
Save alexbevi/3436040 to your computer and use it in GitHub Desktop.
Git pre-commit hook that checks ruby source files for Pry breakpoints
# Git pre-commit hook to check all staged Ruby (*.rb/haml/coffee) files
# for Pry binding references
#
# Installation
#
# ln -s /path/to/pre-commit.sh /path/to/project/.git/hooks/pre-commit
#
# Based on
#
# http://codeinthehole.com/writing/tips-for-using-a-git-pre-commit-hook/
# http://mark-story.com/posts/view/using-git-commit-hooks-to-prevent-stupid-mistakes
# https://gist.github.com/3266940
#
FILES_PATTERN='\.(rb|haml|coffee)(\..+)?$'
FORBIDDEN='binding.pry'
git diff --cached --name-only | \
grep -E $FILES_PATTERN | \
GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n $FORBIDDEN && \
echo 'COMMIT REJECTED' && \
exit 1
exit 0
@cciollaro
Copy link

thanks, very useful and works great.

@MProuts
Copy link

MProuts commented Jun 21, 2014

thanks, this was helpful 👍

@er0ck
Copy link

er0ck commented Jan 26, 2015

thanks for this! i've found, on OSX that this will always return 1 when there are no .rb files staged for commit. this is becasue the xargs in the pipeline will always return true when passed an empty string.

i added a pre-check for ruby files and added checks on require and debugger statements

#!/bin/sh
# Git pre-commit hook to check Ruby (*.rb) files for Pry binding references
# based upon https://gist.github.com/alexbevi/3436040
FILES_PATTERN='\.rb$'
FORBIDDEN='binding.pry\|require .pry.\|require .debugger.'

# xargs below returns true if grep sends empty string
# avoid this by pre-searching on ruby files
# turn on nullglob to match on 0
shopt -s nullglob
set -- *.rb
if [ "$#" -eq 0 ]; then exit 0; fi
shopt -u nullglob

git diff --cached --name-only | \
    grep -E $FILES_PATTERN | \
    GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n \
    "$FORBIDDEN" && echo 'Please remove debugger bindings\nCOMMIT REJECTED' && \
    exit 1

exit 0

it's not super DRY in a couple of places, but it works for me for now...

@wdewind
Copy link

wdewind commented Feb 17, 2015

The problem with the above solution is that it only allows you to support .rb files. If you'd like your pre-commit hook to run on multiple file types (ie: JS + CSS as well) you can do something like:

#!/bin/sh
FILES='(js|css|rb)'
FORBIDDEN='(binding.pry|console.log|\!important)'
GREP_COLOR='4;5;37;41'

if [[ $(git diff --cached --name-only | grep -E $FILES) ]]; then
  git diff --cached --name-only | grep -E $FILES | \
  xargs grep --color --with-filename -n -E $FORBIDDEN && \
  echo "Looks like you are trying to commit something you shouldn't.  Please fix your diff, or run 'git commit --no-verify' to skip this check, if you must." && \
  exit 1
fi

exit 0

That's what I am currently using and it seems to work well.

@ArunkumarNagarajan
Copy link

How to do the same operation in ruby?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment