Guest User

Untitled

a guest
Jan 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #!/bin/bash
  2. output_type='text'
  3. days_back='60'
  4. branch_command=`git branch -r | grep -v HEAD`
  5. dryrun=false
  6. sanityCheck=false
  7. protected=("master" "develop" "origin/master" "origin/develop")
  8.  
  9. get_backlog_date() {
  10. local backlog_date=""
  11. if [[ "$(uname)" == "Darwin" ]]; then
  12. backlog_date=$(date -v-${days_back}d +%s)
  13. else
  14. backlog_date=$(date -d"now-${days_back}days" +%s)
  15. fi
  16. return $backlog_date
  17. }
  18.  
  19. containsElement () {
  20. local e
  21. for e in "${@:2}";
  22. do
  23. [[ "$e" == "$1" ]] && return 0;
  24. done
  25. return 1
  26. }
  27.  
  28. usage() {
  29. echo "$(basename "$0") [-h] [-o TEXT] [-m -nm -dryrun] [-d NUMBER]
  30.  
  31. where:
  32. -h|--help show this help text
  33. -o TEXT set the output (text or csv). Text is default, CSV will force dryrun.
  34. -m Only search for branches that have been merged and meet the days_back
  35. -nm Only search for branches that have not been merged and meet the days_back
  36. -d NUMBER Number of days to go back. Defaults to 60
  37. --dryrun Will only output the list of branches to be deleted
  38. "
  39. }
  40.  
  41. _setArgs() {
  42. while [ "$1" != "" ]; do
  43. case $1 in
  44. "-h" | "--help")
  45. usage
  46. exit
  47. ;;
  48. "-o" | "--output")
  49. shift
  50. if [ $1 != "csv" ] && [ $1 != "text" ]; then
  51. echo "Only allowed types are csv and text."
  52. exit 1
  53. fi
  54. output_type=$1
  55. dryrun=true
  56. ;;
  57. "-m" | "--merged")
  58. branch_command=`git branch -r --merged | grep -v HEAD`
  59. ;;
  60. "-nm" | "--not-merged")
  61. branch_command=`git branch -r --no-merged | grep -v HEAD`
  62. ;;
  63. "-d" | "--days")
  64. shift
  65. days_back=$1
  66. ;;
  67. "--dryrun")
  68. dryrun=true
  69. ;;
  70. esac
  71. shift
  72. done
  73. }
  74.  
  75. get_converted_date() {
  76. local converted_date=""
  77. if [[ "$(uname)" == "Darwin" ]]; then
  78. converted_date=$(date -j -f "%Y-%m-%d %T %z" "${last_touched}" +%s)
  79. else
  80. converted_date=$(date -d"${last_touched}" +%s)
  81. fi
  82. return $converted_date
  83. }
  84.  
  85. main() { if [[ $output_type == "csv" ]]; then
  86. echo "Branch, Last Commited To"
  87. fi
  88.  
  89. for branch in $branch_command;
  90. do
  91. last_touched=`git show --format="%ci" $branch | head -n 1`
  92. months_ago=`git show --format="%cr" $branch | head -n 1`
  93. coverted_date=$(get_converted_date)
  94.  
  95. # Stringify months ago for csv output
  96. if [[ $output_type == "csv" ]]; then
  97. months_ago="\"${months_ago}\""
  98. fi
  99.  
  100. # Rewrite branch name to drop origin
  101. branch="${branch/origin\/}"
  102.  
  103. # Check if the last commit is greater than the backlog date
  104. if [ $backlog_date -ge $converted_date ]; then
  105. if (containsElement "${branch}" "${protected[@]}") ; then
  106. echo "Skipping protected branch: ${branch}"
  107. continue
  108. fi
  109. if [[ $output_type == "csv" ]]; then
  110. echo "${branch},${months_ago}"
  111. else
  112. echo "${branch}"
  113. fi
  114. fi
  115.  
  116. if [ ! $dryrun ]; then
  117. branches_to_delete+=($branch)
  118. fi
  119. done
  120.  
  121. if [ ! $dryrun ] && [ sanityCheck ]; then
  122. delete_branches $branches_to_delete
  123. fi
  124. }
  125.  
  126. delete_branches() {
  127. local branches="$1"
  128. for branch in "${branches[@]}";
  129. do
  130. deleteBranch $branch
  131. done
  132. }
  133.  
  134. sanityCheck() {
  135. echo "Are you sure you want to delete all branches that have not had a commit
  136. in the last ${days_back} days? (This will also prune local branches) (y/N)"
  137.  
  138. read check_value
  139. return [[ "${check_value}" == "y" ]]
  140. }
  141.  
  142. deleteBranch() {
  143. local branch=$1
  144. echo "Deleting ${branch}"
  145. git push origin :$branch
  146. git fetch -p
  147. }
  148.  
  149. # Run the main function
  150. _setArgs "$@"
  151. backlog_date=$(get_backlog_date)
  152. main
Add Comment
Please, Sign In to add comment