Files
install-sh/install-lazygit.ps1
T

403 lines
16 KiB
PowerShell

#Requires -Version 5.1
<#
.SYNOPSIS
Installs lazygit terminal UI for git from GitHub releases.
.DESCRIPTION
Auto-detects the latest release from
https://github.com/jesseduffield/lazygit/releases, downloads the zip
file matching your Windows architecture, and installs it.
Supported architectures:
- x64 (AMD64 / Intel 64bit)
- ARM64 (Snapdragon X Elite, Qualcomm ARM, etc.)
.PARAMETER InstallDir
Directory to install lazygit.exe. Default: $env:USERPROFILE\.local\bin
.PARAMETER AddToPath
Whether to permanently add InstallDir to user PATH. Default: $true
.EXAMPLE
.\install-lazygit.ps1
.\install-lazygit.ps1 -InstallDir "C:\tools\bin"
.\install-lazygit.ps1 -AddToPath $false
# One-liner install from remote (works on Windows 10+):
powershell -ExecutionPolicy Bypass -c "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $t=\"$env:TEMP\install-lazygit.ps1\"; iwr -useb 'https://gl.isitsoft.kr/publicproj/install-sh/-/raw/main/install-lazygit.ps1' -OutFile $t; & $t"
# With parameters:
powershell -ExecutionPolicy Bypass -c "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $t=\"$env:TEMP\install-lazygit.ps1\"; iwr -useb 'https://gl.isitsoft.kr/publicproj/install-sh/-/raw/main/install-lazygit.ps1' -OutFile $t; & $t -InstallDir 'C:\tools\bin'"
.NOTES
If execution policy error occurs:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
#>
[CmdletBinding()]
param(
[string] $InstallDir = "$env:USERPROFILE\.local\bin",
[bool] $AddToPath = $true
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# ────────────────────────────────────────────
# Constants
# ────────────────────────────────────────────
$REPO = "jesseduffield/lazygit"
$API_URL = "https://api.github.com/repos/$REPO/releases"
$FALLBACK_TAG = "v0.61.1"
$GIT_REPO = "git-for-windows/git"
$GIT_API_URL = "https://api.github.com/repos/$GIT_REPO/releases"
# ────────────────────────────────────────────
# Utility functions
# ────────────────────────────────────────────
function Write-Info { param([string]$msg) Write-Host "[INFO] $msg" -ForegroundColor Cyan }
function Write-Warn { param([string]$msg) Write-Host "[WARN] $msg" -ForegroundColor Yellow }
function Write-Success { param([string]$msg) Write-Host "[OK] $msg" -ForegroundColor Green }
function Write-Err { param([string]$msg) Write-Host "[ERROR] $msg" -ForegroundColor Red; exit 1 }
# ────────────────────────────────────────────
# Enforce TLS 1.2 (for Windows 7/8.1 compatibility)
# ────────────────────────────────────────────
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# ────────────────────────────────────────────
# Check for git (required by lazygit)
# ────────────────────────────────────────────
function Install-GitPortable {
Write-Info "git is not installed. Installing PortableGit..."
# Detect architecture
$envArch = $env:PROCESSOR_ARCHITEW6432
if (-not $envArch) { $envArch = $env:PROCESSOR_ARCHITECTURE }
$gitArch = ""
if ($envArch -eq "AMD64") {
$gitArch = "64-bit"
} elseif ($envArch -eq "ARM64") {
$gitArch = "arm64"
} else {
Write-Err "Unsupported architecture for PortableGit: $envArch"
}
# Find latest Git for Windows release
Write-Info "Fetching latest Git for Windows release..."
try {
$releases = Invoke-RestMethod -Uri $GIT_API_URL -UseBasicParsing `
-Headers @{ 'User-Agent' = 'install-lazygit-ps1' }
} catch {
Write-Err "Failed to call GitHub API for Git: $_"
}
$latestRelease = $releases | Where-Object { $_.prerelease -eq $false } | Select-Object -First 1
if (-not $latestRelease) {
Write-Err "Could not find latest Git for Windows release."
}
$tagName = $latestRelease.tag_name
Write-Info "Latest Git for Windows: $tagName"
# Find the PortableGit asset for our architecture (e.g. PortableGit-2.55.0.5-64-bit.7z.exe)
$asset = $latestRelease.assets | Where-Object {
$_.name -match "^PortableGit-.*-${gitArch}\.7z\.exe$"
} | Select-Object -First 1
if (-not $asset) {
$asset = $latestRelease.assets | Where-Object {
$_.name -like "PortableGit*$gitArch*.7z.exe"
} | Select-Object -First 1
}
if (-not $asset) {
Write-Err "Could not find PortableGit asset for architecture '$gitArch' in release $tagName."
}
$assetName = $asset.name
$downloadUrl = $asset.browser_download_url
$installPath = Join-Path $env:USERPROFILE "PortableGit"
# Download
$tmpDir = Join-Path $env:TEMP "git_install_$(Get-Random)"
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
$zipPath = Join-Path $tmpDir "$assetName"
try {
Write-Info "Downloading: $downloadUrl"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $downloadUrl -OutFile $zipPath -UseBasicParsing
$ProgressPreference = 'Continue'
# PortableGit .7z.exe is a self-extracting archive
# Run it to extract to the install path
Write-Info "Extracting PortableGit to $installPath..."
# Try 7z first, then fall back to running the .7z.exe self-extractor
$extracted = $false
if (Get-Command 7z -ErrorAction SilentlyContinue) {
& 7z x $zipPath -o"$installPath" -y 2>&1 | Out-Null
$extracted = $true
} else {
# The .7z.exe is a self-extracting archive; run it with -y and -o"path"
# It needs to be run as an executable
try {
& $zipPath -y -o"$installPath" 2>&1 | Out-Null
$extracted = $true
} catch {
Write-Warn "Self-extractor failed, trying PowerShell expansion..."
}
}
if (-not $extracted) {
Write-Err "Cannot extract PortableGit. Install 7-Zip or install git manually from https://git-scm.com/download/win"
}
} finally {
if (Test-Path $tmpDir) {
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
# Verify git.exe
$gitExe = Join-Path $installPath "cmd\git.exe"
if (-not (Test-Path $gitExe)) {
# Try bin directory as fallback
$gitExe = Join-Path $installPath "bin\git.exe"
}
if (-not (Test-Path $gitExe)) {
# Search recursively
$found = Get-ChildItem -Path $installPath -Recurse -Filter "git.exe" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($found) { $gitExe = $found.FullName }
}
if (-not (Test-Path $gitExe)) {
Write-Err "git.exe not found after extraction. Please install git manually."
}
# Add git's directory to PATH for this session and permanently
$gitDir = Split-Path $gitExe
if ($env:Path -notlike "*$gitDir*") {
$env:Path = "$gitDir;$env:Path"
Write-Info "Added $gitDir to PATH for this session"
}
$userPath = [System.Environment]::GetEnvironmentVariable('Path', 'User')
if ($userPath -notlike "*$gitDir*") {
[System.Environment]::SetEnvironmentVariable('Path', "$gitDir;$userPath", 'User')
Write-Success "Added $gitDir to user PATH permanently"
} else {
Write-Info "$gitDir already in user PATH"
}
# Also add the main bin dir for bash, ssh, etc.
$gitBinDir = Join-Path $installPath "bin"
if ((Test-Path $gitBinDir) -and ($env:Path -notlike "*$gitBinDir*")) {
$env:Path = "$gitBinDir;$env:Path"
$userPath = [System.Environment]::GetEnvironmentVariable('Path', 'User')
if ($userPath -notlike "*$gitBinDir*") {
[System.Environment]::SetEnvironmentVariable('Path', "$gitBinDir;$userPath", 'User')
Write-Success "Added $gitBinDir to user PATH permanently"
}
}
Write-Success "PortableGit installed to $installPath"
}
# Check if git is available
$gitCmd = Get-Command git -ErrorAction SilentlyContinue
if (-not $gitCmd) {
Install-GitPortable
# Refresh PATH and re-check
$env:Path = [System.Environment]::GetEnvironmentVariable('Path', 'User') + ';' + [System.Environment]::GetEnvironmentVariable('Path', 'Machine')
$gitCmd = Get-Command git -ErrorAction SilentlyContinue
if (-not $gitCmd) {
Write-Err "git installation failed. Please install git manually from https://git-scm.com/download/win and re-run this script."
}
}
Write-Success "git is available: $($gitCmd.Source)"
# ────────────────────────────────────────────
# Architecture detection
# ────────────────────────────────────────────
function Get-Platform {
$envArch = $env:PROCESSOR_ARCHITEW6432
if (-not $envArch) { $envArch = $env:PROCESSOR_ARCHITECTURE }
switch -Regex ($envArch) {
'^AMD64$' { return "windows_x86_64" }
'^ARM64$' { return "windows_arm64" }
'^x86$' { return "windows_32-bit" }
}
try {
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture
switch ($arch) {
'X64' { return "windows_x86_64" }
'Arm64' { return "windows_arm64" }
'X86' { return "windows_32-bit" }
default { Write-Err "Unsupported architecture: $arch" }
}
} catch {
Write-Err "Failed to detect architecture: $_"
}
}
# ────────────────────────────────────────────
# Get latest stable release tag
# ────────────────────────────────────────────
function Get-LatestTag {
Write-Info "Fetching latest release from GitHub API..."
try {
$releases = Invoke-RestMethod -Uri $API_URL -UseBasicParsing `
-Headers @{ 'User-Agent' = 'install-lazygit-ps1' } -TimeoutSec 15
$latest = $releases | Where-Object {
$_.prerelease -eq $false -and $_.assets -and $_.assets.Count -gt 0
} | Select-Object -First 1
if ($latest -and $latest.tag_name) {
return $latest.tag_name
}
} catch {
Write-Warn "GitHub API lookup failed: $_"
}
Write-Warn "Using fallback release tag: $FALLBACK_TAG"
return $FALLBACK_TAG
}
# ────────────────────────────────────────────
# Build download URL
# ────────────────────────────────────────────
function Get-DownloadUrl {
param([string]$Tag, [string]$Platform)
$version = $Tag -replace '^v', ''
$filename = "lazygit_${version}_${Platform}.zip"
return "https://github.com/$REPO/releases/download/$Tag/$filename"
}
# ────────────────────────────────────────────
# Download, extract & install
# ────────────────────────────────────────────
function Install-Lazygit {
param([string]$Url, [string]$FallbackUrl)
$tmpDir = Join-Path $env:TEMP "lazygit_install_$(Get-Random)"
New-Item -ItemType Directory -Path $tmpDir | Out-Null
try {
$zipPath = Join-Path $tmpDir "lazygit.zip"
Write-Info "Downloading: $Url"
try {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $Url -OutFile $zipPath -UseBasicParsing
$ProgressPreference = 'Continue'
} catch {
if ($FallbackUrl -and ($FallbackUrl -ne $Url)) {
Write-Warn "Primary download failed. Retrying with fallback URL: $FallbackUrl"
try {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $FallbackUrl -OutFile $zipPath -UseBasicParsing
$ProgressPreference = 'Continue'
} catch {
Write-Err "Download failed: $_"
}
} else {
Write-Err "Download failed: $_"
}
}
Write-Info "Extracting..."
Expand-Archive -Path $zipPath -DestinationPath $tmpDir -Force
$lazygitExe = Get-ChildItem -Path $tmpDir -Recurse -Filter "lazygit.exe" |
Select-Object -First 1
if (-not $lazygitExe) {
Write-Err "Cannot find 'lazygit.exe' in the archive."
}
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$destPath = Join-Path $InstallDir "lazygit.exe"
Write-Info "Installing: $destPath"
try {
Copy-Item -Path $lazygitExe.FullName -Destination $destPath -Force
} catch {
Write-Err "lazygit.exe is currently running. Please close lazygit and run this script again."
}
Write-Success "Installation complete!"
Write-Info "Version check:"
& $destPath --version
} finally {
if (Test-Path $tmpDir) {
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
# ────────────────────────────────────────────
# Add to PATH (user environment variable)
# ────────────────────────────────────────────
function Add-ToUserPath {
param([string]$Dir)
$currentPath = [Environment]::GetEnvironmentVariable('PATH', 'User')
if (-not $currentPath) { $currentPath = '' }
$dirs = @($currentPath -split ';' | Where-Object { $_ -ne '' })
if ($dirs -contains $Dir) {
Write-Info "'$Dir' is already in your PATH."
return
}
$newPath = ($dirs + $Dir) -join ';'
[Environment]::SetEnvironmentVariable('PATH', $newPath, 'User')
$env:PATH = "$env:PATH;$Dir"
Write-Success "'$Dir' has been permanently added to user PATH."
Write-Warn "Open a new terminal for PATH changes to take effect."
}
# ────────────────────────────────────────────
# Main
# ────────────────────────────────────────────
function Main {
Write-Info "===== lazygit install script (Windows PowerShell) ====="
$platform = Get-Platform
Write-Info "Detected platform: $platform"
$tag = Get-LatestTag
Write-Info "Latest release: $tag"
$url = Get-DownloadUrl -Tag $tag -Platform $platform
$fallbackUrl = Get-DownloadUrl -Tag $FALLBACK_TAG -Platform $platform
Install-Lazygit -Url $url -FallbackUrl $fallbackUrl
if ($AddToPath) {
Add-ToUserPath -Dir $InstallDir
} else {
Write-Warn "Skipping automatic PATH registration."
Write-Warn "Add to PATH manually: $InstallDir"
}
Write-Success "All tasks completed."
Write-Info "Usage: lazygit"
}
Main