Update githubclone.sh

This commit is contained in:
Darren 2023-04-17 12:00:19 +08:00 committed by GitHub
parent cd90d55958
commit 4ffc2278a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,54 +1,32 @@
#!/bin/bash #!/bin/bash
# 请在此处设置您的仓库 URL 和镜像 URL # 获取外部参数
USERNAME="$1"
REPO="$2"
USERNAME=$1 # 生成仓库 URL
REPO=$2
# 构建仓库 URL 和镜像 URL
REPO_URL="https://github.com/$USERNAME/$REPO.git" REPO_URL="https://github.com/$USERNAME/$REPO.git"
MIRROR_URLS=("https://hub.fastgit.org/$USERNAME/$REPO.git" "https://github.com.cnpmjs.org/$USERNAME/$REPO.git")
# 定义最大重试次数 # 加速地址列表
MAX_RETRIES=3 MIRRORS=(
"https://github.com"
"https://github.com.cnpmjs.org"
"https://hub.fastgit.org"
"https://gitclone.com"
"https://gh.api.99988866.xyz"
"https://github.zhlh6.cn"
"https://toolwa.com/github"
)
# 定义克隆函数 for mirror in "${MIRRORS[@]}"; do
clone_repo() { # 生成加速后的 URL
local url=$1 mirror_url="${REPO_URL/https:\/\/github.com/$mirror}"
local retries=0 # 尝试克隆仓库
for i in {1..3}; do
# 尝试克隆,直到成功或达到最大重试次数 echo "Trying to clone from $mirror_url (attempt $i)"
while [ $retries -lt $MAX_RETRIES ]; do if git clone "$mirror_url"; then
git clone $url echo "Successfully cloned from $mirror_url"
exit 0
# 如果克隆成功,则退出循环 fi
if [ $? -eq 0 ]; then done
return 0 done
fi
# 增加重试次数并等待一段时间再重试
retries=$((retries + 1))
echo "无法从 $url 克隆,等待 5 秒后重试 ($retries/$MAX_RETRIES)"
sleep 5
done
# 如果达到最大重试次数,则返回非零值表示失败
return 1
}
# 尝试从原始 URL 克隆
clone_repo $REPO_URL
# 检查上一个命令的退出状态
if [ $? -ne 0 ]; then
# 遍历镜像 URL 数组
for MIRROR_URL in "${MIRROR_URLS[@]}"; do
echo "无法从 $REPO_URL 克隆,尝试从镜像 $MIRROR_URL 克隆"
clone_repo $MIRROR_URL
# 如果克隆成功,则退出循环
if [ $? -eq 0 ]; then
break
fi
done
fi