1
0
Fork 0
dotfiles/bin/git-branch-prune

63 lines
1,009 B
Bash
Executable file

#!/usr/bin/env bash
export USAGE='<remote>
'
# 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
set -- "${POSITIONAL[@]}"
case "$#" in
0)
remote='origin'
;;
1)
remote="$1"
;;
*) 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