#!/usr/bin/env bash
# read-only census of hertz-* worktrees on this box
cd /c/Users/decid/Documents/projects/spt-core || exit 9
git fetch -q origin || echo "FETCH_FAIL"
M=$(git rev-parse origin/main)
echo "main=$M box=$(hostname) at=$(date -u +%FT%TZ)"
printf 'path\thead\tbranch\tancestor_of_main\tcherry_unlanded\tremote_branch\tdir_exists\ttarget\talloc_B\tapparent_B\n'
git worktree list --porcelain | awk '/^worktree /{p=substr($0,10)} /^HEAD /{h=$2} /^branch /{b=$2} /^detached/{b="DETACHED"} /^$/{if(p~/hertz/)print p"\t"h"\t"b; p="";h="";b=""}' |
while IFS=$'\t' read -r p h b; do
  d=$(cygpath -u "$p" 2>/dev/null || echo "$p"); d=${d%/.git}
  anc=no; git merge-base --is-ancestor "$h" "$M" 2>/dev/null && anc=yes
  ch=$(git cherry "$M" "$h" 2>/dev/null | grep -c '^+')
  rb=-; [ "$b" != DETACHED ] && { git ls-remote --exit-code --heads origin "${b#refs/heads/}" >/dev/null 2>&1 && rb=yes || rb=no; }
  ex=no; [ -d "$d" ] && ex=yes
  t=none; al=-; ap=-
  if [ -L "$d/target" ]; then t="LINK->$(readlink "$d/target")"
  elif [ -d "$d/target" ]; then t=realdir; al=$(du -sB1 "$d/target" 2>/dev/null | cut -f1); ap=$(du -sb --apparent-size "$d/target" 2>/dev/null | cut -f1); fi
  printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$d" "${h:0:8}" "${b#refs/heads/}" "$anc" "$ch" "$rb" "$ex" "$t" "$al" "$ap"
done
echo "END"
