Git预先提交钩子来查找文件中的文本 [英] Git pre-commit hook to find text in files

查看:134
本文介绍了Git预先提交钩子来查找文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



不是专家,不是专家,而是专家。在这。到目前为止,我已经得到了这个

  git diff --cached --name-status |同时读取x文件;如果[$ x=='D']做
;然后继续; fi
if [[egrepDISALLOWED_TEXT$ {file}]];然后
echoERROR:文件中禁止使用文本:$ {file}
exit 1
fi
done

似乎不起作用。提交时出现这些错误:

  .git / hooks / pre-commit:第16行:条件二元运算符期望值
.git / hooks / pre-commit:第16行:在语法错误附近`DISALLOWED_TEXT'
.git / hooks / pre-commit:第16行:`if [[egrepDISALLOWED_TEXT$ {file }]];然后'

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



已解决:(语法错误和功能异常的退出调用)

  disallowed =word1 word2

git diff --cached --name-status |同时读取x文件;如果[$ x=='D']做
;然后继续; fi
for $不允许

如果egrep $ word $ file;然后
echo错误:不允许的表达式\文件中的$ {word} \:$ {file}
出口1
fi
完成
完成||退出$?


解决方案

回答标记此问题为答案:



结束于:

  disallowed =word1 word2

git diff --cached --name-status |同时读取x文件;如果[$ x=='D']做
;然后继续; fi
for $不允许

如果egrep $ word $ file;然后
echo错误:不允许的表达式\文件中的$ {word} \:$ {file}
出口1
fi
完成
完成||退出$?


I'm writing a git pre-commit hook to check if any of the staged files are containing disallowed text and abort if that's the case.

Not an expert at this. So far I've got this

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

Doesn't seem to work. I'm getting these errors while commiting:

.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'

Any suggestions, ideas and help appreciated. Thanks!

SOLVED: (syntax errors and dysfunctional exit call)

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 $?

解决方案

Answer to mark this question as having an answer:

OP ended up with:

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预先提交钩子来查找文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