ghpc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env bash
  2. #
  3. # ghpc (GitHub Push Current)
  4. #
  5. # - Push current branch to its remote. Try the following until it works:
  6. # - Plain 'git push'
  7. # - 'git push -f'
  8. # - Try the 'git push' command from the 'git push' error message
  9. # - Try adding '-f' to that command
  10. #
  11. yay() { echo "SUCCESS" ; }
  12. boo() { echo "FAIL" ; }
  13. FORCE=$([[ "$1" == "--force" || "$1" == "-f" ]] && echo 1)
  14. if [[ ! $FORCE ]]; then
  15. echo -n "trying 'git push' ...... "
  16. git push >/dev/null 2>&1 && { yay ; exit ; }
  17. boo
  18. fi
  19. echo -n "trying 'git push -f' ... "
  20. # Get the error output from the failed push
  21. # and get the recommended 'git push' line
  22. git push -f 2>&1 | {
  23. CMD=""
  24. ltrim() {
  25. [[ "$1" =~ [^[:space:]].* ]]
  26. printf "%s" "$BASH_REMATCH"
  27. }
  28. while IFS= read -r line
  29. do
  30. #echo "$line"
  31. if [[ -z "$CMD" && $line =~ "git push" ]]; then
  32. CMD=$(ltrim "$line")
  33. fi
  34. done
  35. # if a command was found try it
  36. if [[ -n "$CMD" ]]; then
  37. boo
  38. if [[ ! $FORCE ]]; then
  39. echo -n "trying '$CMD' ...... "
  40. $CMD >/dev/null 2>&1 && { yay ; exit ; }
  41. boo
  42. fi
  43. fCMD=${CMD/ push / push -f }
  44. echo -n "trying '$fCMD' ... "
  45. $fCMD >/dev/null 2>&1 && { yay ; exit ; }
  46. boo
  47. exit 1
  48. else
  49. yay
  50. fi
  51. }
  52. [[ ${PIPESTATUS[1]} == 1 ]] && echo "Sorry! Failed to push current branch."