-
Notifications
You must be signed in to change notification settings - Fork 4
/
delete-old-branches.sh
executable file
·56 lines (40 loc) · 1.49 KB
/
delete-old-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
#!/bin/bash
git fetch -p origin
DAYS=$1
if [ "$DAYS" == "" ]; then
DAYS=7
fi
date --version > /dev/null 2>&1
if [ $? == 0 ]; then
DATE=`date +%Y-%m-%d -d "-${DAYS}days"`
else
DATE=`date -v-${DAYS}d +%Y-%m-%d`
fi
REMOTE_BRANCHES=$(for k in `git branch -r --merged master | perl -pe 's/^..(.*?)( ->.*)?$/\1/'`; do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Creset" --after="$DATE" $k -- | head -n 1`$k; done | sort -r | grep '^origin' | sed 's/ *origin\///')
LOCAL_BRANCHES=$(for k in `git branch --merged master | perl -pe 's/^..(.*?)( ->.*)?$/\1/'`; do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Creset" --after="$DATE" $k -- | head -n 1`$k; done | sort -r | grep '^origin')
if [ "$REMOTE_BRANCHES" != "" ]; then
echo "The following remote branches are fully merged into master and older than $DAYS days and will be removed: $REMOTE_BRANCHES"
read -p "Continue (y/n)? "
if [ "$REPLY" == "y" ]; then
echo $REMOTE_BRANCHES | xargs git push origin --delete
echo "Done!, Obsolete branches are removed"
else
echo "Moving on...."
fi
else
echo "No remote branches"
fi
if [ "$LOCAL_BRANCHES" != "" ]; then
echo "The following local branches are fully merged into master and older than $DAYS days and will be removed: $LOCAL_BRANCHES"
read -p "Continue (y/n)? "
if [ "$REPLY" == "y" ]; then
echo $LOCAL_BRANCHES | xargs git branch -r -d
echo "Done!, Obsolete branches are removed"
else
echo "Moving on...."
fi
else
echo "No local branches"
fi
echo "Ended"
exit 0