blob: f423a9d2b03f63d6eaa59edba710c7fcd06682b0 (
plain) (
blame)
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
|
#!/usr/bin/env dash
# shellcheck source=/dev/null
SHELL_LIBRARY_VERSION="2.1.2" . %SHELL_LIBRARY_PATH
help() {
cat <<EOF
Automatically remove merged branches (remote and local)
USAGE:
git-cleanup [OPTIONS]
OPTIONS:
--remote | -r
Act on remote branches
--help | -h
Display this help and exit.
--version | -v
Display version and copyright information and exit.
EOF
}
# This should always be the correct answer.
get_default_branch() {
# source: https://stackoverflow.com/a/50056710
# We assume, that 'origin' is the remote in use
git remote show origin | sed -n '/HEAD branch/s|.*: ||p'
}
cleanup() {
default_branch="$(get_default_branch)"
merged_branches="$(git branch --merged "$default_branch" --no-contains "$default_branch" --format='%(refname:short)')"
# shellcheck disable=2086
# We expect the branches to not contain spaces and want git to deal with them
# separately
[ "$merged_branches" ] && git branch --delete $merged_branches
}
cleanup_remote() {
default_branch="$(get_default_branch)"
merged_branches="$(git branch --remotes --merged "$default_branch" --no-contains "$default_branch" --format='%(refname:short)' | sed 's|origin/||')"
# shellcheck disable=2086
# We expect the branches to not contain spaces and want git to deal with them
# separately
[ "$merged_branches" ] && git push --delete origin $merged_branches
}
remote=false
for arg in "$@"; do
case "$arg" in
"--help" | "-h")
help
exit 0
;;
"--version" | "-v")
version
exit 0
;;
"--remote" | "-r")
remote=true
;;
esac
done
if [ "$remote" = "true" ]; then
cleanup_remote
elif [ "$remote" = "false" ]; then
cleanup
else
die "BUG: 'remote' is not true or false but: '$remote'"
fi
# vim: ft=sh
|