git pre-push-hook - 缺少构建文件

时间:2014-04-17 09:39:28

标签: git githooks

我尝试使用git的pre-push-hook在每次推送之前执行构建任务。这到目前为止工作正常(文件得到构建,添加和提交)但文件不会被推送。下面是代码:

#!/bin/sh
# Pre-Push hook to to build
# files before pushing them.
# If files are in the staging area
# the push aborts.

unstagedFiles=`expr $(git status --porcelain 2>/dev/null| grep "^ M" | wc -l)`
stagedFiles=`expr $(git diff --cached --numstat | wc -l)`

# if there are unstaged files abort push
if [ "$unstagedFiles" -gt 0 ]; then
    echo "Pre-Push-Hook: ERROR - You have unstaged files! - please add and commit them"
    exit 1
fi

# if there are staged files abort push
if [ "$stagedFiles" -gt 0 ]; then
    echo "Pre-Push-Hook: ERROR - You have uncommited files! - please commit them"
    exit 1
fi

echo "Pre-Push-Hook: Building files"
grunt build
echo "Pre-Push-Hook: Files built"

echo "Pre-Push-Hook: Adding following build-files"
git add -A .

git commit -m "automatically added build files"
echo "Pre-Push-Hook: Build-files commited"

exit 0

我尝试在脚本中再做一次这样的推送:

branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
git push origin $branch --no-verify
exit 1

这很有效,但退出当然有错误信息,这不是很完美!有没有人有任何想法要么实际推送工作或在脚本中使用另一个推送并退出1时隐藏错误消息?

类似的问题已经被问到here,但答案仍然是这个问题......

1 个答案:

答案 0 :(得分:0)

如果我已正确理解您的方法,我认为您需要密切关注链接中提供的答案,特别是这一点:

  

预推钩意味着"验证此推送是否正常"操作,   不是"改变这种推动意味着其他一些不同的推动"操作。   使用预推钩作为验证器;如果你想要一个脚本   调用git push ...将其写为脚本,使用不同的名称,例如   build-and-push