From 06f4776be48d5449d1cf541d51edbaeb4b3204ad Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Thu, 20 Apr 2017 21:10:41 -0500
Subject: [PATCH 1/4] mfprune: fetch remotes, offer checkout option

---
 buildroot/share/git/mfprune | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/buildroot/share/git/mfprune b/buildroot/share/git/mfprune
index 48fa9fdb80..cbf10b0c23 100755
--- a/buildroot/share/git/mfprune
+++ b/buildroot/share/git/mfprune
@@ -6,6 +6,11 @@
 # Great way to clean up your branches after messing around a lot
 #
 
+echo "Fetching latest upstream and origin..."
+git fetch upstream
+git fetch origin
+echo
+
 echo "Pruning Merged Branches..."
 git branch --merged | egrep -v "^\*|RC|RCBugFix|dev" | xargs -n 1 git branch -d
 echo
@@ -14,9 +19,9 @@ echo "Pruning Remotely-deleted Branches..."
 git branch -vv | egrep -v "^\*|RC|RCBugFix|dev" | grep ': gone]' | gawk '{print $1}' | xargs -n 1 git branch -D
 echo
 
-echo "You may want to remove these remote tracking references..."
+echo "You may want to remove (or checkout) these refs..."
 comm -23 \
   <(git branch --all | sed 's/^[\* ] //' | grep origin/ | grep -v "\->" | awk '{ print $1; }' | sed 's/remotes\/origin\///') \
   <(git branch --all | sed 's/^[\* ] //' | grep -v remotes/ | awk '{ print $1; }') \
-  | awk '{ print "git branch -d -r origin/" $1; }'
+  | awk '{ print "git branch -d -r origin/" $1; print "git checkout origin/" $1 " -b " $1; }'
 echo

From 7b4c245de3834ade935f5556e6f894b92a226dd5 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Thu, 20 Apr 2017 21:24:43 -0500
Subject: [PATCH 2/4] Improve existing git helper scripts

---
 buildroot/share/git/mfinfo |  4 ++++
 buildroot/share/git/mfinit | 12 ++++++++++--
 buildroot/share/git/mfnew  |  9 ++-------
 buildroot/share/git/mfpr   |  5 -----
 buildroot/share/git/mfrb   |  5 -----
 buildroot/share/git/mfup   |  5 -----
 6 files changed, 16 insertions(+), 24 deletions(-)

diff --git a/buildroot/share/git/mfinfo b/buildroot/share/git/mfinfo
index 533b30a56a..84b067caeb 100755
--- a/buildroot/share/git/mfinfo
+++ b/buildroot/share/git/mfinfo
@@ -37,4 +37,8 @@ case "$#" in
   * ) echo "Usage: `basename $0` [branch]" 1>&2 ; exit 1 ;;
 esac
 
+if [[ $BRANCH == "(no" ]]; then
+  echo "Git is busy with merge, rebase, etc." 1>&2 ; exit 1
+fi
+
 echo "$ORG $FORK $REPO $TARG $BRANCH"
diff --git a/buildroot/share/git/mfinit b/buildroot/share/git/mfinit
index 5e2cbc0d88..2a37914601 100755
--- a/buildroot/share/git/mfinit
+++ b/buildroot/share/git/mfinit
@@ -2,7 +2,15 @@
 #
 # mfinit
 #
-# Create the upstream repository for Marlin
+# Create the upstream remote for a forked repository
 #
 
-git remote add upstream git@github.com:MarlinFirmware/Marlin.git
+REPO=$(git remote get-url origin 2>/dev/null | sed -E 's/.*\/(.*)\.git/\1/')
+
+if [[ -z $REPO ]]; then
+  echo "`basename $0`: No 'origin' remote found." 1>&2 ; exit 1
+fi
+
+git remote add upstream "git@github.com:MarlinFirmware/$REPO.git"
+
+git fetch upstream
\ No newline at end of file
diff --git a/buildroot/share/git/mfnew b/buildroot/share/git/mfnew
index 0a07513bf9..7f3d7876c0 100755
--- a/buildroot/share/git/mfnew
+++ b/buildroot/share/git/mfnew
@@ -2,20 +2,15 @@
 #
 # mfnew
 #
