From 9bd230cf64fcd9166ef3a414f8713ff1af7b1cb5 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Sat, 4 Nov 2017 17:20:43 -0500
Subject: [PATCH] Update git helper scripts

---
 buildroot/share/git/mfinfo | 26 +++++++++++++++++++++-----
 buildroot/share/git/mfnew  | 22 ++++++++++++++++++----
 buildroot/share/git/mfqp   | 13 ++++++-------
 buildroot/share/git/mfrb   | 10 +++++-----
 buildroot/share/git/mfup   | 24 +++++++-----------------
 5 files changed, 57 insertions(+), 38 deletions(-)

diff --git a/buildroot/share/git/mfinfo b/buildroot/share/git/mfinfo
index febbcc3ecf6..51b1e869958 100755
--- a/buildroot/share/git/mfinfo
+++ b/buildroot/share/git/mfinfo
@@ -7,11 +7,15 @@
 #   - Remote (upstream) Org name (MarlinFirmware)
 #   - Remote (origin) Org name (your Github username)
 #   - Repo Name (Marlin, MarlinDev, MarlinDocumentation)
-#   - PR Target branch (bugfix-1.1.x, dev, or master)
+#   - PR Target branch (bugfix-1.1.x, bugfix-2.0.x, or master)
 #   - Branch Arg (the branch argument or current branch)
 #   - Current Branch
 #
 
+usage() {
+  echo "Usage: `basename $0` [1|2] [branch]" 1>&2
+}
+
 CURR=$(git branch 2>/dev/null | grep ^* | sed 's/\* //g')
 [[ -z $CURR ]] && { echo "No git repository here!" 1>&2 ; exit 1; }
 [[ $CURR == "(no"* ]] && { echo "Git is busy with merge, rebase, etc." 1>&2 ; exit 1; }
@@ -23,17 +27,29 @@ ORG=$(git remote get-url upstream 2>/dev/null | sed -E 's/.*[\/:](.*)\/.*$/\1/')
 [[ $ORG == MarlinFirmware ]] || { echo "`basename $0`: Not a Marlin repository." 1>&2 ; exit 1; }
 
 case "$REPO" in
-  Marlin              ) TARG=bugfix-1.1.x ;;
-  MarlinDev           ) TARG=dev ;;
+  Marlin              ) TARG=bugfix-1.1.x ;
+                        [[ $# > 0 ]] && [[ $1 == 2 ]] && TARG=bugfix-2.0.x
+                        ;;
   MarlinDocumentation ) TARG=master ;;
 esac
 
 FORK=$(git remote get-url origin 2>/dev/null | sed -E 's/.*[\/:](.*)\/.*$/\1/')
 
+# BRANCH can be given as the last argument
 case "$#" in
   0 ) BRANCH=$CURR ;;
-  1 ) BRANCH=$1 ;;
-  * ) echo "Usage: `basename $0` [branch]" 1>&2 ; exit 1 ;;
+  1 )
+    case "$1" in
+      1|2) BRANCH=$CURR ;;
+      *) BRANCH=$1 ;;
+    esac
+    ;;
+  2 )
+    case "$1" in
+      1|2) BRANCH=$2 ;;
+      *) usage ; exit 1 ;;
+    esac
+    ;;
 esac
 
 echo "$ORG $FORK $REPO $TARG $BRANCH $CURR"
diff --git a/buildroot/share/git/mfnew b/buildroot/share/git/mfnew
index f1e495cbc48..622622734bf 100755
--- a/buildroot/share/git/mfnew
+++ b/buildroot/share/git/mfnew
@@ -5,15 +5,29 @@
 # Create a new branch from the default target with the given name
 #
 
-[[ $# < 2 ]] || { echo "Usage: `basename $0` [branch]" 1>&2 ; exit 1; }
+usage() {
+  echo "Usage: `basename $0` [1|2] [name]" 1>&2
+}
 
-MFINFO=$(mfinfo) || exit 1
+[[ $# < 3 ]] || { usage ; exit 1 ; }
+
+MFINFO=$(mfinfo "$@") || exit 1
 IFS=' ' read -a INFO <<< "$MFINFO"
 TARG=${INFO[3]}
+BRANCH=pr_for_$TARG-$(date +"%G-%m-%d_%H.%M.%S")
 
+# BRANCH can be given as the last argument
 case "$#" in
-  0 ) BRANCH=pr_for_$TARG-$(date +"%G-%m-%d_%H.%M.%S") ;;
-  1 ) BRANCH=$1 ;;
+  1 ) case "$1" in
+        1|2) ;;
+        *) BRANCH=$1 ;;
+      esac
+      ;;
+  2 ) case "$1" in
+        1|2) BRANCH=$2 ;;
+        *) usage ; exit 1 ;;
+      esac
+      ;;
 esac
 
 git fetch upstream
