Skip to content

Commit

Permalink
fix: Recursively search parent directories for lefthook
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperupcall committed Sep 22, 2023
1 parent b67182f commit 5e52c52
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions internal/templates/hook.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,32 @@ 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
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 "$@"
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}}
Expand All @@ -57,4 +47,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}}" "$@"

0 comments on commit 5e52c52

Please sign in to comment.