Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Default is 6 months
  4. MONTHS=6
  5. # Default is not dry run
  6. DRY_RUN=
  7. # Default is to ask for confirmation
  8. ASK_CONFIRMATION=1
  9. # Default is to prune remotes
  10. PRUNE_REMOTES=1
  11.  
  12. usage()
  13. {
  14. echo "usage: $0 [[[-m months ] [-n]] [-h] [-y]]"
  15. }
  16.  
  17. while [ "$1" != "" ]; do
  18. case $1 in
  19. -m | --months ) shift
  20. MONTHS=$1
  21. ;;
  22. -n | --dry-run ) DRY_RUN=1
  23. ;;
  24. -y | --no-confirm ) ASK_CONFIRMATION=
  25. ;;
  26. -p | --no-prune ) PRUNE_REMOTES=
  27. ;;
  28. -h | --help ) usage
  29. exit
  30. ;;
  31. * ) usage
  32. exit 1
  33. esac
  34. shift
  35. done
  36.  
  37. if [[ "$PRUNE_REMOTES" = "1" ]]; then
  38. for remote in $(git remote); do
  39. echo "Pruning remote $remote references"
  40. git remote prune $remote
  41. done
  42. fi
  43. echo "Will delete branches older than $MONTHS months"
  44.  
  45. for branch in $(git branch -a | sed 's/^\s*//' | grep -v 'master$'); do
  46. if [[ "$(git log $branch --since "$MONTHS months ago" | wc -l)" -eq 0 ]]; then
  47. # Make sure to get only local branches
  48. local_branch_name=$(echo "$branch" | sed 's/remotes\/.*\///')
  49. if [[ "$DRY_RUN" = "1" ]]; then
  50. echo "DRY: Will delete $local_branch_name"
  51. else
  52. if [[ "$ASK_CONFIRMATION" = "1" ]]; then
  53. read -p "Will delete $local_branch_name. Are you sure? " -n 1 -r
  54. echo
  55. if [[ $REPLY =~ ^[Yy]$ ]]; then
  56. git branch -D "$local_branch_name"
  57. else
  58. echo "Did not delete $local_branch_name"
  59. fi
  60. else
  61. git branch -D "$local_branch_name"
  62. fi
  63. fi
  64. fi
  65. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement