diff --git a/justfile b/justfile index e41f3dc..2cafc1a 100644 --- a/justfile +++ b/justfile @@ -271,7 +271,29 @@ prek-run-on-all *args: _ensure_mise_tools_installed # Installs the git pre-commit hook (runs prek automatically before each commit) prek-install-git-pre-commit-hook: _ensure_mise_tools_installed - @just --justfile {{ justfile() }} mise exec -- prek install + #!/usr/bin/env sh + set -eu + just --justfile {{ justfile() }} mise exec -- prek install + # The installed git hooks run later under Git, outside this just/mise environment, + # so they need to be told how to find their tooling: + # + # - MISE_DATA_DIR / MISE_TRUSTED_CONFIG_PATHS make mise resolve against this project's + # own data directory. Without them mise falls back to the global one and silently + # installs a second copy of the tool there. + # - prek bakes the full path of the currently installed version into the hook + # (var/mise/installs/prek//...), which stops working as soon as the pinned + # version changes or old versions are pruned. Pointing at mise's shim instead makes + # the hook resolve whatever mise.toml pins, at the time it runs. + # + # Which hook files prek installs depends on `default_install_hook_types` in + # .pre-commit-config.yaml, so patch every hook file that prek generated. + for hook in "{{ justfile_directory() }}"/.git/hooks/*; do + [ -f "$hook" ] || continue + grep -q 'generated by prek' "$hook" || continue + grep -q '^export MISE_DATA_DIR=' "$hook" || sed -i '2iexport MISE_DATA_DIR="{{ mise_data_dir }}"' "$hook" + grep -q '^export MISE_TRUSTED_CONFIG_PATHS=' "$hook" || sed -i '3iexport MISE_TRUSTED_CONFIG_PATHS="{{ mise_trusted_config_paths }}"' "$hook" + sed -i 's#^PREK=".*"$#PREK="{{ mise_data_dir }}/shims/prek"#' "$hook" + done # Internal - ensures var/mise directory exists _ensure_mise_data_directory: