Skip to content

Instantly share code, notes, and snippets.

@mhudasch
Created November 9, 2016 16:45
Show Gist options
  • Save mhudasch/3fa3ee22d514d74dfd356efb95a60fbb to your computer and use it in GitHub Desktop.
Save mhudasch/3fa3ee22d514d74dfd356efb95a60fbb to your computer and use it in GitHub Desktop.
Git local pre-commit hook script that ensures correct file encoding
function Get-FileEncoding {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] [string]$Path
)
[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path
$encoding = 'utf8';
if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf )
{ $encoding = 'utf8-with-bom'; }
elseif (($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) <#big endian#>)
{ $encoding = 'bigendian-unicode'; }
elseif (($byte[0] -eq 0xff -and $byte[1] -eq 0xfe) <#lower endian#>)
{ $encoding = 'unicode'; }
elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff <#big endian#>)
{ $encoding = 'bigendian-utf32' }
elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xff -and $byte[3] -eq 0xfe <#lower endian#>)
{ $encoding = 'utf32'; }
elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76)
{ $encoding = 'utf7'; }
return $encoding;
}
$files = (& cmd /c "git diff --name-only --diff-filter=ACM | git check-attr --stdin encoding");
$split = $files | ForEach-Object {
$s = $_.Split(":");
$set = @{ File=$s[0].Trim();Attr=$s[1].Trim();Info=$s[2].Trim(); };
if($set.Info -match "unspecified" -or $set.Info -match "unset") { return; }
$guessed = (Get-FileEncoding -Path $set.File);
$allowed = @("utf8","utf8-with-bom","bigendian-unicode","unicode","bigendian-utf32","utf32","utf7");
if($allowed -notcontains $set.Info) {
Write-Error "Target encoding $($set.Info) for $($set.File) is unknown allowed are: $([string]::Join(',', $allowed)).";
exit 1;
}
if($guessed -notmatch $set.Info) {
Write-Host $($set.File);
Write-Host ": Encoding $($guessed) -> $($set.Info).";
$content = Get-Content -Path $set.File -Encoding $($guessed.Replace("-",""));
if($set.Info -match "utf8") {
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False);
$p = Join-Path $(Get-Location).Path $set.File;
[System.IO.File]::WriteAllLines($p, $content, $Utf8NoBomEncoding);
} elseif($set.Info -match "utf8-with-bom") {
Set-Content -Path $set.File -Encoding UTF8 -Value $content;
} else {
Set-Content -Path $set.File -Encoding $($set.Info.Replace("-","")) -Value $content;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment