#!/usr/bin/env bash
# branch-audit [repo]: writes registry/branch-audit.md — every local branch with no upstream or unpushed commits.
source "$(python3 -c 'import os,sys;print(os.path.dirname(os.path.realpath(sys.argv[1])))' "$0")/lib.sh"
out="$REGISTRY/branch-audit.md"; { echo "# Branch audit — $(date +%F)"; echo; echo "Local branches that exist only on this machine (no upstream) or are ahead of upstream. Decide: lane / keep / delete."; }> "$out"
for r in "$REPOS"/*/; do n=$(basename "$r"); if [ -n "${1:-}" ] && [ "$1" != "$n" ]; then continue; fi
  { echo; echo "## $n"; echo; echo "| branch | last commit | age | state |"; echo "|---|---|---|---|"; } >> "$out"
  git -C "$r" for-each-ref --sort=-committerdate --format='%(refname:short)|%(objectname:short) %(contents:subject)|%(committerdate:relative)|%(upstream:track)|%(upstream:short)' refs/heads \
  | while IFS='|' read -r b c age track up; do
      st=""; [ -z "$up" ] && st="no upstream"; case "$track" in *ahead*) st="${st:+$st, }$track";; esac
      if [ -n "$st" ]; then printf '| %s | %s | %s | %s |\n' "$b" "$(echo "$c" | cut -c1-70 | sed 's/|/\\|/g')" "$age" "$st"; fi
    done >> "$out"
done
echo "wrote $out ($(grep -c '^| ' "$out") rows)"
