#!/bin/bash
# ContextFS Post-Commit Hook
# Incrementally indexes new/changed files after each commit
#
# Install: cp hooks/post-commit .git/hooks/ && chmod +x .git/hooks/post-commit
# Or run: contextfs install-hooks

# Exit on error
set -e

# Check if contextfs is available
if ! command -v contextfs &> /dev/null; then
    # Try common locations
    if [ -f "$HOME/.local/bin/contextfs" ]; then
        CONTEXTFS="$HOME/.local/bin/contextfs"
    elif [ -f "$HOME/.pyenv/shims/contextfs" ]; then
        CONTEXTFS="$HOME/.pyenv/shims/contextfs"
    else
        # Silently exit if contextfs not found
        exit 0
    fi
else
    CONTEXTFS="contextfs"
fi

# Get the repo root
REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -z "$REPO_ROOT" ]; then
    exit 0
fi

# Run incremental index in background to not block commit
# Only index files, skip commits (the commit just happened)
(
    cd "$REPO_ROOT"
    $CONTEXTFS index --incremental --mode files_only --quiet 2>/dev/null &
) &

exit 0
