在git钩子中检查工作副本是否已更新

6tqwzwtp  于 2023-02-02  发布在  Git
关注(0)|答案(1)|浏览(138)

我有一个git repo,它有很多分支,分别是masterdev,还有两个站点:mysite.example(master分支的工作副本)和dev.mysite.example(dev分支的工作副本).对于git push钩子post-received写入后的自动部署:

cd /var/www/mysite.example
unset GIT_DIR
git pull origin master
./deploy.sh

cd /var/www/dev.mysite.example
unset GIT_DIR
git pull origin dev
./deploy.sh

deploy.sh 执行的一些操作可能会花费很多时间。每次推送后(即使master和dev分支没有更新),钩子会运行deploy.sh两次。
如何检查工作副本是否被git pull更新?我不能使用git pull && ./deploy.sh,因为git pull对于“Already up to date”和update都返回0。

yqyhoc1h

yqyhoc1h1#

你可以使用git ls-remote来检查远程端给定分支的当前哈希值:

$ git ls-remote origin refs/heads/master
f64ae57f352acd326ca3215f61fa423abe806edf    refs/heads/master

你可以将哈希值与git rev-parse <branch>进行比较:

remote=$(git ls-remote origin refs/heads/$branch | awk '{ print $1 }')
local=$(git rev-parse $branch)
if [ "$remote" = "$local" ]; then
    echo "up to date"
    exit 0
fi

您可能还希望异步运行操作:

# hooks/post-receive:

# wrap your actions together in another script,
# use nohup, and detach stdin/stdout from current process' stdin/stdout
nohup ./post-receive-actions.sh >& /tmp/deploy.log &

相关问题