#!/usr/bin/env bash
# Refresh the codegraph index after a branch switch.
#
# git passes: $1 = previous HEAD, $2 = new HEAD, $3 = 1 for a branch checkout
# and 0 for a file checkout. Only a branch switch replaces the whole working
# tree, so file checkouts are ignored here.
set -uo pipefail

root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
branch_checkout="${3:-0}"

if [ "$branch_checkout" != "1" ]; then
    exit 0
fi

if [ -x "$root/tools/codegraph-sync.sh" ]; then
    "$root/tools/codegraph-sync.sh"
else
    echo "post-checkout: tools/codegraph-sync.sh missing or not executable; skipping index" >&2
fi

exit 0
