From daee7bb1775d83a09ca46baec693fdb49cf6ad02 Mon Sep 17 00:00:00 2001 From: Valentine Kiselev Date: Wed, 30 Nov 2022 19:18:25 +0300 Subject: [PATCH] feature: Expand env variables (#391) Closes https://github.com/evilmartians/lefthook/issues/178 --- docs/configuration.md | 23 +++++++++++++++++++++ internal/lefthook/runner/execute_unix.go | 2 +- internal/lefthook/runner/execute_windows.go | 2 +- internal/templates/hook.tmpl | 2 -- internal/templates/templates.go | 5 ++--- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 0ce614d6..749a078f 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -692,6 +692,29 @@ pre-commit: run: bundle exec rspec ``` +#### Extending PATH + +If your hook is run by GUI program, and you use some PATH tweaks in your ~/.rc, you might see an error saying *executable not found*. In that case You can extend the **$PATH** variable with `lefthook-local.yml` configuration the following way. + +```yml +# lefthook.yml + +pre-commit: + commands: + test: + run: yarn test +``` + +```yml +# lefthook-local.yml + +pre-commit: + commands: + test: + env: + PATH: $PATH:/home/me/path/to/yarn +``` + **Notes** This option is useful when using lefthook on different OSes or shells where ENV variables are set in different ways. diff --git a/internal/lefthook/runner/execute_unix.go b/internal/lefthook/runner/execute_unix.go index 0f307d68..ae6828fd 100644 --- a/internal/lefthook/runner/execute_unix.go +++ b/internal/lefthook/runner/execute_unix.go @@ -41,7 +41,7 @@ func (e CommandExecutor) Execute(opts ExecuteOptions) (*bytes.Buffer, error) { for name, value := range opts.env { envList = append( envList, - fmt.Sprintf("%s=%s", strings.ToUpper(name), value), + fmt.Sprintf("%s=%s", strings.ToUpper(name), os.ExpandEnv(value)), ) } diff --git a/internal/lefthook/runner/execute_windows.go b/internal/lefthook/runner/execute_windows.go index 459a433f..90d8b505 100644 --- a/internal/lefthook/runner/execute_windows.go +++ b/internal/lefthook/runner/execute_windows.go @@ -25,7 +25,7 @@ func (e CommandExecutor) Execute(opts ExecuteOptions) (*bytes.Buffer, error) { for name, value := range opts.env { envList = append( envList, - fmt.Sprintf("%s=%s", strings.ToUpper(name), value), + fmt.Sprintf("%s=%s", strings.ToUpper(name), os.ExpandEnv(value)), ) } diff --git a/internal/templates/hook.tmpl b/internal/templates/hook.tmpl index cc351d43..a7f603ab 100644 --- a/internal/templates/hook.tmpl +++ b/internal/templates/hook.tmpl @@ -42,6 +42,4 @@ call_lefthook() fi } -{{.AutoInstall}} - call_lefthook "run {{.HookName}} $@" diff --git a/internal/templates/templates.go b/internal/templates/templates.go index e4f72bef..567b0933 100644 --- a/internal/templates/templates.go +++ b/internal/templates/templates.go @@ -14,9 +14,8 @@ const checksumFormat = "%s %d\n" var templatesFS embed.FS type hookTmplData struct { - AutoInstall string - HookName string - Extension string + HookName string + Extension string } func Hook(hookName string) []byte {