-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-delete-merged-branches
executable file
·280 lines (267 loc) · 8.8 KB
/
git-delete-merged-branches
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env bash
# git-delete-merged-branches
# --------------------------------------------------------------------------------------------------
# Version
# -------
# 2.0.0
# --------------------------------------------------------------------------------------------------
# Authors
# -------
# Filipe Kiss <[email protected]> http://github.com/filipekiss
# --------------------------------------------------------------------------------------------------
# Usage
# -----
# Add this to your $PATH and invoke as a git command:
#
# git delete-merged-branches
#
# Or simply run ./git-delete-merged-branches
# --------------------------------------------------------------------------------------------------
# Options
# -------
#
# --remote
# Run against remote branches. Default is running on local
#
# --dry-run, -n
# Don't actually delete anything. Useful for checking what would be deleted
#
# --default-branch [main]
# Use this to pass the name of the branch that act as the default, in case your
# repository uses a different name. Default value is main
#
# --remote-name [origin]
# Set the name of the remote to run against. Defaults to origin
#
# --force
# Force deletion of branches. You shouldn't need this option, ever
#
# --allow [branch-name]
# If this option is given, only branches that match the given names we'll be
# deleted. It can be passed multiple times. * wildcard is supported
#
# e.g. git delete-merged-branches --allow 'feature/*' --allow 'release/*'
#
# Would delete feature/some-feature, release/1.0.0 but would leave hotfix/1.0.1
# alone
#
# --deny [branch-name]
# If this option is given, only branches that DON'T match the given names we'll be
# deleted. It can be passed multiple times. * wildcard is supported
#
# e.g. git delete-merged-branches --deny 'feature/*' --deny 'release/*'
#
# Would delete hotfix/1.0.1, but would leave alone feature/some-feature, release/1.0.0
#
# --squashed
# Also check for branches that were merged to main using the squash strategy
#
# --squashed-only
# Check only for branches that we're merged using the squash strategy
#
# --no-squash-warning
# Supress the output information when squashed branches are found but no
# --squashed option was passed
# --------------------------------------------------------------------------------------------------
# Arguments
# ---------
# [branch name]
# The only argument this command accepts is the branch name to compare other branches against.
# Defaults to origin/main (or whatever remote you pass using the --remote-name option)
_parse_arguments() {
local arg
while (($# > 0)); do
arg="$1"
case "$arg" in
--remote | --remotes)
_use_remotes="-r"
;;
--dry-run | -n)
_dry_run="1"
;;
--default-branch=*)
_default_branch=$(echo "$arg" | cut -d '=' -f2)
;;
--default-branch)
_default_branch=$2
shift
;;
--remote-name=*)
_remote_name=$(echo "$arg" | cut -d '=' -f2)
;;
--remote-name)
_remote_name=$2
shift
;;
--allow=*)
_branch_allow+=("$(echo "$arg" | cut -d '=' -f2)")
;;
--allow)
_branch_allow+=("$2")
shift
;;
--deny=*)
_branch_deny+=("$(echo "$arg" | cut -d '=' -f2)")
;;
--deny)
_branch_deny+=("$2")
shift
;;
--squashed)
_include_squashed_merge_branches="yes"
;;
--squashed-only)
_only_squashed_merge_branches="yes"
;;
--no-squash-warning)
_no_squash_warning="yes"
;;
--force-delete)
_force_delete="-D"
;;
--delete)
_delete="yes"
;;
--*)
# Inexistent dashed option, do nothing
;;
-*)
# Inexistent dashed option, do nothing
;;
"") ;;
*)
[[ -z ${_branch_name:-} ]] && _branch_name="$arg"
;;
esac
shift
done
return 0
}
_get_branches() {
git branch ${_use_remotes:-} --merged "${_branch_name:-"${_remote_name:-"origin"}/${_default_branch:-"main"}"}" | grep -v '\*' | grep -v HEAD
}
_get_squashed_branches() {
git checkout -q "${_default_branch:-"main"}" && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read -r branch; do mergeBase=$(git merge-base "${_default_branch:-"main"}" "$branch") && [[ $(git cherry ${_default_branch:-"main"} "$(git commit-tree "$(git rev-parse "${branch}^{tree}")" -p "$mergeBase" -m _)") == "-"* ]] && echo "$branch"; done
}
_validate_branch() {
local targetBranch="${1}"
local defaultBranch="${2}"
if _validate_branch_name "$targetBranch" "$defaultBranch"; then
return 1
fi
if ! _match_whitelist "$targetBranch"; then
return 1
fi
if _match_blacklist "$targetBranch"; then
if _match_whitelist "$targetBranch" "yes"; then
return 0
fi
return 1
fi
return 0
}
_match_blacklist() {
local targetBranch="$1"
[[ -z ${_branch_deny[*]} ]] && return 1
for blacklisted in ${_branch_deny[*]}; do
if _validate_branch_name "$targetBranch" "$blacklisted"; then
# echo "Branch ${targetBranch} ignored. Matches blacklisted pattern ${blacklisted}"
return 0
fi
done
return 1
}
_match_whitelist() {
local targetBranch="$1"
local errorIfNoWhitelist="${2:-}"
[[ -z ${_branch_allow[*]} && -z ${errorIfNoWhitelist} ]] && return 0
[[ -z ${_branch_allow[*]} && -n ${errorIfNoWhitelist} ]] && return 1
for whitelisted in ${_branch_allow[*]}; do
if _validate_branch_name "$targetBranch" "$whitelisted"; then
# echo "Branch ${targetBranch} matched allow pattern ${whitelisted}"
return 0
fi
done
return 1
}
_validate_branch_name() {
local targetBranch="${1}"
local defaultBranch="${2}"
if [[ ${_use_remotes:-} ]]; then
defaultBranch="${_remote_name:-"origin"}/${2}"
fi
case "$targetBranch" in
*\**)
[[ $targetBranch == *$defaultBranch* ]] && return 0 || return 1
;;
*)
[[ $targetBranch == "$defaultBranch" ]] && return 0 || return 1
;;
esac
}
_delete_remote_branch() {
local branch="$1"
local push_name
push_name="$(echo "${branch}" | cut -d '/' -f2-)"
git push "${_remote_name:-"origin"}" --delete "$push_name"
}
_delete_local_branch() {
local branch="$1"
local forceDelete=${_force_delete:-"-d"}
if [[ " ${_squashed_list[*]} " =~ ' '${branch}' ' ]]; then
forceDelete="-D"
fi
git branch "${forceDelete}" "$branch"
}
_show_squashed_branches() {
if [[ -z ${_include_squashed_merge_branches} && -z ${_only_squashed_merge_branches:-} && -z ${_no_squash_warning:-} && -n ${_squashed_list} ]]; then
echo ""
echo "The following branches were merge-squashed into ${_default_branch:-"main"} and can be included with the following command:"
echo ""
echo "git delete-merged-branches ${_passed_args}${_passed_args:+ }--squashed"
echo ""
for branch in ${_squashed_list[*]}; do
echo "${branch}"
done
echo ""
echo "To supress this message, use git delete-merged-branches ${_passed_args}${_passed_args:+ }--no-squash-warning"
fi
}
main() {
_branch_allow=()
_branch_deny=()
_passed_args="$*"
_parse_arguments "$@"
read -r -a _branch_list <<<"$(_get_branches)"
read -r -a _squashed_list <<<"$(_get_squashed_branches)"
# Ensure duplicates are removed
_squashed_list=("$(echo "${_squashed_list[*]} ${_branch_list[*]} ${_branch_list[*]}" | tr ' ' '\n' | sort | uniq -u)")
if [[ -n ${_include_squashed_merge_branches:-} ]]; then
_branch_list+=("${_squashed_list[*]}")
elif [[ -n ${_only_squashed_merge_branches:-} ]]; then
read -r -a _branch_list <<<"${_squashed_list[*]}"
fi
[[ -z ${_branch_list[*]} ]] && echo "No branches merged into ${_default_branch:-'main'}" && _show_squashed_branches && exit 0
for branch in ${_branch_list[*]}; do
if _validate_branch "$branch" "${_default_branch:-"main"}"; then
if [[ ${_delete:-"no"} == "no" ]]; then
echo "Would Deleted: $branch"
continue
fi
if [[ -n ${_use_remotes:-} ]]; then
_delete_remote_branch "$branch"
continue
fi
_delete_local_branch "$branch"
fi
done
_show_squashed_branches
}
# Save current shell options to STATE
# Ensure that no globs are expanded, so we can use * wildcards for (black|white)list
# Execute our main function
# Restore shell options from STATE
STATE=$(set +o)
set -f
main "$@"
eval "$STATE"