-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_to_codeberg.sh
More file actions
executable file
·86 lines (73 loc) · 3 KB
/
migrate_to_codeberg.sh
File metadata and controls
executable file
·86 lines (73 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
# === INPUT CHECK ===
if [ -z "$1" ]; then
echo "❌ Usage: ./migrate.sh <GitHub repo URL>"
echo " Example: ./migrate.sh https://github.com/username/repo"
echo " ./migrate.sh git@github.com:username/repo.git"
exit 1
fi
# === USER CONFIGURATION ===
CODEBERG_USERNAME="YOUR_CODEBERG_USERNAME" # e.g., "joergi"
CODEBERG_TOKEN="YOUR_CODEBERG_TOKEN" # Generate at https://codeberg.org/user/settings/applications
GITHUB_EMAIL="YOUR_GITHUB_EMAIL@email.com" # 123456789+YOUR_GITHUB_USERNAME@users.noreply.github.com"
CODEBERG_NAME="YOUR_CODEBERG_NAME" # e.g., "joergi"
CODEBERG_EMAIL="YOUR_CODEBERG_EMAIL@email.com" # e.g., "your.email@example.com"
GITHUB_BASE="/home/username/folder/for/github/repos
# === PARSE INPUT ===
INPUT="$1"
if [[ "$INPUT" == https://github.com/* ]]; then
GITHUB_USER=$(echo "$INPUT" | cut -d'/' -f4)
REPO_NAME=$(echo "$INPUT" | cut -d'/' -f5)
elif [[ "$INPUT" == git@github.com:* ]]; then
GITHUB_USER=$(echo "$INPUT" | cut -d':' -f2 | cut -d'/' -f1)
REPO_NAME=$(echo "$INPUT" | cut -d'/' -f2 | sed 's/.git$//')
else
echo "❌ Unsupported GitHub URL format."
exit 1
fi
GITHUB_REPO_SSH="git@github.com:$GITHUB_USER/$REPO_NAME.git"
GITHUB_EMAIL="$GITHUB_USER@users.noreply.github.com"
GITHUB_DIR="$GITHUB_BASE/$REPO_NAME.git"
CODEBERG_REPO_SSH="git@codeberg.org:$CODEBERG_USERNAME/$REPO_NAME.git"
# === SAFETY CHECK ===
echo "⚠️ This script rewrites Git history and force-pushes to Codeberg."
echo " It will clone from: $GITHUB_REPO_SSH"
echo " and push to: $CODEBERG_REPO_SSH"
read -p "Proceed with migration? (y/n): " CONFIRM
if [[ "$CONFIRM" != "y" ]]; then
echo "❌ Aborted."
exit 1
fi
# === CREATE REPO ON CODEBERG ===
echo "🆕 Creating repository '$REPO_NAME' on Codeberg..."
curl -s -X POST "https://codeberg.org/api/v1/user/repos" \
-H "Content-Type: application/json" \
-H "Authorization: token $CODEBERG_TOKEN" \
-d "{\"name\":\"$REPO_NAME\",\"private\":false,\"auto_init\":false}" > /dev/null
# === CLONE FROM GITHUB ===
echo "📦 Cloning GitHub repo into $GITHUB_DIR..."
git clone --mirror "$GITHUB_REPO_SSH" "$GITHUB_DIR"
cd "$GITHUB_DIR" || exit
# === REWRITE HISTORY ===
echo "🛠️ Rewriting commit history..."
git filter-branch --env-filter "
if [ \"\$GIT_COMMITTER_EMAIL\" = \"$GITHUB_EMAIL\" ]; then
export GIT_COMMITTER_NAME=\"$CODEBERG_NAME\"
export GIT_COMMITTER_EMAIL=\"$CODEBERG_EMAIL\"
fi
if [ \"\$GIT_AUTHOR_EMAIL\" = \"$GITHUB_EMAIL\" ]; then
export GIT_AUTHOR_NAME=\"$CODEBERG_NAME\"
export GIT_AUTHOR_EMAIL=\"$CODEBERG_EMAIL\"
fi
" --tag-name-filter cat -- --all
# === CLEANUP ===
echo "🧹 Cleaning up backup refs..."
rm -rf refs/original/
git reflog expire --expire=now --all
git gc --prune=now --aggressive
# === PUSH TO CODEBERG ===
echo "🚀 Pushing to Codeberg via SSH..."
git remote set-url origin "$CODEBERG_REPO_SSH"
git push --mirror --force
echo "✅ Migration complete! '$REPO_NAME' is now live on Codeberg with your updated identity."
echo " 👉 https://codeberg.org/$CODEBERG_USERNAME/$REPO_NAME"