Skip to content

Commit ff3e1be

Browse files
committed
git diff --numstat gives different error in newer versions of git. make it not crash on warnings for which no code exists
1 parent a2a4f82 commit ff3e1be

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

Tests/Parse-GitStatus-WithNumstat.Tests.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,40 @@ Describe 'Parse-GitStatus - with Numstat' {
8484

8585
$result.lineEndings | Should -Be 'LF -> CRLF'
8686
}
87+
88+
It 'does not crash when there is some new, unexpected, warning' {
89+
# This test fails on the CI because of Write-Error resulting in a WriteErrorException
90+
# Adding -ErrorAction silentlycontinue already made it crash locally
91+
Mock Invoke-Git {
92+
if (([string]$args).StartsWith("status")) {
93+
" M TestDrive:\file0"
94+
} else {
95+
# git diff --numstat
96+
Write-Error "warning: this is completely unexpected"
97+
" `t5`t3`tTestDrive:\file0"
98+
}
99+
}
100+
101+
$result = Parse-GitStatus $true
102+
103+
$result.lineEndings | Should -Be $null
104+
}
105+
106+
It 'adds LF/CRLF warnings to the fileInfo (updated error message for git 2.26+)' {
107+
# This test fails on the CI because of Write-Error resulting in a WriteErrorException
108+
# Adding -ErrorAction silentlycontinue already made it crash locally
109+
Mock Invoke-Git {
110+
if (([string]$args).StartsWith("status")) {
111+
" M TestDrive:\file0"
112+
} else {
113+
# git diff --numstat
114+
Write-Error "warning: in the working copy of 'TestDrive:\file0', LF will be replaced by CRLF the next time Git touches it"
115+
" `t5`t3`tTestDrive:\file0"
116+
}
117+
}
118+
119+
$result = Parse-GitStatus $true
120+
121+
$result.lineEndings | Should -Be 'LF -> CRLF'
122+
}
87123
}

lib/Parse-GitStatus.ps1

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,16 @@ function Add-GitNumstat($allFiles, $staged) {
120120

121121
} elseif ($line.StartsWith('warning: ')) {
122122
# Add EOL notice
123-
$match = $line | Select-String -Pattern 'warning: (\w+) will be replaced by (\w+) in (.*)\.'
124-
$fromEol = $match.matches.groups[1]
125-
$toEol = $match.matches.groups[2]
126-
$fileName = $match.matches.groups[3].Value.Replace('/', '\')
127-
$matchingStatus = $allFiles | Where {$_.file -eq $fileName}
128-
if ($matchingStatus) {
129-
$matchingStatus | Add-Member -NotePropertyName 'lineEndings' -NotePropertyValue "$fromEol -> $toEol" -Force
123+
$match = $line | Select-String -Pattern "warning: (?<from>\w+) will be replaced by (?<to>\w+) in (?<file>.*)\.|warning: in the working copy of '(?<file>.*)', (?<from>\w+) will be replaced by (?<to>\w+) the next time Git touches it"
124+
125+
if ($match) {
126+
$fromEol = $match.matches[0].groups['from']
127+
$toEol = $match.matches[0].groups['to']
128+
$fileName = $match.matches[0].groups['file'].Value.Replace('/', '\')
129+
$matchingStatus = $allFiles | Where {$_.file -eq $fileName}
130+
if ($matchingStatus) {
131+
$matchingStatus | Add-Member -NotePropertyName 'lineEndings' -NotePropertyValue "$fromEol -> $toEol" -Force
132+
}
130133
}
131134
}
132135
}

0 commit comments

Comments
 (0)