mfup 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env bash
  2. #
  3. # mfup
  4. #
  5. # - Fetch latest upstream and replace the PR Target branch with
  6. # - Rebase the (current or specified) branch on the PR Target
  7. # - Force-push the branch to 'origin'
  8. #
  9. [[ $# < 3 && $1 != "-h" && $1 != "--help" ]] || { echo "usage: `basename $0` [1|2] [branch]" 1>&2 ; exit 1; }
  10. MFINFO=$(mfinfo "$@") || exit 1
  11. IFS=' ' read -a INFO <<< "$MFINFO"
  12. ORG=${INFO[0]}
  13. FORK=${INFO[1]}
  14. REPO=${INFO[2]}
  15. TARG=${INFO[3]}
  16. BRANCH=${INFO[4]}
  17. CURR=${INFO[5]}
  18. set -e
  19. # Prevent accidental loss of current changes
  20. [[ $(git stash) != "No local "* ]] && HAS_STASH=1
  21. echo "Fetching upstream ($ORG/$REPO)..."
  22. git fetch upstream
  23. if [[ $BRANCH != $TARG ]]; then
  24. echo ; echo "Rebasing $BRANCH on $TARG..."
  25. if [[ $BRANCH == $CURR ]] || git checkout $BRANCH; then
  26. if git rebase upstream/$TARG; then
  27. git push -f
  28. else
  29. echo "Looks like merge conflicts. Stopping here."
  30. exit
  31. fi
  32. else
  33. echo "No such branch!"
  34. fi
  35. else
  36. git reset --hard upstream/$TARG
  37. fi
  38. echo
  39. [[ $BRANCH != $CURR ]] && git checkout $CURR
  40. [[ $HAS_STASH == 1 ]] && git stash pop