[英]Find out staged files in git `pre-commit` hook when using `git commit -a`
[英]Git pre-commit hook to find text in files
我正在编写一个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
似乎没有用。 我在提交时遇到这些错误:
.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 $?
我使用与上面相同的逻辑,即使交错的文件包含不允许的单词,它也会将更改提交给分支。 任何领导都将非常感激。
#!/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 $?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.