Files
claude-engineering-plugin/plugins/compound-engineering/skills/git-clean-gone-branches/scripts/clean-gone
Trevin Chow f83305e22a fix: harden git workflow skills with better state handling (#406)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 01:04:54 -07:00

49 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# clean-gone: List local branches whose remote tracking branch is gone.
# Outputs one branch name per line, or nothing if none found.
set -euo pipefail
# Ensure we have current remote state
git fetch --prune 2>/dev/null
# Find branches marked [gone] in tracking info.
# `git branch -vv` output format:
# * main abc1234 [origin/main] commit msg
# + feature-x def5678 [origin/feature-x: gone] commit msg
# old-branch 789abcd [origin/old-branch: gone] commit msg
#
# The leading column can be: ' ' (normal), '*' (current), '+' (worktree).
# We match lines containing ": gone]" to find branches whose remote is deleted.
gone_branches=()
while IFS= read -r line; do
# Skip the currently checked-out branch (marked with '*').
# git branch -D cannot delete the active branch, and attempting it
# would halt cleanup before other stale branches are processed.
if [[ "$line" =~ ^\* ]]; then
continue
fi
# Strip the leading marker character(s) and whitespace
# The branch name is the first non-whitespace token after the marker
branch_name=$(echo "$line" | sed 's/^[+* ]*//' | awk '{print $1}')
# Validate: skip empty, skip if it looks like a hash or flag, skip HEAD
if [[ -z "$branch_name" ]] || [[ "$branch_name" =~ ^[0-9a-f]{7,}$ ]] || [[ "$branch_name" == "HEAD" ]]; then
continue
fi
gone_branches+=("$branch_name")
done < <(git branch -vv 2>/dev/null | grep ': gone]')
if [[ ${#gone_branches[@]} -eq 0 ]]; then
echo "__NONE__"
exit 0
fi
for branch in "${gone_branches[@]}"; do
echo "$branch"
done