Created
September 29, 2017 05:08
-
-
Save anonymous/b5eafa7ed94350f2d74fbc4795d5b127 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
# Returns true if git needs to be updated; false otherwise | |
function update_git() { | |
pushd "$1" | |
git remote update | |
UPSTREAM=${1:-'@{u}'} | |
LOCAL=$(git rev-parse @) | |
REMOTE=$(git rev-parse "$UPSTREAM") | |
BASE=$(git merge-base @ "$UPSTREAM") | |
if [ "$LOCAL" = "$REMOTE" ]; then | |
echo "Up-to-date" | |
res=0 | |
elif [ "$LOCAL" = "$BASE" ]; then | |
echo "Need to pull" | |
res=1 | |
elif [ "$REMOTE" = "$BASE" ]; then | |
git reset --hard HEAD | |
git clean -f -d | |
res=1 | |
else | |
git reset --hard HEAD | |
git clean -f -d | |
res=1 | |
fi | |
popd | |
return "$res" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on: https://stackoverflow.com/a/3278427