#!/usr/bin/env bash
# status-all: one board — repos, lanes, drift.
# Runs `git fetch origin` in every repo under repos/ first (quietly; a failed fetch is ignored),
# so "behind origin" is current. That is the only network access it makes.
source "$(python3 -c 'import os,sys;print(os.path.dirname(os.path.realpath(sys.argv[1])))' "$0")/lib.sh"
source "$PRODUCT/scripts/gitstate.sh"
"$PRODUCT/scripts/hooks/guard-check" -q || echo "   (fix guard.py before trusting any of the state below)"
if [ "${LANE_MODE:-}" = workspace ]; then echo "== workspace (its own git repo, if you made one: docs/install.md)"
else echo "== control plane (this checkout itself — docs/concepts.md: \"the clone is the control plane\")"; fi
if [ -d "$AD/.git" ] || [ -f "$AD/.git" ]; then
  cp_dirty=$(git -C "$AD" status --porcelain | wc -l | tr -d ' ')
  # No upstream configured (a fresh clone that's never been pushed) makes `git log @{u}..HEAD`
  # exit non-zero; under lib.sh's `set -euo pipefail` that kills the whole script right here,
  # silently truncating everything below -- `|| true` so an absent upstream just reads as 0.
  # same definition as every other count: commits no origin branch has (gitstate.sh), not "ahead of @{u}"
  cp_unpushed=$(lane_unpushed "$AD"); case "$cp_unpushed" in local|'?'|'') cp_unpushed=0 ;; esac
  if git -C "$AD" remote get-url origin >/dev/null 2>&1; then
    cp_push_url=$(git -C "$AD" remote get-url --push origin 2>/dev/null || echo "")
    if [ "$cp_push_url" = "DISABLED-set-a-private-remote-first" ]; then
      cp_remote="push disabled"
    else
      cp_remote="push remote: $cp_push_url"
    fi
  else
    cp_remote="no push remote configured"
  fi
  echo "  $cp_dirty dirty, $cp_unpushed unpushed, $cp_remote"
else
  echo "  not a git repo — memory/, knowledge/, CLAUDE.md are not tracked anywhere"
fi
lane_migrate_reminder | sed 's/^/  /'
echo "== repos (primary mirrors)"
for r in "$REPOS"/*/; do [ -e "$r/.git" ] || continue   # no repos yet: the glob stays literal
  n=$(basename "$r"); def=$(repo_default "$n")
  git -C "$r" fetch -q origin 2>/dev/null || true
  b=$(git -C "$r" rev-list --count "$def..origin/$def" 2>/dev/null || echo ?)
  dirty=$(git -C "$r" status --porcelain | wc -l | tr -d ' '); cur=$(git -C "$r" branch --show-current)
  flag=""; [ "$cur" != "$def" ] && flag=" !! not on $def"; [ "$dirty" != 0 ] && flag="$flag !! dirty (mirror must stay clean)"
  echo "  $n: $cur, behind origin by $b, worktrees=$(( $(git -C "$r" worktree list | wc -l) - 1 ))$flag"
  git -C "$r" worktree list --porcelain | sed -n 's/^worktree //p' | tail -n +2 | { grep -v "^$LANES/" || true; } | sed 's/^/     !! worktree outside lanes\/: /'
done
nested=$("$PRODUCT/scripts/lane-nested" 2>/dev/null || true)
if [ -n "$nested" ]; then echo "  !! nested worktrees (a worktree inside another) — fix with: lane-nested --fix"; printf '%s\n' "$nested"; fi
big=$(find "$LANES" -name settings.local.json -size +20k 2>/dev/null | sed "s|$AD/||" || true)
if [ -n "$big" ]; then echo "  !! oversized guard files (rules are repeating) — fix with: lane-resume <id>"; printf '%s\n' "$big" | sed 's/^/     /'; fi
echo "== lanes"
for f in "$LANES_DIR"/*.md; do [ -f "$f" ] || continue; id=$(basename "$f" .md)
  echo "  $id [$(lane_get "$id" status)] updated $(lane_get "$id" updated) ticket=$(lane_get "$id" ticket)"
  while read -r repo branch; do d="$LANES/$id/$repo"
    if [ -d "$d" ]; then echo "     $repo/$branch: $(lane_dirty_count "$d") dirty, $(lane_unpushed_text "$d")"; else echo "     $repo/$branch: no worktree (lane-resume recreates)"; fi
  done < <(lane_repos "$id")
done
echo "== drift: checkouts outside the $PLANE_WORD (should be none; work happens here)"
[ -f "$REGISTRY/old-checkouts.json" ] || exit 0
python3 - "$REGISTRY/old-checkouts.json" <<'PY'
import json,subprocess,sys
for p,h in json.load(open(sys.argv[1])).items():
    try: cur=subprocess.check_output(["git","-C",p,"rev-parse","HEAD"],text=True,stderr=subprocess.DEVNULL).strip()
    except Exception: print(f"  {p}: gone"); continue
    dirty=subprocess.check_output(["git","-C",p,"status","--porcelain"],text=True).count("\n")
    if cur!=h["head"] or dirty!=h["dirty"]: print(f"  !! {p}: HEAD or dirty state changed since it was recorded")
PY
