diff --git a/Build/Agent/check-and-fix-whitespace.ps1 b/Build/Agent/check-and-fix-whitespace.ps1 index 95edb1d09c..6d75df4c4a 100644 --- a/Build/Agent/check-and-fix-whitespace.ps1 +++ b/Build/Agent/check-and-fix-whitespace.ps1 @@ -1,9 +1,18 @@ #!/usr/bin/env pwsh $ErrorActionPreference = 'Continue' +if (Test-Path -LiteralPath 'check-results.log') { + Remove-Item -LiteralPath 'check-results.log' -Force +} + & "$PSScriptRoot/check-whitespace.ps1" $ec = $LASTEXITCODE -& "$PSScriptRoot/fix-whitespace.ps1" +if ($ec -eq 0 -or (Test-Path -LiteralPath 'check-results.log')) { + & "$PSScriptRoot/fix-whitespace.ps1" +} +else { + Write-Error 'Whitespace checker failed before producing results; skipping fixer.' +} exit $ec diff --git a/Build/Agent/check-whitespace.ps1 b/Build/Agent/check-whitespace.ps1 index dcfd38ea5d..b31f46b1f7 100644 --- a/Build/Agent/check-whitespace.ps1 +++ b/Build/Agent/check-whitespace.ps1 @@ -38,6 +38,9 @@ Write-Host "Base ref: $baseRef ($baseSha)" $log = git log --check --pretty=format:'---% h% s' "$baseSha.." 2>&1 $null = $log | Tee-Object -FilePath check-results.log $log | Out-Host + if (-not (Test-Path -LiteralPath 'check-results.log')) { + New-Item -ItemType File -Path 'check-results.log' -Force | Out-Null + } $problems = New-Object System.Collections.Generic.List[string] $commit = '' @@ -46,7 +49,7 @@ $commitTextmd = '' $repoPath = Get-RepoPath $headRef = (git rev-parse --abbrev-ref HEAD 2>$null) -Get-Content check-results.log | ForEach-Object { + $log | ForEach-Object { $line = $_ switch -regex ($line) { '^---\s' { @@ -83,7 +86,7 @@ Get-Content check-results.log | ForEach-Object { } if ($problems.Count -gt 0) { - Write-Host "`u26A0`uFE0F Please review the output for further information." + Write-Host '[WARN] Please review the output for further information.' Write-Host '### A whitespace issue was found in one or more of the commits.' Write-Host 'This check validates commit history from origin/main..HEAD, not just the current working tree.' Write-Host 'If the report names an older commit, fix the file and then amend, squash, or rebase so that commit no longer appears in the branch history.' diff --git a/Build/Agent/fix-whitespace.ps1 b/Build/Agent/fix-whitespace.ps1 index 324a254a2f..821b62f6fc 100644 --- a/Build/Agent/fix-whitespace.ps1 +++ b/Build/Agent/fix-whitespace.ps1 @@ -78,7 +78,7 @@ if (Test-Path -LiteralPath 'check-results.log') { if (-not $fixFiles -or $fixFiles.Count -eq 0) { $base = Get-BaseRef Write-Host "Fixing whitespace for files changed since $base..HEAD" - $fixFiles = git diff --name-only "$base"..HEAD + $fixFiles = git diff --name-only $base HEAD } $files = $fixFiles | Where-Object { $_ -and (Test-Path $_) }