-# Create a new branch based on RCBugFix or dev a given branch name
+# Create a new branch from the default target with the given name
 #
 
 MFINFO=$(mfinfo) || exit
 IFS=' ' read -a INFO <<< "$MFINFO"
 TARG=${INFO[3]}
 
-if [[ ${INFO[4]} == "(no" ]]; then
-  echo "Branch is unavailable!"
-  exit 1
-fi
-
 case "$#" in
-  0 ) BRANCH=pr_for_$TARG-$(date +"%G-%d-%m|%H:%M:%S") ;;
+  0 ) BRANCH=pr_for_$TARG-$(date +"%G-%m-%d_%H.%M.%S") ;;
   1 ) BRANCH=$1 ;;
   * ) echo "Usage: `basename $0` [branch]" 1>&2 ; exit 1 ;;
 esac
diff --git a/buildroot/share/git/mfpr b/buildroot/share/git/mfpr
index 8eff48483a..eb2e102aed 100755
--- a/buildroot/share/git/mfpr
+++ b/buildroot/share/git/mfpr
@@ -15,11 +15,6 @@ REPO=${INFO[2]}
 TARG=${INFO[3]}
 BRANCH=${INFO[4]}
 
-if [[ $BRANCH == "(no" ]]; then
-  echo "Git is busy with merge, rebase, etc."
-  exit 1
-fi
-
 if [[ ! -z "$1" ]]; then { BRANCH=$1 ; git checkout $1 || exit 1; } fi
 
 if [[ $BRANCH == $TARG ]]; then
diff --git a/buildroot/share/git/mfrb b/buildroot/share/git/mfrb
index c1366f7f98..b0b7008680 100755
--- a/buildroot/share/git/mfrb
+++ b/buildroot/share/git/mfrb
@@ -8,11 +8,6 @@
 MFINFO=$(mfinfo) || exit
 IFS=' ' read -a INFO <<< "$MFINFO"
 
-if [[ ${INFO[4]} == "(no" ]]; then
-  echo "Branch is unavailable!"
-  exit 1
-fi
-
 case "$#" in
   0 ) ;;
   * ) echo "Usage: `basename $0`" 1>&2 ; exit 1 ;;
diff --git a/buildroot/share/git/mfup b/buildroot/share/git/mfup
index ff19e0e1c0..8d339c0685 100755
--- a/buildroot/share/git/mfup
+++ b/buildroot/share/git/mfup
@@ -15,11 +15,6 @@ REPO=${INFO[2]}
 TARG=${INFO[3]}
 OLDBRANCH=${INFO[4]}
 
-if [[ $OLDBRANCH == "(no" ]]; then
-  echo "Branch is unavailable!"
-  exit 1
-fi
-
 case "$#" in
   0 ) BRANCH=$OLDBRANCH ;;
   1 ) BRANCH=$1 ;;

From 78308b1c0b40dee326567a0c360b8fd429c7abc5 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Thu, 20 Apr 2017 21:27:32 -0500
Subject: [PATCH 3/4] New git helper scripts 'mfadd' and 'mfqp'
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- `mfadd` adds and fetches a user’s fork of Marlin
- `mfqp` invokes `git commit -m`, `mfrb`, `git push -f`
---
 buildroot/share/git/mfadd | 24 ++++++++++++++++++++++++
 buildroot/share/git/mfqp  | 19 +++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100755 buildroot/share/git/mfadd
 create mode 100755 buildroot/share/git/mfqp

diff --git a/buildroot/share/git/mfadd b/buildroot/share/git/mfadd
new file mode 100755
index 0000000000..6d69124498
--- /dev/null
+++ b/buildroot/share/git/mfadd
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+#
+# mfadd
+#
+# Add a remote and fetch it
+#
+
+MFINFO=$(mfinfo) || exit
+
+IFS=' ' read -a INFO <<< "$MFINFO"
+
+REPO=${INFO[2]}
+OLDBRANCH=${INFO[4]}
+
+case "$#" in
+  1 ) USER=$1 ;;
+  * ) echo "Usage: `basename $0` [user]" 1>&2 ; exit 1 ;;
+esac
+
+set -e
+
+echo "Adding and fetching $USER..."
+
+git remote add -f "$USER" "git@github.com:$USER/$REPO.git"
diff --git a/buildroot/share/git/mfqp b/buildroot/share/git/mfqp
new file mode 100755
index 0000000000..2420f48886
--- /dev/null
+++ b/buildroot/share/git/mfqp
@@ -0,0 +1,19 @@
+#!/usr/bin/env bash
+#
+# mfqp
+#
+# Add all changed files, commit as "patch", do `mfrb` and `git push -f`
+#
+
+MFINFO=$(mfinfo) || exit
+IFS=' ' read -a INFO <<< "$MFINFO"
+
+case "$#" in
+  0 ) ;;
+  * ) echo "Usage: `basename $0`" 1>&2 ; exit 1 ;;
+esac
+
+git add * .travis.yml
+git commit -m "patch"
+mfrb
+git push -f

From dd436ceeb7dad20d278bffef336aa58d222d4586 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Thu, 20 Apr 2017 21:28:54 -0500
Subject: [PATCH 4/4] Add 'mfdoc' and 'mfpub' scripts to help with
 documentation

---
 buildroot/share/git/mfdoc  | 48 ++++++++++++++++++++
 buildroot/share/git/mfinfo |  5 ++-
 buildroot/share/git/mfpub  | 90 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 141 insertions(+), 2 deletions(-)
 create mode 100755 buildroot/share/git/mfdoc
 create mode 100755 buildroot/share/git/mfpub

diff --git a/buildroot/share/git/mfdoc b/buildroot/share/git/mfdoc
new file mode 100755
index 0000000000..89bc5a9f17
--- /dev/null
+++ b/buildroot/share/git/mfdoc
@@ -0,0 +1,48 @@
+#!/usr/bin/env bash
+#
+# mfdoc
+#
+# Start Jekyll in watch mode to work on Marlin Documentation and preview locally
+#
+
+MFINFO=$(mfinfo "$@") || exit
+IFS=' ' read -a INFO <<< "$MFINFO"
+ORG=${INFO[0]}
+REPO=${INFO[2]}
+BRANCH=${INFO[4]}
+
+if [[ $ORG != "MarlinFirmware" || $REPO != "MarlinDocumentation" ]]; then
+  echo "Wrong repository."
+  exit
+fi
+
+if [[ $BRANCH != "master" ]]; then
+  echo "Stashing changes and changing to master."
+  git stash
+  git checkout master
+fi
+
+opensite() {
+  TOOL=$(which gnome-open xdg-open open | awk '{ print $1 }')
+  URL="http://127.0.0.1:4000/"
+  if [ -z "$TOOL" ]; then
+    echo "Can't find a tool to open the URL:"
+    echo $URL
+  else
+    echo "Opening preview site in the browser..."
+    "$TOOL" "$URL"
+  fi
+}
+
+echo "Previewing MarlinDocumentation..."
+
+# wait to open the url for about 8s
+( sleep 8; opensite ) &
+
+bundle exec jekyll serve --watch --incremental
+
+if [[ $BRANCH != "master" ]]; then
+  echo "Restoring branch '$BRANCH'"
+  git checkout $BRANCH
+  git stash pop
+fi
diff --git a/buildroot/share/git/mfinfo b/buildroot/share/git/mfinfo
index 84b067caeb..5715317552 100755
--- a/buildroot/share/git/mfinfo
+++ b/buildroot/share/git/mfinfo
@@ -25,8 +25,9 @@ if [[ $ORG != MarlinFirmware ]]; then
 fi
 
 case "$REPO" in
