Skip to content

Commit

Permalink
fix: git log parsing improvement ( Fixes #306, Fixes #308, Fixes #309 )
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Sep 28, 2024
1 parent 9cad849 commit cfbef02
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions Extensions/Git.Log.UGit.Extension.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,33 @@ begin {
'@, 'IgnoreCase,IgnorePatternWhitespace', '00:00:05')

$lines = [Collections.Generic.List[string]]::new()
$StartsWithCommit = [Regex]::new('^commit', 'IgnoreCase')
$gitLogTypeName =
if ($gitCommand -match '--merges') {
'git.merge.log'
} else {
'git.log'
}

function OutGitLog {
param([string[]]$OutputLines)
if (-not $OutputLines) { return }
$gitLogMatch = $Git_Log.Match($OutputLines -join [Environment]::NewLine)
if (-not $gitLogMatch.Success) { return }

$gitLogOut = [Ordered]@{PSTypeName='git.log';GitCommand=$gitCommand}
if ($gitCommand -like '*--merges*') {
$gitLogOut.PSTypeName = 'git.merge.log'
}
$gitLogOut = [Ordered]@{PSTypeName=$gitLogTypeName;GitCommand=$gitCommand}
foreach ($group in $gitLogMatch.Groups) {
if ($group.Name -as [int] -ne $null) { continue }
if (-not $gitLogOut.Contains($group.Name)) {
$gitLogOut[$group.Name] = $group.Value
$gitLogOut[$group.Name] = $group.Value
} else {
$gitLogOut[$group.Name] = @( $gitLogOut[$group.Name] ) + $group.Value
$gitLogOut[$group.Name] = @( $gitLogOut[$group.Name] ) + $group.Value
}
}
$gitLogOut.Remove("HexDigits")
if ($gitLogOut.CommitDate) {
$gitLogOut.CommitDate = [datetime]::ParseExact($gitLogOut.CommitDate.Trim(), "ddd MMM d HH:mm:ss yyyy K", [cultureinfo]::InvariantCulture)
$gitLogOut.CommitDateString = $gitLogOut.CommitDate
$gitLogOut.Remove("CommitDate")
}
if ($gitLogOut.CommitMessage) {
$gitLogOut.CommitMessage = $gitLogOut.CommitMessage.Trim()
Expand Down Expand Up @@ -136,47 +141,30 @@ begin {
if (-not $gitLogOut.Insertions) {
$gitLogOut.Insertions = 0
}
}

if ($gitArgument -contains '--stat') {
$gitLogOut.Changes = @()
foreach ($outLine in $OutputLines) {
if ($outLine -notlike ' *|*') { continue }
$nameOfFile, $fileChanges = $outLine -split '\|'
$nameOfFile = $nameOfFile -replace '^\s+' -replace '\s+$'
$match = [Regex]::Match($fileChanges, "(?<c>\d+)\s(?<i>\+{0,})(?<d>\-{0,})")
$linesChanged = $match.Groups["c"].Value -as [int]
$linesInserted = $match.Groups["i"].Length
$linesDeleted = $match.Groups["d"].Length
$gitLogOut.Changes +=
[PSCustomObject][Ordered]@{
FilePath = $nameOfFile
LinesChanged = $linesChanged
LinesInserted = $linesInserted
LinesDeleted = $linesDeleted
}
}
}
}

$gitLogOut.GitOutputLines = $OutputLines
$gitLogOut.Merged = $script:LogChangesMerged
$gitLogOut.GitRoot = $GitRoot
[PSCustomObject]$gitLogOut
}

$shouldSkip = $gitCommand -match '--(?>pretty|format)'
}


process {
if ($gitCommand -match '--(?>pretty|format)') {
if ($shouldSkip) {
continue
}
}

if ("$gitOut" -like 'Commit*' -and $lines) {
$gitOutString = "$gitOut"
if ($lines.Count -and $gitOutString -match $StartsWithCommit) {
OutGitLog $lines

$lines = [Collections.Generic.List[string]]::new()
}
$lines.Add("$gitOut")
$lines.Add($gitOutString)
}

end {
Expand Down

0 comments on commit cfbef02

Please sign in to comment.