Last active
December 14, 2016 13:58
-
-
Save JeroenBoersma/7eb978f1fef8c7ec08773d7b0329df73 to your computer and use it in GitHub Desktop.
Delete merged branches with origing/master
This file contains 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 | |
# Configurable | |
remote='origin'; | |
master='master'; | |
days=0; | |
unmerged=0; | |
eval set -- `getopt -l master: -l remote: -l days: -l help -l unmerged -- 'm:?r:?d:?u?h?' "$@"`; | |
while [ "X$1" != "X--" ]; do | |
case "$1" in | |
-m|--master) | |
shift | |
master=$1; | |
;; | |
-r|--remote) | |
shift | |
remote=$1; | |
;; | |
-d|--days) | |
shift | |
days=$1; | |
;; | |
-u|--unmerged) | |
unmerged=1; | |
;; | |
-h|--help) | |
echo `basename $0`' [-m|--master=master] [-r|--remote=origin] [-d|--days=days] [-u|--unmerged] [-h|--help]'; | |
exit 0; | |
;; | |
esac | |
shift | |
done | |
shift; | |
# Needed | |
latest='dgaghdfjkyhier32583459354'; | |
prefix='73258654fdsfds'; | |
# Program | |
git fetch -p ${remote}; | |
git branch ${latest} ${remote}/${master} >/dev/null 2>&1; | |
now=`date +'%s'`; | |
git branch -a | | |
grep remotes/${remote} | | |
grep -vE '/'${master}'$' | | |
sed -e 's/remotes\///' | | |
while read b; do | |
c=`echo "$b" | sed -e 's/'${remote}'\///'`; | |
git branch ${prefix}/$c $b >/dev/null 2>&1; | |
d=`git branch --merged ${latest} | grep ${prefix}/$c`; | |
ts=`git show -q --pretty=%at ${prefix}/$c`; | |
ago=$(( (${now} - ${ts}) / (60 * 60 * 24) )); | |
if [ ${unmerged} -eq 0 ] && [ -n "$d" ]; then | |
if [ ${ago} -ge ${days} ]; then | |
echo "git push ${remote} :$c # $ago days ago"; | |
fi | |
elif [ ${unmerged} -eq 1 ] && [ -z "${d}" ]; then | |
if [ ${ago} -ge ${days} ]; then | |
echo "git checkout -tb $c ${remote}/$c # $ago days ago"; | |
fi | |
fi | |
git branch -d ${prefix}/$c > /dev/null 2>&1; | |
done; | |
git branch -d ${latest} >/dev/null 2>&1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment