Skip to content

Instantly share code, notes, and snippets.

@burkenyo
Created July 19, 2022 03:33
Show Gist options
  • Save burkenyo/b8a78569b55d4d955f9aefe24f4b15c2 to your computer and use it in GitHub Desktop.
Save burkenyo/b8a78569b55d4d955f9aefe24f4b15c2 to your computer and use it in GitHub Desktop.
(PowerShell Core) find long lines within a git commit range
#!/usr/bin/env pwsh
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0)]
$StartRef,
[Parameter(Mandatory=$true, Position=1)]
$EndRef,
[ValidateRange(72, 132)]
[int]$Cutoff = 88
)
cd $PSScriptRoot/..
$files = git --no-pager diff $StartRef $EndRef --name-only |
% { $PWD.Path + '/' + $_ }
$commits = @{}
foreach($commitInfo in (git --no-pager log --pretty=format:'%h %s' "$StartRef...$EndRef")){
$commits += (ConvertFrom-StringData $commitInfo -Delimiter ' ')
}
foreach ($file in $files) {
$lineNum = 1
$path = Resolve-Path $file -Relative
$first = $true
foreach ($line in (Get-Content $file)) {
if ($line.Length -gt $Cutoff) {
$commit = git --no-pager log --pretty=format:"%h %s" -n 1 -L $lineNum,+1:$file --no-patch
$commit = $commit.Split(' ', 2)[0]
if ($commit -notin $commits.Keys) {
continue
}
if ($first) {
$path
$first = $false
}
" line $lineNum is $($line.Length) chars:"
" $($line.Trim())"
" modified in:"
" $commit – $($commits[$commit])"
""
}
$lineNum++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment