Skip to content
Snippets Groups Projects
Commit e266a84a authored by Martin Christoph Hierholzer's avatar Martin Christoph Hierholzer
Browse files

add script to mirror gitlab to github

parent 07ceff0f
No related branches found
No related tags found
No related merge requests found
#!/bin/bash -e
REPOLIST=`gh repo list ChimeraTK -L 300 | grep '^ChimeraTK/' | sed -e 's_^ChimeraTK/__' -e 's_[[:space:]].*$__'`
export GITLAB_HOST=gitlab.desy.de
NMIRRORS=0
ERROR=0
for repo in $REPOLIST; do
NMIRRORS=$(( NMIRRORS + 1 ))
echo "=== Mirroring $repo"
if [[ "$repo" == "chimeratk.github.io" ]]; then
echo "(Skipping, big and easy to recreate so no backup necessary)"
continue
fi
mirrorname="gitlab.desy.de/chimeratk-mirror/${repo}"
if ! glab repo view "${mirrorname}" > /dev/null 2>&1 ; then
rm -rf temp-checkout
mkdir temp-checkout
cd temp-checkout
git init .
NO_PROMPT=1 glab repo create -P "${mirrorname}"
cd ..
fi
rm -rf temp-checkout
mkdir temp-checkout
cd temp-checkout
git init --bare
git remote add origin --mirror=fetch "https://github.com/ChimeraTK/$repo"
git remote add gitlab --mirror=push "git@gitlab.desy.de:chimeratk-mirror/${repo}"
# Configure which refs to fetch, to exclude GitHub pull requests. Including them would lead to an error message
# when trying to push the pull request refs to GitLab ("deny updating a hidden ref").
FETCH='\tfetch = +refs/heads/*:refs/heads/*\n\tfetch = +refs/tags/*:refs/tags/*'
sed -i config -e 's_^[[:space:]]*fetch = +refs/\*:refs/\*$_'"$FETCH"'_'
git remote update
# Make sure the master branch is protected, just in case a previous run of this script was interrupted in the wrong place
glab api projects/chimeratk-mirror%2F$repo/protected_branches/master -F allow_force_push=false -X PATCH
PUSHFAIL=0
git push --mirror gitlab || PUSHFAIL=1
echo "HIER PUSHFAIL = $PUSHFAIL"
if [ "$PUSHFAIL" != "0" ]; then
# Pushing to protected master might fail if upstream had force-pushed changes to master
# First create a backup of the previous master branch with a new, unique name and protect it
MASTER_BACKUP_NAME="master-`date +%Y-%m-%d_%H.%M.%S`"
git remote add gitlab_nomirror "git@gitlab.desy.de:chimeratk-mirror/${repo}"
git remote update
git branch $MASTER_BACKUP_NAME gitlab_nomirror/master
git push gitlab_nomirror $MASTER_BACKUP_NAME
git remote remove gitlab_nomirror
glab api projects/chimeratk-mirror%2F$repo/protected_branches/ -F name=$MASTER_BACKUP_NAME -F allow_force_push=false -X POST
git remote update
# Now unprotect the master branch, retry push and finally protect the branch again
glab api projects/chimeratk-mirror%2F$repo/protected_branches/master -F allow_force_push=false -X PATCH
PUSHFAIL=0
git push --mirror gitlab || PUSHFAIL=1
glab api projects/chimeratk-mirror%2F$repo/protected_branches/master -F allow_force_push=true -X PATCH
# Check for error during push
if [ "$PUSHFAIL" != "0" ]; then
echo "*** ERROR: Pushing still failed. Continue with other repos and fail later."
ERROR=1
fi
fi
cd ..
rm -rf temp-checkout
done
if [ $NMIRRORS -lt 10 ]; then
echo "Only $NMIRRORS repos have been mirrored. Something probably went wrong..."
exit 1
fi
if [ "$ERROR" != "0" ]; then
echo "Errors have occurred before!"
exit 1
fi
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment