-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgit-merged
executable file
·68 lines (58 loc) · 1.25 KB
/
git-merged
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
#!/bin/bash
set -e
die() {
echo $@
exit 1
}
usage() {
cat << EOF
usage: $0 options [delete]
-r [remote] Show remote merged branches. Active by default. remote defaults
to origin if not specified.
-i Ignored branches regex for remotes.
-l Show local merged. Current branch and main are ignored.
-h Show this message.
EOF
}
git rev-parse 2> /dev/null || die "It looks like you're not in a git repo."
branch_type='remote'
while getopts "hr:li:" opt
do
case $opt in
i)
exclude_pattern=$OPTARG
;;
l)
branch_type='local'
;;
r)
branch_type='remote'
remote_name=$OPTARG
;;
h)
usage
exit
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
esac
done
: ${remote_name:="origin"}
: ${exclude_pattern:="main"}
shift $((OPTIND-1))
delete=$1
if [ $branch_type == "local" ]; then
branches=$(git branch --merged | grep -Ev "^\*|main")
echo $branches
if [ "$delete" == "delete" ]; then
echo $branches | xargs git branch -d
fi
else
branches=$(git branch -r --merged | grep $remote_name | grep -Ev $exclude_pattern | sed "s/$remote_name\///")
echo $branches
if [ "$delete" == "delete" ]; then
echo $branches | xargs git push $remote_name --delete
fi
fi