Skip to content

Instantly share code, notes, and snippets.

@mofas
Created August 29, 2016 06:49
Show Gist options
  • Save mofas/3174ae1535e88c25e2c9b1e6a698f562 to your computer and use it in GitHub Desktop.
Save mofas/3174ae1535e88c25e2c9b1e6a698f562 to your computer and use it in GitHub Desktop.
checkgit.sh
#
# This script determines if current git state is the up to date master. If so
# it exits normally. If not it prompts for an explicit continue. This script
# intends to protect from versioning for NPM without first pushing changes
# and including any changes on master.
#
# First fetch to ensure git is up to date. Fail-fast if this fails.
git fetch;
if [[ $? -ne 0 ]]; then exit 1; fi;
# Extract useful information.
GITBRANCH=$(git branch -v 2> /dev/null | sed '/^[^*]/d');
GITBRANCHNAME=$(echo "$GITBRANCH" | sed 's/* \([A-Za-z0-9_\-]*\).*/\1/');
GITBRANCHSYNC=$(echo "$GITBRANCH" | sed 's/* [^[]*.\([^]]*\).*/\1/');
# Check if master is checked out
if [ "$GITBRANCHNAME" != "master" ]; then
read -p "Git not on master but $GITBRANCHNAME. Continue? (y|N) " yn;
if [ "$yn" != "y" ]; then exit 1; fi;
fi;
# Check if branch is synced with remote
if [ "$GITBRANCHSYNC" != "" ]; then
read -p "Git not up to date but $GITBRANCHSYNC. Continue? (y|N) " yn;
if [ "$yn" != "y" ]; then exit 1; fi;
fi;
@mofas
Copy link
Author

mofas commented Aug 29, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment