Skip to content

Instantly share code, notes, and snippets.

@robsonke
Last active July 21, 2018 11:21
Show Gist options
  • Save robsonke/dfeadfb21f25a691eb071257073a82d0 to your computer and use it in GitHub Desktop.
Save robsonke/dfeadfb21f25a691eb071257073a82d0 to your computer and use it in GitHub Desktop.
#!/bin/sh
#############################################
## Bash script to cleanup git repositories ##
## Author: Rob Sonke ##
#############################################
# To execute this in all subdirs of the current dir:
# for d in ./*
# do
# (cd $d && ~/Downloads/removeOldBranches.sh)
# done
# Settings
# how old should commits be in old branches?
WEEKS=6
# which branch will we use to check if it's merged into this?
BASEBRANCH=master
COLOR_OFF='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
##########################################
# DO NOT CHANGE ANYTHING BELOW THIS LINE #
##########################################
function getDateLimit {
unamestr=$(uname)
if [[ "$unamestr" == 'Darwin' ]]; then
LIMIT=$(date -j -v-${WEEKS}w +%s)
else
LIMIT=$(date --date="$WEEKS weeks ago" +%s)
fi
}
function filterByDate {
while read branch date zone; do
if [[ "$date" -le "$LIMIT" ]]; then
echo $branch
fi
done
}
function skipBranches {
sed 's/ *origin\///' | grep -v HEAD | grep -v master | grep -v develop \
| sed -e 's~^\s\+~~' \
| sed -e 's~refs/remotes/~~' \
| sed -e 's~remotes/origin/~~' \
| sed -e 's~^origin/~~'
}
function getRemoteBranches {
git for-each-ref --sort=-committerdate refs/remotes --format="%(refname) %(committerdate:raw)"
}
##########################
# processing starts here #
##########################
# get a unix timestamp based on the number of weeks
getDateLimit
# Bring all up to date and make sure we work from the right branch
echo "Lets switch to $BASEBRANCH and make sure we're up to date."
git checkout $BASEBRANCH
git pull
git fetch --prune origin
echo
MERGED_BRANCHES=$(git branch -r --merged origin/$BASEBRANCH | skipBranches)
if [ -z "$MERGED_BRANCHES" ]
then
echo "There are no old merged branches, let's move on to the unmerged branches.\n"
else
echo "The following remote branches are fully merged into $BASEBRANCH and will be removed:"
echo "$YELLOW$MERGED_BRANCHES$COLOR_OFF"
read -p "\nAre you sure? (y/n)? "
if [ "$REPLY" == "y" ]
then
# Remove remote fully merged branches
echo "$MERGED_BRANCHES" | xargs -I% git push origin :%
echo "${GREEN}Done${COLOR_OFF}\n"
else
echo "${RED}Cancelled${COLOR_OFF}\n"
fi
fi
# Lets move on with unmerged branches but with only old commits
echo "--------------------------------------------------------------------\n"
# get all remote branches and filter them by our needs
OLD_BRANCHES=$(getRemoteBranches | filterByDate | skipBranches)
if [ -z "$OLD_BRANCHES" ]
then
echo "There are no old unmerged branches.\n"
else
echo "The following remote branches are NOT merged into $BASEBRANCH but contain only commits older then $WEEKS weeks and will be removed:"
echo "$YELLOW$OLD_BRANCHES$COLOR_OFF"
echo
read -p "Are you sure? (y/n)? "
if [ "$REPLY" == "y" ]
then
# Remove remote fully merged branches
echo "$OLD_BRANCHES" | xargs -I% git push origin :%
echo "${GREEN}Done${COLOR_OFF}\n"
else
echo "${RED}Cancelled${COLOR_OFF}\n"
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment