[CmdletBinding()] param( [string]$Path = "." ) $ErrorActionPreference = 'Stop' if (-not (Get-Command git -ErrorAction SilentlyContinue)) { throw "git not found in PATH" } $repo = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path $null = & git -C $repo rev-parse --is-inside-work-tree 2>$null if ($LASTEXITCODE -ne 0) { throw "Not a git repository: $repo" } $branch = ((& git -C $repo rev-parse --abbrev-ref HEAD 2>$null) -join "`n").Trim() $head = ((& git -C $repo rev-parse --short HEAD 2>$null) -join "`n").Trim() if ($LASTEXITCODE -ne 0) { Write-Host "Repository has no commits yet: $repo" exit 0 } $tracking = ((& git -C $repo rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>$null) -join "`n").Trim() $ahead = 0 $behind = 0 if ($LASTEXITCODE -eq 0 -and $tracking) { $counts = ((& git -C $repo rev-list --left-right --count "HEAD...$tracking" 2>$null) -join " ").Trim() $parts = $counts -split '\s+' if ($parts.Count -ge 2) { $ahead = [int]$parts[0] $behind = [int]$parts[1] } } $staged = 0 $unstaged = 0 $untracked = 0 $conflicted = 0 $statusLines = @(& git -C $repo status --porcelain=v1 2>$null) foreach ($line in $statusLines) { if ([string]::IsNullOrWhiteSpace($line) -or $line.Length -lt 2) { continue } $x = [string]$line[0] $y = [string]$line[1] if ($x -eq '?' -and $y -eq '?') { $untracked++ continue } if ($x -eq 'U' -or $y -eq 'U' -or ($x -eq 'A' -and $y -eq 'A') -or ($x -eq 'D' -and $y -eq 'D')) { $conflicted++ continue } if ($x -ne ' ') { $staged++ } if ($y -ne ' ') { $unstaged++ } } $stashes = @(& git -C $repo stash list 2>$null).Count Write-Host "Repository: $repo" Write-Host "Branch: $branch ($head)" if ($tracking) { Write-Host "Upstream: $tracking, ahead $ahead, behind $behind" } else { Write-Host "Upstream: not configured" } Write-Host "Changes: staged $staged, unstaged $unstaged, untracked $untracked, conflicts $conflicted" Write-Host "Stashes: $stashes" Write-Host "" Write-Host "Recent commits:" & git -C $repo log -5 --date=short --pretty=format:'%h %ad %an %s' Write-Host ""