-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-branch-cleanup-based-on-production-branch.sh
executable file
·47 lines (38 loc) · 1.42 KB
/
git-branch-cleanup-based-on-production-branch.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
#!/bin/bash
echo "* Fetching remotes"
git fetch --all
echo "### Pruning remote branches "
# git branch prune origin
local_branches=$(git branch --merged origin/production | cut -c 2-| grep -v 'production$')
echo "### Cleaning local,merged branches '$(echo $local_branches|xargs)'"
echo ""
echo ""
echo $local_branches | xargs -I{} echo "git branch -d {}";
remote_branches=$(git branch -r --merged origin/production | grep -Ev '/(production|HEAD)$')
echo "### Cleaning remote,merged branch references '$(echo $remote_branches|xargs)'"
echo ""
echo ""
echo $remote_branches | xargs -I{} echo "git branch -d -r {}";
merged_branches=$(git for-each-ref refs/heads/ '--format=%(refname:short)' |
while read branch; do
mergeBase=$(git merge-base origin/production $branch) &&
[[ $(git cherry origin/production $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] &&
echo $branch;
done
)
echo "### Cleaning squashed,merged branches '$(echo $merged_branches|xargs)'"
echo ""
echo ""
echo $merged_branches | xargs -I{} echo "git branch -d {}";
echo "### Deleting remote branches '$(echo $remote_branches|xargs)'"
echo ""
echo ""
for b in $remote_branches; do
# `${b/\// }` splits `origin/name` into `origin name`
# TODO: push can accept multiple names per origin
echo "git push --delete ${b/\// }"
echo "https://github.com/Wonolo/platform-core/tree/${b/origin\//}"
done
echo ""
echo ""
# vim: ft=sh