summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTails developers <tails@boum.org>2015-02-24 15:58:38 +0100
committerTails developers <tails@boum.org>2015-02-24 15:58:38 +0100
commitf5d95800d69aaa1dc8216f81831687569a4bb44b (patch)
tree4eee97927f2fc21a92a465cad1fd004e8a414310
parenta18aaaae2dea1ba4b8c1f7cacfcd33da652e9063 (diff)
added script to delete obsolete branches
-rw-r--r--bin/git-delete-branches.sh71
1 files changed, 71 insertions, 0 deletions
diff --git a/bin/git-delete-branches.sh b/bin/git-delete-branches.sh
new file mode 100644
index 0000000..1ac4e72
--- /dev/null
+++ b/bin/git-delete-branches.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+#############################
+# Configure as needed #
+#############################
+# Set to 1 to remove merged branches from remotes
+REMOVE_FROM_REMOTES=1
+
+# If REMOVE_FROM_REMOTES was set to 1 above, set this to the remotes for which
+# you have push privileges
+MANAGED_REMOTES="tails"
+
+# These branches will never be removed
+BRANCHES_TO_KEEP="master head devel experimental testing"
+
+######################################################
+# Nothing below this point should need to be edited #
+######################################################
+
+generate() {
+ # Take in space delimited string, output | delimited
+ echo "$1" | sed 's/\s/|/g'
+}
+
+if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
+ echo "${PWD} is not a Git tree. Exiting."
+ exit 1
+fi
+
+CURRENT=$(git rev-parse --abbrev-ref HEAD)
+if [ "$CURRENT" != 'master' ]; then
+ echo "Switch to the master branch before running this script." >&2
+ exit 1
+fi
+
+echo "Fetching remote(s)"
+git remote update --prune
+
+[ $REMOVE_FROM_REMOTES -eq 1 ] && \
+ REMOTE_BR=$(git branch -r --merged | grep -v '\->' |\
+ grep -vE "\s+([^/]+)/($(generate "$BRANCHES_TO_KEEP"))$" |\
+ grep -E "($(generate "$MANAGED_REMOTES"))/")
+LOCAL_BR=$(git branch --merged | grep -Ev "\s+($(generate "$BRANCHES_TO_KEEP"))$")
+
+if [ -z "$REMOTE_BR" ] && [ -z "$LOCAL_BR" ]; then
+ echo "Woohoo! No unmerged branches!" >&2
+else
+ [ -n "$REMOTE_BR" ] && \
+ echo "The following merged remote branches will be removed:" && \
+ echo "$REMOTE_BR"
+
+ [ -n "$LOCAL_BR" ] && \
+ echo "The following merged local branches will be removed:" && \
+ echo "$LOCAL_BR"
+ echo -n "Remove branches? (Y/N): "
+ read answer
+ case $answer in
+ y|Y|Yes|yes)
+ for BRANCH in $REMOTE_BR; do
+ git push $(echo $BRANCH | sed 's/\([^/]\+\)\/\(.\+\)/\1 :\2/')
+ done
+ if [ -n "$LOCAL_BR" ]; then
+ git branch --merged | grep -Ev "\s+($(generate "$BRANCHES_TO_KEEP"))$" | xargs -n30 git branch -d
+ fi
+ ;;
+ *)
+ echo "Aborting due to user request." >&2
+ exit 0
+ esac
+fi
+