Search code examples
regexgithubgreppre-commit

grep with regex in pre-commit git hook


I'm writing a little pre-commit hook to check for debug code this works well when using:

FORBIDDEN='console.log'

but when I change to:

FORBIDDEN='die(|console.log(|print_r('

it fails to catch anything.

FULL CODE:

FILES_PATTERN='(\..+)?$'
FORBIDDEN='die(|console.log(|print_r('

git diff --cached --name-only | \
    grep -E $FILES_PATTERN | \
    xargs grep -E --with-filename -n $FORBIDDEN | \
    grep -v '//';

Not sure if its regex or something else


Solution

  • Try escaping your parenthesis:

    FORBIDDEN='die\(|console.log\(|print_r\('