|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Parses git blame output |
| 4 | +.DESCRIPTION |
| 5 | + Parses the output of git blame. |
| 6 | +.EXAMPLE |
| 7 | + git blame ugit.psd1 |
| 8 | +#> |
| 9 | +[Management.Automation.Cmdlet("Out","Git")] # It's an extension for Out-Git |
| 10 | +[ValidatePattern("^u?git blame",Options='IgnoreCase')] # when the pattern is "git blame" |
| 11 | +param() |
| 12 | + |
| 13 | +begin { |
| 14 | + $gitBlameOutput = @() |
| 15 | + $blameHeaderPattern = @( |
| 16 | + '(?<CommitHash>[0-9a-f]{8})\s' |
| 17 | + |
| 18 | + # If your filename contains parenthesis, this may not work, and you'll only have yourself to blame. |
| 19 | + '(?:(?<FileName>[\S-[\(]]+)\s)?' |
| 20 | + |
| 21 | + '\((?<WhoWhenWhat>.+?)\)' |
| 22 | + ) -join '' |
| 23 | + $blameRevision = @($gitCommand -split '\s' -notmatch '^--')[-1] |
| 24 | +} |
| 25 | + |
| 26 | +process { |
| 27 | + $gitBlameOutput += $gitout |
| 28 | +} |
| 29 | + |
| 30 | +end { |
| 31 | + $blameObjects = @( |
| 32 | + foreach ($gitBlameLine in $gitBlameOutput) { |
| 33 | + if ($gitBlameLine -notmatch $blameHeaderPattern) { continue } |
| 34 | + $lineContent = $gitBlameLine -replace $blameHeaderPattern |
| 35 | + $commitMatch = [Ordered]@{} + $matches |
| 36 | + $whoWhenWhat = $commitMatch.WhoWhenWhat -split '\s+' |
| 37 | + |
| 38 | + [PSCustomObject][Ordered]@{ |
| 39 | + PSTypeName = 'git.blame' |
| 40 | + CommitHash = $commitMatch.CommitHash |
| 41 | + CommitDate = ($whoWhenWhat[-4..-2] -join ' ') -as [DateTime] |
| 42 | + Line = $whoWhenWhat[-1] |
| 43 | + File = $matches.File |
| 44 | + Revision = $blameRevision |
| 45 | + Content = $lineContent |
| 46 | + GitRoot = $GitRoot |
| 47 | + Author = $whoWhenWhat[0..$( |
| 48 | + ($whoWhenWhat.Length - 5) |
| 49 | + )] -join ' ' |
| 50 | + GitOutputLines = $gitBlameLine |
| 51 | + } |
| 52 | + } |
| 53 | + ) |
| 54 | + |
| 55 | + if ($blameObjects) { |
| 56 | + $blameObjects |
| 57 | + } else { |
| 58 | + $gitBlameOutput |
| 59 | + } |
| 60 | +} |
| 61 | + |
0 commit comments