Skip to content

Instantly share code, notes, and snippets.

@xphere
Created June 2, 2016 15:24
Show Gist options
  • Save xphere/9421060f5805574c554f401a788f76fe to your computer and use it in GitHub Desktop.
Save xphere/9421060f5805574c554f401a788f76fe to your computer and use it in GitHub Desktop.
#!/bin/bash
branchlist() {
git for-each-ref -s --format '%(refname:short)' refs/heads/
}
filter_stale() {
while read branch; do
(git rev-parse --verify --quiet "$branch"@{u} > /dev/null 2>&1 && git merge-base --is-ancestor "$branch"@{u} "$branch") || echo "$branch"
done
}
filter_unmerged() {
xargs git merge-base --independent ${1:-} | xargs git name-rev --name-only
}
filter_by_type() {
fgrep "$1"
}
TYPE="${1-feature}"
BRANCHES=$(branchlist | filter_by_type $TYPE | filter_unmerged | filter_stale)
if [ "$BRANCHES" == "" ]; then
echo No stale branches
exit 0
fi
CURRENT=$(git rev-parse --abbrev-ref HEAD)
echo Rebasing stale branches...
for BRANCH in $BRANCHES; do
echo -n Rebasing branch ""$BRANCH""..." "
git checkout "$BRANCH" > /dev/null 2>&1 && git pull --rebase --no-tags > /dev/null 2>&1
if [[ $? != 0 ]]; then
echo 'KO!'
exit 1
else
echo 'OK'
fi
done
git checkout $CURRENT > /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment