Automatically install client-side Git hooks

This is how npm-style RCE hacks happen by the way. You should all be
vetting `dotnet-tools.json` and all MSBuild and PowerShell scripts.
This commit is contained in:
YoshiRulz 2024-06-07 15:01:11 +10:00
parent c6270241f5
commit 5c31711710
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 36 additions and 0 deletions

14
Dist/git_hooks/commit-msg Executable file
View File

@ -0,0 +1,14 @@
#!/usr/bin/env -S dotnet pwsh
# placed here by BizHawk build scripts and may be updated automatically
$msg = Get-Content $args[0] -TotalCount 1 # this commit hook is always passed the commit message scratch file's path, so read the first line of that
if ($msg -Match "^fix(?:ed|es)? #\d+$") {
echo "An issue reference alone is not a suitable commit message. Vetoed."
exit 1
}
if ($msg.Length -lt 20) { # arbitrary
if ($msg.Length -lt 8) { # semi-arbitrary; I figured "Fix typo" would be the shortest reasonable message --yoshi
echo "Your commit message is too short. Vetoed."
exit 1
}
echo "Your commit message is a bit short. Do you have more to add? (If you included a longer description already, ignore this.)"
}

View File

@ -0,0 +1,19 @@
$targetDir = "$PSScriptRoot/../.git/hooks"
if (Test-Path $targetDir -PathType Container) { # is Git repo
$magicHeader = '# placed here by BizHawk build scripts and may be updated automatically'
$PSCommandFilename = Split-Path $PSCommandPath -Leaf
foreach ($f in Get-ChildItem "$PSScriptRoot/git_hooks") {
$target = Join-Path $targetDir $f.Name
if ($(Get-FileHash $target).Hash -ne $(Get-FileHash $f.FullName).Hash) {
$head = Get-Content $target -TotalCount 3
if ($magicHeader -in $head) {
echo "[$PSCommandFilename] updating existing Git hook $($f.Name)"
Copy-Item $f $target
} else {
echo "[$PSCommandFilename] found existing Git hook $($f.Name), please resolve conflict manually"
# should probably make the scripts extensible then...
exit 1
}
}
}
}

View File

@ -16,4 +16,7 @@
<ItemGroup>
<Analyzer Include="$(MSBuildProjectDirectory)/../../References/BizHawk.SrcGen.VersionInfo.dll" />
</ItemGroup>
<Target Name="InstallGitHooks" BeforeTargets="PreBuildEvent">
<Exec Command="dotnet pwsh $(MSBuildProjectDirectory)/../../Dist/install_git_hooks.ps1" />
</Target>
</Project>