Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristianWitts/be49dc72dfa584baa18fc0f988b9bdf8 to your computer and use it in GitHub Desktop.
Save ChristianWitts/be49dc72dfa584baa18fc0f988b9bdf8 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Copyright(c) 2018 Asit Dhal.
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
#
LIGHT_BLUE='\033[1;34m'
RED='\033[0;31m'
NC='\033[0m' # No Color
INTERACTIVE=""
FORCE_DELETE=""
MASTER="master"
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-f)
FORCE_DELETE="y"
shift
;;
-i)
INTERACTIVE="i"
shift
;;
-fi|-if)
INTERACTIVE="i"
FORCE_DELETE="y"
shift
;;
-h)
echo "$(basename $0) -i Intecative deletion of local branches"
echo "$(basename $0) -f Force delete the branch(es)"
echo "$(basename $0) List local branches with creation and last update date"
exit
;;
*)
shift
esac
done
current_branch=$(git branch | grep "*")
current_branch=${current_branch/* /}
if [ "${current_branch}" != "${MASTER}" ]; then
printf "Please change the current branch to ${LIGHT_BLUE}${MASTER}${NC}\n"
exit
fi
if ! git diff-files --quiet; then
printf "you have unstaged changes. $(basename $0) needs a clean working index\n"
git diff-files --name-status
printf "Please commit or stash them.\n"
exit
fi
if ! git diff-index --cached --quiet HEAD; then
printf "your index contains uncommitted changes. $(basename $0) needs a clean working index\n"
git diff-index --cached --name-status HEAD
printf "Please commit or stash them.\n"
exit
fi
for brnch in $(git branch | sed /\*/d); do
created_commit_id=$(git merge-base ${brnch} ${MASTER})
created_date=$(git log --pretty=format:"%ad" --date=short -n 1 ${created_commit_id})
created_ago=$(git log --pretty=format:"%cr" --date=short -n 1 ${created_commit_id})
last_updated_date=$(git log --pretty=format:"%ad" --date=short -n 1 ${brnch})
updated_before=$(git log --pretty=format:"%cr" --date=short -n 1 ${brnch})
commit_message_subject=$(git log --pretty=format:'%s' -n 1 ${brnch})
printf "Branch name : ${brnch} \n"
printf "Created on : ${created_date}${RED}(${created_ago})${NC}\n"
printf "Last updated on : ${last_updated_date}${RED}(${updated_before}${NC})\n"
printf "Last commit message : ${commit_message_subject}\n"
if [ "${INTERACTIVE}" == "i" ]; then
printf "${LIGHT_BLUE}Delete the branch, followed by [y/n]?${NC} "
read ip
if [ "$ip" == "y" ]; then
if [ "${FORCE_DELETE}" == "y" ]; then
git branch -D ${brnch}
else
git branch -d ${brnch}
if [ $? -ne 0 ]; then
printf "${LIGHT_BLUE}Run with -fi command to delete ${brnch}${NC}\n"
fi
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment