From 9a12054e0ebae51c193c609edabe929bcad95ce2 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 18 Apr 2016 21:18:35 -0700 Subject: [PATCH] Git helper shell scripts for MarlinFirmware --- Marlin/scripts/firstpush | 3 +++ Marlin/scripts/mfinfo | 40 ++++++++++++++++++++++++++++++ Marlin/scripts/mfnew | 23 +++++++++++++++++ Marlin/scripts/mfpr | 40 ++++++++++++++++++++++++++++++ Marlin/scripts/mfprune | 22 +++++++++++++++++ Marlin/scripts/mfrb | 21 ++++++++++++++++ Marlin/scripts/mfup | 53 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 202 insertions(+) create mode 100755 Marlin/scripts/firstpush create mode 100755 Marlin/scripts/mfinfo create mode 100755 Marlin/scripts/mfnew create mode 100755 Marlin/scripts/mfpr create mode 100755 Marlin/scripts/mfprune create mode 100755 Marlin/scripts/mfrb create mode 100755 Marlin/scripts/mfup diff --git a/Marlin/scripts/firstpush b/Marlin/scripts/firstpush new file mode 100755 index 000000000000..e8eef06da849 --- /dev/null +++ b/Marlin/scripts/firstpush @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +git push --set-upstream origin `git branch | grep \* | sed 's/\* //g'` \ No newline at end of file diff --git a/Marlin/scripts/mfinfo b/Marlin/scripts/mfinfo new file mode 100755 index 000000000000..533b30a56ae7 --- /dev/null +++ b/Marlin/scripts/mfinfo @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# +# mfinfo +# +# Get the following helpful git info about the working directory: +# +# - Remote (upstream) Org name (MarlinFirmware) +# - Remote (origin) Org name (your Github username) +# - Repo Name (Marlin or MarlinDev) +# - Marlin Target branch (RCBugFix or dev) +# - Branch Name (the current branch or the one that was passed) +# + +REPO=$(git remote get-url upstream 2>/dev/null | sed -E 's/.*\/(.*)\.git/\1/') + +if [[ -z $REPO ]]; then + echo "`basename $0`: No 'upstream' remote found." 1>&2 ; exit 1 +fi + +ORG=$(git remote get-url upstream 2>/dev/null | sed -E 's/.*[\/:](.*)\/.*$/\1/') + +if [[ $ORG != MarlinFirmware ]]; then + echo "`basename $0`: Not a Marlin repository." + exit 1 +fi + +case "$REPO" in + Marlin ) TARG=RCBugFix ;; + MarlinDev ) TARG=dev ;; +esac + +FORK=$(git remote get-url origin 2>/dev/null | sed -E 's/.*[\/:](.*)\/.*$/\1/') + +case "$#" in + 0 ) BRANCH=$(git branch 2>/dev/null | grep ^* | sed 's/\* //g') ;; + 1 ) BRANCH=$1 ;; + * ) echo "Usage: `basename $0` [branch]" 1>&2 ; exit 1 ;; +esac + +echo "$ORG $FORK $REPO $TARG $BRANCH" diff --git a/Marlin/scripts/mfnew b/Marlin/scripts/mfnew new file mode 100755 index 000000000000..0a07513bf971 --- /dev/null +++ b/Marlin/scripts/mfnew @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +# +# mfnew +# +# Create a new branch based on RCBugFix or dev a given branch 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") ;; + 1 ) BRANCH=$1 ;; + * ) echo "Usage: `basename $0` [branch]" 1>&2 ; exit 1 ;; +esac + +git checkout $TARG -b $BRANCH diff --git a/Marlin/scripts/mfpr b/Marlin/scripts/mfpr new file mode 100755 index 000000000000..8eff48483ac5 --- /dev/null +++ b/Marlin/scripts/mfpr @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# +# mfpr +# +# Make a PR of the current branch against RCBugFix or dev +# + +MFINFO=$(mfinfo "$@") || exit + +IFS=' ' read -a INFO <<< "$MFINFO" + +ORG=${INFO[0]} +FORK=${INFO[1]} +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 + echo "Can't make a PR from $BRANCH" ; exit +fi + +if [ -z "$(git branch -vv | grep ^\* | grep \\[origin)" ]; then firstpush; fi + +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 diff --git a/Marlin/scripts/mfprune b/Marlin/scripts/mfprune new file mode 100755 index 000000000000..48fa9fdb8095 --- /dev/null +++ b/Marlin/scripts/mfprune @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# +# mfprune +# +# Prune all your merged branches and any branches whose remotes are gone +# Great way to clean up your branches after messing around a lot +# + +echo "Pruning Merged Branches..." +git branch --merged | egrep -v "^\*|RC|RCBugFix|dev" | xargs -n 1 git branch -d +echo + +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..." +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; }' +echo diff --git a/Marlin/scripts/mfrb b/Marlin/scripts/mfrb new file mode 100755 index 000000000000..c1366f7f98df --- /dev/null +++ b/Marlin/scripts/mfrb @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# +# mfrb +# +# Do "git rebase -i" against the "target" branch (RCBugFix or dev) +# + +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 ;; +esac + +git rebase -i ${INFO[3]} diff --git a/Marlin/scripts/mfup b/Marlin/scripts/mfup new file mode 100755 index 000000000000..ff19e0e1c034 --- /dev/null +++ b/Marlin/scripts/mfup @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +# +# mfup +# +# Fetch and merge upstream changes, optionally with a branch +# + +MFINFO=$(mfinfo) || exit + +IFS=' ' read -a INFO <<< "$MFINFO" + +ORG=${INFO[0]} +FORK=${INFO[1]} +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 ;; + * ) echo "Usage: `basename $0` [branch]" 1>&2 ; exit 1 ;; +esac + +set -e + +echo "Fetching upstream ($ORG/$REPO)..." +git fetch upstream + +echo ; echo "Bringing $TARG up to date..." +git checkout -q $TARG || git branch checkout upstream/$TARG -b $TARG && git push --set-upstream origin $TARG +git merge upstream/$TARG +git push origin + +if [[ $BRANCH != $TARG ]]; then + echo ; echo "Rebasing $BRANCH on $TARG..." + if git checkout $BRANCH; then + echo + if git rebase $TARG; then + git push -f ; echo + [[ $BRANCH != $OLDBRANCH ]] && git checkout $OLDBRANCH + else + echo "Looks like merge conflicts. Stopping here." + fi + else + echo "No such branch!" ; echo + git checkout $OLDBRANCH + fi +fi