Skip to content

Instantly share code, notes, and snippets.

@bearzk
Last active March 3, 2020 11:28
Show Gist options
  • Save bearzk/003606da54427e7fba29f45607b68551 to your computer and use it in GitHub Desktop.
Save bearzk/003606da54427e7fba29f45607b68551 to your computer and use it in GitHub Desktop.
[grepPattern.sh] sh script to catch pattern in staged file #bash #git
#!/bin/sh
# this script will look into current staged (about to be commited) files
# searching for the pattern from $1
# if found, would exit with error status code 1
# Usage:
# when staged files contain the search pattern 'TEST'
#
# > ./grepPattern.sh TEST
#
# will exit with 1
# you can add more ignore patterns by editing the ignorePattern variable
# for example:
# ignorePattern='package|again|anotherThing'
ignorePattern='package.json|hookScripts'
# in staged flies
# find (M)odified and (A)dded file names,
# find those DOES NOT conotain $ignorePattern
toSearch=$(git diff --name-status --cached | grep -E "^[M|A]" | awk '{print $2}' | grep -E -v $ignorePattern)
if [ -z "$toSearch" ]; then
echo "no file to check pattern '${1}'."
exit 0
fi
if echo $toSearch | xargs grep -n -r --include "*.js" $1; then
echo ""
echo "please remove '${1}' in these lines."
echo "this is really easy to fix, please don't try to bypass in git hooks."
echo ""
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment