简体   繁体   中英

Synchronize branches of two bare repositories

I have the following scenario: Two remote bare repositories repo1 and repo2. They should share a branch, ie in fact there are regular pushes to repo1 regarding a particular branch. Thesese changes should be synchronized to repo2.

I think, synchronize means something like automated pulling based on hooks or sth. like this.

Do you have an idea, how to meet the abov-mentioned requirements?

Thanks a lot!

Set up a hook that will forward the changes. You can use choose from four hooks: pre-receive , update , post-receive and post-update .

The first two run during the push, so they slow it down, but can abort it. The later two run after the push, so the user doesn't have to wait for them, but they can't abort it and require separate method of notifying about errors.

The code will be mostly similar and quite easy. Simple post-update (file hooks/post-update in repo1 ) variant would be:

#!/bin/sh
for ref; do
    if [ refs/heads/branch-to-mirror = "$ref" ]; then
        git push repo2 "$ref"
    fi
done

The pre-receive / update would need to say git push repo2 $newsha:branch-to-mirror since the local branch is not yet updated. The condition can of course be different; anything that will tell that the branch should be mirrored in repo2 .

An update hook that aborts the push to repo1 if it fails to forward to repo2 would be:

#!/bin/sh
set -e
if [ refs/heads/branch-to-mirror = "$1" ]; then
    git push repo2 "$3:$1"
fi

The set -e is used to propagate errors out of the script. Alternatively || exit 1 || exit 1 can be added to the command failure of which should be propagated. This hook is called once for each updated ref, so there is no loop. The arguments are ref name, old SHA1 and new SHA1 and the push needs to use the new SHA1, because the local ref is not updated yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM