Last active
September 13, 2021 12:14
-
-
Save joaocarmo/ffc5f36c43aacc19a4dfde6aa987ccdf to your computer and use it in GitHub Desktop.
Interactively delete local branches (git)
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 Git Delete | |
# ------------------------------------------------------------------------------ | |
# Author: Joao Carmo | |
# License: MIT | |
# ------------------------------------------------------------------------------ | |
# This script will list all available local branches and you can select one or | |
# more to delete using numbers (from the list). | |
################################################################################ | |
# Variables / Settings | |
current_b=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) | |
local_b=$(git branch 2>/dev/null | awk -F ' +' '! /\(no branch\)/ {print $2}') | |
master_b='master' | |
branches=() | |
base_dashes=18 | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
NOCOLOR='\033[0m' | |
DASH='*' | |
SEPR='**' | |
num=0 | |
zero=0 | |
TOTAL=0 | |
ERADICATE=() | |
# Functions | |
function repl { | |
local ts=$(printf "%${2}s") | |
printf %s "${ts// /$1}" | |
} | |
function listb { | |
IFS=$'\n' read -rd '' -a branches <<<"$1" | |
for branch in "${branches[@]}"; do | |
if [ "$branch" = "$current_b" ]; then | |
HIGHLIGHT=$GREEN | |
elif [ "$branch" = "$master_b" ]; then | |
HIGHLIGHT=$RED | |
else | |
HIGHLIGHT=$NOCOLOR | |
fi | |
num=$(($num + 1)) | |
printf " %5s ${HIGHLIGHT}%s${NOCOLOR}\n" "[$num]" "$branch" | |
done | |
TOTAL=$num | |
printf "\n" | |
} | |
function deleteb { | |
num=0 | |
printf " Select the branches for deletion: " | |
read user_input_raw | |
user_input=$(printf "$user_input_raw" | sed -E 's/[^0-9]+/ /g') | |
user_input_s=$(printf "$user_input" | xargs -n1 | sort -g | xargs) | |
IFS=$' ' read -rd '' -a choices <<<"$user_input_s" | |
printf "\n" | |
printf " The following local branch(es) will be deleted:\n" | |
for raw_choice in "${choices[@]}"; do | |
choice=$(printf $raw_choice | tr -d ' ') | |
if [ ! -z "$choice" ] && [ "$choice" -gt "$zero" ]; then | |
u_choice=$(printf $choice | tr -d '\n') | |
u_branch=${branches[$(($choice - 1))]} | |
if [ "$u_choice" -le "$TOTAL" ] && [ ! "$u_branch" = "$master_b" ]; then | |
printf " %5s %s\n" "[$u_choice]" "$u_branch" | |
num=$(($num + 1)) | |
ERADICATE+=("$u_branch") | |
fi | |
fi | |
done | |
printf "\n" | |
if [ "$num" -gt "0" ]; then | |
printf " Are you sure? (y/N): " | |
read confirm | |
printf "\n" | |
if [ "$confirm" = "y" ]; then | |
printf " Deleting:" | |
printf "\n\n" | |
for d_branch in "${ERADICATE[@]}"; do | |
printf " %s... " "$d_branch" | |
git branch -D "$d_branch" -q | |
exit_code=$? | |
if [ "$exit_code" -eq "0" ]; then | |
printf "Done\n" | |
else | |
printf "Failed\n" | |
fi | |
done | |
else | |
printf " Cancelled" | |
fi | |
else | |
printf " %5s %s" "[x]" "No valid choices available." | |
fi | |
printf "\n" | |
} | |
# Main | |
if [ ! -z "$current_b" ] && [ ! -z "$local_b" ]; then | |
# Display the current branch | |
branch_str_len=${#current_b} | |
printf "${SEPR}" | |
repl "${DASH}" $(($base_dashes + $branch_str_len)) | |
printf "${SEPR}\n" | |
printf "${SEPR} Current branch: ${GREEN}%s${NOCOLOR} ${SEPR}\n" "$current_b" | |
printf "${SEPR}" | |
repl "${DASH}" $(($base_dashes + $branch_str_len)) | |
printf "${SEPR}\n" | |
# List local branches | |
printf "\n" | |
printf " Local branches:\n" | |
listb "$local_b" | |
deleteb | |
printf "\n" | |
else | |
>&2 printf "Error: Not in a .git repository\n" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment