-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup-gerrit-branches
executable file
·81 lines (74 loc) · 2.77 KB
/
cleanup-gerrit-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
#!/bin/bash
# Copied from https://blog.mandre.org/2017/10/22/cleanup_local_git_branches_gerrit_case.html
function prompt_for_missing_commit {
commit=$1
branch=$2
current_branch=$3
git log --oneline -n1 $commit
read -p "Commit $commit in $branch is missing from $current_branch. Inspect? [Yn] " answer
if ! [[ "${answer}" =~ ^(n|no)$ ]]; then
git show $commit
fi
}
function prompt_for_commit_diff {
local_commit=$1
merged_commit=$2
local_branch=$3
current_branch=$4
git log --oneline -n1 $commit
read -p "Commit $local_commit in $local_branch and $merged_commit in $current_branch differ. Inspect? [Yn] " answer
if ! [[ "${answer}" =~ ^(n|no)$ ]]; then
interdiff <(git show $local_commit) <(git show $merged_commit) | colordiff
fi
}
current_branch=$(git symbolic-ref --short HEAD)
if [ "$current_branch" != "main" ]; then
echo "This script should be run from the main branch" >&2
exit 1
fi
for branch in $(git for-each-ref --format='%(refname:short)' refs/heads/); do
if [ "$branch" == "$current_branch" ]; then
continue
fi
echo
echo "Checking branch $branch"
branch_differs=0
for commit in $(git log --no-merges --pretty=format:"%h" ${current_branch}..${branch}); do
change_id=$(git log -n1 --pretty=format:%b $commit | awk '/Change-Id:/ {print $0}')
if [ -z "$change_id" ]; then
branch_differs=1
#prompt_for_missing_commit $commit $branch $current_branch
continue
fi
merged_commit=$(git log --pretty=format:%h --grep "$change_id" ${current_branch})
if [ -z "$merged_commit" ]; then
branch_differs=1
#prompt_for_missing_commit $commit $branch $current_branch
continue
else
# Check that the merged patch is similar to what is in local branch
# NOTE needs interdiff from patchutils and colordiff
if [[ $(interdiff <(git show $commit) <(git show $merged_commit) 2>&1) ]]; then
branch_differs=1
#prompt_for_commit_diff $commit $merged_commit $branch $current_branch
fi
fi
done
if [ $branch_differs -eq 0 ]; then
read -p "$branch fully merged. Delete? [yN] " answer
if [[ "${answer}" =~ ^(y|yes)$ ]]; then
git branch -D $branch
fi
elif [[ "$branch" =~ ^review/[^s][^h].* ]]; then
read -p "$branch is a gerrit review branch. Delete? [yN] " answer
if [[ "${answer}" =~ ^(y|yes)$ ]]; then
git branch -D $branch
fi
else
continue
read -p "$branch differs from $current_branch. Delete anyway? [yN] " answer
if [[ "${answer}" =~ ^(y|yes)$ ]]; then
git branch -D $branch
fi
fi
done