Skip to content

Commit

Permalink
git trailer sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Nov 2, 2023
1 parent d44e963 commit 89d4912
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Pwsh/Git/ParsingGitTrailers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using namespace System.Collections.Generic

$sampleCommit = @'
subject
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Signed-off-by: Alice <[email protected]>
resolved: crisis 314
Signed-off-by: Bob <[email protected]>
'@

function Parse-CommitTrailer {
<#
.NOTES
trailers can use duplicate key names, see: <https://git-scm.com/docs/git-interpret-trailers>
#>
[CmdletBinding()]
param( $CommitMessage )

[List[Object]]$GitArgs = @(
'interpret-trailers'
'--parse'
)
$GitArgs
| Join-String -sep ' ' -op 'Invoking GitArgs: '
| write-verbose

[List[Object]]$trailerList = @(
$CommitMessage
# | CountOf 'orig line count'
| git @gitArgs
# | CountOf 'after interpret-trailer'
| %{
$delim = ': '
$Key, $Remaining = $_ -split $Delim, 2

[PSCustomObject]@{
PSTypeName = 'test.Git.Trailer.KeyCollectionElement'
Key = $Key
Value = $Remaining
}
}
)
return [pscustomobject]@{
PSTypeName = 'test.Git.ParsedCommit.Record'
KeyCollection = $trailerList
RawMessage = $CommitMessage
ShortKeys = $trailerList | Group-Object Key
| %{
$_.Group | % Value | Join-string -f "`n {0}" -op $_.Name
}
}
}

'original'
| Microsoft.PowerShell.Utility\Write-Host -fore blue

$sampleCommit

'parsed'
| Microsoft.PowerShell.Utility\Write-Host -fore blue

( $Parsed = Parse-CommitTrailer $sampleCommit ) | ConvertTo-Json
$Parsed.KeyCollection | Format-Table -AutoSize
$Parsed | Format-List

'render shared as collapsed'
$Parsed.ShortKeys
# | Microsoft.PowerShell.Utility\Write-Host -fore blue

# $Items = $parsed.KeyCollection
# $items | Group-Object Key | %{
# $_.Group | % Value | Join-string -f "`n {0}" -op $_.Name
# }

0 comments on commit 89d4912

Please sign in to comment.