我正在编写一个git预提交挂钩来检查是否有任何已暂存的文件包含不允许的文本,如果是这样的话就会中止。
不是这方面的专家。到目前为止,我已经有了这个
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 $?
答案 0 :(得分: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 $?
答案 1 :(得分:0)
我使用与上述相同的逻辑,即使标记文件包含禁止的单词,它也会将更改提交到分支。任何线索将不胜感激。
#!/bin/bash
import os
echo "Running pre-commit hook"
checks=os.environ["APPSETTING_DEVPASSWORD"],os.environ["APPSETTING_DEVUSER"],os.environ["APPSETTING_DEVPASS_ELMAH"]
git diff --cached --name-status | while read x file; do
if [ "$x" == 'D' ]; then continue; fi
for word in $checks
do
if egrep $word $file ; then
echo "ERROR: Disallowed expression \"${word}\" in file: ${file}"
exit 1
fi
done
done || exit $?