Home > Net >  Check in git hook that working copy is updated
Check in git hook that working copy is updated

Time:02-01

I have a git repo that has many branches. There are branches master and dev. And there are two sites: mysite.example (working copy of master branch) and dev.mysite.example (working copy of dev branch). For autodeploy after git push hook post-received is written:

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 performs some actions which can take a lot of time. After each push (even if master and dev branches are not updated) the hook run deploy.sh twice.

How can I check that working copy was updated by git pull? I can't git pull && ./deploy.sh as git pull returns 0 for both "Already up to date" and update.

CodePudding user response:

You can use git ls-remote to check the current hash of a given branch on the remote side :

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

you can compare the hash with 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

You may also want to run your actions asynchronously:

# 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 &
  • Related