Skip to content

Commit

Permalink
Fix lefthook binary extension on Windows
Browse files Browse the repository at this point in the history
This commit has been extracted from #188
  • Loading branch information
aminya authored and Envek committed Jun 1, 2021
1 parent 8d14958 commit 5310679
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
12 changes: 11 additions & 1 deletion cmd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"embed"
"runtime"
"text/template"

"github.com/spf13/afero"
Expand All @@ -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 {
Expand All @@ -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)

Expand All @@ -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 ""
}
16 changes: 8 additions & 8 deletions cmd/templates/hook.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ 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
}

{{.AutoInstall}}

call_lefthook "lefthook run {{.HookName}} $@"
call_lefthook "run {{.HookName}} $@"
12 changes: 6 additions & 6 deletions spec/fixtures/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ 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
}



call_lefthook "lefthook run pre-commit $@"
call_lefthook "run pre-commit $@"
12 changes: 6 additions & 6 deletions spec/fixtures/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ 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
}



call_lefthook "lefthook run pre-push $@"
call_lefthook "run pre-push $@"

0 comments on commit 5310679

Please sign in to comment.