neon-sprawl/scripts/install-git-hooks.ps1

31 lines
1.2 KiB
PowerShell

# Installs repo pre-commit and pre-push hooks. Same behavior as install-git-hooks.sh.
# Run from repo root: pwsh -File scripts/install-git-hooks.ps1
$ErrorActionPreference = "Stop"
$repoRoot = (git rev-parse --show-toplevel).Trim()
$hookDir = Join-Path $repoRoot ".git/hooks"
$preCommitHookPath = Join-Path $hookDir "pre-commit"
$hookPath = Join-Path $hookDir "pre-push"
New-Item -ItemType Directory -Force -Path $hookDir | Out-Null
$unixRoot = $repoRoot -replace "\\", "/"
$preCommitLines = @(
"#!/bin/sh",
"set -e",
"repo_root=`"$unixRoot`"",
'cd "$repo_root" || exit 1',
'exec sh "$repo_root/scripts/git-hooks/pre-commit" "$@"'
)
$preCommitContent = ($preCommitLines -join "`n") + "`n"
$pushLines = @(
"#!/bin/sh",
"set -e",
"repo_root=`"$unixRoot`"",
'cd "$repo_root" || exit 1',
'exec sh "$repo_root/scripts/git-hooks/pre-push" "$@"'
)
$content = ($pushLines -join "`n") + "`n"
$utf8 = New-Object System.Text.UTF8Encoding $false
[System.IO.File]::WriteAllText($preCommitHookPath, $preCommitContent, $utf8)
[System.IO.File]::WriteAllText($hookPath, $content, $utf8)
Write-Host "Installed pre-commit hook at $preCommitHookPath"
Write-Host "Installed pre-push hook at $hookPath"