-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarbage.sh
executable file
·45 lines (35 loc) · 1.07 KB
/
garbage.sh
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
#!/usr/bin/env bash
if test -n "$(git status --porcelain)"; then
echo 'Unclean working tree. Commit or stash changes first.' >&2;
exit 1;
fi
if ! git fetch --quiet 2> /dev/null; then
echo 'There was a problem fetching your branch.' >&2;
exit 1;
fi
current="$(git rev-parse --abbrev-ref HEAD)"
declare -a branches
# merged
for branch in $(git for-each-ref --format "%(refname:short)" refs/heads/ --merged | grep -E -v "$current"); do
branches+=("$branch")
done
# squashed
for branch in $(git for-each-ref --format "%(refname:short)" refs/heads/); do
mergeBase=$(git merge-base "$current" "$branch")
if [[ $(git cherry "$current" "$(git commit-tree "$(git rev-parse "$branch^{tree}")" -p "$mergeBase" -m _)") == "-"* ]]; then
branches+=("$branch")
fi
done
if [[ ${#branches[@]} -eq 0 ]]; then
printf "\n Nothing to garbage."
exit
fi
echo
printf ' %s\n' "${branches[@]}"
echo
read -rp " Will be removed. Continue? (y/N) " -n 1
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo && echo
message=$(git branch -D "${branches[@]}")
echo "${message//Deleted/ Deleted}"
fi