-
-
Save jsanders/2968558 to your computer and use it in GitHub Desktop.
Find and remove old git branches
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/bash | |
| ############################################################################ | |
| progname="${0##*/}" | |
| version="1.0" | |
| author="Simon Chiang" | |
| usestr="usage: %s [-h] [ref]\n" | |
| optstr=" %s %s\n" | |
| repo=`pwd` | |
| remote="" | |
| while getopts "d:hr:" opt | |
| do | |
| case $opt in | |
| d ) repo="$OPTARG" ;; | |
| h ) printf "$usestr" "$progname" | |
| printf "$optstr" "-d" "path to repo" | |
| printf "$optstr" "-h" "prints this help" | |
| printf "$optstr" "-r" "operate on named remote" | |
| printf "\n" | |
| exit 0 ;; | |
| r ) remote="$OPTARG" ;; | |
| \? ) printf "$usestr" "$progname" | |
| exit 2 ;; | |
| esac | |
| done | |
| shift $(($OPTIND - 1)) | |
| ref="${1:-master}" | |
| ############################################################################ | |
| git="git --git-dir $repo/.git" | |
| git_branch="$git branch" | |
| branch_filter="." | |
| if ! [ x"$remote" = x ] | |
| then | |
| git_branch="$git_branch -r" | |
| branch_filter="$remote/" | |
| fi | |
| ############################################################################ | |
| set -e | |
| # list reverted commits | |
| $git log "$ref" | | |
| grep 'This reverts commit' | | |
| awk '{ print substr($4, 1, 7) }' | | |
| tr -d '.,' > reverts.tmp | |
| # list branched merged to ref | |
| $git_branch -v | tr -d '*' > branches.tmp | |
| $git_branch --merged "$ref" | tr -d '*' > merged.tmp | |
| # list sha+branches that have been merged, | |
| # minus any reverts and the ref commit itself | |
| join merged.tmp branches.tmp | | |
| awk '{ print $2 " " $1 }' | | |
| grep "$branch_filter" | | |
| grep -v -e "^$($git log --format='%h' -n 1 "$ref")" | | |
| join -v1 - reverts.tmp | |
| rm branches.tmp merged.tmp reverts.tmp | |
| # remove the branches | |
| # | awk '{print $2}' | | |
| # if [ x"$remote" = x ] | |
| # then | |
| # while read name | |
| # do $git branch -d "$name" | |
| # done | |
| # else | |
| # awk -F '/' '{print $2}' | | |
| # while read name | |
| # do $git push "$remote" ":$name" | |
| # done | |
| # | |
| # $git remote prune "$remote" | |
| # fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment