diff --git a/Dist/git_hook_shims/commit-msg b/Dist/git_hook_shims/commit-msg new file mode 100755 index 0000000000..943f0ed582 --- /dev/null +++ b/Dist/git_hook_shims/commit-msg @@ -0,0 +1,12 @@ +@{a="\"} >$null # " 2>/dev/null || true #" >NUL 2>&1 || TYPE NUL & ECHO OFF + +echo \" <<'BATCH_SCRIPT' >/dev/null ">NUL "\" +dotnet pwsh .\Dist\git_hooks\commit-msg.ps1 %* +GOTO :eof +BATCH_SCRIPT +# else this is BASH +# heredoc trick for polyglot taken from https://github.com/llamasoft/polyshell#how-it-works -- this script is basically that sans PowerShell support (since that's not the shell on Windows) +# improved Batch `ECHO OFF` taken from https://github.com/tingstad/polyscript#explanation + +dotnet pwsh "./Dist/git_hooks/$(basename "$0").ps1" "$@" +exit $? diff --git a/Dist/git_hooks/commit-msg b/Dist/git_hooks/commit-msg.ps1 similarity index 100% rename from Dist/git_hooks/commit-msg rename to Dist/git_hooks/commit-msg.ps1 diff --git a/Dist/install_git_hooks.ps1 b/Dist/install_git_hooks.ps1 index dbae733b31..571e9b3aac 100644 --- a/Dist/install_git_hooks.ps1 +++ b/Dist/install_git_hooks.ps1 @@ -1,24 +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") { + foreach ($f in Get-ChildItem "$PSScriptRoot/git_hook_shims") { $target = Join-Path $targetDir $f.Name - if (Test-Path $target -PathType Leaf) { # target file exists - if ($(Get-FileHash $target).Hash -ne $(Get-FileHash $f.FullName).Hash) { # files differ - $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 - } - } - } else { + if (!(Test-Path $target -PathType Leaf)) { # target file doesn't exist echo "[$PSCommandFilename] creating Git hook $($f.Name)" Copy-Item $f $target + #TODO generate shim? the only difference between different shims would be the filename in the Batch part (and if there was an equivalent to `basename $0` then that would be the same too + #TODO use symlinks on Linux + } elseif ($(Get-FileHash $target).Hash -ne $(Get-FileHash $f.FullName).Hash) { # files differ + $head = Get-Content $target -TotalCount 3 + echo "[$PSCommandFilename] found existing Git hook $($f.Name), please resolve conflict manually" + #TODO should REALLY make the scripts extensible then... + exit 1 } + # else no-op } }