diff --git a/buildroot/share/git/mfqp b/buildroot/share/git/mfqp
index 97cac5dbef5..5a91a8af920 100755
--- a/buildroot/share/git/mfqp
+++ b/buildroot/share/git/mfqp
@@ -5,24 +5,23 @@
 # Add all changed files, commit as "patch", do `mfrb` and `git push -f`
 #
 
-[[ $# == 0 ]] || { echo "Usage: `basename $0`" 1>&2 ; exit 1; }
+[[ $# < 2 ]] || { echo "Usage: `basename $0` [1|2]" 1>&2 ; exit 1; }
 
-MFINFO=$(mfinfo) || exit 1
+MFINFO=$(mfinfo "$@") || exit 1
 IFS=' ' read -a INFO <<< "$MFINFO"
 REPO=${INFO[2]}
 TARG=${INFO[3]}
-BRANCH=${INFO[5]}
+CURR=${INFO[5]}
 
 git add .
 git commit -m "patch"
 
-if [[ $BRANCH == $TARG ]]; then
+if [[ $CURR == $TARG ]]; then
   if [[ $REPO == "MarlinDocumentation" ]]; then
-    git rebase -i HEAD~2
+    git rebase -i HEAD~2 && git push -f
   else
     echo "Don't alter the PR Target branch."; exit 1
   fi
 else
-  mfrb
-  git push -f
+  mfrb "$@" && git push -f
 fi
diff --git a/buildroot/share/git/mfrb b/buildroot/share/git/mfrb
index b376b407467..af4de26dc40 100755
--- a/buildroot/share/git/mfrb
+++ b/buildroot/share/git/mfrb
@@ -2,18 +2,18 @@
 #
 # mfrb
 #
-# Do "git rebase -i" against the "target" branch (RCBugFix or dev)
+# Do "git rebase -i" against the "target" branch (bugfix-1.1.x, bugfix-2.0.x, or master)
 #
 
-[[ $# == 0 ]] || { echo "Usage: `basename $0`" 1>&2 ; exit 1; }
+[[ $# < 2 ]] || { echo "Usage: `basename $0` [1|2]" 1>&2 ; exit 1; }
 
-MFINFO=$(mfinfo) || exit 1
+MFINFO=$(mfinfo "$@") || exit 1
 IFS=' ' read -a INFO <<< "$MFINFO"
 TARG=${INFO[3]}
-BRANCH=${INFO[5]}
+CURR=${INFO[5]}
 
 # If the branch isn't currently the PR target
-if [[ $TARG != $BRANCH ]]; then
+if [[ $TARG != $CURR ]]; then
   git fetch upstream
   git rebase upstream/$TARG && git rebase -i upstream/$TARG
 fi
diff --git a/buildroot/share/git/mfup b/buildroot/share/git/mfup
index df2da87b2cd..88b564f9fb5 100755
--- a/buildroot/share/git/mfup
+++ b/buildroot/share/git/mfup
@@ -5,10 +5,9 @@
 # - Fetch latest upstream and replace the PR Target branch with
 # - Rebase the (current or specified) branch on the PR Target
 # - Force-push the branch to 'origin'
-# -
 #
 
-[[ $# < 2 ]] || { echo "Usage: `basename $0` [branch]" 1>&2 ; exit 1; }
+[[ $# < 3 ]] || { echo "Usage: `basename $0` [1|2] [branch]" 1>&2 ; exit 1; }
 
 MFINFO=$(mfinfo "$@") || exit 1
 IFS=' ' read -a INFO <<< "$MFINFO"
@@ -17,7 +16,7 @@ FORK=${INFO[1]}
 REPO=${INFO[2]}
 TARG=${INFO[3]}
 BRANCH=${INFO[4]}
-OLDBRANCH=${INFO[5]}
+CURR=${INFO[5]}
 
 set -e
 
@@ -27,23 +26,14 @@ set -e
 echo "Fetching upstream ($ORG/$REPO)..."
 git fetch upstream
 
-echo ; echo "Bringing $TARG up to date..."
-if [[ ! $(git checkout -q $TARG) ]]; then
-  git reset --hard upstream/$TARG
-  git push -f origin
-else
-  git checkout upstream/$TARG -b $TARG
-  git push --set-upstream origin $TARG
-fi
-
 if [[ $BRANCH != $TARG ]]; then
   echo ; echo "Rebasing $BRANCH on $TARG..."
-  if git checkout $BRANCH; then
-    echo
-    if git rebase $TARG; then
+  if [[ $BRANCH == $CURR ]] || git checkout $BRANCH; then
+    if git rebase upstream/$TARG; then
       git push -f
     else
-      echo "Looks like merge conflicts. Stopping here." ; exit
+      echo "Looks like merge conflicts. Stopping here."
+      exit
     fi
   else
     echo "No such branch!"
@@ -51,6 +41,6 @@ if [[ $BRANCH != $TARG ]]; then
 fi
 
 echo
-[[ $BRANCH != $OLDBRANCH ]] && git checkout $OLDBRANCH
+[[ $BRANCH != $CURR ]] && git checkout $CURR
 
 [[ $HAS_STASH == 1 ]] && git stash pop