commit.ps1 1.91 KB
# PowerShell script to ensure Git is in PATH before running czg
# This script ensures Git is available before running czg (commitizen-git)

# Add common Git installation paths to PATH
$gitPaths = @(
    "C:\Program Files\Git\cmd",
    "C:\Program Files (x86)\Git\cmd",
    "$env:LOCALAPPDATA\GitHubDesktop\bin",
    "$env:ProgramFiles\Git\cmd"
)

# Get the actual PATH from system/user environment variables
$systemPath = [Environment]::GetEnvironmentVariable("Path", "Machine")
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$currentPath = $env:PATH

# Merge all PATH sources and deduplicate
$allPaths = ($systemPath, $userPath, $currentPath) -join ';' -split ';' | Where-Object { $_ }
$allPaths += $gitPaths | Where-Object { Test-Path $_ }
$env:PATH = ($allPaths | Select-Object -Unique) -join ';'

# Verify Git is available
try {
    $gitVersion = git --version 2>&1
    if ($LASTEXITCODE -ne 0 -or $gitVersion -match "error|not found") {
        Write-Host "❌ Error: Git is not available in PATH." -ForegroundColor Red
        Write-Host "Please install Git from https://git-scm.com/downloads" -ForegroundColor Yellow
        Write-Host "Or add Git to your system PATH manually." -ForegroundColor Yellow
        exit 1
    }
} catch {
    Write-Host "❌ Error: Git is not available in PATH." -ForegroundColor Red
    Write-Host "Please install Git from https://git-scm.com/downloads" -ForegroundColor Yellow
    exit 1
}

# Check if there are staged files
$stagedFiles = git diff --cached --name-only 2>&1
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ Error: Failed to check staged files. Git may not be properly configured." -ForegroundColor Red
    exit 1
}

if ([string]::IsNullOrWhiteSpace($stagedFiles)) {
    Write-Host "⚠️  Warning: No files staged for commit." -ForegroundColor Yellow
    Write-Host "Please run 'git add' to stage files before committing." -ForegroundColor Yellow
    Write-Host ""
}

# Run czg
npx czg