Skip to content

Instantly share code, notes, and snippets.

@m4rkk
Last active November 10, 2017 22:53
Show Gist options
  • Select an option

  • Save m4rkk/5a603f2c23e0c9f2e84edaf40a88ff7c to your computer and use it in GitHub Desktop.

Select an option

Save m4rkk/5a603f2c23e0c9f2e84edaf40a88ff7c to your computer and use it in GitHub Desktop.
Lists or removes branches already merged to HEAD. Runs prompted depending on options.
#!/bin/bash
function usage
{
cat <<EOF
Usage: `basename $0` [options] namespace
Lists or deletes <<namespace>> branches merged with HEAD; namespace defaults to "bugfix/"
Options:
--delete|-d Delete resulting branches instead of just listing them.
--unsafe|-u No prompts before deleting a branch.
--help|-h|-? Print usage.
EOF
}
# Variables for user options
delete=""
unsafe=""
namespace="bugfix/"
# Parameters handled
while [[ $# > 0 ]]
do
key="$1"
case $key in
-d|--delete)
delete="yes"
shift
;;
-u|--unsafe)
unsafe="yes"
shift
;;
-?|-h|--help)
usage
exit 0
;;
*)
namespace="$1"
shift
;;
esac
done
function promptToDeleteBranch()
{
if [[ $unsafe == "yes" ]];
then
printf "deleting branch $1\n"
git push origin $1 --delete
else
printf "Delete branch $1?\nyes|no|all|quit: "; read answer
case $answer in
Y|y|yes|Yes|YES)
printf "Deleting...\n"
git push origin $1 --delete
;;
N|n|no|No|NO)
printf "Not deleting\n"
;;
A|a|all|All|ALL)
printf "Deleting all\n"
unsafe="yes"
;;
Q|q|quit|Quit|QUIT)
printf "Done, quitting\n"
exit 0
;;
esac
fi
}
for branch in $(git branch -r --merged HEAD | grep origin | grep -v develop | grep -v master | grep "origin/${namespace}" | sed -E "s|^ *origin/||g")
do
if [[ $delete != "yes" ]];
then
echo $branch
else
promptToDeleteBranch $branch
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment