Skip to content

Instantly share code, notes, and snippets.

@anonymuse
Forked from nikreiman/gist:5458386
Created March 8, 2016 18:44
Show Gist options
  • Save anonymuse/abfa6d21ccc22ff6a13c to your computer and use it in GitHub Desktop.
Save anonymuse/abfa6d21ccc22ff6a13c 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…
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