[CmdletBinding()] param( [string]$Path = ".", [string]$Csv ) $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" } $format = '%(refname:short)|%(upstream:short)|%(committerdate:short)|%(subject)' $lines = @(& git -C $repo for-each-ref "--format=$format" refs/heads 2>$null) $merged = @(& git -C $repo branch --merged HEAD --format='%(refname:short)' 2>$null) $mergedSet = @{} foreach ($name in $merged) { $mergedSet[$name.Trim()] = $true } $current = ((& git -C $repo rev-parse --abbrev-ref HEAD 2>$null) -join "`n").Trim() $rows = @() foreach ($line in $lines) { if ([string]::IsNullOrWhiteSpace($line)) { continue } $parts = $line -split '\|', 4 $name = $parts[0].Trim() $upstream = if ($parts.Count -gt 1 -and $parts[1].Trim()) { $parts[1].Trim() } else { '-' } $date = if ($parts.Count -gt 2) { $parts[2].Trim() } else { '' } $subject = if ($parts.Count -gt 3) { $parts[3].Trim() } else { '' } $rows += [pscustomobject]@{ Branch = $name Current = ($name -eq $current) Upstream = $upstream Last = $date Merged = $mergedSet.ContainsKey($name) Subject = $subject } } if ($rows.Count -eq 0) { Write-Host "No local branches found." exit 0 } $rows | Sort-Object @{ Expression = 'Merged'; Descending = $true }, Branch | Format-Table -AutoSize if ($Csv) { $rows | Export-Csv -LiteralPath $Csv -NoTypeInformation -Encoding UTF8 Write-Host "CSV saved: $Csv" } Write-Host "Branches are not deleted. Review merged entries before cleanup."