From 5310679f904e0c1aba0ac32832c27b67388c5d9e Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sat, 15 May 2021 11:10:24 -0500 Subject: [PATCH] Fix lefthook binary extension on Windows This commit has been extracted from https://github.com/evilmartians/lefthook/pull/188 --- cmd/templates.go | 12 +++++++++++- cmd/templates/hook.tmpl | 16 ++++++++-------- spec/fixtures/pre-commit | 12 ++++++------ spec/fixtures/pre-push | 12 ++++++------ 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/cmd/templates.go b/cmd/templates.go index 5540dfe6..eca29f81 100644 --- a/cmd/templates.go +++ b/cmd/templates.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "embed" + "runtime" "text/template" "github.com/spf13/afero" @@ -14,6 +15,7 @@ var templatesFS embed.FS type hookTmplData struct { AutoInstall string HookName string + Extension string } func hookTemplate(hookName string, fs afero.Fs) []byte { @@ -22,6 +24,7 @@ func hookTemplate(hookName string, fs afero.Fs) []byte { err := t.Execute(buf, hookTmplData{ AutoInstall: autoInstall(hookName, fs), HookName: hookName, + Extension: getExtension(), }) check(err) @@ -40,5 +43,12 @@ func autoInstall(hookName string, fs afero.Fs) string { return "" } - return "# lefthook_version: " + configChecksum(fs) + "\n\ncall_lefthook \"lefthook install\"" + return "# lefthook_version: " + configChecksum(fs) + "\n\ncall_lefthook \"install\"" +} + +func getExtension() string { + if runtime.GOOS == "windows" { + return ".exe" + } + return "" } diff --git a/cmd/templates/hook.tmpl b/cmd/templates/hook.tmpl index ffc0738c..f731a278 100644 --- a/cmd/templates/hook.tmpl +++ b/cmd/templates/hook.tmpl @@ -12,21 +12,21 @@ dir="$(git rev-parse --show-toplevel)" call_lefthook() { - if lefthook -h >/dev/null 2>&1 + if lefthook{{.Extension}} -h >/dev/null 2>&1 then - eval $1 - elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook" + eval lefthook{{.Extension}} $1 + elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook{{.Extension}}" then - eval $dir/node_modules/@arkweid/lefthook/bin/$1 + eval "$dir/node_modules/@arkweid/lefthook/bin/lefthook{{.Extension}} $1" elif bundle exec lefthook -h >/dev/null 2>&1 then - bundle exec $1 + bundle exec lefthook $1 elif npx lefthook -h >/dev/null 2>&1 then - npx $1 + npx lefthook $1 elif yarn lefthook -h >/dev/null 2>&1 then - yarn $1 + yarn lefthook $1 else echo "Can't find lefthook in PATH" fi @@ -34,4 +34,4 @@ call_lefthook() {{.AutoInstall}} -call_lefthook "lefthook run {{.HookName}} $@" +call_lefthook "run {{.HookName}} $@" diff --git a/spec/fixtures/pre-commit b/spec/fixtures/pre-commit index 53e4d47c..c887993d 100644 --- a/spec/fixtures/pre-commit +++ b/spec/fixtures/pre-commit @@ -14,19 +14,19 @@ call_lefthook() { if lefthook -h >/dev/null 2>&1 then - eval $1 + eval lefthook $1 elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook" then - eval $dir/node_modules/@arkweid/lefthook/bin/$1 + eval "$dir/node_modules/@arkweid/lefthook/bin/lefthook $1" elif bundle exec lefthook -h >/dev/null 2>&1 then - bundle exec $1 + bundle exec lefthook $1 elif npx lefthook -h >/dev/null 2>&1 then - npx $1 + npx lefthook $1 elif yarn lefthook -h >/dev/null 2>&1 then - yarn $1 + yarn lefthook $1 else echo "Can't find lefthook in PATH" fi @@ -34,4 +34,4 @@ call_lefthook() -call_lefthook "lefthook run pre-commit $@" +call_lefthook "run pre-commit $@" diff --git a/spec/fixtures/pre-push b/spec/fixtures/pre-push index 2dcb5124..ada173a8 100644 --- a/spec/fixtures/pre-push +++ b/spec/fixtures/pre-push @@ -14,19 +14,19 @@ call_lefthook() { if lefthook -h >/dev/null 2>&1 then - eval $1 + eval lefthook $1 elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook" then - eval $dir/node_modules/@arkweid/lefthook/bin/$1 + eval "$dir/node_modules/@arkweid/lefthook/bin/lefthook $1" elif bundle exec lefthook -h >/dev/null 2>&1 then - bundle exec $1 + bundle exec lefthook $1 elif npx lefthook -h >/dev/null 2>&1 then - npx $1 + npx lefthook $1 elif yarn lefthook -h >/dev/null 2>&1 then - yarn $1 + yarn lefthook $1 else echo "Can't find lefthook in PATH" fi @@ -34,4 +34,4 @@ call_lefthook() -call_lefthook "lefthook run pre-push $@" +call_lefthook "run pre-push $@"