Skip to content

Instantly share code, notes, and snippets.

@lsaffie
Created August 5, 2012 20:00
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save lsaffie/3266940 to your computer and use it in GitHub Desktop.
Save lsaffie/3266940 to your computer and use it in GitHub Desktop.
pre-commit git hook that checks for values in the FORBIDDEN array
#!/bin/bash
# Pre commit hook that prevents FORBIDDEN code from being commited.
# Add unwanted code to the FORBIDDEN array as necessary
FILES_PATTERN='\.(rb|js|coffee)(\..+)?$'
FORBIDDEN=( debugger ruby-debug )
for i in "${FORBIDDEN[@]}"
do
git diff --cached --name-only| grep ".js" |xargs sed 's/ //g'|grep "ha_mobile.debug=true" && \
echo 'COMMIT REJECTED Found ha_mobile.debug=true references. Please remove them before commiting' && exit 1
git diff --cached --name-only | \
grep -E $FILES_PATTERN | \
GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n $i && \
echo 'COMMIT REJECTED Found' $i 'references. Please remove them before commiting' && exit 1
done
exit 0
@pke
Copy link

pke commented Mar 24, 2015

this does seem to check also files that have been modified but not staged for commit. Shouldn't it check only the files to be commited? If "debugger" is contained in a part of a file that is not being committed yet, this hook should not prevent the commit.

@WuChenDi
Copy link

Hi guys, I'm trying to understand how this code works, I created pre-commit file in directory .git/hooks/ and then copied the above code to pre-commit and executed:

git add .
git commit -m 'feat: test'
git push

But there is no interception operation, the push operation is executed successfully.

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