about summary refs log tree commit diff
path: root/pkgs/tools/misc/pre-commit
diff options
context:
space:
mode:
authorRebecca Turner <rbt@sent.as>2022-10-07 13:26:12 -0400
committerRebecca Turner <rbt@sent.as>2022-10-07 13:38:15 -0400
commit5a01cf108466331a3223bbcc3afc7d014b18d351 (patch)
treee421fb0544e2368158527358a2b93fd0f92188ff /pkgs/tools/misc/pre-commit
parent4e43b16917b2d699ffd329bd7a4b5def0681062b (diff)
pre-commit: use absolute path for binary in hook
This changes the generated Git hook to refer to the `pre-commit` binary
by its absolute path. This means that Git hooks created with
`nix-shell --run 'pre-commit install'` or similar will be usable outside
of the Nix shell they were created in.

I think this is the intended behavior for this package, considering that
the `postPatch` phase already includes a substitution for this variable,
otherwise unused:

    substituteInPlace pre_commit/resources/hook-tmpl \
      --subst-var-by pre-commit $out
Diffstat (limited to 'pkgs/tools/misc/pre-commit')
-rw-r--r--pkgs/tools/misc/pre-commit/default.nix4
-rw-r--r--pkgs/tools/misc/pre-commit/hook-tmpl.patch14
2 files changed, 12 insertions, 6 deletions
diff --git a/pkgs/tools/misc/pre-commit/default.nix b/pkgs/tools/misc/pre-commit/default.nix
index 4c455d6fb2d7b..7fef96dae312e 100644
--- a/pkgs/tools/misc/pre-commit/default.nix
+++ b/pkgs/tools/misc/pre-commit/default.nix
@@ -142,6 +142,10 @@ buildPythonPackage rec {
     "test_install_existing_hooks_no_overwrite"
     "test_installed_from_venv"
     "test_uninstall_restores_legacy_hooks"
+
+    # Expects `git commit` to fail when `pre-commit` is not in the `$PATH`,
+    # but we use an absolute path so it's not an issue.
+    "test_environment_not_sourced"
   ];
 
   pythonImportsCheck = [
diff --git a/pkgs/tools/misc/pre-commit/hook-tmpl.patch b/pkgs/tools/misc/pre-commit/hook-tmpl.patch
index 1d5fd17274baa..56aa59ef48320 100644
--- a/pkgs/tools/misc/pre-commit/hook-tmpl.patch
+++ b/pkgs/tools/misc/pre-commit/hook-tmpl.patch
@@ -1,15 +1,17 @@
 diff --git a/pre_commit/resources/hook-tmpl b/pre_commit/resources/hook-tmpl
-index 53d29f9..66a8ad3 100755
+index 53d29f9..9b5dc2c 100755
 --- a/pre_commit/resources/hook-tmpl
 +++ b/pre_commit/resources/hook-tmpl
-@@ -10,9 +10,7 @@ ARGS=(hook-impl)
+@@ -10,11 +10,4 @@ ARGS=(hook-impl)
  HERE="$(cd "$(dirname "$0")" && pwd)"
  ARGS+=(--hook-dir "$HERE" -- "$@")
  
 -if [ -x "$INSTALL_PYTHON" ]; then
 -    exec "$INSTALL_PYTHON" -mpre_commit "${ARGS[@]}"
 -elif command -v pre-commit > /dev/null; then
-+if command -v pre-commit > /dev/null; then
-     exec pre-commit "${ARGS[@]}"
- else
-     echo '`pre-commit` not found.  Did you forget to activate your virtualenv?' 1>&2
+-    exec pre-commit "${ARGS[@]}"
+-else
+-    echo '`pre-commit` not found.  Did you forget to activate your virtualenv?' 1>&2
+-    exit 1
+-fi
++exec @pre-commit@/bin/pre-commit "${ARGS[@]}"