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 <noreply@anthropic.com>
This commit is contained in:
Rick Mu
2026-02-13 19:05:40 +11:00
parent 1a84adb97b
commit 966f7e0ac8
2 changed files with 41 additions and 0 deletions

View File

@@ -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) { 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." 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."
} }

View File

@@ -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) { if (-not $gitInstalled) {
Write-Err "无法自动安装 Git。请手动下载安装`n`n 下载地址https://git-scm.com/download/win`n`n 安装完成后重新打开 PowerShell 运行此脚本。" Write-Err "无法自动安装 Git。请手动下载安装`n`n 下载地址https://git-scm.com/download/win`n`n 安装完成后重新打开 PowerShell 运行此脚本。"
} }