Files
zulip/tools/clean-branches
Keegan McAllister 47e2f01f40 clean-branches: Print the abbrev SHA for of each deleted ref
This should make it somewhat easier to recover from accidents.

(imported from commit cf7650342a4f525ffb16e77850b6e451961831b1)
2012-11-06 15:45:24 -05:00

52 lines
1.4 KiB
Bash
Executable File

#!/bin/bash -e
# Deletes any local branches which are ancestors of origin/master,
# and also any branches in origin which are ancestors of
# origin/master and are named like $USER-*.
push_args=''
function is_merged {
! git rev-list -n 1 origin/master.."$1" | grep -q .
}
function clean_ref {
ref="$1"
case "$ref" in
*/master | */HEAD)
return
;;
refs/heads/*)
if is_merged "$ref"; then
echo -n "Deleting local branch $(echo "$ref" | sed 's!^refs/heads/!!')"
echo " (was $(git rev-parse --short "$ref"))"
git update-ref -d "$ref"
fi
;;
refs/remotes/origin/$USER-*)
if is_merged "$ref"; then
remote_name="$(echo "$ref" | sed 's!^refs/remotes/origin/!!')"
echo -n "Deleting remote branch $remote_name"
echo " (was $(git rev-parse --short "$ref"))"
# NB: this won't handle spaces in ref names
push_args="$push_args :$remote_name"
fi
;;
esac
}
if [ "$(git symbolic-ref HEAD)" != 'refs/heads/master' ]; then
echo "Check out master before you run this script." >&2
exit 1
fi
git fetch --prune origin
eval $(git for-each-ref --shell --format='clean_ref %(refname);')
if [ -n "$push_args" ]; then
git push origin $push_args
fi