From 785c9f4dd731eb86f3a243f5d2e8b13bb54f3a02 Mon Sep 17 00:00:00 2001 From: Minidoracat Date: Thu, 9 Apr 2026 02:39:43 +0800 Subject: [PATCH] fix: detect correct Python interpreter for pipx installs in git hooks * fix: git hooks fail when graphify is installed via pipx When installed via pipx, the graphify module is only available in pipx's isolated venv, not the system python3. The git hooks (post-commit, post-checkout) hardcoded `python3` which cannot import graphify in this case. Detect the correct Python interpreter from the graphify binary's shebang line, matching the approach already used in skill.md Step 1. Falls back to python3 for system installs. * fix: handle env-style shebangs and improve interpreter detection - Use POSIX `command -v` instead of non-standard `which` - Parse `#!/usr/bin/env python3` shebangs correctly (previous `tr -d ' '` would produce `/usr/bin/envpython3`) - Add import validation fallback to python3 if resolved interpreter cannot import graphify --- graphify/hooks.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/graphify/hooks.py b/graphify/hooks.py index dc5e8033..51959ffb 100644 --- a/graphify/hooks.py +++ b/graphify/hooks.py @@ -8,6 +8,23 @@ _HOOK_MARKER_END = "# graphify-hook-end" _CHECKOUT_MARKER = "# graphify-checkout-hook-start" _CHECKOUT_MARKER_END = "# graphify-checkout-hook-end" +_PYTHON_DETECT = """\ +# Detect the correct Python interpreter (handles pipx, venv, system installs) +GRAPHIFY_BIN=$(command -v graphify 2>/dev/null) +if [ -n "$GRAPHIFY_BIN" ]; then + _SHEBANG=$(head -1 "$GRAPHIFY_BIN" | sed 's/^#![[:space:]]*//') + case "$_SHEBANG" in + */env\\ *) GRAPHIFY_PYTHON="${_SHEBANG#*/env }" ;; + *) GRAPHIFY_PYTHON="$_SHEBANG" ;; + esac + if ! "$GRAPHIFY_PYTHON" -c "import graphify" 2>/dev/null; then + GRAPHIFY_PYTHON="python3" + fi +else + GRAPHIFY_PYTHON="python3" +fi +""" + _HOOK_SCRIPT = """\ # graphify-hook-start # Auto-rebuilds the knowledge graph after each commit (code files only, no LLM needed). @@ -18,8 +35,9 @@ if [ -z "$CHANGED" ]; then exit 0 fi +""" + _PYTHON_DETECT + """ export GRAPHIFY_CHANGED="$CHANGED" -python3 -c " +$GRAPHIFY_PYTHON -c " import os, sys from pathlib import Path @@ -67,8 +85,9 @@ if [ ! -d "graphify-out" ]; then exit 0 fi +""" + _PYTHON_DETECT + """ echo "[graphify] Branch switched - rebuilding knowledge graph (code files)..." -python3 -c " +$GRAPHIFY_PYTHON -c " from graphify.watch import _rebuild_code from pathlib import Path import sys