Facebook
From Blush Bird, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 37
  1. #!/usr/bin/env zsh
  2.  
  3. function removeCurrentBranch {
  4.   sed -E '/\*/d'
  5. }
  6.  
  7. function removeMaster {
  8.   sed -E '/master/d'
  9. }
  10.  
  11. function leftTrim {
  12.   sed -E 's/\*?[[:space:]]+//'
  13. }
  14.  
  15. # Clean up all cleanly merged branches
  16. for branch in $(git branch | removeCurrentBranch | removeMaster | leftTrim); do
  17.     git rebase origin/master $branch &> /dev/null || git rebase --abort &> /dev/null
  18.  
  19.     if (git cherry origin/master $branch | grep '^+') {
  20.         echo $branch is not merged
  21.     } else {
  22.         echo Deleting branch: $branch
  23.     }
  24. done
  25.  
  26. git branch --merged | removeCurrentBranch | removeMaster | leftTrim | xargs -n 1 git branch -d
  27.  
  28. # Ask about which other branches to delete interactively
  29. file="/tmp/git-cleanup-branches-$(uuidgen)"
  30.  
  31. all_branches=($(git branch | removeCurrentBranch | leftTrim))
  32.  
  33. # write branches to file
  34. for branch in $all_branches; do
  35.   echo "keep $branch" >> $file
  36. done
  37.  
  38. # write instructions to file
  39. echo "
  40. # All of your branches are listed above
  41. # (except for the current branch, which you can't delete)
  42. # change keep to d to delete the branch
  43. # all other lines are ignored" >> $file
  44.  
  45. # prompt user to edit file
  46. vim "$file"
  47.  
  48. # check each line of the file
  49. cat $file | while read -r line; do
  50.   # if the line starts with "d "
  51.   if echo $line | grep --extended-regexp "^d " > /dev/null; then
  52.     # delete the branch
  53.     branch=$(echo $line | sed -E 's/^d //')
  54.     git branch -D $branch
  55.   fi
  56. done
  57.  
  58. # clean up
  59. rm $file
  60.