Git : Migrar repositório

De TheNets Wiki
Ir para: navegação, pesquisa

O script abaixo migra um repositório para outro:

#!/bin/bash

DIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

# migrate_git_repo Migrates one git repository to another
# args:
#   <remote_origin> <remote_destination>
migrate_git_repo() {
    ORIGIN=$1
    DESTINATION=$2

    REPO_NAME=$(echo ${ORIGIN} | rev | cut -d/ -f1 | rev | cut -d. -f1)
    REPOS_PATH=${DIR}/repos
    GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

    # Go to repositories path
    mkdir -p ${REPOS_PATH}

    # Clone the repo if not exist
    # or update if already exist
    if [[ ! -d "${REPOS_PATH}/${REPO_NAME}" ]]; then
        echo "[info] Clone origin repository"
        cd ${REPOS_PATH}
        git clone --mirror ${ORIGIN} ${REPO_NAME}

        echo "[info] Add destination repository"
        cd ${REPOS_PATH}/${REPO_NAME}
        git remote add destination ${DESTINATION}
    else
        echo "[info] Update origin repository"
        cd ${REPOS_PATH}/${REPO_NAME}
        git remote update
    fi

    # Push everything to the destination    
    echo "[info] Push to destination repository"
    cd ${REPOS_PATH}/${REPO_NAME}
    git push destination --mirror

    # DEBUG
    # echo ${REPO_NAME}
    # echo ${ORIGIN}
    # echo ${DESTINATION}
}


# Example
migrate_git_repo \
    git@github.com:thenets/my_awesome_project.git \
    git@github.com:thenets/my_awesome_project_mirrored.git