#!/bin/bash #if [ $# -ne 2 ]; then # echo "Usage: $0 " # exit 1 #fi SOURCE_REPO="$1" TARGET_REPO="$2" REPO_NAME=$(basename "$SOURCE_REPO" .git) # ============================== # ?? 全局 Git 配置:提升大文件支持 # ============================== echo "?? Increasing Git buffer and timeout settings..." git config --global http.postBuffer 1048576000 # 1 GB git config --global https.postBuffer 1048576000 # 1 GB git config --global http.lowSpeedLimit 0 git config --global http.lowSpeedTime 999999 git config --global core.compression 1 # 减少 CPU 压力(可选) echo "Cloning from $SOURCE_REPO (mirror)..." git clone --mirror "$SOURCE_REPO" if [ ! -d "${REPO_NAME}.git" ]; then echo "? Error: Clone failed or directory '${REPO_NAME}.git' not found." exit 1 fi cd "${REPO_NAME}.git" # 初始化 LFS echo "Initializing Git LFS..." git lfs install --local # 拉取所有 LFS 对象 echo "Fetching all LFS objects from source..." GIT_LFS_SKIP_SMUDGE=1 git lfs fetch origin --all || { echo "?? LFS fetch failed. Retrying once..." sleep 3 GIT_LFS_SKIP_SMUDGE=1 git lfs fetch origin --all || { echo "? LFS fetch failed after retry." exit 1 } } # 设置目标远程 echo "Setting new remote origin to $TARGET_REPO ..." git remote set-url origin "$TARGET_REPO" git config remote.origin.mirror false # ============================== # ?? 安全推送策略:先推 HEAD(main/master),再推全部 # ============================== echo "Pushing initial branch (HEAD) to establish base ref..." # 尝试推 HEAD 到 main 或 master(Git 2.30+ 支持) if git push --force origin HEAD:main 2>/dev/null; then echo "? Pushed to 'main' branch." elif git push --force origin HEAD:master 2>/dev/null; then echo "? Pushed to 'master' branch." else # 如果 HEAD 推送失败,尝试列出所有本地分支并推第一个 FIRST_BRANCH=$(git branch -r | head -1 | sed 's|origin/||') if [ -n "$FIRST_BRANCH" ]; then echo "Trying to push first detected branch: $FIRST_BRANCH" git push --force origin "refs/heads/$FIRST_BRANCH:refs/heads/$FIRST_BRANCH" || { echo "? Failed to push any initial branch." exit 1 } else echo "?? No branches detected. Falling back to --all (may fail on empty remote)." fi fi # 推送所有分支和标签 echo "Pushing all branches and tags..." git push --all --force origin || { echo "? Failed to push all branches." exit 1 } git push --tags --force origin || { echo "? Failed to push tags." exit 1 } # 推送 LFS 对象 echo "Pushing LFS objects to target repository..." git lfs push --all origin || { echo "? LFS push failed." exit 1 } # ============================== # ? 验证:检查远程是否有 refs # ============================== echo "Verifying remote repository content..." if ! git ls-remote --heads origin | grep -q .; then echo "? ERROR: Remote repository appears empty! Push may have failed silently." exit 1 fi echo "? Migration succeeded! Repository is now available at $TARGET_REPO" #rm -fr *.git