Last active
July 16, 2020 17:50
-
-
Save nikreiman/5458386 to your computer and use it in GitHub Desktop.
A quick script to clean up Gerrit branches. I tend to have 1 branch per commit for Gerrit reviews, which means that after some time there are a ton of stale branches sitting around. This script examines all your branches, looking for ones which have a change-id that also exists in the current branch. If it has found such a commit, it prompts you…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function git-branch-current() { | |
printf "%s\n" $(git branch 2> /dev/null | grep -e ^* | tr -d "\* ") | |
} | |
function git-branch-cleanup() { | |
local currentBranch=$(git-branch-current) | |
local otherBranch= | |
for otherBranch in $(git branch | grep -v $currentBranch) ; do | |
printf "Branch %s:\n" "$otherBranch" | |
printf " HEAD commit is: %s\n" "$(git log --oneline -n 1 $otherBranch)" | |
local gerritChangeId=$(git log --format=full -n 1 $otherBranch | tail -1 | cut -d ':' -f 2) | |
if ! [ "$gerritChangeId" ] ; then | |
printf " HEAD of %s does not have a Gerrit Change-Id\n" "$otherBranch" | |
continue | |
fi | |
local mergedCommit=$(git log --oneline -n 1 --grep="$gerritChangeId" $currentBranch) | |
if [ "$mergedCommit" ] ; then | |
printf " Commit merged to %s: %s\n" "$currentBranch" "$mergedCommit" | |
local reply="n" | |
read -p " Delete branch? " reply | |
if [ $reply = "y" ] ; then | |
git branch -D $otherBranch | |
fi | |
else | |
echo " Branch unmerged, moving on" | |
fi | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great piece of code, thanks