#!/usr/bin/env zsh
function removeCurrentBranch {
sed -E '/\*/d'
}
function removeMaster {
sed -E '/master/d'
}
function leftTrim {
sed -E 's/\*?[[:space:]]+//'
}
# Clean up all cleanly merged branches
for branch in $(git branch | removeCurrentBranch | removeMaster | leftTrim); do
git rebase origin/master $branch &> /dev/null || git rebase --abort &> /dev/null
if (git cherry origin/master $branch | grep '^+') {
echo $branch is not merged
} else {
echo Deleting branch: $branch
}
done
git branch --merged | removeCurrentBranch | removeMaster | leftTrim | xargs -n 1 git branch -d
# Ask about which other branches to delete interactively
file="/tmp/git-cleanup-branches-$(uuidgen)"
all_branches=($(git branch | removeCurrentBranch | leftTrim))
# write branches to file
for branch in $all_branches; do
echo "keep $branch" >> $file
done
# write instructions to file
echo "
# All of your branches are listed above
# (except for the current branch, which you can't delete)
# change keep to d to delete the branch
# all other lines are ignored" >> $file
# prompt user to edit file
vim "$file"
# check each line of the file
cat $file | while read -r line; do
# if the line starts with "d "
if echo $line | grep --extended-regexp "^d " > /dev/null; then
# delete the branch
branch=$(echo $line | sed -E 's/^d //')
git branch -D $branch
fi
done
# clean up
rm $file
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}