diff --git a/internal/templates/hook.tmpl b/internal/templates/hook.tmpl index af750cd5..ad022390 100644 --- a/internal/templates/hook.tmpl +++ b/internal/templates/hook.tmpl @@ -10,42 +10,33 @@ set -a [ -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 + elif lefthook.bat -h >/dev/null 2>&1; then lefthook.bat "$@" + return {{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 "$@" + fi + + gitDir=$(git rev-parse --show-toplevel) + 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 "$@" else echo "Can't find lefthook in PATH" {{- if .AssertLefthookInstalled}} @@ -57,4 +48,23 @@ call_lefthook() fi } +try_exec() { + _path=$1; shift + _orig_pwd=$PWD + + while [ ! -x "$_path" ]; do + if [ "$PWD" = / ] || [ "$PWD" = "$gitDir" ]; then + CDPATH= cd -- "$_orig_pwd" + return 1 + fi + + if ! CDPATH= cd ..; then + CDPATH= cd -- "$_orig_pwd" + return 1 + fi + done + + exec "$_path" "$@" +} + call_lefthook run "{{.HookName}}" "$@"