2016-01-12 13:08:43 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
2012-11-06 20:38:26 +01:00
|
|
|
|
2017-06-01 20:27:44 +02:00
|
|
|
# usage: clean-branches
|
2012-11-06 20:38:26 +01:00
|
|
|
# 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-*.
|
|
|
|
|
2017-06-01 20:27:44 +02:00
|
|
|
# usage: clean-branches --reviews
|
|
|
|
# Deletes all the above mentioned branches as well as branches
|
|
|
|
# created by the scripts like `fetch-rebase-pull-request`. Be careful
|
2020-03-28 01:25:56 +01:00
|
|
|
# as this would also remove other branches with names like review-*
|
2017-06-01 20:27:44 +02:00
|
|
|
|
|
|
|
review=0
|
|
|
|
if [ $# -ne 0 ] && [ "$1" == "--reviews" ]; then
|
|
|
|
review=1
|
|
|
|
fi
|
2015-09-26 03:47:30 +02:00
|
|
|
push_args=()
|
2012-11-06 20:38:26 +01:00
|
|
|
|
|
|
|
function is_merged {
|
|
|
|
! git rev-list -n 1 origin/master.."$1" | grep -q .
|
|
|
|
}
|
|
|
|
|
|
|
|
function clean_ref {
|
|
|
|
ref="$1"
|
|
|
|
case "$ref" in
|
|
|
|
*/master | */HEAD)
|
|
|
|
return
|
|
|
|
;;
|
|
|
|
|
2017-06-01 20:27:44 +02:00
|
|
|
refs/heads/review-*)
|
|
|
|
if [ $review -ne 0 ]; then
|
2018-08-03 02:14:48 +02:00
|
|
|
echo -n "Deleting local branch ${ref#refs/heads/}"
|
2017-06-01 20:27:44 +02:00
|
|
|
echo " (was $(git rev-parse --short "$ref"))"
|
|
|
|
git update-ref -d "$ref"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
2012-11-06 20:38:26 +01:00
|
|
|
refs/heads/*)
|
|
|
|
if is_merged "$ref"; then
|
2018-08-03 02:14:48 +02:00
|
|
|
echo -n "Deleting local branch ${ref#refs/heads/}"
|
2012-11-06 21:12:48 +01:00
|
|
|
echo " (was $(git rev-parse --short "$ref"))"
|
2012-11-06 20:38:26 +01:00
|
|
|
git update-ref -d "$ref"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
refs/remotes/origin/$USER-*)
|
|
|
|
if is_merged "$ref"; then
|
2018-08-03 02:14:48 +02:00
|
|
|
remote_name="${ref#refs/remotes/origin/}"
|
2012-11-06 21:12:48 +01:00
|
|
|
echo -n "Deleting remote branch $remote_name"
|
|
|
|
echo " (was $(git rev-parse --short "$ref"))"
|
2012-11-06 20:38:26 +01:00
|
|
|
# NB: this won't handle spaces in ref names
|
2015-09-26 03:47:30 +02:00
|
|
|
push_args=("${push_args[@]}" ":$remote_name")
|
2012-11-06 20:38:26 +01:00
|
|
|
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
|
|
|
|
|
2015-09-26 03:47:30 +02:00
|
|
|
eval "$(git for-each-ref --shell --format='clean_ref %(refname);')"
|
2012-11-06 20:38:26 +01:00
|
|
|
|
2015-09-26 03:47:30 +02:00
|
|
|
if [ "${#push_args}" -ne 0 ]; then
|
|
|
|
git push origin "${push_args[@]}"
|
2012-11-06 20:38:26 +01:00
|
|
|
fi
|