1
0
Fork 0
dotfiles/bin/git-branch-prune
2020-10-14 10:51:25 -07:00

65 lines
1.1 KiB
Bash
Executable file

#!/usr/bin/env bash
export USAGE='<remote> [-f]
Flags:
-f, --force Prune branches without confirmation
'
# This is the recommended pattern from `man git-sh-setup`.
#
# shellcheck disable=SC1090
. "$(git --exec-path)/git-sh-setup"
set -euo pipefail
POSITIONAL=()
FORCE=0
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-f|--force)
FORCE=1
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
case "${#POSITIONAL[@]}" in
0)
remote='origin'
;;
1)
remote="${POSITIONAL[0]}"
;;
*) usage ;;
esac
git remote prune "${remote}"
# This grep "fails" if there are no matches, so temporarily disable error
# checking.
set +e
pruned_branches=$(git branch -vv | grep ': gone]')
set -e
if [ -z "${pruned_branches}" ]; then
>&2 echo 'Nothing to do!'
exit 0
fi
if [ "${FORCE}" -eq 1 ]; then
selector='cat'
else
selector='vipe'
fi
"${selector}" <<< "${pruned_branches}" | \
awk '{print $1}' | \
xargs git branch -D