-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a polyglot BASH/Batch script as a shim for the Git commit hook
- Loading branch information
Showing
3 changed files
with
22 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 $? |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |