|
#!/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 |
|
|