5

每当我提交时,我担心我可能错过了一个依赖项,我正在寻找最简单的方法来单独测试我的 git 树,以确保 git 索引(“暂存”)中的任何内容实际上都可以编译/运行他们自己的。

我的代码的依赖项存在于我执行“git add”的文件系统中,因此简单的编译和运行测试并不能保证如果树(或暂存区域)被签出到干净的文件系统上,我签入的任何内容都会编译/运行.

我可以有一个连续的构建,在提交后会检查,但我不希望在历史中出现任何错误的提交,我以后必须修补。因此,我想要一种创建隔离环境的方法,其中包括检查树以及索引/暂存区域。

我考虑过的一件事是使用 git stash 两次,即:

  1. 调用 'git stash' 将文件保存在索引中
  2. 以某种方式获取未跟踪的文件列表,“git add”所有这些文件,保存一个新的存储
  3. 删除所有以前未跟踪的文件
  4. 恢复原始存储
  5. 我现在应该有一个干净的环境,只有已经签入的代码和暂存区域中的代码,我可以编译和测试。
  6. 完成后,我会恢复未跟踪文件的存储,然后取消跟踪它们以使我处于与最初相同的位置。

(这些未跟踪的文件可能很有用,但不一定是我想要签入到存储库的东西——例如 eclipse 项目)。

不过,我有一种感觉,我过度设计了一个简单的问题。

4

3 回答 3

3

安装这个脚本(或类似的东西——我的也被盗了)作为预提交挂钩。它将索引复制到临时工作目录并在那里运行构建。它将捕获您错过的文件。

我知道至少有一个或两个其他 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
于 2010-11-05T16:43:25.820 回答
2

git stash -u --keep-index考前,考git stash pop后。

于 2012-08-30T10:18:43.520 回答
0

安装程序忽略重要文件并简单地删除那些不重要的文件git clean -df

于 2010-11-05T14:12:22.793 回答