Created
September 3, 2015 10:36
-
-
Save shoaibi/c6878472a672c391d436 to your computer and use it in GitHub Desktop.
A simple script script to generate a git-flow like branch name and check it out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Change this to your name initials | |
| user_initials="ms" | |
| E_BADARGS=65 | |
| if [ $# -lt 1 -a $# -gt 3 ]; then | |
| >&2 echo "Usage: $0 jira_ticket_title [ticket_type_or_branch_prefix=feature] [checkout=1]" | |
| exit $E_BADARGS | |
| fi | |
| # Get the JIRA ticket title from clipboard and remove the first and last - | |
| branch_name=$(echo $1 | tr -c '[:alnum:]' '-' | tr -s '-' '-') | |
| branch_name=${branch_name#"-"} | |
| branch_name=${branch_name%"-"} | |
| # Ensure that the branch name has the name of relevant project in it | |
| if [ $(echo "${branch_name}" | grep -q "INTPASS"; echo $?) -ne 0 ]; then | |
| >&2 echo "Invalid branch name: ${branch_name}" | |
| exit $E_BADARGS | |
| fi | |
| ticket_type="$2" | |
| # Set default ticket type to feature | |
| if [ "x${ticket_type}" = "x" ]; then | |
| ticket_type="feature" | |
| fi | |
| # should we checkout the branch name after generating it? | |
| checkout="$3" | |
| if [ "x${checkout}" = "x" ]; then | |
| checkout=1 | |
| fi | |
| # generate qualified branch name. | |
| qualified_branch_name="${ticket_type}/${user_initials}/${branch_name}" | |
| if [ $checkout -eq 1 ]; then | |
| echo "Checking out branch: ${qualified_branch_name}" | |
| git checkout -b "${qualified_branch_name}" | |
| else | |
| echo "Branch name: ${qualified_branch_name}." | |
| echo "Not checking out" | |
| fi | |
| echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment