Skip to content

Instantly share code, notes, and snippets.

@bitmvr
Last active April 13, 2018 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bitmvr/efdb85dd453c1d948b6a5523b597cc15 to your computer and use it in GitHub Desktop.
Save bitmvr/efdb85dd453c1d948b6a5523b597cc15 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Ensure you install somewhere on your path.
# I prefer creating a symlink to /usr/local/bin/branch_blaster
manifestPath="/tmp/branches-in-master.txt"
branchBlasterLog="/tmp/branch-blaster.log"
initLog() {
touch "${branchBlasterLog}"
}
timestamp() {
date +"%y%m%dT%H%M%S"
}
getMergedBranchesFromMaster() {
git branch --merged master | grep -vE "dev|master" | awk '{print $1}'
}
generateBranchManifest() {
$1 > "${manifestPath}"
}
branchCount() {
wc -l "${manifestPath}" | awk '{print $1}'
}
deleteLocalBranches() {
while read -r branch; do
git branch -D "${branch}" > /dev/null
echo "$(timestamp) - Removed Branch: ${branch}" | tee -a "${branchBlasterLog}"
done < "${manifestPath}"
}
main() {
initLog
echo "$(timestamp) - Run Started" | tee -a "${branchBlasterLog}"
echo "$(timestamp) - Identifying branches merged into master..."
generateBranchManifest getMergedBranchesFromMaster
echo "$(timestamp) - Found $(branchCount) branch(es)" | tee -a "${branchBlasterLog}"
echo -e "\r\n$(timestamp) - Manifest can be viewed here: ${manifestPath}"
echo -e "\r\n$(timestamp) - Would you like to delete these branches?\r\n"
echo "Press [ENTER] to PROCEED"
echo "Press [CTRL+C] to EXIT"
read -r
echo "$(timestamp) - Cleanup Started" | tee -a "${branchBlasterLog}"
deleteLocalBranches
echo "$(timestamp) - Cleanup Finished" | tee -a "${branchBlasterLog}"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment