dotfiles/bin/migrate2gitlab

83 lines
2.4 KiB
Bash
Executable file

#!/usr/bin/env bash
# migrate-to-gitlab.sh
# Usage: migrate-to-gitlab.sh <pseudo/nom_dépôt>
# Migre le dépôt Git du dossier courant vers GitLab.
# - Récupère toutes les branches distantes (Gitea)
# - Remplace l'URL d'origin par GitLab
# - Pousse toutes les branches et les tags
set -euo pipefail
GITLAB_BASE="git@gitlab.com"
# --- Validation des arguments ---
if [[ $# -ne 1 ]]; then
echo "Usage: $(basename "$0") <pseudo/nom_dépôt>" >&2
echo "Exemple: $(basename "$0") odupont/lighty-dmap" >&2
exit 1
fi
TARGET="$1"
if [[ ! "$TARGET" =~ ^[^/]+/[^/]+$ ]]; then
echo "Erreur : le format attendu est pseudo/nom_dépôt (ex: odupont/lighty-dmap)" >&2
exit 1
fi
# --- Vérification : être dans un dépôt Git ---
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "Erreur : le dossier courant n'est pas un dépôt Git." >&2
exit 1
fi
REPO_ROOT="$(git rev-parse --show-toplevel)"
GITLAB_URL="${GITLAB_BASE}:${TARGET}.git"
echo "==> Dépôt local : $REPO_ROOT"
echo "==> Destination : $GITLAB_URL"
echo ""
# --- Récupération de toutes les branches distantes ---
echo "==> Récupération des branches depuis origin (Gitea)..."
git fetch --all --prune
# Créer localement toutes les branches distantes qui n'existent pas encore
git branch -r \
| grep -v '\->' \
| grep '^ origin/' \
| sed 's|^ origin/||' \
| while read -r branch; do
if ! git show-ref --verify --quiet "refs/heads/$branch"; then
echo " Création branche locale : $branch"
git branch "$branch" "origin/$branch"
fi
done
# --- Affichage de l'état avant modification ---
OLD_URL="$(git remote get-url origin 2>/dev/null || echo '(aucun remote origin)')"
echo ""
echo "==> Remote actuel (origin) : $OLD_URL"
echo "==> Nouveau remote (GitLab): $GITLAB_URL"
echo ""
read -rp "Confirmer la migration ? [o/N] " confirm
if [[ ! "$confirm" =~ ^[oO]$ ]]; then
echo "Migration annulée."
exit 0
fi
# --- Remplacement de l'URL d'origin ---
echo ""
echo "==> Mise à jour de l'URL d'origin..."
git remote set-url origin "$GITLAB_URL"
# --- Push de toutes les branches ---
echo "==> Push de toutes les branches..."
git push --all origin
# --- Push des tags ---
echo "==> Push des tags..."
git push --tags origin
echo ""
echo "Migration terminée."
echo "Dépôt disponible sur : https://gitlab.com/${TARGET}"
echo "(Le dépôt a été créé automatiquement par GitLab si besoin.)"