Last active
March 21, 2017 22:02
-
-
Save Aleksandr-ru/64e7885841af93d057b56b68fcfb214a to your computer and use it in GitHub Desktop.
Clean old branches in a git repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Usage: | |
# ./clean-branches.sh /path/to/reponanme | |
if [ ! -d "$1" ]; then | |
echo "'$1' is not a directory!" | |
echo "Usage:" | |
echo "$0 /path/to/reponame" | |
exit 1 | |
fi | |
REPO_DIR=${1%/} | |
REPO_NAME=${REPO_DIR##*/} | |
LOG=/var/log/git/$REPO_NAME.log | |
DATE=`date +%Y-%m-%d.%H:%M:%S` | |
MERGED="90 days" | |
NOMERGED="180 days" | |
# check the master branch | |
cd $REPO_DIR | |
if [ `git rev-parse --abbrev-ref HEAD` != "master" ]; then | |
echo "Current branch is not 'master'" | |
exit 1; | |
fi; | |
# find and delete merged branches older than | |
for BRANCH in `git branch --merged | grep -v -e master -e develop`; do | |
NCOMMITS=`git log --oneline --since="$MERGED" -1 $BRANCH | wc -l` | |
#echo $BRANCH $MERGED $NCOMMITS | |
if [ $NCOMMITS == "0" ]; then | |
LASTCOMMIT=`git log --oneline -1 --format="%h by '%cn' %cr: %s" $BRANCH` | |
echo "$DATE merged branch '$BRANCH' with last commit $LASTCOMMIT" | |
# delete branch | |
git branch -d $BRANCH | |
fi; | |
done; | |
# find and delete not merged branches older than | |
for BRANCH in `git branch --no-merged | grep -v -e master -e develop`; do | |
NCOMMITS=`git log --oneline --since="$NOMERGED" -1 $BRANCH | wc -l` | |
#echo $BRANCH $MERGED $NCOMMITS | |
if [ $NCOMMITS == "0" ]; then | |
LASTCOMMIT=`git log --oneline -1 --format="%h by '%cn' %cr: %s" $BRANCH` | |
echo "$DATE not merged branch '$BRANCH' with last commit $LASTCOMMIT" | |
# delete branch | |
git branch -D $BRANCH | |
fi; | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment