-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit-fp
More file actions
executable file
·40 lines (35 loc) · 1.41 KB
/
Copy pathgit-fp
File metadata and controls
executable file
·40 lines (35 loc) · 1.41 KB
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
33
34
35
36
37
38
39
40
#!/bin/bash -e
# Git script for cleaning up stale local branches.
if echo "$*" | grep -Eq -- '--help\b|-h\b'; then
echo "usage: ${0##*/} [-h|--help]" 1>&2
echo "Clean up merged tracking branches and merged agent worktree branches." 1>&2
exit 1
fi
git fetch -p
# 1. Delete local branches whose upstream tracking branch no longer exists.
gone=$(git branch -r |
awk '{print $1}' |
egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) |
awk '{print $1}')
if [ -n "$gone" ]; then
echo "$gone" | while read -r branch; do
echo "Removing branch with gone upstream: $branch"
git branch -D "$branch"
done
fi
# 2. Delete agent-created worktree branches already merged into upstream main.
# These have no upstream, so pass 1 never sees them.
main_ref=origin/main
git rev-parse --verify --quiet "$main_ref" >/dev/null || main_ref=main
git for-each-ref --format='%(refname:short)%09%(objectname)%09%(worktreepath)' refs/heads/ |
while IFS=$'\t' read -r branch sha wt; do
case "$branch" in agents/*) ;; *) continue ;; esac
[ -n "$wt" ] || continue
git merge-base --is-ancestor "$sha" "$main_ref" 2>/dev/null || continue
echo "Removing merged agent worktree branch: $branch"
if git worktree remove "$wt"; then
git branch -D "$branch"
else
echo " skipped: worktree has uncommitted changes or is locked" 1>&2
fi
done