Skip to content

Instantly share code, notes, and snippets.

@pferreirafabricio
Created October 26, 2022 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pferreirafabricio/cd1cec3a8ec7e65fac3e9e1ec902201f to your computer and use it in GitHub Desktop.
Save pferreirafabricio/cd1cec3a8ec7e65fac3e9e1ec902201f to your computer and use it in GitHub Desktop.
Powershell script to convert git logs in JSON
# 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