Skip to content

Instantly share code, notes, and snippets.

@AlKach
Last active March 3, 2017 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlKach/138690778504646828f7e1ea406cec6d to your computer and use it in GitHub Desktop.
Save AlKach/138690778504646828f7e1ea406cec6d to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
GIT_OPTS=""
OUTPUT_FILTER="cat"
function usage() {
echo "NAME"
echo " git-task - Start new feature branch or merge it into development branch"
echo ""
echo "SYNOPSIS"
echo " git task"
echo " git task start <branch name>"
echo " git task publish"
echo " git task complete [feature branch [development branch]]"
echo ""
echo "OPTIONS"
echo " start create new feature branch and switch to it; all uncommitted changes will be moved to new branch"
echo " publish push current branch to default remote"
echo " complete merge specified feature branch into specified merge branch, push it to upstream, drop local"
echo " and remote feature branch; if no merge branch specified, default development branch is used;"
echo " if no feature branch specified, current branch is used"
}
REMOTE_CONFIG_NAME=git.task.default.remote
DEV_BRANCH_CINFIG_NAME=git.task.default.branch
DEFAULT_REMOTE_NAME=origin
DEFAULT_DEV_BRANCH_NAME=develop
function _init() {
if [[ $(git config --local --get $REMOTE_CONFIG_NAME) == "" ]]; then
echo -n "Specify default remote name for publishing changes [$DEFAULT_REMOTE_NAME]: "
read defaultRemote
if [[ $(echo $defaultRemote | xargs echo -n) == "" ]]; then
defaultRemote=$DEFAULT_REMOTE_NAME
fi
git config --local --add $REMOTE_CONFIG_NAME $defaultRemote
echo "Default remote set to $defaultRemote."
echo "You can change it using git config --local --add $REMOTE_CONFIG_NAME <default remote name>"
echo ""
fi
if [[ $(git config --local --get $DEV_BRANCH_CINFIG_NAME) == "" ]]; then
echo -n "Specify default development branch name name for merging changes [$DEFAULT_DEV_BRANCH_NAME]: "
read defaultBranch
if [[ $(echo $defaultBranch | xargs echo -n) == "" ]]; then
defaultBranch=$DEFAULT_DEV_BRANCH_NAME
fi
git config --local --add $DEV_BRANCH_CINFIG_NAME $defaultBranch
echo "Default development branch set to $defaultBranch."
echo "You can change it using git config --local --add $DEFAULT_DEV_BRANCH_NAME <default development branch name>"
echo ""
fi
}
function _stash() {
local changes=$(git status -s | wc -l)
if [[ changes -gt 0 ]]; then
git stash
fi
echo "$changes"
}
function _unstash() {
if [[ $1 != 0 ]]; then
git stash pop
fi
}
function _start() {
_init
if [[ $# == 1 ]]; then
changes=$(_stash)
git pull -r && git checkout -b $1
_unstash $changes
else
usage
fi
}
function _publish() {
_init
remoteToPush=$(git config --local --get $REMOTE_CONFIG_NAME)
changes=$(_stash)
git push -u $remoteToPush
_unstash $changes
}
function _complete() {
_init
currentBranch=$(git branch | grep \* | cut -d ' ' -f2)
case "$#" in
0)
mergeBranch=$currentBranch
devBranch=$(git config --local --get $DEV_BRANCH_CINFIG_NAME)
;;
1)
mergeBranch=$1
devBranch=$(git config --local --get $DEV_BRANCH_CINFIG_NAME)
;;
2)
mergeBranch=$1
devBranch=$2
;;
*)
usage
exit
;;
esac
changes=$(_stash)
remoteToPush=$(git config --local --get $REMOTE_CONFIG_NAME)
if [[ $(git branch --list $mergeBranch) == "" ]]; then
echo "Branch $mergeBranch does not exist"
return 1
fi
if [[ $(git branch --list $devBranch) == "" ]]; then
echo "Branch $devBranch does not exist"
return 1
fi
git checkout $1 \
&& git pull -r \
&& git checkout $devBranch \
&& git pull -r \
&& git merge $mergeBranch --no-edit \
&& git push -u $remoteToPush \
&& git branch -D $mergeBranch \
&& git push $remoteToPush :$mergeBranch
if [[ $(git branch --list $currentBranch) != "" ]]; then
git checkout $currentBranch
else
git checkout $devBranch
fi
_unstash $changes
}
case "$1" in
start)
_start $2
;;
publish)
_publish
;;
complete)
_complete $2 $3
;;
*)
usage
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment