|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Strip UTF-8 BOM from selected text files under the current subtree, respecting |
| 4 | +.gitignore. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | +Enumerates tracked and untracked-but-not-ignored files under the current |
| 8 | +directory (via Git), filters to texty extensions and dotfiles, skips likely |
| 9 | +binary files (NUL probe), and removes a leading UTF-8 BOM (EF BB BF) in place. |
| 10 | +
|
| 11 | +Refuses to run if there are uncommitted changes as a safeguard. Use -Force to override. |
| 12 | +Supports -WhatIf/-Confirm via ShouldProcess. |
| 13 | +#> |
| 14 | + |
| 15 | +[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')] |
| 16 | +param( |
| 17 | + [switch]$Force |
| 18 | +) |
| 19 | + |
| 20 | +Set-StrictMode -Version Latest |
| 21 | +$ErrorActionPreference = 'Stop' |
| 22 | + |
| 23 | +# --- File sets (ILSpy) ------------------------------------------------------ |
| 24 | +$Dotfiles = @( |
| 25 | + '.gitignore', '.editorconfig', '.gitattributes', '.gitmodules', |
| 26 | + '.tgitconfig', '.vsconfig' |
| 27 | +) |
| 28 | + |
| 29 | +$AllowedExts = @( |
| 30 | + '.bat','.config','.cs','.csproj','.css','.filelist','.fs','.html','.il', |
| 31 | + '.ipynb','.js','.json','.less','.manifest','.md','.projitems','.props', |
| 32 | + '.ps1','.psd1','.ruleset','.shproj','.sln','.slnf','.svg','.template', |
| 33 | + '.tt', '.txt','.vb','.vsct','.vsixlangpack','.wxl','.xaml','.xml','.xshd','.yml' |
| 34 | +) |
| 35 | + |
| 36 | +$IncludeNoExt = $true # include names like LICENSE |
| 37 | + |
| 38 | +# --- Git checks / enumeration ----------------------------------------------- |
| 39 | +function Assert-InGitWorkTree { |
| 40 | + $inside = (& git rev-parse --is-inside-work-tree 2>$null).Trim() |
| 41 | + if ($LASTEXITCODE -ne 0 -or $inside -ne 'true') { |
| 42 | + throw 'Not in a Git work tree.' |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function Assert-CleanWorkingTree { |
| 47 | + if ($Force) { return } |
| 48 | + |
| 49 | + $status = & git status --porcelain -z |
| 50 | + if ($LASTEXITCODE -ne 0) { throw 'git status failed.' } |
| 51 | + |
| 52 | + if (-not [string]::IsNullOrEmpty($status)) { |
| 53 | + throw 'Working tree not clean. Commit/stash changes or use -Force.' |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function Get-GitFilesUnderPwd { |
| 58 | + Assert-InGitWorkTree |
| 59 | + |
| 60 | + $repoRoot = (& git rev-parse --show-toplevel).Trim() |
| 61 | + $pwdPath = (Get-Location).Path |
| 62 | + |
| 63 | + $tracked = & git -C $repoRoot ls-files -z |
| 64 | + $others = & git -C $repoRoot ls-files --others --exclude-standard -z |
| 65 | + |
| 66 | + $allRel = ("$tracked$others").Split( |
| 67 | + [char]0, [System.StringSplitOptions]::RemoveEmptyEntries) |
| 68 | + |
| 69 | + foreach ($relPath in $allRel) { |
| 70 | + $fullPath = Join-Path $repoRoot $relPath |
| 71 | + if ($fullPath.StartsWith($pwdPath, |
| 72 | + [System.StringComparison]::OrdinalIgnoreCase)) { |
| 73 | + if (Test-Path -LiteralPath $fullPath -PathType Leaf) { |
| 74 | + $fullPath |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +# --- Probes ----------------------------------------------------------------- |
| 81 | +function Test-HasUtf8Bom { |
| 82 | + param([Parameter(Mandatory)][string]$Path) |
| 83 | + |
| 84 | + try { |
| 85 | + $stream = [System.IO.File]::Open($Path,'Open','Read','ReadWrite') |
| 86 | + try { |
| 87 | + if ($stream.Length -lt 3) { return $false } |
| 88 | + |
| 89 | + $header = [byte[]]::new(3) |
| 90 | + [void]$stream.Read($header,0,3) |
| 91 | + |
| 92 | + return ($header[0] -eq 0xEF -and |
| 93 | + $header[1] -eq 0xBB -and |
| 94 | + $header[2] -eq 0xBF) |
| 95 | + } |
| 96 | + finally { |
| 97 | + $stream.Dispose() |
| 98 | + } |
| 99 | + } |
| 100 | + catch { return $false } |
| 101 | +} |
| 102 | + |
| 103 | +function Test-ProbablyBinary { |
| 104 | + # Binary if the first 8 KiB contains any NUL byte. |
| 105 | + param([Parameter(Mandatory)][string]$Path) |
| 106 | + |
| 107 | + try { |
| 108 | + $stream = [System.IO.File]::Open($Path,'Open','Read','ReadWrite') |
| 109 | + try { |
| 110 | + $len = [int][Math]::Min(8192,$stream.Length) |
| 111 | + if ($len -le 0) { return $false } |
| 112 | + |
| 113 | + $buffer = [byte[]]::new($len) |
| 114 | + [void]$stream.Read($buffer,0,$len) |
| 115 | + |
| 116 | + return ($buffer -contains 0) |
| 117 | + } |
| 118 | + finally { |
| 119 | + $stream.Dispose() |
| 120 | + } |
| 121 | + } |
| 122 | + catch { return $false } |
| 123 | +} |
| 124 | + |
| 125 | +# --- Mutation --------------------------------------------------------------- |
| 126 | +function Remove-Utf8BomInPlace { |
| 127 | + # Write the existing buffer from offset 3, no extra full-size allocation. |
| 128 | + param([Parameter(Mandatory)][string]$Path) |
| 129 | + |
| 130 | + $bytes = [System.IO.File]::ReadAllBytes($Path) |
| 131 | + if ($bytes.Length -lt 3) { return $false } |
| 132 | + |
| 133 | + if ($bytes[0] -ne 0xEF -or |
| 134 | + $bytes[1] -ne 0xBB -or |
| 135 | + $bytes[2] -ne 0xBF) { |
| 136 | + return $false |
| 137 | + } |
| 138 | + |
| 139 | + $stream = [System.IO.File]::Open($Path,'Create','Write','ReadWrite') |
| 140 | + try { |
| 141 | + $stream.Write($bytes, 3, $bytes.Length - 3) |
| 142 | + $stream.SetLength($bytes.Length - 3) |
| 143 | + } |
| 144 | + finally { |
| 145 | + $stream.Dispose() |
| 146 | + } |
| 147 | + |
| 148 | + return $true |
| 149 | +} |
| 150 | + |
| 151 | +# --- Main ------------------------------------------------------------------- |
| 152 | +Assert-InGitWorkTree |
| 153 | +Assert-CleanWorkingTree |
| 154 | + |
| 155 | +$allFiles = Get-GitFilesUnderPwd |
| 156 | + |
| 157 | +$targets = $allFiles | % { |
| 158 | + $fileName = [IO.Path]::GetFileName($_) |
| 159 | + $ext = [IO.Path]::GetExtension($fileName) |
| 160 | + |
| 161 | + $isDot = $Dotfiles -contains $fileName |
| 162 | + $isNoExt = -not $fileName.Contains('.') |
| 163 | + |
| 164 | + if ($isDot -or ($AllowedExts -contains $ext) -or |
| 165 | + ($IncludeNoExt -and $isNoExt -and -not $isDot)) { |
| 166 | + $_ |
| 167 | + } |
| 168 | +} |
| 169 | +| ? { Test-HasUtf8Bom $_ } |
| 170 | +| ? { -not (Test-ProbablyBinary $_) } |
| 171 | + |
| 172 | +$changed = 0 |
| 173 | +$byExtension = @{} |
| 174 | +$dotfileChanges = 0 |
| 175 | + |
| 176 | +$targets | % { |
| 177 | + $relative = Resolve-Path -LiteralPath $_ -Relative |
| 178 | + |
| 179 | + if ($PSCmdlet.ShouldProcess($relative,'Strip UTF-8 BOM')) { |
| 180 | + if (Remove-Utf8BomInPlace -Path $_) { |
| 181 | + $changed++ |
| 182 | + |
| 183 | + $fileName = [IO.Path]::GetFileName($_) |
| 184 | + if ($Dotfiles -contains $fileName) { $dotfileChanges++ } |
| 185 | + |
| 186 | + $ext = [IO.Path]::GetExtension($fileName) |
| 187 | + if (-not $byExtension.ContainsKey($ext)) { $byExtension[$ext] = 0 } |
| 188 | + $byExtension[$ext]++ |
| 189 | + |
| 190 | + "stripped BOM: $relative" |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +"Done. Stripped BOM from $changed file(s)." |
| 196 | + |
| 197 | +if ($byExtension.Keys.Count -gt 0) { |
| 198 | + "" |
| 199 | + "By extension:" |
| 200 | + $byExtension.GetEnumerator() | Sort-Object Name | % { |
| 201 | + $key = if ([string]::IsNullOrEmpty($_.Name)) { '[noext]' } else { $_.Name } |
| 202 | + " {0}: {1}" -f $key, $_.Value |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +if ($dotfileChanges -gt 0) { |
| 207 | + " [dotfiles]: $dotfileChanges" |
| 208 | +} |
0 commit comments