-  Marlin    ) TARG=RCBugFix ;;
-  MarlinDev ) TARG=dev ;;
+  Marlin              ) TARG=RCBugFix ;;
+  MarlinDev           ) TARG=dev ;;
+  MarlinDocumentation ) TARG=master ;;
 esac
 
 FORK=$(git remote get-url origin 2>/dev/null | sed -E 's/.*[\/:](.*)\/.*$/\1/')
diff --git a/buildroot/share/git/mfpub b/buildroot/share/git/mfpub
new file mode 100755
index 0000000000..d883561bd2
--- /dev/null
+++ b/buildroot/share/git/mfpub
@@ -0,0 +1,90 @@
+#!/usr/bin/env bash
+#
+# mfpub
+#
+# Use Jekyll to publish Marlin Documentation to the HTML site
+#
+
+MFINFO=$(mfinfo "$@") || exit
+IFS=' ' read -a INFO <<< "$MFINFO"
+ORG=${INFO[0]}
+FORK=${INFO[1]}
+REPO=${INFO[2]}
+TARG=${INFO[3]}
+BRANCH=${INFO[4]}
+
+if [[ $ORG != "MarlinFirmware" || $REPO != "MarlinDocumentation" ]]; then
+  echo "Wrong repository."
+  exit
+fi
+
+if [[ $BRANCH == "gh-pages" ]]; then
+  echo "Can't build from 'gh-pages.' Only the Jekyll branches."
+  bundle exec jekyll serve --watch
+  exit
+fi
+
+if [[ $BRANCH != "master" ]]; then
+  echo "Don't forget to update and push 'master'!"
+fi
+
+git checkout $BRANCH
+
+echo "Generating MarlinDocumentation..."
+
+# GOJF Card
+git stash
+
+TMPFOLDER=$( mktemp -d )
+COMMIT=$( git log --format="%H" -n 1 )
+
+# Clean out changes and other junk in the branch
+git reset --hard
+git clean -d -f
+
+# Push 'master' to the fork and make a proper PR...
+if [[ $BRANCH == "master" ]]; then
+
+  if [ -z "$(git branch -vv | grep ^\* | grep \\[origin)" ]; then firstpush; fi
+
+  git push -f origin
+
+  TOOL=$(which gnome-open xdg-open open | awk '{ print $1 }')
+  URL="https://github.com/$ORG/$REPO/compare/$TARG...$FORK:$BRANCH?expand=1"
+
+  if [ -z "$TOOL" ]; then
+    echo "Can't find a tool to open the URL:"
+    echo $URL
+  else
+    echo "Opening a New PR Form..."
+    "$TOOL" "$URL"
+  fi
+
+fi
+
+# Uncomment to compress the final html files
+# mv ./_plugins/jekyll-press.rb-disabled ./_plugins/jekyll-press.rb
+# bundle install
+
+bundle exec jekyll build --profile --trace --no-watch
+bundle exec htmlproofer ./_site --only-4xx --allow-hash-href --check-favicon --check-html --url-swap ".*marlinfw.org/:/"
+
+rsync -av _site/ ${TMPFOLDER}/
+
+# Clean out changes and other junk in the branch
+git reset --hard
+git clean -d -f
+
+# Sync built-site with gh-pages
+git checkout gh-pages
+rsync -av ${TMPFOLDER}/ ./
+
+# Commit and push the new live site directly
+git add --all
+git commit --message "Built from ${COMMIT}"
+git push upstream
+
+rm -rf ${TMPFOLDER}
+
+# Go back to the branch we started from
+git checkout $BRANCH