Git pre-commit hook用于在文件中查找文本

Sar*_*101 6 unix git bash shell git-bash

我正在编写一个git pre-commit钩子来检查是否有任何暂存文件包含不允许的文本,如果是这种情况则中止.

不是这方面的专家.到目前为止我已经有了这个

git diff --cached --name-status | while read x file; do
        if [ "$x" == 'D' ]; then continue; fi
        if [[ egrep "DISALLOWED_TEXT" ${file}]]; then
                echo "ERROR: Disallowed text in file: ${file}"
                exit 1
        fi
done
Run Code Online (Sandbox Code Playgroud)

似乎没有用.我在提交时遇到这些错误:

.git/hooks/pre-commit: line 16: conditional binary operator expected
.git/hooks/pre-commit: line 16: syntax error near `"DISALLOWED_TEXT"'
.git/hooks/pre-commit: line 16: `        if [[ egrep "DISALLOWED_TEXT" ${file}]]; then'
Run Code Online (Sandbox Code Playgroud)

任何建议,想法和帮助表示赞赏.谢谢!

解决:(语法错误和退出调用功能不正常)

disallowed="word1 word2"

git diff --cached --name-status | while read x file; do
        if [ "$x" == 'D' ]; then continue; fi
        for word in $disallowed
        do
            if egrep $word $file ; then
                echo "ERROR: Disallowed expression \"${word}\" in file: ${file}"
                exit 1
            fi
        done
done || exit $?
Run Code Online (Sandbox Code Playgroud)

Joh*_*don 5

回答将此问题标记为有答案:

OP结束了:

disallowed="word1 word2"

git diff --cached --name-status | while read x file; do
        if [ "$x" == 'D' ]; then continue; fi
        for word in $disallowed
        do
            if egrep $word $file ; then
                echo "ERROR: Disallowed expression \"${word}\" in file: ${file}"
                exit 1
            fi
        done
done || exit $?
Run Code Online (Sandbox Code Playgroud)