#!/usr/bin/env bash
#
# ---
# source: https://github.com/chrishwiggins/mise

set -u

# prompt for user confirmation
confirm() {
  printf '%s' "$1 (y/n): "
  # read -r -p "$1 (y/n): " response
  read -r reply
  case "${reply}" in
    [yY][eE][sS] | [yY])
      true
      ;;
    *)
      false
      ;;
  esac
}

# prune remote tracking branches
if confirm "Prune remote tracking branches"; then
  echo "Executing: git remote prune origin"
  git remote prune origin
fi

# remove unused objects
if confirm "Remove unused objects"; then
  echo "Executing: git gc --prune=now"
  git gc --prune=now
fi

# clear reflog
if confirm "Clear reflog"; then
  echo "Executing: git reflog expire --expire=now --all"
  git reflog expire --expire=now --all
fi

# perform garbage collection
if confirm "Perform garbage collection"; then
  echo "Executing: git gc --aggressive --prune=now"
  git gc --aggressive --prune=now
fi

# remove unnecessary local branches
if confirm "Remove unnecessary local branches"; then
  printf '%s' "Enter the branch name to delete: "
  read -r branch_name
  echo "Executing: git branch --delete ${branch_name}"
  git branch --delete "${branch_name}"
fi
