Skip to content

Instantly share code, notes, and snippets.

@hectorcanto
Last active August 11, 2024 15:14
Show Gist options
  • Save hectorcanto/b7fef72c4d23c511b6f79e2c53ce7ed4 to your computer and use it in GitHub Desktop.
Save hectorcanto/b7fef72c4d23c511b6f79e2c53ce7ed4 to your computer and use it in GitHub Desktop.
Branch name generator
#!/usr/bin/env bash
# SYNTAX branchtype/[subproject/]description_with_underscore-#CODE-123
# Example feature/main_api/new_feature-#MAPI-123
# Separators: slash (/) dash (-), don't use dashes as spaces, please
# Why this syntax?
# ticket at the end to keep autocomplete working in terminal
# optional subproject for better CI pipeline decissions per subproject (multirepo, subapps, contexts ..)
# loosely based in gitflow
valid_branches="feature|feat|hotfix|fix|docs|deploy|ops|refactor"
BRANCHES=(${valid_branches//|/ })
echo "Press Enter for default options"
# Remember to change the default below
echo "project? Enter for default [PROJ]"
read -r project
echo "branch type? ${valid_branches} [feature]"
read -r branchtype
echo "subproject code? at|cw|kl|sn|auth0|... [] (empty)"
read -r sub
echo "ticket id? options #PROJ-123 proj123 (#- not needed)
read -r ticket
# TODO extra tickets or tickets separated per comma
echo "description with spaces?"
read -r desc
BRANCH_TYPE=$(echo "${branchtype:-feature}" | tr '[:upper:]' '[:lower:]')
echo "BRANCH TYPE: ${BRANCH_TYPE}"
# add heading slash if subj is set
SUBP=${sub:+/$sub}
echo "SUBPROJECT: ${SUBP:-none}"
# description: replace spaces and dashes
DESC=$(echo "${desc}" | tr ' ' '_' )
echo "DESCRIPTION: ${DESC}"
# project to uppercase and no spaces
PROJECT=$(echo "${project:-PROJ}" | tr '[:lower:]' '[:upper:]' | sed 's/ //g')
echo "PROJECT: #${PROJECT}"
# ticket remove insensitive #XD-
TICKET=$(echo "${ticket}" | tr '[:lower:]' '[:upper:]' | sed "s/^#//; s/$PROJECT//i; s/-//" )
echo "TICKET: ${PROJECT}-${TICKET}"
echo ""
BRANCH="${BRANCH_TYPE}${SUBP}/${DESC}-#${PROJECT}-${TICKET}"
[[ -z "$BRANCH_TYPE" ]] && { echo "ERROR: Branch type is empty" ; exit 1; }
printf '%s\0' "${BRANCHES[@]}" | grep -Fqxz -- "${BRANCH_TYPE}" || { echo "ERROR: Branch type '$BRANCH_TYPE' is not valid"; exit 1; }
[[ -z "$PROJECT" ]] && { echo "ERROR: Project is empty" ; exit 1; }
[[ -z "$TICKET" ]] && { echo "ERROR: Ticket is empty" ; exit 1; }
[[ -z "$DESC" ]] && { echo "ERROR: Description is empty" ; exit 1; }
echo ""
echo "${BRANCH}"
echo "copied to your clipboard (Mouse Middle Button or Shift+Insert)"
echo -n "${BRANCH}" | xclip

Branch name generator

A shell script to generate a branch name in a standard manner with:

  • branch type
  • slash
  • description
  • ticket number

Installation

  • Download/copy the shell script
  • Modify to your needs
sudo cp branch_gen.sh /usr/local/bin/branch_gen
sudo chmod +x /usr/local/bin/branch_gen

Goals

  • create all branches with a given syntax
  • clear usage of branch types
  • add ticket reference for automations
  • keep things readable and traceable

License

None, use it at your own risk ;) If you make an improvement, please sent it to me too!

Usage

  • run branch_gen
  • fill the asked questions (use Enter to choose the default)
  • the result will be in your terminal clipboard (middle mouse button and maybe Shift+Insert)
  • modify it manually before creating the branch

Posible improvements

  • pass the branch types options to numbers
  • use Env Vars
  • add more options to branch types
  • add more examples of subproject code
  • allow several tickets
  • copy git checkout -b to the clipboard

Known troubleshooting

Some plugins may have problems with the '#' in the ticket code, so, you may add or remove it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment