我正在尝试设置一个预提交 Hook 来在任何提交通过之前测试我的项目,但我找不到如何确保只测试 HEAD(带有当前提交的补丁)而不是当前的 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 --)
最佳答案
将取决于您实际验证和测试的内容。因此,如果您试图查看某些内容是否不在 checkin 的文件中,您必须执行 git diff --cached
而不是 git diff
这样您将获得适当的改变。同样,您必须查看正在使用的命令等。
对于单元测试的事情,我可以这样建议(也会有其他方法):
像这样写一个post-commit
(不是pre-commit)钩子(Hook):
#!/bin/sh
git stash
#run unit tests
#if tests fail
git stash pop --index
git reset --soft HEAD~1
exit 0 # if tests pass
使用预提交的替代方法是将索引 check out 到一个单独的目录中,然后从那里运行测试。看看 git checkout-index
。我没有以这种方式使用过它,因此无法进一步评论。
关于git - 如何测试当前提交而不是工作树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7495819/