|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + git shortlog extension |
| 4 | +.DESCRIPTION |
| 5 | + Outputs git shortlog as objects |
| 6 | +.EXAMPLE |
| 7 | + git shortlog # Get a shortlog |
| 8 | +.EXAMPLE |
| 9 | + git shortlog --email # Get a shortlog with email information |
| 10 | +.EXAMPLE |
| 11 | + git shortlog --summary # Get a shortlog summary |
| 12 | +.EXAMPLE |
| 13 | + git shortlog --sumary --email # Get a shortlog summary, with email. |
| 14 | +#> |
| 15 | +[Management.Automation.Cmdlet("Out","Git")] # It's an extension for Out-Git |
| 16 | +[ValidatePattern("^git shortlog",Options='IgnoreCase')] # when the pattern is "git branch" |
| 17 | +param() |
| 18 | + |
| 19 | +begin { |
| 20 | + $shortlogLines = @() |
| 21 | + |
| 22 | + $SummaryLineRegex = [Regex]::new(@' |
| 23 | +^\s{1,}(?<Count>\d+)\s{1,}(?<Name>[^\<]+)(?:\<(?<Email>[^\>]+)\>){0,1}$ |
| 24 | +'@,'IgnoreCase,IgnorePatternWhitespace') |
| 25 | + |
| 26 | + $CommitterLineRegex = [Regex]::new(@' |
| 27 | +^(?<Name>\S[^\<\()]+)(?:\<(?<Email>[^\>]+)\>){0,1}\s{0,}\((?<Count>\d+)\)\:$ |
| 28 | +'@,'IgnoreCase,IgnorePatternWhitespace') |
| 29 | + |
| 30 | + $CommitMessageLineRegex = [Regex]::new('^\s{4,}(?<CommitMessage>.+)$') |
| 31 | +} |
| 32 | + |
| 33 | +process { |
| 34 | + $shortlogLines += $gitOut |
| 35 | +} |
| 36 | + |
| 37 | +end { |
| 38 | + $currentCommitter = $null |
| 39 | + $hadSummaries = $false |
| 40 | + foreach ($shortLogLine in $shortlogLines) { |
| 41 | + if ($shortLogLine -match $SummaryLineRegex) { |
| 42 | + $hadSummaries = $true |
| 43 | + $shortLogExtract = [Ordered]@{} + $matches |
| 44 | + $shortLogExtract.Remove(0) |
| 45 | + $shortLogExtract.GitRoot = $GitRoot |
| 46 | + $shortLogExtract.PSTypeName = 'Git.Shortlog.Summary' |
| 47 | + [PSCustomObject]$shortLogExtract |
| 48 | + } elseif ($shortlogLine -match $CommitterLineRegex) { |
| 49 | + if ($currentCommitter) { |
| 50 | + [PSCustomObject]$currentCommitter |
| 51 | + } |
| 52 | + $shortLogExtract = [Ordered]@{} + $matches |
| 53 | + $shortLogExtract.Remove(0) |
| 54 | + $shortLogExtract.Commits = @() |
| 55 | + $shortLogExtract.GitRoot = $GitRoot |
| 56 | + $shortLogExtract.PSTypeName = 'Git.Shortlog' |
| 57 | + $currentCommitter = $shortLogExtract |
| 58 | + } elseif ($currentCommitter -and |
| 59 | + $shortlogLine -match $CommitMessageLineRegex) { |
| 60 | + $currentCommitter.Commits += $matches.CommitMessage |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if ($currentCommitter) { |
| 65 | + [PSCustomObject]$currentCommitter |
| 66 | + } elseif (-not $hadSummaries) { |
| 67 | + $shortlogLines |
| 68 | + } |
| 69 | +} |
0 commit comments