Last active
March 3, 2020 11:28
-
-
Save bearzk/003606da54427e7fba29f45607b68551 to your computer and use it in GitHub Desktop.
[grepPattern.sh] sh script to catch pattern in staged file #bash #git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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