|
| 1 | +# SPDX-FileCopyrightText: 2020-2025 VerifyEncoding contributors <https://github.com/ForNeVeR/VerifyEncoding> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +<# |
| 6 | +.SYNOPSIS |
| 7 | + This function will verify that there's no UTF-8 BOM or CRLF line endings in the files inside of the project. |
| 8 | +#> |
| 9 | + |
| 10 | +param ( |
| 11 | + [string] $SourceRoot, |
| 12 | + [switch] $Autofix, |
| 13 | + [string[]] $ExcludeExtensions = @( |
| 14 | + '.dotsettings' |
| 15 | +) |
| 16 | +) |
| 17 | + |
| 18 | +function Test-Encoding |
| 19 | +{ |
| 20 | + param ( |
| 21 | + # Path to the repository root. All text files under the root will be checked for UTF-8 BOM and CRLF. |
| 22 | + # |
| 23 | + # By default (if nothing's passed), the script will try auto-detecting the nearest Git root. |
| 24 | + [string] $SourceRoot, |
| 25 | + |
| 26 | + # Makes the script to perform file modifications to bring them to the standard. |
| 27 | + [switch] $Autofix, |
| 28 | + |
| 29 | + # List of file extensions (with leading dots) to ignore. Case-insensitive. |
| 30 | + [string[]] $ExcludeExtensions = @( |
| 31 | + '.dotsettings' |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + Set-StrictMode -Version Latest |
| 36 | + $ErrorActionPreference = 'Stop' |
| 37 | + |
| 38 | + if (!$SourceRoot) |
| 39 | + { |
| 40 | + $SourceRoot = git rev-parse --show-toplevel |
| 41 | + |
| 42 | + if (!$?) |
| 43 | + { |
| 44 | + throw "Cannot call `"git rev-parse`": exit code $LASTEXITCODE." |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + # For PowerShell to properly process the UTF-8 output from git ls-tree we need to set up the output encoding: |
| 49 | + [Console]::OutputEncoding = [Text.Encoding]::UTF8 |
| 50 | + |
| 51 | + try |
| 52 | + { |
| 53 | + Push-Location $SourceRoot |
| 54 | + |
| 55 | + # Step 1: Get all file paths from git |
| 56 | + $gitFiles = git -c core.quotepath=off ls-tree -r HEAD --name-only |
| 57 | + |
| 58 | + # Step 2: Filter out deleted files |
| 59 | + $existingFiles = $gitFiles | Where-Object { Test-Path -LiteralPath $_ } |
| 60 | + |
| 61 | + # Step 3: Filter out directories (keep only files) |
| 62 | + $allFiles = @($existingFiles | Where-Object { -not (Get-Item -Force -LiteralPath $_).PSIsContainer }) |
| 63 | + |
| 64 | + if (!$?) |
| 65 | + { |
| 66 | + throw "Cannot call `"git ls-tree`": exit code $LASTEXITCODE." |
| 67 | + } |
| 68 | + |
| 69 | + Write-Output "Total files in the repository: $($allFiles.Length)" |
| 70 | + |
| 71 | + $counter = [pscustomobject]@{ Value = 0 } |
| 72 | + |
| 73 | + $groupSize = 50 |
| 74 | + |
| 75 | + $chunks = @($allFiles | Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) }) |
| 76 | + |
| 77 | + Write-Output "Split into $( $chunks.Count ) chunks." |
| 78 | + |
| 79 | + # https://stackoverflow.com/questions/6119956/how-to-determine-if-git-handles-a-file-as-binary-or-as-text#comment15281840_6134127 |
| 80 | + $nullHash = '4b825dc642cb6eb9a060e54bf8d69288fbee4904' |
| 81 | + |
| 82 | + $textFiles = @($chunks | ForEach-Object { |
| 83 | + $chunk = $_.Group |
| 84 | + $filePaths = git -c core.quotepath=off diff --numstat $nullHash HEAD -- @chunk |
| 85 | + if (!$?) |
| 86 | + { |
| 87 | + throw "Cannot call `"git diff`": exit code $LASTEXITCODE." |
| 88 | + } |
| 89 | + $filePaths | |
| 90 | + Where-Object { -not $_.StartsWith('-') } | |
| 91 | + ForEach-Object { [Regex]::Unescape($_.Split("`t", 3)[2]) } |
| 92 | + }) |
| 93 | + |
| 94 | + Write-Output "Text files in the repository: $( $textFiles.Length )" |
| 95 | + |
| 96 | + $bom = @(0xEF, 0xBB, 0xBF) |
| 97 | + $bomErrors = @() |
| 98 | + $lineEndingErrors = @() |
| 99 | + |
| 100 | + foreach ($file in $textFiles) |
| 101 | + { |
| 102 | + if ($ExcludeExtensions -contains [IO.Path]::GetExtension($file).ToLowerInvariant()) |
| 103 | + { |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + $fullPath = Resolve-Path -LiteralPath $file |
| 108 | + |
| 109 | + $bytes = [IO.File]::ReadAllBytes($fullPath) | Select-Object -First $bom.Length |
| 110 | + |
| 111 | + if (!$bytes) |
| 112 | + { |
| 113 | + continue |
| 114 | + } # filter empty files |
| 115 | + |
| 116 | + $bytesEqualsBom = @(Compare-Object $bytes $bom -SyncWindow 0).Length -eq 0 |
| 117 | + |
| 118 | + if ($bytesEqualsBom -and $Autofix) |
| 119 | + { |
| 120 | + $fullContent = [IO.File]::ReadAllBytes($fullPath) |
| 121 | + $newContent = $fullContent | Select-Object -Skip $bom.Length |
| 122 | + [IO.File]::WriteAllBytes($fullPath, $newContent) |
| 123 | + Write-Output "Removed UTF-8 BOM from file $file" |
| 124 | + } |
| 125 | + elseif ($bytesEqualsBom) |
| 126 | + { |
| 127 | + $bomErrors += @($file) |
| 128 | + } |
| 129 | + |
| 130 | + $text = [IO.File]::ReadAllText($fullPath) |
| 131 | + |
| 132 | + $crlf = "`r`n" |
| 133 | + $lf = "`n" |
| 134 | + $cr = "`r" |
| 135 | + |
| 136 | + $hasWrongLineEndings = $text.Contains($crlf) -or $text.Contains($cr) |
| 137 | + |
| 138 | + if ($hasWrongLineEndings -and $Autofix) |
| 139 | + { |
| 140 | + $newText = $text -replace $crlf, $lf -replace $cr, $lf |
| 141 | + [IO.File]::WriteAllText($fullPath, $newText) |
| 142 | + Write-Output "Fixed the line endings for file $file" |
| 143 | + } |
| 144 | + elseif ($hasWrongLineEndings) |
| 145 | + { |
| 146 | + $lineEndingErrors += @($file) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if ($bomErrors.Length) |
| 151 | + { |
| 152 | + throw "The following $( $bomErrors.Length ) files have UTF-8 BOM:`n" + ($bomErrors -join "`n") |
| 153 | + } |
| 154 | + if ($lineEndingErrors.Length) |
| 155 | + { |
| 156 | + throw "The following $( $lineEndingErrors.Length ) files have CRLF instead of LF:`n" + ($lineEndingErrors -join "`n") |
| 157 | + } |
| 158 | + } |
| 159 | + finally |
| 160 | + { |
| 161 | + Pop-Location |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +# Convenience launch mode when not invoked as part of a module: |
| 166 | +if (!$MyInvocation.PSCommandPath -or !$MyInvocation.PSCommandPath.EndsWith('.psm1')) { |
| 167 | + Write-Output "Direct script launcher mode.$(if ($MyInvocation.PSCommandPath) { |
| 168 | + ' Launched from "' + $MyInvocation.PSCommandPath + '".' |
| 169 | + })" |
| 170 | + Test-Encoding -SourceRoot:$SourceRoot -Autofix:$Autofix -ExcludedExtensions:$ExcludeExtensions |
| 171 | +} |
0 commit comments