Skip to content

Instantly share code, notes, and snippets.

@Iranon
Created July 14, 2022 08:40
Show Gist options
  • Save Iranon/a2168d2a50a3e09b43c5d10458c989a7 to your computer and use it in GitHub Desktop.
Save Iranon/a2168d2a50a3e09b43c5d10458c989a7 to your computer and use it in GitHub Desktop.
A script to ensure the rebase was done before the push.
#!/bin/bash
#Text Colors
GREEN='\033[0;32m';
BLUE='\033[0;34m';
RED='\033[0;31m';
PURPLE='\033[0;35m';
NOCOLOR='\033[0m';
wait_for_resolution() {
if [[ $1 == 1 || $# == 0 ]]; then
echo -e "\n${PURPLE}Waiting for conflicts resolution...${NOCOLOR}\n"
echo -n -e "Press enter to continue the rebase process or digit 'q' to abort:\n_> "
read -n 1 -r; echo
if [[ $REPLY == "" ]]; then
echo -e "\nContinuing...\n"
git rebase --continue
wait_for_resolution $?
elif [[ $REPLY =~ ^[Qq]$ ]]; then
echo -e "\n${RED}Aborting!${NOCOLOR}\n"
git rebase --abort
return 1
else
wait_for_resolution
fi
else
return 0
fi
}
main_branch="main"
current_branch=$(git branch --show-current)
# The first script argument is the main branch name
if [ $# -ne 0 ]; then
main_branch=$1
fi
echo -e "\nMain branch: ${GREEN}$main_branch${NOCOLOR}"
echo -e "\n${BLUE}Did you do rebase? (y/N)${NOCOLOR}"
read -p "_> " -n 1 -r; echo
if [[ $REPLY =~ ^[Yy]$ || $REPLY == YES || $REPLY == yes ]]; then
# Checking if rebase is needed
if [[ $(git show-ref --heads -s $main_branch) == $(git merge-base $main_branch $current_branch) ]]; then
echo -e "\nPushing...\n"
git push
else
echo -e "\n${BLUE}Rebase required: do you want to rebase? (y/N)${NOCOLOR}\n"
read -n 1 -r; echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "\n${PURPLE}Running 'git rebase $main_branch'${NOCOLOR}\n"
git rebase $main_branch
wait_for_resolution $?
if [[ $? == 0 ]]; then
echo -e "\nPushing...\n"
git push
fi
elif [[ $REPLY =~ ^[Nn]$ || $REPLY == "" ]]; then
echo -e "\nQuitting...\n"
exit 0
fi
fi
elif [[ $REPLY =~ ^[Nn]$ || $REPLY == "" ]]; then
echo -e "\nDo rebase first"
exit 0
else
echo -e "\n${RED}Invalid response${NOCOLOR}"
exit -1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment