From 907882c0a7862352b0e0129da8a56d900a81e439 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Sun, 12 Apr 2026 17:36:45 +0800 Subject: [PATCH] fix: git clone pre-check --- README.md | 2 +- run.sh | 17 +++++++++++------ scripts/run.ps1 | 43 ++++++++++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 1cdbc309..e86b7835 100644 --- a/README.md +++ b/README.md @@ -898,7 +898,7 @@ FAQs: # 📌 项目更名说明 -本项目原名 `chatgpt-on-wechat`(GitHub 原地址:https://github.com/zhayujie/chatgpt-on-wechat), +本项目原名 `chatgpt-on-wechat`(GitHub 原地址:https://github.com/zhayujie/chatgpt-on-wechat ), 于 2026.04.13 正式更名为 **CowAgent**。GitHub 已自动设置重定向,原有链接仍可正常访问。 如需更新本地仓库的远程地址(可选): diff --git a/run.sh b/run.sh index c364deab..7ad6f85d 100755 --- a/run.sh +++ b/run.sh @@ -171,12 +171,17 @@ clone_project() { mv CowAgent-master CowAgent rm CowAgent.zip else - GIT_HTTP_CONNECT_TIMEOUT=10 GIT_HTTP_LOW_SPEED_LIMIT=1024 GIT_HTTP_LOW_SPEED_TIME=15 \ - git clone --depth 10 --progress https://github.com/zhayujie/CowAgent.git || { - echo -e "${YELLOW}⚠️ GitHub is slow, switching to Gitee mirror...${NC}" - git clone --depth 10 --progress https://gitee.com/zhayujie/CowAgent.git - } - if [[ $? -ne 0 ]]; then + local clone_ok=false + # Test GitHub connectivity before attempting clone + if curl -s --connect-timeout 5 --max-time 10 https://github.com > /dev/null 2>&1; then + echo -e "${YELLOW}🌐 GitHub is reachable, cloning from GitHub...${NC}" + timeout 15 git clone --depth 10 --progress https://github.com/zhayujie/CowAgent.git && clone_ok=true + fi + if [ "$clone_ok" = false ]; then + echo -e "${YELLOW}⚠️ GitHub clone failed or timed out, switching to Gitee mirror...${NC}" + timeout 30 git clone --depth 10 --progress https://gitee.com/zhayujie/CowAgent.git && clone_ok=true + fi + if [ "$clone_ok" = false ]; then echo -e "${RED}❌ Project clone failed. Please check network connection.${NC}" exit 1 fi diff --git a/scripts/run.ps1 b/scripts/run.ps1 index 66ae2539..b931e637 100644 --- a/scripts/run.ps1 +++ b/scripts/run.ps1 @@ -90,20 +90,37 @@ function Install-Project { } Write-Cow "Cloning CowAgent project..." - $prevEAP = $ErrorActionPreference; $ErrorActionPreference = "Continue" - git clone https://github.com/zhayujie/CowAgent.git 2>&1 | Out-Null - $cloneExit = $LASTEXITCODE - $ErrorActionPreference = $prevEAP - if ($cloneExit -ne 0) { - Write-Warn "GitHub failed, trying Gitee..." - $ErrorActionPreference = "Continue" - git clone https://gitee.com/zhayujie/CowAgent.git 2>&1 | Out-Null - $cloneExit = $LASTEXITCODE - $ErrorActionPreference = $prevEAP - if ($cloneExit -ne 0) { - Write-Err "Clone failed. Check your network." - exit 1 + $cloneOk = $false + + # Test GitHub connectivity before attempting clone + try { + $null = Invoke-WebRequest -Uri "https://github.com" -UseBasicParsing -TimeoutSec 5 -ErrorAction Stop + Write-Cow "GitHub is reachable, cloning from GitHub..." + $proc = Start-Process -FilePath "git" -ArgumentList "clone","--depth","10","--progress","https://github.com/zhayujie/CowAgent.git" ` + -NoNewWindow -PassThru -RedirectStandardError "$env:TEMP\git_clone_err.txt" + if ($proc.WaitForExit(15000) -and $proc.ExitCode -eq 0) { + $cloneOk = $true + } else { + if (-not $proc.HasExited) { $proc.Kill() } + if (Test-Path "CowAgent") { Remove-Item -Recurse -Force "CowAgent" } } + } catch {} + + if (-not $cloneOk) { + Write-Warn "GitHub clone failed or timed out, switching to Gitee mirror..." + $proc = Start-Process -FilePath "git" -ArgumentList "clone","--depth","10","--progress","https://gitee.com/zhayujie/CowAgent.git" ` + -NoNewWindow -PassThru -RedirectStandardError "$env:TEMP\git_clone_err.txt" + if ($proc.WaitForExit(30000) -and $proc.ExitCode -eq 0) { + $cloneOk = $true + } else { + if (-not $proc.HasExited) { $proc.Kill() } + if (Test-Path "CowAgent") { Remove-Item -Recurse -Force "CowAgent" } + } + } + + if (-not $cloneOk) { + Write-Err "Clone failed. Check your network." + exit 1 } Set-Location "CowAgent"