|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +function safeExit() { |
| 4 | + exit 0 |
| 5 | +} |
| 6 | + |
| 7 | +function error() { |
| 8 | + exit 1; |
| 9 | +} |
| 10 | + |
| 11 | +############## Default values ################### |
| 12 | +scriptName="git jc-branch" |
| 13 | +scriptArgs="<shortDescription>" |
| 14 | +namePrefix=feature/ |
| 15 | +parentBranch=develop |
| 16 | +args=() |
| 17 | + |
| 18 | + |
| 19 | +############## Main Script Here ################### |
| 20 | + |
| 21 | +function handle() { |
| 22 | + |
| 23 | +if [ -z "${args[0]}" ] |
| 24 | +then |
| 25 | + echo -e 'ERROR! Issue short description is missing.\n'; |
| 26 | + usage; |
| 27 | + error; |
| 28 | +fi; |
| 29 | + |
| 30 | +echo 'Fetching remote changes...'; |
| 31 | +git fetch; |
| 32 | + |
| 33 | +branchName="${namePrefix}${args[0]}"; |
| 34 | + |
| 35 | +echo '---------------------------------------'; |
| 36 | +echo "Creating [${branchName}] branch of [origin/${parentBranch}]..."; |
| 37 | +git switch -c ${branchName} origin/${parentBranch}; |
| 38 | + |
| 39 | +echo '---------------------------------------'; |
| 40 | +echo 'Push and set it upstream...'; |
| 41 | +git push --set-upstream origin ${branchName}; |
| 42 | + |
| 43 | +} |
| 44 | + |
| 45 | +############## Begin Options and Usage ################### |
| 46 | + |
| 47 | +# Print usage |
| 48 | +usage() { |
| 49 | + echo -n "${scriptName} [OPTIONS] ${scriptArgs} |
| 50 | +
|
| 51 | +Creates feature branch off of origin/develop. |
| 52 | +
|
| 53 | +Options: |
| 54 | + -h, --help Display this help and exit |
| 55 | + -p, --prefix Branch name prefix, default 'feature/' |
| 56 | + -s, --src Parent branch |
| 57 | +" |
| 58 | +} |
| 59 | + |
| 60 | +# Iterate over options breaking -ab into -a -b when needed and --foo=bar into |
| 61 | +# --foo bar |
| 62 | +optstring=h |
| 63 | +unset options |
| 64 | +while (($#)); do |
| 65 | + case $1 in |
| 66 | + # If option is of type -ab |
| 67 | + -[!-]?*) |
| 68 | + # Loop over each character starting with the second |
| 69 | + for ((i=1; i < ${#1}; i++)); do |
| 70 | + c=${1:i:1} |
| 71 | + |
| 72 | + # Add current char to options |
| 73 | + options+=("-$c") |
| 74 | + |
| 75 | + # If option takes a required argument, and it's not the last char make |
| 76 | + # the rest of the string its argument |
| 77 | + if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then |
| 78 | + options+=("${1:i+1}") |
| 79 | + break |
| 80 | + fi |
| 81 | + done |
| 82 | + ;; |
| 83 | + |
| 84 | + # If option is of type --foo=bar |
| 85 | + --?*=*) options+=("${1%%=*}" "${1#*=}") ;; |
| 86 | + # add --endopts for -- |
| 87 | + --) options+=(--endopts) ;; |
| 88 | + # Otherwise, nothing special |
| 89 | + *) options+=("$1") ;; |
| 90 | + esac |
| 91 | + shift |
| 92 | +done |
| 93 | +set -- "${options[@]}" |
| 94 | +unset options |
| 95 | + |
| 96 | +# Print help if no arguments were passed. |
| 97 | +# Uncomment to force arguments when invoking the script |
| 98 | +# [[ $# -eq 0 ]] && set -- "--help" |
| 99 | + |
| 100 | +# Read the options and set stuff |
| 101 | +while [[ $1 = -?* ]]; do |
| 102 | + case $1 in |
| 103 | + -h|--help) usage >&2; safeExit ;; |
| 104 | + -p|--prefix) shift; namePrefix=${1} ;; |
| 105 | + -s|--src) shift; parentBranch=${1} ;; |
| 106 | + --endopts) shift; break ;; |
| 107 | + *) die "invalid option: '$1'." ;; |
| 108 | + esac |
| 109 | + shift |
| 110 | +done |
| 111 | + |
| 112 | +# Store the remaining part as arguments. |
| 113 | +args+=("$@") |
| 114 | + |
| 115 | +############## End Options and Usage ################### |
| 116 | + |
| 117 | + |
| 118 | +############## Script Run Code ################### |
| 119 | + |
| 120 | +# Set IFS to preferred implementation |
| 121 | +IFS=$'\n\t' |
| 122 | + |
| 123 | +# Exit on error. Append '||true' when you run the script if you expect an error. |
| 124 | +set -o errexit |
| 125 | + |
| 126 | +# Bash will remember & return the highest exitcode in a chain of pipes. |
| 127 | +# This way you can catch the error in case mysqldump fails in `mysqldump |gzip`, for example. |
| 128 | +set -o pipefail |
| 129 | + |
| 130 | +# Run your script |
| 131 | +handle |
| 132 | + |
| 133 | +# Exit cleanly |
| 134 | +safeExit |
0 commit comments