安装这个脚本(或类似的东西——我的也被盗了)作为预提交挂钩。它将索引复制到临时工作目录并在那里运行构建。它将捕获您错过的文件。
我知道至少有一个或两个其他 SO 问题可以解决这个确切的问题——在预提交挂钩中测试/验证索引而不是工作目录——但我现在似乎找不到它们。
(为了完整起见,我在我的 repo 中有这个脚本作为 .git-hooks/pre-commit/test-the-index;那里还有其他几个脚本。请参阅下面的 .git/钩子/预提交。)
#!/bin/sh
#
# Via: http://github.com/jwiegley/git-scripts/blob/master/pre-commit.sh
#
if [ ! $(git rev-parse --symbolic-full-name HEAD) = refs/heads/master ]; then
exit 0
fi
# These are the locations I keep my temporary source and build trees in
TMPDIR=$HOME/code/myproject-pre-commit
MIRROR=$HOME/code/myproject-pre-commit-mirror
# Exit with status 1 if any command below fails
set -e
# Checkout a copy of the current index into MIRROR
git checkout-index --prefix=$MIRROR/ -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 $MIRROR && xargs -0 rm -f --)
# Copy only _changed files_ from MIRROR to TMPDIR, without copying timestamps.
# This includes copying over new files, and deleting removed ones. This way,
# "make check" will only rebuild what is necessary to validate the commit.
rsync -rlpgoDOc --delete --exclude-from=.git-hooks/excludes $MIRROR/ $TMPDIR/
# Everything else happens in the temporary build tree
cd $TMPDIR
nosetests
exit 0
这是我实际的 .git/hooks/pre-commit:
#!/bin/bash
set -e
for hook in $(find .git-hooks/pre-commit -perm /u+x,g+x,o+x -type f -not -name '*~' 2>/dev/null)
do
echo "@@ Running hook: $(basename $hook)"
$hook "$@"
done