4

我正在尝试设置一个预提交挂钩以在任何提交通过之前测试我的项目,但我找不到如何确保只测试 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 --)
4

1 回答 1

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。我没有以这种方式使用它,因此无法进一步评论。

于 2011-09-21T07:14:12.767 回答