#!/bin/bash
# OOB harness recovery. Rescues a node whose normal /sync or /save is broken by a bad
# harness change. Hook-free, guard-free, minimal deps. Resets the ClaudeTools repo to
# origin/main. Does NOT touch the vault or submodules.
#
#   bash .claude/scripts/force-pull-raw.sh            # dry-run: show what would change
#   bash .claude/scripts/force-pull-raw.sh --confirm  # hard-reset to origin/main
#
# --confirm first saves your current HEAD to a local branch recovery/pre-force-pull-<sha>
# so no committed work is truly lost.
set -uo pipefail
ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || { echo "[ERROR] not in a git repo"; exit 1; }
cd "$ROOT"
echo "[force-pull-raw] repo: $ROOT"
if ! git fetch origin 2>&1 | tail -2; then echo "[ERROR] git fetch origin failed"; exit 1; fi
LOCAL=$(git rev-parse --short HEAD 2>/dev/null)
REMOTE=$(git rev-parse --short origin/main 2>/dev/null)
echo "--- local HEAD: $LOCAL | origin/main: $REMOTE ---"
echo "--- working-tree changes a hard reset would discard ---"
git status --short
echo "--- local-only commits a hard reset would discard ---"
git log --oneline origin/main..HEAD 2>/dev/null | head
if [ "${1:-}" != "--confirm" ]; then
  echo ""
  echo "DRY RUN. Re-run with --confirm to hard-reset to origin/main (discards the above;"
  echo "current HEAD will be saved to a local recovery branch first)."
  exit 0
fi
git branch -f "recovery/pre-force-pull-$LOCAL" HEAD 2>/dev/null || true
git reset --hard origin/main
echo "[OK] reset to origin/main ($REMOTE). Prior HEAD saved at recovery/pre-force-pull-$LOCAL"
