* 执行 git ls-remote 检查仓库可达性(适配 403/仓库禁用场景) * @param {string} url 仓库链接 * @returns { { accessible: boolean, rawUrl: string, message: string } }
(url)
| 39 | * @returns { { accessible: boolean, rawUrl: string, message: string } } |
| 40 | */ |
| 41 | function checkRepoAccessibility(url) { |
| 42 | const { normalized, raw } = normalizeRepoUrl(url); |
| 43 | |
| 44 | // 链接格式不合法 |
| 45 | if (!normalized) { |
| 46 | return { |
| 47 | accessible: false, |
| 48 | rawUrl: raw, |
| 49 | message: '❌ 链接格式不合法,无法解析', |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | try { |
| 54 | // 执行 git ls-remote,静默模式(--quiet),超时 10 秒 |
| 55 | const output = execSync(`git ls-remote --quiet ${normalized}`, { |
| 56 | timeout: 10000, |
| 57 | stdio: ['pipe', 'pipe', 'pipe'], // 捕获 stdout/stderr |
| 58 | }) |
| 59 | .toString() |
| 60 | .trim(); |
| 61 | |
| 62 | // 正常情况:git ls-remote 返回 refs 信息(非空),或空(部分公共仓库也可能返回空但可访问) |
| 63 | return { |
| 64 | accessible: true, |
| 65 | rawUrl: raw, |
| 66 | message: `✅ 仓库可正常访问 ${output ? '- 引用信息:' + output.substring(0, 50) + '...' : ''}`, |
| 67 | }; |
| 68 | } catch (err) { |
| 69 | let errorMsg = '未知错误'; |
| 70 | const stderr = err.stderr ? err.stderr.toString().trim() : ''; |
| 71 | const stdout = err.stdout ? err.stdout.toString().trim() : ''; |
| 72 | |
| 73 | // 1. 超时场景 |
| 74 | if (err.status === null) { |
| 75 | errorMsg = '❌ 检查超时(10秒)'; |
| 76 | } |
| 77 | // 2. GitHub 禁用仓库(匹配你提供的 "Access to this repository has been disabled") |
| 78 | else if (stderr.includes('Access to this repository has been disabled')) { |
| 79 | errorMsg = '❌ 仓库已被 GitHub 官方禁用(403 Forbidden)'; |
| 80 | } |
| 81 | // 3. 403 权限错误(通用) |
| 82 | else if ( |
| 83 | stderr.includes('403') || |
| 84 | (stderr.includes('fatal: unable to access') && stderr.includes('The requested URL returned error: 403')) |
| 85 | ) { |
| 86 | errorMsg = '❌ 403 Forbidden - 无访问权限/仓库被限制'; |
| 87 | } |
| 88 | // 4. 其他错误(如仓库不存在、网络问题) |
| 89 | else if (stderr) { |
| 90 | // 截取 stderr 前 120 个字符,保留关键信息 |
| 91 | errorMsg = `❌ 访问失败: ${stderr.substring(0, 120)}`; |
| 92 | } else if (stdout) { |
| 93 | errorMsg = `❌ 访问失败: ${stdout.substring(0, 120)}`; |
| 94 | } else { |
| 95 | errorMsg = `❌ 访问失败(退出码:${err.status})`; |
| 96 | } |
| 97 | |
| 98 | return { |
no test coverage detected