当var_dump存在时,终止git pre-commit hook [英] aborting git pre-commit hook when var_dump present
问题描述
我正在尝试(但失败)做一个git pre-commit钩子来检查我的修改过的文件中是否存在var_dump,如果找到它就退出。我遇到的问题是它似乎总是放弃提交。这里是我的预先提交文件的内容:
$ $ p $ code VAR = $(git diff | grep -wvar_dump)
如果[-z $ VAR];那么
echo你在其中一个文件中留下了var_dump!正在中止提交...
exit 1
fi
首先,请注意, git diff --cached
来查看即将提交的内容。
我遇到的第二件事是我正在试验的是,如果[-z $ VAR] 直接抛出一个错误,使用,因为
+
Bash解释了 git diff
输出。确保围绕 $ VAR
加引号以防止这种情况发生。
至于脚本,你忘了否定测试 $ VAR
是否为空。如果grep的输出为空,那么找不到var_dump,并且钩子应该返回成功。你想要的情况是,如果不是为空,意味着找到了var_dump,并且它应该放弃提交。
$ p
$ b
VAR = $(git diff --cached | grep -wvar_dump)
if [! -z$ VAR];那么
echo你在其中一个文件中留下了var_dump!正在中止提交...
exit 1
fi
I am trying (but failing miserably) to make a git pre-commit hook that checks for the presence of a var_dump in my modified files and exits if it finds one. The problem that I'm having is that it appears to always be aborting the commit. Here is the contents of my pre-commit file:
VAR=$(git diff | grep -w "var_dump")
if [ -z $VAR ]; then
echo "You've left a var_dump in one of your files! Aborting commit..."
exit 1
fi
First of all, note that plain git diff
gives the difference between the working tree and the index (i.e. what can still be staged), not what is about to be committed. Use git diff --cached
to see what is about to be committed.
The second thing I encountered as I was experimenting was that using if [ -z $VAR ]
directly threw an error, because the +
at the beginning of the git diff
output was interpreted by Bash. Make sure to surround $VAR
with quotes to prevent this.
As for the script, you forgot to negate the test if $VAR
is empty. If the output from grep is empty, then "var_dump" was not found, and the hook should return success. The case you want is if it is not empty, meaning "var_dump" was found, and it should abort the commit.
All together:
VAR=$(git diff --cached | grep -w "var_dump")
if [ ! -z "$VAR" ]; then
echo "You've left a var_dump in one of your files! Aborting commit..."
exit 1
fi
这篇关于当var_dump存在时,终止git pre-commit hook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!