Last active
August 29, 2015 14:03
-
-
Save phalkunz/643e7b6304963a9b6221 to your computer and use it in GitHub Desktop.
Script to check whether local branch and the origin's counterpart are in sync
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
### | |
# Returns a code that determine whether local branch and its origin's counterpart | |
# are in sync. In sync, in this case, means there's no commits in local branch | |
# that have not been pushed to the origin. In short, it means the following | |
# command returns nothing. | |
# | |
# git log {branch} --not origin/{branch} | |
# | |
# The code will be be in `$?` variable after the funciton call. | |
# The status code can be: | |
# - 0: local and origin are in sync | |
# - 1: the folder is not a git repository | |
# - 2: local and origin are not in sync | |
### | |
function getOriginSyncStatus() { | |
# This line is used just for checking whether | |
# the folder is a git repos or not. | |
# Output and error are muted. | |
git branch 1> /dev/null 2> /dev/null | |
# Check the status code of the line above | |
if [ $? -ne 0 ]; then | |
return 1 | |
else | |
BRANCH=`git branch 2> /dev/null | grep \* | awk '{print $2}'` | |
OUTPUT=`git log $BRANCH --not origin/$BRANCH` | |
if [ "$OUTPUT" == "" ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
fi | |
} | |
### | |
# Starting point | |
### | |
COLOR_RESET="\033[0m" | |
COLOR_ERROR="\033[31m" | |
COLOR_WARNING="\033[33m" | |
COLOR_SUCCESS="\033[32m" | |
# Empty newline | |
echo | |
# Get the function | |
# The return code will be stored in `$?` | |
getOriginSyncStatus | |
case $? in | |
0) | |
echo "${COLOR_SUCCESS}Yay! ${BRANCH} local and origin are in sync.${COLOR_RESET}";; | |
1) | |
echo "${COLOR_ERROR}This is not a git repository.${COLOR_RESET}";; | |
2) | |
echo "${COLOR_WARNING}Wait! ${BRANCH} local and origin are NOT in sync.${COLOR_RESET}";; | |
esac | |
# Empty newline | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment