Last active
November 3, 2020 20:26
-
-
Save jakub-g/63e9606137557dbdd299 to your computer and use it in GitHub Desktop.
git pre-push hook to check and print how many commits behind origin/master you are
This file contains 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/bash | |
git-log-flat-message-author () { | |
git log --format="%s %an" "$@" | |
} | |
git-log-flat-colored() { | |
git --no-pager log --format="%C(yellow)%h%Creset %C(cyan)%cd%Creset %s %Cgreen%an%Creset" --date=short "$@" | |
} | |
REFERENCE_BRANCH="origin/releases/master" | |
# check if "origin/releases/master" branch exists, if so, fetch from origin and check if we're behind it | |
git rev-parse --verify ${REFERENCE_BRANCH} > /dev/null 2>&1 | |
if [ "$?" == "0" ]; then | |
echo "[-] Fetching from origin..." | |
#git fetch origin | |
echo "[-] Checking if your branch is in sync with ${REFERENCE_BRANCH} (ignoring automated build commits)..." | |
NB_COMMITS_BEHIND=$(git rev-list --left-right --count ${REFERENCE_BRANCH}...@ | cut -f1) | |
# check how many of the commits we're behind are really important ones (not automated version bumps from build scripts) | |
NB_IMPORTANT_COMMITS_BEHIND=$(git-log-flat-message-author ${REFERENCE_BRANCH} | head -"${NB_COMMITS_BEHIND}" | grep -v "BUILD_BOT" | wc -l) | |
if [ "${NB_IMPORTANT_COMMITS_BEHIND}" -gt "0" ]; then | |
echo -e "[-] Your branch is ${NB_COMMITS_BEHIND} commits behind ${REFERENCE_BRANCH}, think about rebasing\n" | |
git-log-flat-colored ${REFERENCE_BRANCH} | head -"${NB_COMMITS_BEHIND}" | |
echo -e "\n[-] To rebase, run 'git rebase ${REFERENCE_BRANCH}' or 'remaster'" | |
else | |
echo -e "[-] Good job, your branch is in sync with ${REFERENCE_BRANCH}" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment