Skip to content

Instantly share code, notes, and snippets.

@agazso
Last active April 25, 2018 11:40
Show Gist options
  • Select an option

  • Save agazso/6340078 to your computer and use it in GitHub Desktop.

Select an option

Save agazso/6340078 to your computer and use it in GitHub Desktop.
#!/bin/sh
COLOR="\033[0;32m"
RESET_COLOR="\033[0m"
output() {
echo "$COLOR$1$RESET_COLOR"
}
PUSH=1
BRANCH=$(git status | head -1 | sed -n 's/^On branch //p')
if [ "$BRANCH" = "" ]; then
BRANCH=master
fi
STATUS_TAIL=$(git status | tail -1)
if [ "$STATUS_TAIL" = "nothing added to commit but untracked files present (use \"git add\" to track)" ]; then
PUSH=0
fi
if [ "$PUSH" = "1" ]; then
output "-> Adding changes..."
git add -u -v
if [ $? -ne 0 ]; then exit 1; fi
output "-> Commiting files..."
COMMIT_OUTPUT=$(git commit --dry-run --porcelain)
COMMIT_STATUS=$?
if [ $COMMIT_STATUS -eq 0 ]; then
if [ "$1" = "" ]; then
git commit
else
git commit -m "$1"
fi
COMMIT_STATUS=$?
echo "After git commit: $COMMIT_STATUS"
if [ $COMMIT_STATUS -ne 0 ]; then
exit $COMMIT_STATUS
fi
else
if [ "$COMMIT_OUTPUT" != "" ]; then
exit $COMMIT_STATUS
fi
fi
fi
output "-> Pulling from origin/$BRANCH..."
git pull --rebase origin $BRANCH
HEAD=$(git rev-parse HEAD)
ORIGIN=$(git rev-parse origin/$BRANCH)
if [ "$HEAD" != "$ORIGIN" ]; then
PUSH=1
fi
STATUS=0
if [ "$PUSH" = "1" ]; then
output "-> Pushing to origin/$BRANCH..."
git push origin $BRANCH
STATUS=$?
fi
if [ $STATUS -eq 0 ]; then
COMMIT_ID=$(git rev-parse HEAD)
output "-> Commit hash: $RESET_COLOR$COMMIT_ID"
DESCRIBE=$(git describe 2> /dev/null)
if [ "$DESCRIBE" = "" ]; then
DESCRIBE=$(git describe --tags 2> /dev/null)
fi
output "$DESCRIBE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment