From 5c317117107f9719c13e29f9da171d6c9ca165d8 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Fri, 7 Jun 2024 15:01:11 +1000 Subject: [PATCH] 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. --- Dist/git_hooks/commit-msg | 14 ++++++++++++++ Dist/install_git_hooks.ps1 | 19 +++++++++++++++++++ src/BizHawk.Common/BizHawk.Common.csproj | 3 +++ 3 files changed, 36 insertions(+) create mode 100755 Dist/git_hooks/commit-msg create mode 100644 Dist/install_git_hooks.ps1 diff --git a/Dist/git_hooks/commit-msg b/Dist/git_hooks/commit-msg new file mode 100755 index 0000000000..6e96f0fc1e --- /dev/null +++ b/Dist/git_hooks/commit-msg @@ -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.)" +} diff --git a/Dist/install_git_hooks.ps1 b/Dist/install_git_hooks.ps1 new file mode 100644 index 0000000000..2d2d4cd870 --- /dev/null +++ b/Dist/install_git_hooks.ps1 @@ -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 + } + } + } +} diff --git a/src/BizHawk.Common/BizHawk.Common.csproj b/src/BizHawk.Common/BizHawk.Common.csproj index 24c4133698..45ee07b38e 100644 --- a/src/BizHawk.Common/BizHawk.Common.csproj +++ b/src/BizHawk.Common/BizHawk.Common.csproj @@ -16,4 +16,7 @@ + + +