-
-
Save ajduke/4201286 to your computer and use it in GitHub Desktop.
Very rough script that cleans up all old branches that are loosely hanging around after a while. Also updates the origin remote.
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
#!/bin/sh | |
fix_branch_output () { | |
# Strips first columns from the output, to remove whitespace and "current | |
# branch" marker from git-branch's output | |
cut -c 3- | |
} | |
local_branches () { | |
git branch | fix_branch_output | |
} | |
remote_branches () { | |
git branch -r | grep -v ' -> ' | fix_branch_output | |
} | |
all_branches () { | |
local_branches | |
remote_branches | |
} | |
filter_nontrunks () { | |
#grep -vEe '(^|/)(master|develop|trunk|production)$' | |
grep -vEe '^(origin/)?master$' | |
} | |
branches () { | |
all_branches | filter_nontrunks | |
} | |
to_sha () { | |
git show | awk "NR==1 {print \$2}" | |
} | |
startswith() { [ "$1" != "${1#$2}" ]; } | |
base_master=$(git merge-base "master" "origin/master") | |
for branch in `branches`; do | |
echo "$branch... \c" | |
#if is_ancestor "$branch" master; then | |
branch_sha=$(git rev-parse "$branch") | |
base=$(git merge-base "$branch" "$base_master") | |
if [ "$base" == "$branch_sha" -a "$base" != "$base_master" ]; then | |
echo "OK" | |
git branch -d "$branch" | |
if startswith "$branch" origin/; then | |
git push origin --delete "${branch#origin/}" | |
fi | |
else | |
echo "SKIP" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment