diff --git a/autotag b/autotag index c0e82bb..ae48afa 100755 --- a/autotag +++ b/autotag @@ -8,95 +8,125 @@ # https://github.com/MichaelCurrin/auto-tag/blob/master/LICENSE FALLBACK_TAG='v0.0.0' - USAGE='USAGE: autotag LEVEL [-p] [-h] ' -HELP='HELP: +HELP="HELP: Increment git tag using given increment level. Positional arguments: - LEVEL : 'M' for major, 'm' for minor or 'b' for bug. + LEVEL : 'M' for major, 'm' for minor or 'b' for bug. Flags: - -h --help : Show help and exit. - -p --preview: Do a dry run to show the new tag label only, without creating it. -' - -if [[ "$#" -eq 0 ]] || [[ "$1" == '-h' ]] || [[ "$1" == '--help' ]]; then - echo "$USAGE" - echo "$HELP" - exit 1 -fi - -LEVEL_CHOICE="$1" - -if [[ "$2" ]]; then - if [[ "$2" == '-p' ]] || [[ "$2" == '--preview' ]]; then - PREVIEW='true' - else - echo "🛑 Invalid arguments: \"$*\"" - echo + -h --help : Show help and exit. + -p --preview : Do a dry run to show the new tag label only, without creating it. +" +USER_ARGS="$*" + +# Dynamic variables. Unfortunately all are global but at least there are functions now so the script +# is easier to work with. Also they don't have to be set here even, but are set for clarity. +LEVEL_CHOICE='' +PREVIEW='' +MAJOR='' +MINOR='' +BUG='' + +need_help() { + if [[ "$#" -eq 0 ]] || [[ "$1" == '-h' ]] || [[ "$1" == '--help' ]]; then echo "$USAGE" echo "$HELP" exit 1 fi -else - PREVIEW='false' -fi - -echo '🚛 Fetching tags...' - -git fetch --tags - -echo '🔍 Finding most recent tag...' - -LAST_TAG=$(git describe --abbrev=0 --tags 2>/dev/null) -LAST_TAG="${LAST_TAG:-$FALLBACK_TAG}" -LAST_TAG="${LAST_TAG/v/}" - -# Replace dot with space then split into array. -LAST_TAG_ARR=(${LAST_TAG//./ }) - -MAJOR="${LAST_TAG_ARR[0]}" -MINOR="${LAST_TAG_ARR[1]}" -BUG="${LAST_TAG_ARR[2]}" - -echo "👴 Last tag: v$MAJOR.$MINOR.$BUG" - -# Although the exit only happens after fetching, this needs to happen here so variables are set. -# Otherwise a refactor is needed to check M|m|b and exit if needed, then actually calculate here. -case "$LEVEL_CHOICE" in -"M") - ((MAJOR += 1)) - MINOR=0 - BUG=0 - ;; -"m") - ((MINOR += 1)) - BUG=0 - ;; -"b") - ((BUG += 1)) - ;; -*) - echo "🛑 Invalid arguments: '$*'" +} + +invalid_args_error() { + echo "🛑 Invalid arguments: '$USER_ARGS'" echo echo "$USAGE" exit 1 - ;; -esac - -NEW_TAG="v$MAJOR.$MINOR.$BUG" -echo "⭐ New tag: $NEW_TAG" - -# For some reason these emojis need a double space after to avoid looking squashed, at least on -# macOS. -if [[ "$PREVIEW" == true ]]; then - echo '⏭️ Skipping tag creation' -else - echo '🏷️ Creating annotated tag...' +} + +process_args() { + LEVEL_CHOICE="$1" + + if [[ "$2" ]]; then + if [[ "$2" == '-p' ]] || [[ "$2" == '--preview' ]]; then + PREVIEW='true' + else + invalid_args_error + fi + else + PREVIEW='false' + fi +} + +get_last_tag() { + LAST_TAG=$(git describe --abbrev=0 --tags 2>/dev/null) + LAST_TAG="${LAST_TAG:-$FALLBACK_TAG}" + LAST_TAG="${LAST_TAG/v/}" + + # Replace dot with space then split into array. + LAST_TAG_ARR=(${LAST_TAG//./ }) + + MAJOR="${LAST_TAG_ARR[0]}" + MINOR="${LAST_TAG_ARR[1]}" + BUG="${LAST_TAG_ARR[2]}" +} + +set_level() { + # Although the exit only happens after fetching, this needs to happen here so variables are set. + # Otherwise a refactor is needed to check M|m|b and exit if needed, then actually calculate here. + case "$LEVEL_CHOICE" in + "M") + ((MAJOR += 1)) + MINOR=0 + BUG=0 + ;; + "m") + ((MINOR += 1)) + BUG=0 + ;; + "b") + ((BUG += 1)) + ;; + *) + invalid_args_error + ;; + esac +} + +make_tag() { git tag \ -a "$NEW_TAG" \ -m "$NEW_TAG" -fi +} + +run() { + echo '🚛 Fetching tags...' + git fetch --tags + + echo '🔍 Finding most recent tag...' + get_last_tag + echo "👴 Last tag: v$MAJOR.$MINOR.$BUG" + + set_level + NEW_TAG="v$MAJOR.$MINOR.$BUG" + echo "⭐ New tag: $NEW_TAG" + + # For some reason these emojis need a double space after to avoid looking squashed, at least on + # macOS. + if [[ "$PREVIEW" == true ]]; then + echo '⏭️ Skipping tag creation' + else + echo '🏷️ Creating annotated tag...' + make_tag + fi +} + +main() { + need_help $@ + process_args $@ + run +} + +main $@