本文介绍了运行git commit -a时,git pre-commit hook未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地预执行钩子执行,并在运行
git commit


时按预期停止提交

另外,如我所料,当我运行 git commit -n ...


$ b时,我可以绕过钩子$ b

但是,运行 git commit -am我的消息或者确实只是 git commit -a 似乎绕过了钩子,并允许处理提交。



任何想法可能会发生这种情况?



编辑:Hook below。

  PROJECT_ROOT =`git rev-parse --show-toplevel` 
CHANGED =`git diff --stat - $ PROJECT_ROOT / myProj / myfile.ext | wc -l`
if [$ CHANGED -gt 0];
然后
echomyfile.ext文件已更改,您确定要提交
echo使用-n参数来处理此提交。
exit 1
fi


解决方案

所以,这是用户错误。

钩子在两个实例中都运行,但是当我使用逻辑来检测文件中的更改时,一个命令被提供。

检查远程回购是否有用。



CHANGED =`git diff origin / master --stat - $ PROJECT_ROOT / myProj / myfile.ext | wc -l`



编辑:感谢@torek,更合适的检查方式对于一个变化是:

  CHANGED =`git diff --cached --stat  -  $ PROJECT_ROOT / myProj / myfile.ext | wc -l` 


I have a local pre-commit hook which executes, and stops the commit as expected when I rungit commit

Also, as expected, I can bypass the hook when I run git commit -n ...

However, running git commit -am "My message" or indeed, just git commit -a seems to bypass the hook and allow the commit to be processed.

Any idea why this may be happening?

Edit: Hook below.

PROJECT_ROOT=`git rev-parse --show-toplevel`
CHANGED=`git diff --stat -- $PROJECT_ROOT/myProj/myfile.ext | wc -l`
if [ $CHANGED -gt 0 ];
then
    echo "myfile.ext file has changed, are you sure you want to commit"
    echo "Use the -n parameter to process this commit."
    exit 1
fi
解决方案

So, it was user error..

The hook was running in both instances, however the logic I used to detect a change in my file didn't work when the -a command was supplied.

Checking against the remote repo did the trick.

CHANGED=`git diff origin/master --stat -- $PROJECT_ROOT/myProj/myfile.ext | wc -l`

edit: thanks to @torek, a more appropriate way to check for a change is:

CHANGED=`git diff --cached --stat -- $PROJECT_ROOT/myProj/myfile.ext | wc -l`

这篇关于运行git commit -a时,git pre-commit hook未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 03:56