Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.5 KB
Raw Blame
#!/bin/sh
[ -f "$(dirname "$0")/_/husky.sh" ] && . "$(dirname "$0")/_/husky.sh"

# Args: $1=prev_ref, $2=new_ref, $3=1 if branch checkout, 0 if file checkout
[ "$3" = "1" ] || exit 0

# Detect worktree: .git is a file (not a directory) pointing to the main repo
[ -f .git ] || exit 0

# Borrow the main clone's node_modules when lockfiles match, so the commit
# hooks run without a local install. The borrow is a PATH export that dies
# with this process, so it does not populate the worktree. Resolve the
# helper relative to this hook file, not cwd: core.hooksPath may be absolute
# (hook file lives in the main clone) or relative (worktree's own checkout),
# and cwd is always the active worktree, which may not contain the helper.
HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
if [ -f "$HOOK_DIR/../bin/helpers/worktree-borrow.sh" ]; then
    . "$HOOK_DIR/../bin/helpers/worktree-borrow.sh"

    # `git worktree add` checks out from the all-zeros ref. The grep keeps
    # Python off the path of everyone who has not run `devbox:worktree --auto`.
    # hogli, its manifest, its tools and its imports come from the hook's
    # checkout or the main clone, never from the new worktree, whose branch may
    # not be trusted. PYTHONPATH could name the worktree through `.` despite -P.
    case "$1" in
        *[!0]*) ;;
        *)
            if grep -qs '"worktree_auto_box"' "$HOME/.config/posthog/hogli_devbox.json"; then
                (unset PYTHONPATH && posthog_worktree_borrow main-only && HOGLI_MANIFEST="$HOOK_DIR/../hogli.yaml" "$HOOK_DIR/../bin/hogli" devbox:worktree:auto) || echo "husky: couldn't start the automatic devbox. Run 'hogli devbox:worktree'."
            fi
            ;;
    esac

    posthog_worktree_borrow node
fi

if [ "$POSTHOG_BORROW_NODE" = "1" ]; then
    # Being install-free is the point of a light worktree, but "borrowing
    # node_modules" reads as "deps are here". They are not: the borrow is a PATH
    # export scoped to one hook process, so say what the worktree can do.
    echo "husky: light worktree. Commit hooks borrow the main clone's toolchain."
    echo "husky: it has no node_modules or venv of its own. Run 'flox activate' to typecheck, test or run the app."
else
    if ! command -v pnpm > /dev/null 2>&1; then
        echo "husky: pnpm not found, skipping worktree bootstrap"
        exit 0
    fi

    if [ ! -d node_modules/.pnpm ]; then
        echo "husky: worktree detected, bootstrapping with pnpm install ..."
        pnpm install --frozen-lockfile --filter=.
    fi
fi