
Untitled
By: a guest on
Apr 18th, 2012 | syntax:
None | size: 1.39 KB | hits: 6 | expires: Never
#!/bin/bash
function echo_and_run() {
echo -ne "\033[1;32m"
echo -n "$@"
echo -e "\033[0m"
"$@"
}
BRANCH=$(git symbolic-ref HEAD)
RESULT=$?
if [[ "$RESULT" -ne 0 ]]; then
exit $RESULT
fi
if [[ "$BRANCH" =~ ([^/]+$) ]]; then
BRANCH=${BASH_REMATCH[1]}
else
echo Cannot find the current branch.
exit 128
fi
# Pull from origin
if git branch -r | grep -qE "^\\s*origin/$BRANCH($|\s)"; then
echo_and_run git pull --ff-only origin "$BRANCH"
RESULT=$?
if [[ $RESULT -ne 0 ]]; then
exit $RESULT
fi
else
echo "Remote branch 'origin/$BRANCH' does not exist."
echo "Run 'git push origin "$BRANCH"' to push to the origin."
exit 128
fi
# Check if the current workdir is clean with no additional commit
STATUS=$(git status)
PUSH_TO_ORIGIN=false
if [[ "$STATUS" =~ (working directory clean) &&
! "$STATUS" =~ (# Your branch is ([a-z]*) of [^ ]+ by ([0-9]*) commit) ]]; then
PUSH_TO_ORIGIN=true
fi
# Pull from upstream (if available)
if git remote | grep -qE '^upstream$'; then
echo_and_run git pull --ff-only upstream "$BRANCH"
RESULT=$?
if [[ $RESULT -ne 0 ]]; then
exit $RESULT
fi
# Push the commits from upstream if necessary
if $PUSH_TO_ORIGIN; then
STATUS=$(git status)
if [[ "$STATUS" =~ (working directory clean) &&
"$STATUS" =~ (# Your branch is ahead of [^ ]+ by ([0-9]*) commit) ]]; then
echo_and_run git push origin "$BRANCH"
fi
fi
fi