-
-
Save alexbbt/9bb7b36f209d60da3e917b4b4dab6336 to your computer and use it in GitHub Desktop.
Interactive way to delete Git branches
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
#!/bin/bash | |
interactive=0 | |
manual=0 | |
list=0 | |
branches=() | |
response="" | |
shouldDelete=0 | |
function usage() { | |
echo "" | |
echo "useage: gbd [-b <branch_name>]" | |
echo " [--branch <branch_name>]" | |
echo " [-i] [--interactive]" | |
echo " [-m] [--manual]" | |
echo " [-l] [--list]" | |
echo " [-h] [--help]" | |
echo "" | |
echo " -b --branch:" | |
echo " Input as many branches this way as you want." | |
echo "" | |
echo " Manual Mode:" | |
echo " Input each branch name, one at a time" | |
echo " entering nothing to end the input" | |
echo "" | |
echo " List Mode:" | |
echo " List all branches and ask about each one." | |
echo "" | |
echo "" | |
} | |
function get_branch() { | |
echo -n "Enter name of the branch to delete (nothing to stop)> " | |
read response | |
if [ -n "$response" ]; then | |
branches+=($response) | |
fi | |
} | |
function manual_entry() { | |
get_branch | |
while [[ "$response" != "" ]]; do | |
get_branch | |
done | |
} | |
function list_branches() { | |
tempBranches=() | |
eval "$(git for-each-ref --shell --format='tempBranches+=(%(refname))' refs/heads/)" | |
for branch in "${tempBranches[@]}"; do | |
branch=${branch:11} | |
echo -n "Delete $branch? (y/n) > " | |
read response | |
if [ "$response" == "y" ]; then | |
branches+=($branch) | |
fi | |
done | |
} | |
function interactive() { | |
echo -n "List or Manual? (l/m) > " | |
read response | |
if [ "$response" == "l" ]; then | |
list_branches | |
elif [ "$response" == "m" ]; then | |
manual_entry | |
fi | |
} | |
function delete_branches() { | |
for branch in "${branches[@]}"; do | |
output="$(git branch -d $branch 2>&1)" | |
if [ "$?" == "1" ]; then | |
echo "$branch is not fully merged." | |
echo -n "Are you sure you want to delete it? (y/n) > " | |
read response | |
if [ "$response" == "y" ]; then | |
echo "$(git branch -D $branch)" | |
fi | |
else | |
echo $output | |
fi | |
done | |
} | |
#### MAIN | |
while [ "$1" != "" ]; do | |
case $1 in | |
-b | --branch ) shift | |
branches+=($1) | |
shouldDelete=1 | |
;; | |
-i | --interactive ) interactive=1 | |
shouldDelete=1 | |
;; | |
-m | --manual ) manual=1 | |
shouldDelete=1 | |
;; | |
-l | --list ) list=1 | |
shouldDelete=1 | |
;; | |
-h | --help ) usage | |
exit | |
;; | |
esac | |
shift | |
done | |
if [ "$interactive" = "1" ]; then | |
interactive | |
elif [ "$manual" = "1" ]; then | |
manual_entry | |
elif [ "$list" = "1" ]; then | |
list_branches | |
elif [ "$shouldDelete" = "0" ]; then | |
usage | |
fi | |
if [ "$shouldDelete" = "1" ]; then | |
delete_branches | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment