From 61b2a2c703b7c8d8f436977014a1be195851fcc1 Mon Sep 17 00:00:00 2001 From: Mark Pitman Date: Sun, 9 Jul 2017 01:57:46 -0700 Subject: [PATCH] Add ability to specify a remote Use -r to push the new branch to origin Use -r to push to Will not auto push to origin if you do not use the argument --- bin/git-bug | 61 +++++++++++++++++++++++++++++++++++------- bin/git-chore | 62 ++++++++++++++++++++++++++++++++++++------- bin/git-create-branch | 37 ++++++++++++++++++++++---- bin/git-feature | 34 +++++++++++++++++------- bin/git-refactor | 62 ++++++++++++++++++++++++++++++++++++------- 5 files changed, 211 insertions(+), 45 deletions(-) diff --git a/bin/git-bug b/bin/git-bug index 3a538dd..3f6ebb8 100755 --- a/bin/git-bug +++ b/bin/git-bug @@ -1,14 +1,55 @@ #!/usr/bin/env bash -if test "$1" = "finish"; then - test -z $2 && echo "bug required." 1>&2 && exit 1 - branch=bug/$2 - git merge --no-ff $branch && git delete-branch $branch +branch_prefix=bug +declare -a argv +while test $# != 0 +do + case $1 in + -a|--alias ) + if [[ -n $2 ]] + then + shift # shift -a|-alias + branch_prefix=$1 + else + argv+=($1) # treat tail '-a' as + fi + ;; + -r|--remote ) + if [[ -n $2 ]] + then + remote=$2 + shift + else + remote="origin" + fi + ;; + * ) + argv+=($1) + ;; + esac + shift +done + +concatargs(){ + str=$(IFS='-'; echo "$*") + branch="$branch_prefix"/$str +} + +if test "${argv[0]}" = "finish"; then + test -z "${argv[1]}" && echo "$branch_prefix" " required." 1>&2 && exit 1 + branch="$branch_prefix"/"${argv[1]}" + git merge --no-ff "$branch" && git delete-branch "$branch" else - test -z $1 && echo "bug required." 1>&2 && exit 1 - if test -n "$2"; then - echo "too many arguments; if not finishing a bug, only of new bug is expected" && exit 1 - fi - branch=bug/$1 - git create-branch $branch + test -z "${argv[0]}" && echo "$branch_prefix" " required." 1>&2 && exit 1 + if test -n "${argv[1]}"; then + concatargs "${argv[@]}" + else + branch="$branch_prefix"/"${argv[0]}" + fi + if [[ -n $remote ]] + then + git create-branch -r $remote $branch + else + git create-branch $branch + fi fi diff --git a/bin/git-chore b/bin/git-chore index 1050529..c4f5dc3 100755 --- a/bin/git-chore +++ b/bin/git-chore @@ -1,14 +1,56 @@ #!/usr/bin/env bash -if test "$1" = "finish"; then - test -z $2 && echo "chore required." 1>&2 && exit 1 - branch=chore/$2 - git merge --no-ff $branch && git delete-branch $branch +branch_prefix=chore +declare -a argv +while test $# != 0 +do + case $1 in + -a|--alias ) + if [[ -n $2 ]] + then + shift # shift -a|-alias + branch_prefix=$1 + else + argv+=($1) # treat tail '-a' as + fi + ;; + -r|--remote ) + if [[ -n $2 ]] + then + remote=$2 + shift + else + remote="origin" + fi + ;; + * ) + argv+=($1) + ;; + esac + shift +done + +concatargs(){ + str=$(IFS='-'; echo "$*") + branch="$branch_prefix"/$str +} + +if test "${argv[0]}" = "finish"; then + test -z "${argv[1]}" && echo "$branch_prefix" " required." 1>&2 && exit 1 + branch="$branch_prefix"/"${argv[1]}" + git merge --no-ff "$branch" && git delete-branch "$branch" else - test -z $1 && echo "chore required." 1>&2 && exit 1 - if test -n "$2"; then - echo "too many arguments; if not finishing a chore, only of new chore is expected" && exit 1 - fi - branch=chore/$1 - git create-branch $branch + test -z "${argv[0]}" && echo "$branch_prefix" " required." 1>&2 && exit 1 + if test -n "${argv[1]}"; then + concatargs "${argv[@]}" + else + branch="$branch_prefix"/"${argv[0]}" + fi + if [[ -n $remote ]] + then + git create-branch -r $remote $branch + else + git create-branch $branch + fi fi + diff --git a/bin/git-create-branch b/bin/git-create-branch index e0f9035..8e335b7 100755 --- a/bin/git-create-branch +++ b/bin/git-create-branch @@ -1,8 +1,35 @@ #!/usr/bin/env bash -branch=$1 -test -z $branch && echo "branch required." 1>&2 && exit 1 +for param in $* +do + case $param in + -r|--remote) + if [ $# -eq 2 ] + then + BRANCH=$2 + else + REMOTE=$2 + BRANCH=$3 + fi + ;; + *) + BRANCH=$param + esac +done + +test -z $BRANCH && echo "branch required." 1>&2 && exit 1 +if [[ -n $REMOTE ]] +then + git ls-remote --exit-code $REMOTE &>/dev/null + if [ $? = 0 ] + then + git push $REMOTE HEAD:refs/heads/$BRANCH + git fetch $REMOTE + git checkout --track -b $BRANCH $REMOTE/$BRANCH + else + git checkout -b $BRANCH + fi +else + git checkout -b $BRANCH +fi -git push origin HEAD:refs/heads/$branch -git fetch origin -git checkout --track -b $branch origin/$branch diff --git a/bin/git-feature b/bin/git-feature index 4ba4812..cc2ff20 100755 --- a/bin/git-feature +++ b/bin/git-feature @@ -14,6 +14,15 @@ do argv+=($1) # treat tail '-a' as fi ;; + -r|--remote ) + if [[ -n $2 ]] + then + remote=$2 + shift + else + remote="origin" + fi + ;; * ) argv+=($1) ;; @@ -27,15 +36,20 @@ concatargs(){ } if test "${argv[0]}" = "finish"; then - test -z "${argv[1]}" && echo "$branch_prefix" " required." 1>&2 && exit 1 - branch="$branch_prefix"/"${argv[1]}" - git merge --no-ff "$branch" && git delete-branch "$branch" + test -z "${argv[1]}" && echo "$branch_prefix" " required." 1>&2 && exit 1 + branch="$branch_prefix"/"${argv[1]}" + git merge --no-ff "$branch" && git delete-branch "$branch" else - test -z "${argv[0]}" && echo "$branch_prefix" " required." 1>&2 && exit 1 - if test -n "${argv[1]}"; then - concatargs "${argv[@]}" - else - branch="$branch_prefix"/"${argv[0]}" - fi - git create-branch $branch + test -z "${argv[0]}" && echo "$branch_prefix" " required." 1>&2 && exit 1 + if test -n "${argv[1]}"; then + concatargs "${argv[@]}" + else + branch="$branch_prefix"/"${argv[0]}" + fi + if [[ -n $remote ]] + then + git create-branch -r $remote $branch + else + git create-branch $branch + fi fi diff --git a/bin/git-refactor b/bin/git-refactor index 4327335..a5dfe77 100755 --- a/bin/git-refactor +++ b/bin/git-refactor @@ -1,14 +1,56 @@ #!/usr/bin/env bash -if test "$1" = "finish"; then - test -z $2 && echo "refactor required." 1>&2 && exit 1 - branch=refactor/$2 - git merge --no-ff $branch && git delete-branch $branch +branch_prefix=refactor +declare -a argv +while test $# != 0 +do + case $1 in + -a|--alias ) + if [[ -n $2 ]] + then + shift # shift -a|-alias + branch_prefix=$1 + else + argv+=($1) # treat tail '-a' as + fi + ;; + -r|--remote ) + if [[ -n $2 ]] + then + remote=$2 + shift + else + remote="origin" + fi + ;; + * ) + argv+=($1) + ;; + esac + shift +done + +concatargs(){ + str=$(IFS='-'; echo "$*") + branch="$branch_prefix"/$str +} + +if test "${argv[0]}" = "finish"; then + test -z "${argv[1]}" && echo "$branch_prefix" " required." 1>&2 && exit 1 + branch="$branch_prefix"/"${argv[1]}" + git merge --no-ff "$branch" && git delete-branch "$branch" else - test -z $1 && echo "refactor required." 1>&2 && exit 1 - if test -n "$2"; then - echo "too many arguments; if not finishing a refactor, only of new refactoring is expected" && exit 1 - fi - branch=refactor/$1 - git create-branch $branch + test -z "${argv[0]}" && echo "$branch_prefix" " required." 1>&2 && exit 1 + if test -n "${argv[1]}"; then + concatargs "${argv[@]}" + else + branch="$branch_prefix"/"${argv[0]}" + fi + if [[ -n $remote ]] + then + git create-branch -r $remote $branch + else + git create-branch $branch + fi fi +