Skip to content

Instantly share code, notes, and snippets.

@hitsujiwool
Last active August 29, 2015 14:01
Show Gist options
  • Save hitsujiwool/c6fcf430708625734d02 to your computer and use it in GitHub Desktop.
Save hitsujiwool/c6fcf430708625734d02 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# NAME
# gitcmp
#
# SYNOPSIS
# gitcmp dir [branch-name]
#
# DESCRIPTION
# Check if the local head and the remote head refers to the same commit revision.
# It prints 0 when both are the same, otherwise print 1.
#
# EXIT STATUS
# 0 SUCCESS
# 1 FAIL
LOCAL_HEAD=
REMOTE_HEAD=
CURRENT_BRANCH=
TARGET_BRANCH=
if [ $# -lt 1 -o $# -gt 2 ]; then
echo 'Usage: gitcmp dir [branch-name]'
exit 1
fi
cd $1
if [ $? -gt 0 ]; then
exit 1
fi
git status > /dev/null
if [ $? -gt 0 ]; then
exit 1
fi
CURRENT_BRANCH=`git branch --no-color | grep '^\*'`
TARGET_BRANCH=${2:-master}
git checkout "$TARGET_BRANCH" > /dev/null 2>&1
if [ $? -gt 0 ]; then
echo Error: Cannot find branch $TARGET_BRANCH 1>&2
exit 1
fi
LOCAL_HEAD=`git log -1 master --format="%H"`
if [ ! $LOCAL_HEAD ]; then
echo Error: Cannot detect local HEAD. 1>&2
exit 1
fi
REMOTE_HEAD=`git ls-remote origin refs/heads/master | awk '{print $1}'`
if [ ! $REMOTE_HEAD ]; then
echo Error: Cannot detect remote HEAD. 1>&2
exit 1
fi
echo "Local - $LOCAL_HEAD" 1>&2
echo "Remote - $REMOTE_HEAD" 1>&2
if [ ! "$LOCAL_HEAD" = "$REMOTE_HEAD" ]; then
echo 1
else
echo 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment