Skip to content

Instantly share code, notes, and snippets.

@kojinkai
Last active April 29, 2019 10:42
Show Gist options
  • Save kojinkai/eac5f692d9e0e659d8f53f7d06ba7c91 to your computer and use it in GitHub Desktop.
Save kojinkai/eac5f692d9e0e659d8f53f7d06ba7c91 to your computer and use it in GitHub Desktop.

New Branch

Rationale

A small command line tool for creating and naming new topic branches in a consistent way.

Installation

create a new-branch script in a folder somewhere in your path.

touch ~/path/to/your/folder/new-branch

Change the mode of the file to make it executable

chmod +x /path/to/your/folder/new-branch

in a new shell check everything is working. Running which new-branch should output the script location if the folder you created the script in is exported into your path.

→ which new-branch
/path/to/your/folder/new-branch

Requirements

The tool needs a default branch from which it can create the topic branch. This can be passed with the -b flag or exported as an environment variable into your shell. In this example our stable development branch is called develop so we export that to our shell session.

export DEFAULT_BRANCH=develop

The tool does not require but works best when a JIRA_PREFIX environment variable is also set. This will automatically embed the JIRA project into your branch name

export JIRA_PROJECT=ESPI

Usage

new-branch

or to pass a custom target branch

new-branch -b master

This will prompt you to enter an issue type:

~~~~~~~~~~~~~~~~~~~~~
 ISSUE - TYPES
~~~~~~~~~~~~~~~~~~~~~

1. feature
2. fix
3. chore
4. refactor
5. test
6. docs

Enter issueType [1 - 5]

then the JIRA issue number and also a short branch description.

After entering this information the tool will checkout the default branch you specify in your environment variable, pull the latest changes and create a branch based off the HEAD of your stable development branch. THe branch will now have all the information about the issue baked in to the name.

#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment