-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitclean
executable file
·27 lines (24 loc) · 1.18 KB
/
gitclean
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
#!/bin/bash
# gitclean proposes branches to delete (GitHub remote). Requires gh, gum.
#
# Usage:
# gitclean
# NOTE: there's a good deal of gross formatting in this script, but it's all
# really for appearances. Key operation: prune remote refs, then suggest
# deleting branches that *have* a push branch defined but that branch no longer
# exists.
git remote prune --dry-run origin
blog info "If you don't prune, gitclean may miss deletable branches."
gum confirm " Prune remote branches?" && git remote prune origin || blog warn "Skipped pruning."
echo ""
# For branches with no more remote ref...
for branch in $(git for-each-ref --format='%(push:trackshort)%(push:lstrip=3)' refs/heads/ | grep -wv master | grep "^\w")
do
blog info "Considering branch $branch"
GH_PAGER=cat gh pr list --state merged -H $branch \
--json number,title,mergedAt,url \
--template '{{range .}}{{printf "#%v" .number | autocolor "magenta"}} {{.title}} {{printf "m. %v %v\n" (timeago .mergedAt) .url | autocolor "grey"}}{{end}}'
gum confirm " Delete local $branch?" && git branch -D "$branch" || blog info "Not deleted."
echo ""
done
blog info "No more branches to propose deleting."