websoft9/scripts/githubclone.sh

35 lines
1.3 KiB
Bash
Raw Normal View History

#!/bin/bash
2023-04-17 06:14:43 +00:00
# This script is created by Bing Chat
# 你可以使用以下修改后的 shell 脚本来自动尝试从 github.com 克隆仓库,如果失败了,再尝试使用不同的加速地址来克隆仓库。这个脚本会先尝试从 github.com 克隆仓库,如果失败了,再尝试使用我之前提到的加速地址来克隆仓库,直到成功为止。每个地址都会尝试克隆 3 次。如果克隆失败,脚本会输出友好的错误提示。这个脚本接受两个外部参数:$1 是用户名,$2 是仓库名。
2023-04-17 04:00:19 +00:00
# 获取外部参数
USERNAME="$1"
REPO="$2"
2023-04-17 04:00:19 +00:00
# 生成仓库 URL
REPO_URL="https://github.com/$USERNAME/$REPO.git"
2023-04-17 04:00:19 +00:00
# 加速地址列表
MIRRORS=(
2023-04-19 01:19:49 +00:00
"https://github.com"
2023-04-17 06:21:53 +00:00
"https://ghproxy.com/https://github.com"
2023-04-17 04:00:19 +00:00
)
for mirror in "${MIRRORS[@]}"; do
# 生成加速后的 URL
mirror_url="${REPO_URL/https:\/\/github.com/$mirror}"
# 尝试克隆仓库
for i in {1..3}; do
echo "Trying to clone from $mirror_url (attempt $i)"
2023-04-19 01:19:49 +00:00
if git clone --depth=1 "$mirror_url"; then
2023-04-17 04:00:19 +00:00
echo "Successfully cloned from $mirror_url"
exit 0
2023-04-17 04:58:10 +00:00
else
echo "Failed to clone from $mirror_url (attempt $i)"
2023-04-17 04:00:19 +00:00
fi
done
done
2023-04-17 04:58:10 +00:00
echo "Failed to clone the repository after multiple attempts. Please check your network connection and try again later."