|
#!/usr/bin/env bash |
|
|
|
Black='\033[0;30m' # Black |
|
Red='\033[0;31m' # Red |
|
Green='\033[0;32m' # Green |
|
Yellow='\033[0;33m' # Yellow |
|
Blue='\033[0;34m' # Blue |
|
Purple='\033[0;35m' # Purple |
|
Cyan='\033[0;36m' # Cyan |
|
White='\033[0;37m' # White |
|
|
|
infoChar='\xF0\x9f\x92\xa1' |
|
errorChar='\xE2\x9b\x91' |
|
pointRight='\xE2\x98\x9B' |
|
pointRight='\xF0\x9F\xA6\x96' |
|
|
|
function newBranch { |
|
|
|
function checkBranchExists { |
|
# Bail out of the process if the branch is specified but does not exist |
|
echo "checking if $1 exists" |
|
if [ -z `git rev-parse --verify $1` ] |
|
then |
|
echo -e "\n" |
|
echo -e "${errorChar} ${Red} The branch $1 you attemped to branch from does not exist \n" |
|
echo -e "${White} Ensure that you have initialised a git repository or that the target branch you have defined exists" |
|
exit 1 |
|
fi |
|
} |
|
|
|
function setupRoutine { |
|
# Bail out of the process if there is no target branch set either in the environment or passed as an argument |
|
if [ -z ${targetBranch+x} ] && [ -z $DEFAULT_BRANCH ] |
|
then |
|
echo -e "\n" |
|
echo -e "${errorChar} ${Red} You must specify a target branch as the basis for your branch \n" |
|
echo -e "${White} you can either pass the branch name by using -b flag or export the DEFAULT_BRANCH variable to your shell" |
|
exit 1 |
|
fi |
|
|
|
|
|
# Log the settings |
|
if [ -z ${targetBranch+x} ] |
|
then |
|
checkBranchExists ${DEFAULT_BRANCH} |
|
echo -e "${White}${pointRight} ${Green}Branching from: $DEFAULT_BRANCH" |
|
else |
|
checkBranchExists ${targetBranch} |
|
echo -e "${White}${pointRight} ${Green}Branching from: $targetBranch" |
|
fi |
|
|
|
if [ -z $JIRA_PROJECT ] |
|
then |
|
echo -e "${White}${pointRight} ${Green}JIRA project code: not currently set" |
|
else |
|
echo -e "${White}${pointRight} ${Green}JIRA project code: $JIRA_PROJECT" |
|
fi |
|
|
|
# Log tips |
|
if [ -z $JIRA_PROJECT ] |
|
then |
|
echo -e "\n" |
|
echo -e "${infoChar} ${Yellow} This tool can automatically add the JIRA project code to your branches if you export a JIRA_PROJECT variable to your shell" |
|
JIRA_PROJECT='JIRA' |
|
fi |
|
|
|
} |
|
|
|
# function to display menus |
|
function selectIssueType { |
|
echo -e "${White}" |
|
echo "~~~~~~~~~~~~~~~~~~~~~" |
|
echo " ISSUE - TYPES" |
|
echo "~~~~~~~~~~~~~~~~~~~~~" |
|
echo -e "${Green}" |
|
echo "1. feature" |
|
echo "2. fix" |
|
echo "3. chore" |
|
echo "4. refactor" |
|
echo "5. test" |
|
echo "6. docs" |
|
echo -e "${White}" |
|
} |
|
|
|
function readIssueInput { |
|
local issueType |
|
read -p "Enter issueType [1 - 5] " issueType |
|
case $issueType in |
|
1) prefix=feature ;; |
|
2) prefix=fix ;; |
|
3) prefix=chore ;; |
|
4) prefix=refactor ;; |
|
5) prefix=test ;; |
|
6) prefix=docs ;; |
|
*) echo -e "${RED}Error...${STD}" && sleep 2 |
|
esac |
|
} |
|
|
|
# prompt for issue number |
|
function promptForIssueNumber { |
|
echo "now enter the JIRA issue: $JIRA_PREFIX-" |
|
read issueNumber |
|
jiraIssue="$JIRA_PREFIX-$issueNumber" |
|
} |
|
|
|
function promptForBranchDescription { |
|
echo -n "enter a terse branch description: " |
|
read branchDescription |
|
} |
|
|
|
function composeBranchMessage { |
|
# concatenate a branch name replacing any whitespace with a dash and piping to perl to convert to lowercase |
|
branchName=${prefix}/${jiraIssue}-$(echo "$branchDescription" | sed -E -e "s/[[:blank:]]/-/g" | perl -ne 'print lc') |
|
echo ${branchName} |
|
} |
|
|
|
function getbaseBranch { |
|
if [ -z ${targetBranch+x} ] |
|
then |
|
targetBranch=$DEFAULT_BRANCH |
|
fi |
|
} |
|
|
|
function checkoutNewBranch { |
|
echo -e "${infoChar} ${Green} Creating a new branch from ${targetBranch} with your options... ${White}" |
|
local currentBranch=$(git rev-parse --abbrev-ref HEAD) |
|
# if current branch is not target branch then checkout the target branch and pull it before branching |
|
if [ $currentBranch != $targetBranch ] |
|
then |
|
echo -e "${infoChar} ${Green} checking out $targetBranch... \n ${White}" |
|
git checkout ${targetBranch} |
|
fi |
|
|
|
echo -en "${infoChar} ${Green} getting the latest upstream revisions from $targetBranch... \n ${White}" |
|
git pull --ff |
|
git checkout -b ${branchName} |
|
} |
|
|
|
while [ -z ${prefix+x} ] |
|
do |
|
setupRoutine |
|
selectIssueType |
|
readIssueInput |
|
promptForIssueNumber |
|
promptForBranchDescription |
|
composeBranchMessage |
|
getbaseBranch |
|
checkoutNewBranch |
|
done |
|
|
|
} |
|
|
|
while getopts ":b:" opt; do |
|
case $opt in |
|
b) |
|
targetBranch=$OPTARG |
|
;; |
|
\?) |
|
echo "Invalid option: -$OPTARG" >&2 |
|
exit 1 |
|
;; |
|
:) |
|
echo "Option -$OPTARG requires an argument." >&2 |
|
exit 1 |
|
;; |
|
esac |
|
done |
|
|
|
newBranch |