File size: 1,106 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash
# Lista (ou remove com --apply) worktrees port-* cuja branch já foi merged ao
# main OU não tem commits à frente do origin/main. NUNCA toca worktrees com
# trabalho não-commitado. Rodar a partir do checkout principal.
set -euo pipefail
APPLY="${1:-}"
git fetch origin main --quiet || true
git worktree list --porcelain | awk '/^worktree /{w=$2} /^branch /{print w" "$2}' | \
while read -r dir ref; do
  case "$dir" in
    *"/.claude/worktrees/"*port-*|*"/.worktrees/"*port-*) ;;
    *) continue ;;
  esac
  br="${ref#refs/heads/}"
  if [ -n "$(git -C "$dir" status --porcelain 2>/dev/null)" ]; then
    echo "SKIP (dirty): $dir"
    continue
  fi
  ahead=$(git -C "$dir" rev-list --count origin/main.."$br" 2>/dev/null || echo "?")
  if [ "$ahead" = "0" ]; then
    if [ "$APPLY" = "--apply" ]; then
      git worktree remove --force "$dir" && git branch -D "$br" 2>/dev/null || true
      echo "REMOVED: $dir ($br)"
    else
      echo "WOULD REMOVE (0 ahead of main): $dir ($br)"
    fi
  else
    echo "KEEP ($ahead ahead): $dir ($br)"
  fi
done
echo "Done.${APPLY:+ (applied)}"