2025-01-03 14:06:28 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
2025-01-03 17:03:01 +08:00
|
|
|
# 参数获取
|
|
|
|
GITHUB_USER="$1"
|
|
|
|
GITHUB_TOKEN="$2"
|
|
|
|
GITEA_URL="$3"
|
|
|
|
GITEA_USER="$4"
|
|
|
|
GITEA_TOKEN="$5"
|
|
|
|
WORK_DIR="$6"
|
|
|
|
SKIP_REPOS="$7"
|
|
|
|
|
|
|
|
# 同步单个仓库
|
|
|
|
sync_repository() {
|
|
|
|
local repo="$1"
|
2025-01-03 14:06:28 +08:00
|
|
|
|
|
|
|
# 检查 Gitea 仓库是否存在
|
2025-01-03 17:03:01 +08:00
|
|
|
if ! curl -s -o /dev/null -f -H "Authorization: token $GITEA_TOKEN" \
|
|
|
|
"$GITEA_URL/api/v1/repos/$GITEA_USER/$repo"; then
|
2025-01-03 14:06:28 +08:00
|
|
|
|
|
|
|
echo "在 Gitea 上创建仓库 $repo"
|
|
|
|
curl -X POST -H "Authorization: token $GITEA_TOKEN" \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-d "{\"name\":\"$repo\",\"private\":false}" \
|
2025-01-03 17:03:01 +08:00
|
|
|
"$GITEA_URL/api/v1/user/repos"
|
2025-01-03 14:06:28 +08:00
|
|
|
fi
|
|
|
|
|
2025-01-03 17:03:01 +08:00
|
|
|
# 克隆和推送
|
2025-01-03 14:06:28 +08:00
|
|
|
[ -d "$repo" ] && rm -rf "$repo"
|
2025-01-03 17:03:01 +08:00
|
|
|
git clone --mirror "https://${GITHUB_TOKEN:+$GITHUB_TOKEN@}github.com/$GITHUB_USER/$repo.git" "$repo"
|
|
|
|
cd "$repo"
|
2025-01-03 14:06:28 +08:00
|
|
|
|
2025-01-03 15:36:19 +08:00
|
|
|
# 尝试 mirror 推送
|
2025-01-03 17:03:01 +08:00
|
|
|
if ! git push --mirror "https://$GITEA_USER:$GITEA_TOKEN@${GITEA_URL#https://}/$GITEA_USER/$repo.git"; then
|
2025-01-03 15:36:19 +08:00
|
|
|
echo "mirror 推送失败,尝试逐个分支推送..."
|
|
|
|
|
2025-01-03 17:03:01 +08:00
|
|
|
# 获取所有分支
|
|
|
|
git fetch --all
|
2025-01-03 15:36:19 +08:00
|
|
|
|
2025-01-03 17:03:01 +08:00
|
|
|
# 推送每个分支
|
|
|
|
git for-each-ref --format='%(refname:short)' refs/heads/ | while read branch; do
|
2025-01-03 15:36:19 +08:00
|
|
|
echo "推送分支: $branch"
|
2025-01-03 17:03:01 +08:00
|
|
|
git push "https://$GITEA_USER:$GITEA_TOKEN@${GITEA_URL#https://}/$GITEA_USER/$repo.git" "$branch:$branch"
|
2025-01-03 15:36:19 +08:00
|
|
|
done
|
|
|
|
|
|
|
|
# 推送所有标签
|
2025-01-03 17:03:01 +08:00
|
|
|
git push "https://$GITEA_USER:$GITEA_TOKEN@${GITEA_URL#https://}/$GITEA_USER/$repo.git" --tags
|
2025-01-03 15:36:19 +08:00
|
|
|
fi
|
|
|
|
|
2025-01-03 17:03:01 +08:00
|
|
|
cd ..
|
|
|
|
}
|
2025-01-03 14:06:28 +08:00
|
|
|
|
2025-01-03 17:03:01 +08:00
|
|
|
# 主同步逻辑
|
|
|
|
main() {
|
|
|
|
mkdir -p "$WORK_DIR"
|
|
|
|
cd "$WORK_DIR"
|
|
|
|
|
|
|
|
# 获取仓库列表
|
|
|
|
repos=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
|
|
|
|
"https://api.github.com/user/repos?per_page=100&type=all" | \
|
|
|
|
jq -r '.[].name')
|
|
|
|
|
|
|
|
# 同步每个仓库
|
|
|
|
for repo in $repos; do
|
|
|
|
# 检查是否跳过
|
|
|
|
if echo "$SKIP_REPOS" | grep -q "$repo"; then
|
|
|
|
echo "跳过仓库: $repo"
|
|
|
|
continue
|
|
|
|
fi
|
2025-01-03 14:06:28 +08:00
|
|
|
|
2025-01-03 17:03:01 +08:00
|
|
|
echo "处理仓库: $repo"
|
|
|
|
sync_repository "$repo"
|
|
|
|
done
|
|
|
|
}
|
2025-01-03 14:06:28 +08:00
|
|
|
|
2025-01-03 17:03:01 +08:00
|
|
|
main "$@"
|