Skip to content

Instantly share code, notes, and snippets.

@stratedge
Last active July 10, 2017 23:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stratedge/e832e4659275e559ba77e8a636119d61 to your computer and use it in GitHub Desktop.
Save stratedge/e832e4659275e559ba77e8a636119d61 to your computer and use it in GitHub Desktop.
Squash commits on current branch that are different from the develop branch and rebase to the develop branch
#!/bin/sh
# Colors
C_RESET="\033[0m"
C_ERROR="\033[0;97m\033[41m"
C_GOLD="\033[0;33m"
C_CYAN="\033[0;36m"
C_GREEN="\033[0;32m"
display_error () {
echo $C_ERROR$1$C_RESET
};
display_prompt () {
echo $C_GOLD$1" "$C_RESET
}
display_yn_prompt () {
echo $C_GOLD$1" [y/N] "$C_RESET
}
display_sys_message () {
echo $C_CYAN$1$C_RESET
}
blank_line () {
echo ""
}
check_for_git_repo () {
display_sys_message "Checking for git repo..."
INSIDE_REPO="$(git rev-parse --is-inside-work-tree 2>/dev/null)"
if [[ ! $INSIDE_REPO ]]; then
display_error "Not inside a git repo! Exiting."
exit 2
fi
display_sys_message "Git repo found."
}
retrieve_branch () {
display_sys_message "Retrieving branch name..."
BRANCH=$(git rev-parse --abbrev-ref HEAD)
display_sys_message "Branch name retrieved."
}
confirm_branch () {
read -p "$(display_yn_prompt "Is "$C_GREEN$BRANCH$C_GOLD" the correct branch?")" CONFIRM_BRANCH
if [[ ! $CONFIRM_BRANCH =~ ^[Yy]$ ]]; then
display_error "Please check out the correct branch first. Exiting."
exit 3
fi
}
checkout_develop () {
display_sys_message "Checking out the "$C_GREEN"develop"$C_CYAN" branch..."
git checkout develop
}
pull_develop () {
display_sys_message "Pulling latest remote version of the "$C_GREEN"develop"$C_CYAN" branch..."
git pull
}
checkout_source () {
display_sys_message "Checking out the "$C_GREEN$BRANCH$C_CYAN" branch..."
git checkout $BRANCH
}
resolve_hash () {
display_sys_message "Determining the hash of first commit on the branch (the final commit that will be squashed)..."
HASH=$(git rev-list develop..$BRANCH | tail -n 1)
HASH=${HASH:0:8}
display_sys_message "Found commit "$C_GREEN$HASH$C_CYAN" as the first commit on the branch."
}
confirm_hash () {
read -p "$(display_yn_prompt "Is "$C_GREEN$HASH$C_GOLD" the hash of the first commit on this branch?")" CONFIRM_HASH
if [[ ! $CONFIRM_HASH =~ ^[Yy]$ ]]; then
display_error "Wrong hash found. Exiting."
exit 6
fi
}
resolve_num () {
display_sys_message "Determining the number of commits to squash..."
NUM=$(git rev-list --count develop..$BRANCH)
display_sys_message $C_GREEN$NUM$C_CYAN" commit(s) found to squash."
}
confirm_soft_reset () {
read -p "$(display_yn_prompt "Conduct soft reset to squash "$C_GREEN$NUM$C_GOLD" commit(s)?")" CONFIRM_RESET
if [[ ! $CONFIRM_RESET =~ ^[Yy]$ ]]; then
display_error "Reset aborted. Exiting."
exit 4
fi
}
perform_soft_reset () {
display_sys_message "Performing soft reset of "$C_GREEN$NUM$C_CYAN" commit(s)..."
git reset --soft HEAD~$NUM
display_sys_message "Soft reset of "$C_GREEN$NUM$C_CYAN" commit(s) completed."
}
confirm_squash () {
read -p "$(display_yn_prompt "Complete squashing of "$C_GREEN$NUM$C_GOLD" commit(s) (all future steps are destructive and cannot be undone)?")" CONFIRM_SQUASH
if [[ ! $CONFIRM_SQUASH =~ ^[Yy]$ ]]; then
display_sys_message "Reverting soft reset of "$C_GREEN$NUM$C_CYAN" commit(s)..."
git reset HEAD@{1}
display_sys_message "Revert completed."
display_error "Squash aborted. Exiting."
exit 5
fi
}
craft_commit_message () {
read -p "$(display_prompt "What is your commit message?")" MESSAGE
read -p "$(display_prompt "What is the associated JIRA ticket(s), if any?")" JIRA
display_sys_message "Compiling commit message..."
GITLOG=$(git log --format=%B --reverse HEAD..HEAD@{1})
if [[ JIRA ]]; then
MESSAGE=$MESSAGE$'\n\n'"JIRA TICKET(S)"$'\n'"--------------"
fi
MESSAGE=$MESSAGE$'\n\n'"PREVIOUS COMMIT MESSAGES"$'\n'"------------------------"$'\n\n'$GITLOG
display_sys_message "Commit message compiled."
}
commit_changes () {
display_sys_message "Committing squash commit..."
git commit -m"$MESSAGE"
display_sys_message "Squash commit committed."
}
perform_rebase () {
display_sys_message "Performing rebase..."
git rebase develop
display_sys_message "Rebase complete..."
}
# ENSURE WITHIN GIT DIRECTORY
check_for_git_repo
blank_line
# RETRIEVE CURRENT BRANCH
retrieve_branch
blank_line
# CONFIRM CORRECT BRANCH
confirm_branch
blank_line
# CHECK OUT DEVELOP
checkout_develop
blank_line
# PULL LATEST DEVELOP FROM ORIGIN
pull_develop
blank_line
# CHECK OUT SOURCE
checkout_source
blank_line
# GET THE HASH OF THE FINAL COMMIT TO SQUASH
resolve_hash
blank_line
# CONFIRM THE HASH IS CORRECT
confirm_hash
blank_line
# GET NUMBER OF COMMITS TO SQUASH
resolve_num
blank_line
# CONFIRM SOFT RESET
confirm_soft_reset
blank_line
# PERFORM SOFT RESET
perform_soft_reset
blank_line
# CONFIRM SQUASH
confirm_squash
blank_line
# CRAFT COMMIT MESSAGE
craft_commit_message
blank_line
# COMMIT CHANGES
commit_changes
blank_line
# PERFORM REBASE
perform_rebase
blank_line
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment