mirror-gitea-github/lfs-push-repo.sh

86 lines
2.7 KiB
Bash

#!/bin/bash
# ---------------- CONFIG ----------------
PROJECTS_DIR="git_repo_project_files" # Working clones
LFS_EXTENSIONS=("*.so" "*.zip" "*.bin") # Large files to track
# ---------------- HARD-CODED TOKEN ----------------
GITHUB_TOKEN=""
# ---------------- VALIDATION ----------------
if [ $# -lt 3 ]; then
echo "Usage: $0 <repo_name> <gitea_repo_url> <github_repo_url>"
echo "Example: $0 solo-level-app-automation https://gitea.arulbalaji.xyz/arul/solo-level-app-automation.git https://github.com/username/repo.git"
exit 1
fi
REPO_NAME="$1"
GITEA_URL="$2"
GITHUB_URL="$3"
WORKDIR="$PROJECTS_DIR/$REPO_NAME"
echo "🔹 Processing repository: $REPO_NAME"
# ---------------- CREATE BASE DIR ----------------
mkdir -p "$PROJECTS_DIR"
# ---------------- CLONE / UPDATE ----------------
if [ ! -d "$WORKDIR/.git" ]; then
echo "Cloning from Gitea..."
git clone "$GITEA_URL" "$WORKDIR" || { echo "❌ Clone failed"; exit 1; }
else
echo "Working repo exists. Ensuring it's a proper checkout and pulling latest changes..."
cd "$WORKDIR" || { echo "❌ Failed to enter directory"; exit 1; }
git fetch origin
git fetch github 2>/dev/null
git pull origin main || echo "⚠️ Pull failed, continuing..."
cd - >/dev/null
fi
cd "$WORKDIR" || { echo "❌ Failed to cd into $WORKDIR"; exit 1; }
# ---------------- GIT LFS ----------------
echo "Initializing Git LFS..."
git lfs install
echo "Tracking large files: ${LFS_EXTENSIONS[*]}"
for ext in "${LFS_EXTENSIONS[@]}"; do
git lfs track "$ext"
done
# Commit .gitattributes if needed
if git status --porcelain | grep -q ".gitattributes"; then
git add .gitattributes
git commit -m "Track large files with Git LFS" || echo "⚠️ No commit needed"
fi
# ---------------- REWRITE HISTORY WITH LFS ----------------
echo "=== MIGRATING HISTORY TO LFS (this rewrites history) ==="
CMD="git lfs migrate import --include=\"*.so,*.zip,*.bin\" --everything --yes"
echo "Running: $CMD"
eval $CMD || { echo "❌ git lfs migrate import failed"; exit 1; }
echo "✔ History rewritten successfully."
# ---------------- GITHUB REMOTE ----------------
AUTH_URL="${GITHUB_URL/https:\/\//https://$GITHUB_TOKEN@}"
if ! git remote | grep -q github; then
echo "Adding GitHub remote..."
git remote add github "$AUTH_URL"
else
echo "Updating GitHub remote URL..."
git remote set-url github "$AUTH_URL"
fi
# ---------------- FORCE PUSH (because history changed) ----------------
echo "🚀 Force pushing rewritten history to GitHub..."
git push github --all --force
git push github --tags --force
echo
echo "✅ Done pushing $REPO_NAME to GitHub with rewritten LFS history!"
echo "🔥 Your GitHub repo is now clean and optimized."