Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- while getopts "d" opt; do
- case $opt in
- d) dryRunOpt="--dry-run";;
- esac
- done
- # prune local "cache" of remote branches first:
- git fetch --prune origin
- # delete merged to HEAD branches:
- mergedBranches=$(git branch -r --merged origin/HEAD | grep -v -e "origin/master" -e "origin/develop" -e "->" | cut -d/ -f2-)
- if [ -n "${mergedBranches}" ]; then
- echo -e "\033[0;32mDeleting merged branches...\033[0m"
- echo git push $dryRunOpt --delete origin ${mergedBranches}
- fi
- # delete branches with last (cherry picked) commit older than 5 months:
- echo -e "\n\033[0;32mSearching for stale branches...\033[0m"
- unameOut="$(uname -s)"
- case "${unameOut}" in
- Linux*) machine=Linux;;
- Darwin*) machine=Mac;;
- *) machine="UNKNOWN:${unameOut}"
- esac
- if [ $machine == "Mac" ]; then
- staleTimestamp=$(date -v -6m +"%s")
- maybeStaleTimestamp=$(date -v -3m +"%s")
- else
- staleTimestamp=$(date -d "now - 6 months" +"%s")
- maybeStaleTimestamp=$(date -d "now - 3 months" +"%s")
- fi
- notMergedBranches=$(git branch -r --no-merged origin/HEAD | grep -v -e "origin/master" -e "origin/develop" -e "->")
- branchesToDelete=""
- branchesToReview=""
- for branch in ${notMergedBranches}; do
- lastCommitInfo=$(git cherry origin/HEAD ${branch} | grep -v "^-" | cut -d" " -f2 | xargs git show --format="%H|%ct|%cr|%an" --quiet | grep -v "^$(git rev-parse HEAD)" | tail -1)
- lastCommitTimestamp=$(echo "${lastCommitInfo}" | cut -d"|" -f2)
- if [ -z "${lastCommitTimestamp}" ] || [ ${lastCommitTimestamp} -lt ${staleTimestamp} ]; then
- branchesToDelete+=" ${branch#origin/}"
- elif [ ${lastCommitTimestamp} -lt ${maybeStaleTimestamp} ]; then
- branchesToReview+="${branch#origin/}|${lastCommitInfo}"$'\n'
- fi
- echo -n .
- done
- echo # for new line after dots
- if [ -n "${branchesToDelete}" ]; then
- echo -e "\n\033[0;32mDeleting stale branches...\033[0m"
- echo git push $dryRunOpt --delete origin ${branchesToDelete}
- else
- echo -e "\n\033[0;32mNo stale branches...\033[0m"
- fi
- if [ -n "${branchesToReview}" ]; then
- echo -e "\n\033[1;33mBranches to review (may be stale):\033[0m"
- echo "${branchesToReview}" | sort -t"|" -k5 | awk -F"|" 'NF {print $5 " changed branch \"" $1 "\" in project \"'${PWD##*/}'\" " $4}'
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement