-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremove_branches.sh
executable file
·110 lines (96 loc) · 2.33 KB
/
remove_branches.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
# Script to prepare a working feature branch
set -e
usage(){
echo 'remove_branches.sh [OPTIONS]'
echo 'Remove feature branches and clean up remotes.'
echo ''
echo 'Available options:'
echo '--name: Name of the feature branch to remove'
echo '--default: Name of the default branch, 1.x, 2.x, apply, etc.'
echo '--remote: Defaults to origin, allows you to set an alternative remote name'
}
# Set defaults
REMOTE="origin"
DEFAULT="2.x"
DEVEL_DEFAULT="devel-2.x"
# Parse options arguments.
parse_options(){
while [ "${1:-}" ]; do
case "$1" in
"--name")
shift
BRANCH="$1"
;;
"--default")
shift
DEFAULT="$1"
DEVEL_DEFAULT="devel-$1"
;;
"--remote")
shift
REMOTE="$1"
;;
*)
usage
exit 1
;;
esac
shift
done
}
# Parse options.
echo "Parsing provided options."
parse_options "$@"
# Check we received a feature branch name
if [ -z "$BRANCH" ]; then
echo "You must provide a branch name. Exiting."
exit 1
fi
echo "NOW EXECUTING GIT COMMANDS!"
echo "---------------------------"
echo "Determining naming convention and checking out default branch."
# Fetch branches
git_branches=`git branch`
# Determine naming convention applied
if [[ $git_branches == *"$DEFAULT"* ]]; then
echo "Using devel/$DEFAULT naming convention."
elif [[ $git_branches == *"apply"* ]]; then
echo "Using apply/test naming convention."
DEFAULT="apply"
DEVEL_DEFAULT="test"
else
echo "Unable to determine the default branch name. Exiting."
exit 1
fi
git checkout $DEFAULT
echo "Deleting unwanted feature branches."
git branch -D ${BRANCH}
declare -a fb_branches=(
"_PR_$DEVEL_DEFAULT"
"_PR_$DEFAULT"
"_MR_$DEVEL_DEFAULT"
"_MR_$DEFAULT"
"-PR-$DEVEL_DEFAULT"
"-PR-$DEFAULT"
"-MR-$DEVEL_DEFAULT"
"-MR-$DEFAULT"
)
for fb in "${fb_branches[@]}"
do
if $(git branch | grep -q "${BRANCH}${fb}"); then
git branch -D ${BRANCH}${fb}
fi
done
echo "Pruning the 'origin' remote."
git remote prune origin
# If we have a different remote for SSH, prune that too
if [ -n "$REMOTE" ]; then
echo "Pruning the '$REMOTE' remote."
git remote prune $REMOTE
fi
echo "Putting you back on your specified default branch."
git checkout $DEFAULT
git pull $REMOTE $DEFAULT
echo "---------------------------"
echo "ALL DONE!"