-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-branch-purge
executable file
·82 lines (78 loc) · 2.26 KB
/
git-branch-purge
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env zsh
# git-branch-purge
# --------------------------------------------------------------------------------------------------
# Version
# -------
# 1.0.0
# --------------------------------------------------------------------------------------------------
# Authors
# -------
# Filipe Kiss <[email protected]> http://github.com/filipekiss
# --------------------------------------------------------------------------------------------------
# Usage
# -----
# Add this to your $PATH and invoke as a git command:
#
# git branch-purge
#
# Or simply run ./git-branch-purge
# --------------------------------------------------------------------------------------------------
# Options
# -------
#
# --dry-run, -n
# Don't actually delete anything. Useful for checking what would be deleted
#
# --force, -f
# Force deletion of branches. Sometimes, the branches are identified as not
# being merged and git will refuse to delete them (this might happen in cases of
# squashed commits, for example). Using this options makes git ignore this and
# delete the branch anyway.
#
_parse_arguments() {
local arg
while (($# > 0)); do
arg="$1"
case "$arg" in
--dry-run | -n)
_dry_run="1"
;;
--force|-f)
_force_delete="-D"
;;
--*)
# Inexistent dashed option, do nothing
;;
-*)
# Inexistent dashed option, do nothing
;;
"") ;;
esac
shift
done
return 0
}
_get_orphaned_branches() {
git branch --format='%(if:equals=gone)%(upstream:track,nobracket)%(then)%(refname:short)%(else)===NO%(end)' | grep -v "===NO"
}
main() {
_parse_arguments "$@"
echo "🕵️♂️ Searching for branches that don't have a remote counterpart…"
local _orphaned_branches=("${(@f)$(_get_orphaned_branches)}")
for branch in "${_orphaned_branches[@]}"; do
if [[ "$branch" == "" ]]; then
echo "✨ No branches to purge"
exit 0
fi
if [[ -n ${_dry_run:-} ]]; then
echo "🪣 Would Delete: $branch"
else
echo "🧹 Deleting: $branch"
command git branch ${_force_delete:-"-d"} ${branch} > /dev/null
if [[ $? -eq 0 ]]; then
echo "🗑️ Deleted: $branch"
fi
fi
done
}
main "$@"