grep with regex in pre-commit git hook

2019-08-01 14:05发布

问题:

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

回答1:

Try escaping your parenthesis:

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