Created
December 22, 2020 03:02
-
-
Save Martin91/d77a738fc9fa4c95edcd26e429f539a6 to your computer and use it in GitHub Desktop.
git scan released branches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
specialBranches=("master" "uat" "test" "release") | |
echo "\u001b[31mScanning released local branches...\n--------------------------------------------------------\u001b[0m" | |
releasedLocalBranches=() | |
for branch in $(git branch) | |
do | |
git show-ref -s --heads $branch | xargs git branch --contains | grep master > /dev/null | |
if [[ $? -eq 0 && ! " ${specialBranches[@]} " =~ " ${branch} " ]]; then | |
echo $branch | |
releasedLocalBranches+=($branch) | |
fi | |
done | |
echo "Need to delete these branches? [Y/N]: " | |
read choice | |
if [ "$choice" = "Y" ]; then | |
currentBranch=$(git branch --show-current) | |
if [ ! $currentBranch = "master" ]; then | |
echo "\u001b[31mERR: To delete local branches, you should checkout to master branch firstly, instead of $currentBranch!\u001b[0m" | |
exit 255 | |
fi | |
echo "\u001b[31mATTENTION: Deleting local branches...\n--------------------------------------------------------\u001b[0m" | |
for branch in "${releasedLocalBranches[@]}" | |
do | |
git branch -d $branch | |
done | |
fi | |
# Delete remote branches | |
echo "Need to scan remote branches? [Y/N]: " | |
read choice | |
if [ "$choice" != "Y" ]; then | |
echo "Goodbye~" | |
exit 0 | |
fi | |
echo "\u001b[31mScanning released remote branches...\n--------------------------------------------------------\u001b[0m" | |
releasedRemoteBranches=() | |
for ref refName in $(git ls-remote --heads) | |
do | |
refName="$(echo $refName | sed 's/refs\/heads\///g')" | |
git branch -r --contains $ref | grep master > /dev/null | |
if [[ $? -eq 0 && ! " ${specialBranches[@]} " =~ " ${branch} " ]]; then | |
echo $refName | |
releasedRemoteBranches+=($refName) | |
fi | |
done | |
# Delete remote branches | |
echo "Need to delete these remote branches? [Y/N]: " | |
read choice | |
if [ "$choice" = "Y" ]; then | |
for branch in "${releasedRemoteBranches[@]}" | |
do | |
echo "git push origin :$branch" | |
# git push origin ":$branch" | |
done | |
fi | |
echo "\u001b[31mGoodBye~\u001b[0m" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment