[CmdletBinding()] param( [string]$Path = ".", [Parameter(Mandatory = $true)] [string]$File, [string]$Since, [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" } $fullPath = Join-Path $repo $File if (-not (Test-Path -LiteralPath $fullPath -PathType Leaf)) { throw "File not found: $fullPath" } $gitArgs = @('blame', '--line-porcelain') if ($Since) { $gitArgs += "--since=$Since" } $gitArgs += @('--', $File) $lines = @(& git -C $repo @gitArgs 2>$null) if ($LASTEXITCODE -ne 0) { throw "git blame failed." } $counts = @{} $total = 0 foreach ($line in $lines) { if ($line -like 'author *') { $author = $line.Substring(7) $total++ if ($counts.ContainsKey($author)) { $counts[$author]++ } else { $counts[$author] = 1 } } } if ($total -eq 0) { Write-Host "No blame lines found." exit 0 } $rows = $counts.GetEnumerator() | ForEach-Object { [pscustomobject]@{ Author = $_.Key Lines = $_.Value Percent = [math]::Round(100.0 * $_.Value / $total, 1) } } | Sort-Object Lines -Descending Write-Host "File: $File, lines: $total" $rows | Format-Table -AutoSize if ($Csv) { $rows | Export-Csv -LiteralPath $Csv -NoTypeInformation -Encoding UTF8 Write-Host "CSV saved: $Csv" }