我试图在任何提交之前设置一个预提交钩子来测试我的项目,但我找不到如何确保只测试HEAD(来自当前提交的补丁)而不是current working_tree(在大多数情况下对我来说很脏)。
发现解决方案:
找到此链接并最终执行以下操作。
http://newartisans.com/2009/02/building-a-better-pre-commit-hook-for-git/
# Checkout a copy of the current index into MIRROR
git checkout-index --prefix=$TMPDIR/ -af
# Remove files from MIRROR which are no longer present in the index
git diff-index --cached --name-only --diff-filter=D -z HEAD | \
(cd $TMPDIR && xargs -0 rm -f --)
答案 0 :(得分:1)
依赖于您实际验证和测试的内容。因此,如果您要查看检查的文件中是否有某些内容,则必须执行git diff --cached
而不是git diff
,以便您获得相应的更改。同样,您必须查看正在使用的命令,等等。
对于单元测试,我可以建议这样的事情(还有其他方法):
写一个post-commit
(不是预先提交)的钩子,如下所示:
#!/bin/sh
git stash
#run unit tests
#if tests fail
git stash pop --index
git reset --soft HEAD~1
exit 0 # if tests pass
使用预提交的替代方法是将索引签出到单独的目录中,从那里运行测试。看看git checkout-index
。我没有以这种方式使用它,因此无法对其进行进一步评论。