-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-branchkill
executable file
·209 lines (190 loc) · 7.42 KB
/
git-branchkill
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/sh
### Description: this Git method removes an obsolete (i.e. merged)
### branch and its trackers.
### Script Copyright (C) 2015 by Jim Klimov, License: MIT
git_branch_kill() {
EXEC=""
LOCALKILL=-d
REMOTEKILL=no
REPODIRS=""
BASE_REPO=upstream
BASE_BRANCH=master
# We do parse some outputs, so keep it single-language
LANG=C
LC_ALL=C
export LANG LC_ALL
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
echo "Usage: $0 [-n] [-D] [-DD|-DDD] [-R 'repodir ...'] branchname(s)..."
echo "Removes local branches and remote references to branches, so as to"
echo "help clean up after a branch has been merged and is no longer used"
echo " -n Read-only mode to see what might be done"
echo " -D Allow destruction of un-merged local branches"
echo " -DD Cause the remote repo(s) to forget named branch(es)"
echo " -DDD ONLY cause the remote repo(s) to forget named branch(es)"
echo " (do not remove local branches and/or references)"
echo " -R 'dirs...' List one or more local repositories (directories)"
echo " to act upon; the parameter may be specified more than once,"
echo " and its argument may be a space-separated list of directories"
echo " (e.g. from a shell glob or Midnight Commander substitution);"
echo " if no '-R ...' is specified, script works in current dir"
echo " -BR 'repo' Change the untouchable base repo (default: ${BASE_REPO})"
echo " -BB 'branch' Change the untouchable base branch (default: ${BASE_BRANCH})"
echo " branch(es) List one or more branch name(s) to remove"
return 0
;;
-n) EXEC=echo
echo "Info: Running in read-only mode, no branches should get cut" >&2
shift 1
;;
-D) LOCALKILL=-D
echo "Info: Allowing destruction of un-merged local branches!" >&2
shift 1
;;
-DD) REMOTEKILL=yes
echo "Info: Pushing destruction of named branches to remotes!" >&2
shift 1
;;
-DDD) REMOTEKILL=yes
LOCALKILL=""
echo "Info: ONLY pushing destruction of named branches to remotes!" >&2
shift 1
;;
-BR) BASE_REPO="$2"; shift ;;
-BB) BASE_BRANCH="$2"; shift ;;
-R) if [ -n "$2" ] ; then
for D in $2 ; do
if [ -d "$D" ]; then
REPODIRS="$REPODIRS $D"
else
echo "Error: Repo dir '$D' specified but not found" >&2
return 1
fi
done
shift 2
else
echo "Error: Repo dir '$2' specified but not found" >&2
return 1
fi
;;
*) break ;; # fall through with branch names
esac
done
[ $# = 0 ] && echo "Error: branch name(s) required" >&2 && return 1
### List of failed-to-kill objects:
### "[MRLP](RESCODE)\tPATH\tBRANCH\tREASON"
FAILED=""
branchkill() {
CURBRANCH="`git branch | egrep '^\* ' | sed 's,^\* ,,'`" || return
[ -n "$CURBRANCH" ] || return
echo "=== `date`: Currently checked out branch is '$CURBRANCH'"
REMOTES="`git remote -v | egrep 'push' | awk '{print $1}'`" || REMOTES=""
if [ -n "$REMOTES" ] ; then
echo "=== `date`: Known remote repos are: `echo "$REMOTES" | tr '\n' ' '`"
else
echo "=== `date`: WARNING: No remote repos are known; will only process local branches"
fi
RES=0
[ -n "$LOCALKILL" ] && \
for B in "$@" ; do
echo ""
echo "=== `date`: Processing a branch named '$B'"
if [ "$B" = "$CURBRANCH" -o "$B" = "${BASE_BRANCH}" ] ; then
echo "Error: can not remove master branch '${BASE_BRANCH}', or current branch: '$B'" \
"(please change into some other)" >&2
RES=2
FAILED="$FAILED
M($RES) `pwd` $B master branch, or current branch"
else
[ -n "$REMOTES" ] && \
for R in $REMOTES ; do
echo "Info: Removing local reference to remote branch: '$R/$B' ..."
_OUT="`$EXEC git branch -dr "$R/$B" 2>&1`"
_RES=$?
if echo "$_OUT" | egrep " branch '.*' not found" >/dev/null && [ "$_RES" != 0 ]; then
echo "NOTerror: Ignoring the 'not found' error just below..."
else
RES=$_RES
[ "$_RES" != 0 ] && FAILED="$FAILED
R($RES) `pwd` $B `echo ${_OUT} | tr '\n' ' '`"
fi
echo "$_OUT"
done
echo "Info: Removing local branch: '$B' ..."
_OUT="`$EXEC git branch $LOCALKILL "$B" 2>&1`"
_RES=$?
if echo "$_OUT" | egrep " branch '.*' not found" >/dev/null && [ "$_RES" != 0 ]; then
echo "NOTerror: Ignoring the 'not found' error just below..."
else
RES=$_RES
[ "$_RES" != 0 ] && FAILED="$FAILED
L($RES) `pwd` $B `echo ${_OUT} | tr '\n' ' '`"
fi
echo "$_OUT"
fi
# Note that you may (try to) remove any branch on a remote server,
# including your current one or a master one (not all git workflows
# even use a "master" or assign a special meaning to it).
[ -n "$REMOTES" ] && \
if [ "$REMOTEKILL" = yes ]; then
if [ "$B" = "${BASE_BRANCH}" ] ; then
echo "Error: can not remove master branch '${BASE_BRANCH}'" >&2
RES=2
FAILED="$FAILED
P($RES) `pwd` $B master branch"
else
for R in $REMOTES ; do
if [ "$R" = "${BASE_REPO}" ]; then
echo "WARNING: Not removing branches in upstream repo: '$R/$B' ..." >&2
continue
fi
echo "Info: Removing the branch '$B' in remote repo '$R' ..."
_OUT="`$EXEC git push "$R" ":$B" 2>&1`"
_RES=$?
if [ "$_RES" = 0 ] || \
echo "$_OUT" | egrep " does not appear to be a git repository|remote ref does not exist|Please make sure you have the correct access rights" >/dev/null \
; then
echo "OK, now OTHER clones of this remote repo should 'git fetch --all --prune' (so they do not re-push branch '$B' - unless this is desired)"
else
RES=$_RES
[ "$_RES" != 0 ] && FAILED="$FAILED
P($RES) `pwd` $B `echo ${_OUT} | tr '\n' ' '`"
fi
echo "$_OUT"
done
fi
fi
done
[ $RES = 0 ] && \
echo "Overall for this repo: OK" || \
echo "Overall for this repo: FAILED ($RES)"
return $RES
}
echo "Info: Current dir: '`pwd`'"
RRES=0
if [ -z "$REPODIRS" ] ; then
branchkill "$@"
RRES=$?
REPOSTR="single repo"
else
_PWD="`pwd`"
for D in $REPODIRS ; do
echo "Info: Killing branches in repodir '$D'"
trap "cd ${_PWD} ; echo 'Killed by signal!' >&2; [ ${RRES} = 0 ] && RRES=2; break;" 1 2 3 15
{ cd "$D" && branchkill "$@" ; } || RRES=$?
cd "${_PWD}"
trap '-' 1 2 3 15
echo ""
done
REPOSTR="multiple repos"
fi
[ $RRES = 0 ] && \
echo "Overall for ${REPOSTR}: OK" || \
echo "Overall for ${REPOSTR}: FAILED ($RRES)"
[ -n "$FAILED" ] && \
echo "List of failed items: $FAILED"
# First line is empty by construction process
return $RRES
}
git_branch_kill "$@"