diff --git a/watch.sh b/watch.sh new file mode 100755 index 0000000..8668d61 --- /dev/null +++ b/watch.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash +# watch.sh — Poll git remote for new commits, pull and re-run on change. +# Usage: ./watch.sh [poll_interval_seconds] +# +# Watches the git remote. On new commits: +# 1. git pull +# 2. pip install -r requirements.txt (if changed) +# 3. Re-run ingestion (if parser/docgen/ingest changed) +# 4. Restart MCP server +# +# Ctrl+C to stop. + +set -euo pipefail + +POLL_INTERVAL="${1:-10}" +MCP_PID="" +LAST_HASH="" + +cleanup() { + echo -e "\n[watch] Shutting down..." + if [ -n "$MCP_PID" ] && kill -0 "$MCP_PID" 2>/dev/null; then + kill "$MCP_PID" 2>/dev/null + echo "[watch] MCP server stopped." + fi + exit 0 +} +trap cleanup SIGINT SIGTERM + +start_mcp() { + if [ -n "$MCP_PID" ] && kill -0 "$MCP_PID" 2>/dev/null; then + echo "[watch] Stopping MCP server (pid $MCP_PID)..." + kill "$MCP_PID" 2>/dev/null + wait "$MCP_PID" 2>/dev/null || true + fi + echo "[watch] Starting MCP server..." + python mcp_server.py & + MCP_PID=$! + echo "[watch] MCP server started (pid $MCP_PID)" +} + +get_local_hash() { + git rev-parse HEAD 2>/dev/null +} + +get_remote_hash() { + git ls-remote origin HEAD 2>/dev/null | cut -f1 +} + +echo "[watch] Developer Intelligence POC — File Watcher" +echo "[watch] Polling every ${POLL_INTERVAL}s for new commits..." +echo "" + +# Initial setup +LAST_HASH=$(get_local_hash) +LAST_REQS=$(md5sum requirements.txt 2>/dev/null || echo "none") + +# Start MCP server +if [ -f "devintel.db" ]; then + start_mcp +else + echo "[watch] No database found. Run 'python ingest.py' first, then restart watch.sh" + echo "[watch] Watching for code changes only..." +fi + +while true; do + sleep "$POLL_INTERVAL" + + # Check remote + REMOTE_HASH=$(get_remote_hash) + if [ -z "$REMOTE_HASH" ]; then + continue + fi + + LOCAL_HASH=$(get_local_hash) + if [ "$REMOTE_HASH" = "$LOCAL_HASH" ]; then + continue + fi + + echo "" + echo "[watch] New commit detected: ${LOCAL_HASH:0:8} → ${REMOTE_HASH:0:8}" + echo "[watch] Pulling..." + git pull --ff-only origin master + + # Check if requirements changed + NEW_REQS=$(md5sum requirements.txt 2>/dev/null || echo "none") + if [ "$NEW_REQS" != "$LAST_REQS" ]; then + echo "[watch] requirements.txt changed — reinstalling..." + pip install -r requirements.txt + LAST_REQS="$NEW_REQS" + fi + + # Check if ingestion code changed + CHANGED_FILES=$(git diff --name-only "$LOCAL_HASH" "$REMOTE_HASH") + NEEDS_REINGEST=false + for f in $CHANGED_FILES; do + case "$f" in + parser.py|docgen.py|ingest.py|db.py) + NEEDS_REINGEST=true + ;; + esac + done + + if [ "$NEEDS_REINGEST" = true ] && [ -f "devintel.db" ]; then + echo "[watch] Ingestion code changed. Re-running ingestion..." + python ingest.py + fi + + # Always restart MCP server on any code change + if [ -f "devintel.db" ]; then + start_mcp + fi + + echo "[watch] Updated to $(get_local_hash | head -c 8). Watching..." +done