-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit-merged-cleanup
More file actions
executable file
·89 lines (77 loc) · 2.29 KB
/
git-merged-cleanup
File metadata and controls
executable file
·89 lines (77 loc) · 2.29 KB
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
#!/bin/bash
if [[ "$1" == "--help" ]]; then
echo "Find branches which were already merged in."
echo ""
echo "$0 [change_with_merges]"
echo ""
echo "Those recognized as merged even by git branch --merged"
echo " will be deleted (-d)."
echo "Those detected as merged based on Change-Id will be listed"
echo " and command to remove them (with -D) will be printed out"
echo " for you to just copy-paste+run."
echo ""
echo "You can put name of branches you want to ALWAYS keep"
echo "in the ~/.git-merged-cleanup.conf (content like KEEP_BRANCHES='my extra branches')"
exit 0
fi
if ! git log -1 &>/dev/null; then
echo "You are not in the git repository!"
fi
grep_changeid() {
sed -nr 's/ *Change-Id: ([A-Za-z0-9])/\1/p'
}
strip_brmark() {
sed -r 's/[* ] //'
}
contains() {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done;
return 1;
}
THEBRANCH="${1:-master}"
KEEP_BRANCHES=""
if [[ -f ~/.git-merged-cleanup.conf ]]; then
source "$HOME/.git-merged-cleanup.conf"
fi
echo "Using branch $THEBRANCH as the one with merged changes..."
if [[ ! -z "$KEEP_BRANCHES" ]]; then
echo -n "Should keep the branches: "
echo $KEEP_BRANCHES
fi
MERGED="$(git branch --merged ${THEBRANCH} | strip_brmark | grep -vE "^${THEBRANCH}$")"
MERGED_CHANGES="$(git log "$THEBRANCH" | grep_changeid)"
if [[ ! -z "$MERGED" ]]; then
for BRANCH in $MERGED; do
if contains "$BRANCH" $KEEP_BRANCHES; then
echo "Keeping branch $BRANCH"
else
git branch -d "$BRANCH"
fi
done
fi
BRANCHES="$(git branch | strip_brmark | grep -vE "^${THEBRANCH}$")"
MERGED=""
for BRANCH in $BRANCHES; do
CHID="$(git log -1 "$BRANCH" | grep_changeid)"
if [[ ! -z "$CHID" ]] && contains $CHID $MERGED_CHANGES; then
if contains "$BRANCH" $KEEP_BRANCHES; then
echo "Keeping branch $BRANCH"
continue
else
MERGED="${MERGED} ${BRANCH}"
fi
fi
done
if [[ -z "$MERGED" ]]; then
echo "No already merged branches found"
exit 0
fi
echo "==Already merged branches based on change-id:=="
for BRANCH in $MERGED; do
echo "--[ $BRANCH ]--";
PAGER= git log -1 $BRANCH
done
echo ""
echo ""
echo "if those seems ok you can execute:"
echo " git branch -D $MERGED"