Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A script to multiple git clone these github repositories #3

Open
hefeng-0411 opened this issue Jan 24, 2025 · 0 comments
Open

A script to multiple git clone these github repositories #3

hefeng-0411 opened this issue Jan 24, 2025 · 0 comments

Comments

@hefeng-0411
Copy link

hefeng-0411 commented Jan 24, 2025

``
#!/bin/bash

设置超时时间(1分钟)

TIMEOUT=60

定义镜像源列表(优先级从高到低)

MIRRORS=(
"https://github.com" # 原生源
"https://hub.njuu.cf" # 国内稳定镜像
"https://ghproxy.com/https://github.com" # 代理加速
"https://kgithub.com" # 备用镜像
"https://hub.yzuu.cf" # 学术镜像
"https://gitclone.com" # 代码克隆加速
)

定义日志文件

FAILED_LOG="failed_clones.txt"

清空日志文件

"$FAILED_LOG"

读取 README.md 文件

content=$(cat README.md)

定义正则表达式来提取类别和代码链接

category_pattern='### (.*)'
code_pattern='[Code]((https://github.com/[^)]+))'
code_pattern_other='[Code]((https://[^)]+))'

初始化变量

current_category=""

逐行解析 README.md

echo "$content" | while IFS= read -r line; do
# 检查是否是类别行
if [[ "$line" =~ $category_pattern ]]; then
current_category="${BASH_REMATCH[1]// /_}"
echo "Found category: $current_category"
mkdir -p "$current_category"
fi

# 检查是否是代码链接行(GitHub)
if [[ "$line" =~ $code_pattern ]]; then
    original_url="${BASH_REMATCH[1]}"
    project_name=$(basename "$original_url")
    echo "Found GitHub code link: $original_url"

    # 尝试使用不同的镜像源克隆
    success=false
    for mirror in "${MIRRORS[@]}"; do
        # 特殊处理代理类镜像
        if [[ "$mirror" == *"ghproxy.com"* ]]; then
            cloned_url="${mirror}/${original_url#https://}"
        else
            cloned_url="${original_url/https:\/\/github.com/$mirror}"
        fi
        
        echo "Trying to clone from: $cloned_url"

        # 使用 timeout 命令设置超时
        if timeout $TIMEOUT git clone "$cloned_url" "$current_category/$project_name"; then
            echo "Successfully cloned $project_name into $current_category"
            success=true
            break  # 克隆成功,跳出循环
        else
            # 清理失败克隆的残留目录
            rm -rf "$current_category/$project_name" 2>/dev/null
            echo "Failed to clone from $cloned_url, trying next mirror..."
        fi
    done

    # 如果所有镜像源都失败,则记录到日志文件
    if [[ "$success" == false ]]; then
        echo "All mirrors failed, skipping $project_name"
        echo "$current_category -- $original_url" >> "$FAILED_LOG"
    fi
fi

# 处理非GitHub链接
if [[ "$line" =~ $code_pattern_other ]]; then
    code_url="${BASH_REMATCH[1]}"
    echo "Found other code link: $code_url"
    echo "Skipping non-GitHub link: $code_url"
    echo "$current_category -- $code_url" >> "$FAILED_LOG"
fi

done

echo "所有项目已成功克隆到对应的文件夹中。"

输出统计信息

total_failed=$(wc -l < "$FAILED_LOG" | tr -d ' ')
if [[ $total_failed -gt 0 ]]; then
echo "----------------------------------------"
echo "未能成功克隆的项目 ($total_failed 个):"
cat "$FAILED_LOG"
echo "----------------------------------------"
echo "完整日志已保存至: $FAILED_LOG"
else
echo "所有项目均已成功克隆!"
fi
``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant