#!/bin/bash
# MCP Index Post-Checkout Hook
# Automatically downloads index artifacts when switching branches
set -e
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# Get hook parameters
PREV_HEAD=$1
NEW_HEAD=$2
BRANCH_CHECKOUT=$3
# Only run on branch checkouts, not file checkouts
if [ "$BRANCH_CHECKOUT" != "1" ]; then
exit 0
fi
# Skip if it's the same commit
if [ "$PREV_HEAD" = "$NEW_HEAD" ]; then
exit 0
fi
# Check if index management is enabled
if [ -f ".mcp-index.json" ]; then
ENABLED=$(jq -r '.enabled // true' .mcp-index.json 2>/dev/null || echo "true")
AUTO_DOWNLOAD=$(jq -r '.auto_download // true' .mcp-index.json 2>/dev/null || echo "true")
if [ "$ENABLED" != "true" ] || [ "$AUTO_DOWNLOAD" != "true" ]; then
exit 0
fi
else
# No config file, skip hook
exit 0
fi
echo -e "${YELLOW}🔍 MCP Index: Checking branch index...${NC}"
# Check if we need a different index for this branch
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
# For PR branches, try to find PR-specific index
if [[ "$BRANCH_NAME" =~ ^(pull|pr)/[0-9]+/ ]]; then
PR_NUMBER=$(echo "$BRANCH_NAME" | grep -oE '[0-9]+')
echo -e "${YELLOW}📥 MCP Index: PR branch detected, looking for PR-specific index...${NC}"
# This would download PR-specific artifact
# For now, just notify
echo -e "${YELLOW} To download PR index: mcp-index pull --pr $PR_NUMBER${NC}"
fi
# Check if index needs update
if [ -f ".mcp-index/.index_metadata.json" ]; then
LOCAL_COMMIT=$(jq -r '.commit // ""' .mcp-index/.index_metadata.json 2>/dev/null || echo "")
if [ "$LOCAL_COMMIT" != "$NEW_HEAD" ]; then
echo -e "${YELLOW}📥 MCP Index: Index is for different commit${NC}"
echo -e "${YELLOW} Current index: ${LOCAL_COMMIT:0:7}${NC}"
echo -e "${YELLOW} Branch commit: ${NEW_HEAD:0:7}${NC}"
echo -e "${YELLOW} Run 'mcp-index pull --latest' to update${NC}"
else
echo -e "${GREEN}✓ MCP Index: Index matches branch${NC}"
fi
else
echo -e "${YELLOW}📥 MCP Index: No index found for this branch${NC}"
echo -e "${YELLOW} Run 'mcp-index pull --latest' to download${NC}"
fi
# Always allow the checkout to complete
exit 0