小编Sar*_*101的帖子

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

我正在编写一个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 …
Run Code Online (Sandbox Code Playgroud)

unix git bash shell git-bash

6
推荐指数
1
解决办法
1243
查看次数

标签 统计

bash ×1

git ×1

git-bash ×1

shell ×1

unix ×1