Last active
July 28, 2017 15:29
-
-
Save endavid/14a5cfc221a3ce067c4441f40480d64f to your computer and use it in GitHub Desktop.
Remove closed mercurial branches from git
This file contains hidden or 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
# pass --simulate to avoid actual deletion | |
# pass --donotprompt to delete without prompting | |
function rmbHgClosed() { | |
option=$1 | |
current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') | |
if [ "$current_branch" != "master" ]; then | |
echo "WARNING: You are on branch $current_branch, NOT master." | |
fi | |
echo "Fetching remote branches..." | |
remote_branches=$(git branch -r | grep -v '/master$' | grep -v "/$current_branch$") | |
prefix="origin/" | |
branches_ahead="" | |
closed_branches_more_than_one_ahead="" | |
count_removed=0 | |
echo "Closed branches with just 1 close commit ahead:" | |
while read -r branch; do | |
b=${branch#$prefix} | |
git rev-list --left-right ${origin/current_branch}...${branch} -- 2>/dev/null >/tmp/git_upstream_status_delta || continue | |
# https://stackoverflow.com/a/7774433 | |
left_ahead=$(grep -c '^<' /tmp/git_upstream_status_delta) | |
right_ahead=$(grep -c '^>' /tmp/git_upstream_status_delta) | |
last_commit=`git log -1 --oneline ${branch}` | |
info="$current_branch (ahead $left_ahead) | (behind $right_ahead) $branch -- $last_commit" | |
if [[ "$last_commit" =~ [Cc]lose(.*)branch$ ]]; then | |
if [[ $right_ahead == 1 ]]; then | |
echo $info | |
if [[ $option == "--simulate" ]]; then | |
let "count_removed += 1" | |
continue | |
fi | |
if [[ $option == "--donotprompt" ]]; then | |
git push origin ":$b" | |
let "count_removed += 1" | |
else | |
read -p "Remove $branch? (y/n): " -n 1 choice </dev/tty | |
echo | |
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then | |
git push origin ":$b" | |
let "count_removed += 1" | |
fi | |
fi | |
else | |
closed_branches_more_than_one_ahead+="$info\n" | |
fi | |
else | |
branches_ahead+="$info\n" | |
fi | |
done <<< "$remote_branches" | |
echo "Removed $count_removed branches" | |
echo "" | |
echo "Closed branches with more than one commit ahead: " | |
echo -e $closed_branches_more_than_one_ahead | |
echo "Branches ahead: " | |
echo -e $branches_ahead | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment