Mirror a Repository

Warning

I don't know if GitLab / Gitaly will get upset about the files changing under it's feet... I'm trailling this with low-priority projects initially.

First, create the repository via the UI, import an existing project if you wish.

Then, you'll need to modify the Git repositories configuration.

Navigate to https://${GITLAB_URL}/admin/projects/${PROJECT_PATH}, and find the "Gitaly relative path".

Now go to the Git repository on disk (i.e: /path/to/gitlab/repositories/${GITALY_REL_PATH}).

Run the following

git remote add origin ${ORIGIN_URL}
git config remote.origin.fetch "+refs/*:refs/*"
git config remote.origin.mirror true

Subsequently, to update the mirror, run:

git remote update

Script

find /var/opt/gitlab/git-data/repositories/@hashed/ -type d -name '*.git' \
  | while read repo; do \
    [ ! -f "${repo}/config" ] && continue
    grep -qE ' true$' \
      <( GIT_DIR="${repo}" git config --bool --get-regexp 'remote\.([^\.]+)\.mirror' ) \
      || continue
    echo "Updating [${repo}]..."
    GIT_DIR="${repo}" git remote update
  done

read GIT_UID < <( id -u git )
find /var/opt/gitlab/git-data/repositories/@hashed/ \( ! -uid ${GIT_UID} -o ! -gid 0 \) -print0 | xargs -r0t chown ${GIT_UID}:0
find /var/opt/gitlab/git-data/repositories/@hashed/ -maxdepth 3 -type d ! -perm 2750 -print0 | xargs -r0t chmod 2750
find /var/opt/gitlab/git-data/repositories/@hashed/ -mindepth 4 -type d ! -perm 2755 -print0 | xargs -r0t chmod 2755