我正在编写一个 git 预提交 Hook 来检查是否有任何暂存文件包含不允许的文本,如果是这种情况则中止。
不是这方面的专家。到目前为止我已经得到了这个
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
好像不行。我在提交时遇到了这些错误:
.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'
感谢任何建议、想法和帮助。 谢谢!
已解决:(语法错误和功能异常的退出调用)
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 $?
最佳答案
将此问题标记为有答案的答案:
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 $?
关于Git 预提交钩子(Hook)在文件中查找文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32356457/