From 3024a5b845e578d02afbcf1b6ecd565546b4cdfc Mon Sep 17 00:00:00 2001 From: Josh Nichols Date: Tue, 5 Mar 2024 02:21:26 -0500 Subject: [PATCH] feat: use configurable path to lefthook (LEFTHOOK_BIN) (#653) * turn on trace output in git hook when LEFTHOOK_VERBOSE is set * add support to giving an explicit path to lefthook in git hook. useful for developing * Document LEFTHOOK_PATH * Switch to LEFTHOOK_BIN --- docs/usage.md | 10 ++++++++++ internal/templates/hook.tmpl | 9 ++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/usage.md b/docs/usage.md index 3907ca1f..dec1bdf8 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -18,6 +18,7 @@ Then use git as usually, you don't need to reinstall lefthook when you change th - [`LEFTHOOK_EXCLUDE`](#lefthook_exclude) - [`LEFTHOOK_QUIET`](#lefthook_quiet) - [`LEFTHOOK_VERBOSE`](#lefthook_verbose) + - [`LEFTHOOK_BIN`](#lefthook_bin) - [Features and tips](#features-and-tips) - [Disable lefthook in CI](#disable-lefthook-in-ci) - [Local config](#local-config) @@ -192,6 +193,15 @@ SUMMARY: (done in 0.01 seconds) Set `LEFTHOOK_VERBOSE=1` or `LEFTHOOK_VERBOSE=true` to enable verbose printing. +### `LEFTHOOK_BIN` + +Set `LEFTHOOK_BIN` to a location where lefthook is installed to use that instead of trying to detect from the it the PATH or from a package manager. + +Useful for cases when: + +- lefthook is installed multiple ways, and you want to be explicit about which one is used (example: installed through homebrew, but also is in Gemfile but you are using a ruby version manager like rbenv that prepends it to the path) +- debugging and/or developing lefthook + ## Features and tips ### Disable lefthook in CI diff --git a/internal/templates/hook.tmpl b/internal/templates/hook.tmpl index 2698b41a..d39490c8 100644 --- a/internal/templates/hook.tmpl +++ b/internal/templates/hook.tmpl @@ -1,5 +1,9 @@ #!/bin/sh +if [ "$LEFTHOOK_VERBOSE" = "1" -o "$LEFTHOOK_VERBOSE" = "true" ]; then + set -x +fi + if [ "$LEFTHOOK" = "0" ]; then exit 0 fi @@ -15,7 +19,10 @@ call_lefthook() osArch=$(uname | tr '[:upper:]' '[:lower:]') cpuArch=$(uname -m | sed 's/aarch64/arm64/') - if lefthook{{.Extension}} -h >/dev/null 2>&1 + if test -n "$LEFTHOOK_BIN" + then + "$LEFTHOOK_BIN" "$@" + elif lefthook{{.Extension}} -h >/dev/null 2>&1 then lefthook{{.Extension}} "$@" {{if .Extension -}}