From e9ed0a5b45b2d1580632cd5038ed7358d88d8ce0 Mon Sep 17 00:00:00 2001 From: Edwin Kofler Date: Sat, 14 Oct 2023 05:35:59 -0700 Subject: [PATCH] fix: Recursively search parent directories for lefthook --- internal/templates/hook.tmpl | 62 +++++++++++++++++------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/internal/templates/hook.tmpl b/internal/templates/hook.tmpl index 5c79da1c..7a26f2ca 100644 --- a/internal/templates/hook.tmpl +++ b/internal/templates/hook.tmpl @@ -9,45 +9,33 @@ fi [ -f {{.Rc}} ] && . {{.Rc}} {{- end}} -call_lefthook() -{ - dir="$(git rev-parse --show-toplevel)" +call_lefthook() { osArch=$(uname | tr '[:upper:]' '[:lower:]') cpuArch=$(uname -m | sed 's/aarch64/arm64/') - if lefthook{{.Extension}} -h >/dev/null 2>&1 - then - lefthook{{.Extension}} "$@" + if command -v lefthook{{.Extension}} >/dev/null 2>&1; then + exec lefthook{{.Extension}} "$@" {{if .Extension -}} {{/* Check if lefthook.bat exists. Ruby bundler creates such a wrapper */ -}} - elif lefthook.bat -h >/dev/null 2>&1 - then - lefthook.bat "$@" + elif command -v lefthook.bat >/dev/null 2>&1; then + exec lefthook.bat "$@" {{end -}} - elif test -f "$dir/node_modules/lefthook/bin/index.js" - then - "$dir/node_modules/lefthook/bin/index.js" "$@" - elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" - then - "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" "$@" - elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" - then - "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" "$@" - elif bundle exec lefthook -h >/dev/null 2>&1 - then - bundle exec lefthook "$@" - elif yarn lefthook -h >/dev/null 2>&1 - then - yarn lefthook "$@" - elif pnpm lefthook -h >/dev/null 2>&1 - then - pnpm lefthook "$@" - elif command -v npx >/dev/null 2>&1 - then - npx @evilmartians/lefthook "$@" - elif swift package plugin lefthook >/dev/null 2>&1 - then - swift package --disable-sandbox plugin lefthook "$@" + fi + + try_exec "./node_modules/lefthook/bin/index.js" "$@" + try_exec "./node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" "$@" + try_exec "./node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" "$@" + + if bundle exec lefthook -h >/dev/null 2>&1; then + exec bundle exec lefthook "$@" + elif yarn lefthook -h >/dev/null 2>&1; then + exec yarn lefthook "$@" + elif pnpm lefthook -h >/dev/null 2>&1; then + exec pnpm lefthook "$@" + elif command -v npx >/dev/null 2>&1; then + exec npx @evilmartians/lefthook "$@" + elif swift package plugin lefthook >/dev/null 2>&1; then + exec swift package --disable-sandbox plugin lefthook "$@" else echo "Can't find lefthook in PATH" {{- if .AssertLefthookInstalled}} @@ -59,4 +47,12 @@ call_lefthook() fi } +try_exec() { + _path=$1; shift + + if [ -x "$_path" ]; then + exec "$_path" "$@" + fi +} + call_lefthook run "{{.HookName}}" "$@"