Created
October 26, 2022 20:33
-
-
Save pferreirafabricio/cd1cec3a8ec7e65fac3e9e1ec902201f to your computer and use it in GitHub Desktop.
Powershell script to convert git logs in JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Credits: https://stackoverflow.com/a/49515414/12542704 | |
$header = @("commit", "tree", "parent", "refs", "subject", "body", "author", "commiter") | |
[string] $gitLogs = (git --no-pager log -n 10 --no-merges --pretty=format:'%H|%T|%P|%D|%s|%b|%an|%cn;') | |
$replacedArray = $gitLogs.Replace("; ", ';') -split ";", 0, "multiline"; | |
$handledLogs = foreach ($commit in $replacedArray) { | |
$prop = $commit -split "\|" | |
$hash = [ordered]@{} | |
for ($index = 0; $index -lt $header.Count; $index++) { | |
# Example: author: 'John' | |
$hash.add($header[$index], $prop[$index]) | |
} | |
[pscustomobject]$hash | |
} | |
$convertedLogs = $handledLogs | ConvertTo-Json -Depth 10 | |
$convertedLogs | % { Write-Output $_ } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment