From 966f7e0ac8fe38898ebb726d00573f835d0f8742 Mon Sep 17 00:00:00 2001 From: Rick Mu Date: Fri, 13 Feb 2026 19:05:40 +1100 Subject: [PATCH] Add direct Git download+silent install fallback When winget and choco are unavailable, download latest Git for Windows 64-bit from GitHub releases and install silently with /VERYSILENT flag. Only falls through to manual prompt if all three methods fail. Co-Authored-By: Claude Opus 4.6 --- install-windows-en.ps1 | 20 ++++++++++++++++++++ install-windows.ps1 | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/install-windows-en.ps1 b/install-windows-en.ps1 index 0ea38f8..024d460 100644 --- a/install-windows-en.ps1 +++ b/install-windows-en.ps1 @@ -114,6 +114,26 @@ if (Get-Command git -ErrorAction SilentlyContinue) { } } + # Method 3: Direct download and silent install + if (-not $gitInstalled) { + Write-Info "Downloading Git installer directly..." + $gitInstaller = Join-Path $env:TEMP "git-installer.exe" + try { + $releases = Invoke-RestMethod "https://api.github.com/repos/git-for-windows/git/releases/latest" -ErrorAction Stop + $asset = $releases.assets | Where-Object { $_.name -match "Git-.*-64-bit\.exe$" } | Select-Object -First 1 + if ($asset) { + Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $gitInstaller -ErrorAction Stop + Write-Info "Installing Git silently..." + Start-Process -FilePath $gitInstaller -ArgumentList "/VERYSILENT", "/NORESTART", "/NOCANCEL", "/SP-", "/CLOSEAPPLICATIONS", "/RESTARTAPPLICATIONS" -Wait + Remove-Item $gitInstaller -Force -ErrorAction SilentlyContinue + $gitInstalled = $true + Write-Ok "Git installed via direct download" + } + } catch { + Write-Warn "Direct download failed: $_" + } + } + if (-not $gitInstalled) { Write-Err "Could not install Git automatically. Please install manually and re-run this script:`n`n Download: https://git-scm.com/download/win`n`n After installing, reopen PowerShell and run this script again." } diff --git a/install-windows.ps1 b/install-windows.ps1 index 5b637c4..4eb3f87 100644 --- a/install-windows.ps1 +++ b/install-windows.ps1 @@ -114,6 +114,27 @@ if (Get-Command git -ErrorAction SilentlyContinue) { } } + # 方式 3: 直接下载安装 + if (-not $gitInstalled) { + Write-Info "正在直接下载 Git 安装包..." + $gitInstaller = Join-Path $env:TEMP "git-installer.exe" + try { + # 从 GitHub 获取最新 64-bit 安装包 URL + $releases = Invoke-RestMethod "https://api.github.com/repos/git-for-windows/git/releases/latest" -ErrorAction Stop + $asset = $releases.assets | Where-Object { $_.name -match "Git-.*-64-bit\.exe$" } | Select-Object -First 1 + if ($asset) { + Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $gitInstaller -ErrorAction Stop + Write-Info "正在静默安装 Git..." + Start-Process -FilePath $gitInstaller -ArgumentList "/VERYSILENT", "/NORESTART", "/NOCANCEL", "/SP-", "/CLOSEAPPLICATIONS", "/RESTARTAPPLICATIONS" -Wait + Remove-Item $gitInstaller -Force -ErrorAction SilentlyContinue + $gitInstalled = $true + Write-Ok "Git 已通过直接下载安装" + } + } catch { + Write-Warn "直接下载安装失败: $_" + } + } + if (-not $gitInstalled) { Write-Err "无法自动安装 Git。请手动下载安装:`n`n 下载地址:https://git-scm.com/download/win`n`n 安装完成后重新打开 PowerShell 运行此脚本。" }