Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 18th, 2012  |  syntax: None  |  size: 1.39 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2. function echo_and_run() {
  3.   echo -ne "\033[1;32m"
  4.   echo -n  "$@"
  5.   echo -e  "\033[0m"
  6.   "$@"
  7. }
  8.  
  9. BRANCH=$(git symbolic-ref HEAD)
  10. RESULT=$?
  11. if [[ "$RESULT" -ne 0 ]]; then
  12.   exit $RESULT
  13. fi
  14. if [[ "$BRANCH" =~ ([^/]+$) ]]; then
  15.   BRANCH=${BASH_REMATCH[1]}
  16. else
  17.   echo Cannot find the current branch.
  18.   exit 128
  19. fi
  20.  
  21. # Pull from origin
  22. if git branch -r | grep -qE "^\\s*origin/$BRANCH($|\s)"; then
  23.   echo_and_run git pull --ff-only origin "$BRANCH"
  24.   RESULT=$?
  25.   if [[ $RESULT -ne 0 ]]; then
  26.     exit $RESULT
  27.   fi
  28. else
  29.   echo "Remote branch 'origin/$BRANCH' does not exist."
  30.   echo "Run 'git push origin "$BRANCH"' to push to the origin."
  31.   exit 128
  32. fi
  33.  
  34. # Check if the current workdir is clean with no additional commit
  35. STATUS=$(git status)
  36. PUSH_TO_ORIGIN=false
  37. if [[ "$STATUS" =~ (working directory clean) &&
  38.       ! "$STATUS" =~ (# Your branch is ([a-z]*) of [^ ]+ by ([0-9]*) commit) ]]; then
  39.   PUSH_TO_ORIGIN=true
  40. fi
  41.  
  42. # Pull from upstream (if available)
  43. if git remote | grep -qE '^upstream$'; then
  44.   echo_and_run git pull --ff-only upstream "$BRANCH"
  45.   RESULT=$?
  46.   if [[ $RESULT -ne 0 ]]; then
  47.     exit $RESULT
  48.   fi
  49.  
  50.   # Push the commits from upstream if necessary
  51.   if $PUSH_TO_ORIGIN; then
  52.     STATUS=$(git status)
  53.     if [[ "$STATUS" =~ (working directory clean) &&
  54.           "$STATUS" =~ (# Your branch is ahead of [^ ]+ by ([0-9]*) commit) ]]; then
  55.       echo_and_run git push origin "$BRANCH"
  56.     fi
  57.   fi
  58. fi