Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# Use an environment variable to know which branch should be used.
if [ -z "$D8_UPSTREAM_BRANCH" ]; then
echo "Missing the \$D8_UPSTREAM_BRANCH environment variable which must be set to something like '8.7.x'."
exit 1
fi
current_branch=$(git symbolic-ref --short HEAD)
branch=$D8_UPSTREAM_BRANCH
# Find the current and newest upstream hash.
old=$(git rev-parse --short "$branch")
git fetch -q
new=$(git rev-parse --short origin/"$branch")
# Exit if there have been no new commits.
if [ "$old" == "$new" ]; then
echo "No new commits."
exit 1
fi
# Find all branches that were up-to-date before and are now out-of-date.
list=$(git branch --contains "$old" --no-contains "$new" --format='%(refname:short)')
# Count the number of branches.
total=$(echo "$list" | wc -l | awk '{print $1}')
# If there are no out-of-date branches, exit.
if [ "$total" -eq 0 ]; then
echo "No branches need rebasing."
exit
fi
# Loop through all of the out-of-date branches.
commitnum=$(git rev-list --count "$old".."$new")
echo "Rebasing $total branches $commitnum commits from $old to $new"
count=0
rebase=()
for ref in $list; do
if ! (git checkout -q "$ref"); then
sudo git checkout -- sites/default >/dev/null 2>&1
fi
# Attempt to rebase the branch, adding it to the list and aborting the rebase
# if it fails.
if ! (git rebase -q origin/"$branch" >/dev/null 2>&1); then
rebase+=("$ref")
git rebase --abort
fi
# Print a progress bar.
count=$(( $count + 1 ))
printf "\r%3d%% done, %s failed" $(( $count * 100 / $total )) ${#rebase[@]}
done
printf "\n"
if [ ${#rebase[@]} -ne 0 ]; then
printf "\n"
echo "The following branches failed rebasing:"
printf "\n"
printf "%s\n" "${rebase[@]}"
git checkout -q "$current_branch"
else
# If all branches were successfully rebased, perform any custom rebasing.
#git checkout -q ____ && git rebase -q ____ && \
git checkout -q "$current_branch"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.