From d28243d0a15a7469d93d0ee679a7bcab5d58d1fb Mon Sep 17 00:00:00 2001 From: Robin Bowes Date: Thu, 5 Dec 2024 11:06:24 +0000 Subject: [PATCH] Shellcheck fixes --- release.sh | 34 +++----- semtag | 249 ++++++++++++++++++++++++++++++----------------------- 2 files changed, 156 insertions(+), 127 deletions(-) diff --git a/release.sh b/release.sh index 84057eb..255bb35 100755 --- a/release.sh +++ b/release.sh @@ -1,43 +1,35 @@ -#!/usr/bin/env bash +#!/usr/bin/env bash -SCOPE="$1" +set -o errexit # Exit on most errors (see the manual) +set -o nounset # Disallow expansion of unset variables +set -o pipefail # Use last non-zero exit code in a pipeline -if [ -z "$SCOPE" ]; then - SCOPE="auto" -fi +SCOPE="${1:-auto}" echo "Using scope $SCOPE" # We get the next version, without tagging echo "Getting next version" -nextversion="$(source semtag final -fos $SCOPE)" +nextversion="$(source semtag final -fos "$SCOPE")" echo "Publishing with version: $nextversion" # We replace the placeholder in the source with the new version replace="s/^PROG_VERSION=\"[^\"]*\"/PROG_VERSION=\"$nextversion\"/g" -sed -i.bak $replace semtag +sed -i.bak "$replace" semtag + # We replace the version in the README file with the new version replace="s/^\[Version: [^[]*]/[Version: $nextversion]/g" sed -i.bak "$replace" README.md + # We remove the backup README.md generated by the sed command rm semtag.bak rm README.md.bak # We add both changed files -if ! git add semtag README.md ; then - echo "Error adding modified files with new version" - exit 1 -fi - -if ! git commit -m "Update readme and info to $nextversion" ; then - echo "Error committing modified files with new version" - exit 1 -fi +git add semtag README.md -if ! git push ; then - echo "Error pushing modified files with new version" - exit 1 -fi +git commit -m "Update readme and info to $nextversion" +git push # We update the tag with the new version -source semtag final -f -v $nextversion +source semtag final -f -v "$nextversion" diff --git a/semtag b/semtag index a8d0134..2a959d9 100755 --- a/semtag +++ b/semtag @@ -1,5 +1,22 @@ #!/usr/bin/env bash +# Enable xtrace if the DEBUG environment variable is set +if [[ ${DEBUG-} =~ ^1|yes|true$ ]]; then + set -o xtrace # Trace the execution of the script (debug) +fi + +# set bash "strict mode" +set -o errexit # Exit on most errors (see the manual) +set -o nounset # Disallow expansion of unset variables +set -o pipefail # Use last non-zero exit code in a pipeline + +# utility function to print error to stderr +echoerr() { printf "%s\n" "$*" >&2; } + +# utility function to print error and exit +exiterr() { echoerr "$@" ; exit 1; } + + PROG=semtag PROG_VERSION="v0.1.2" @@ -65,9 +82,10 @@ Commands: the commits from the last final version." # Commands and options -ACTION="getlast" -ACTION="$1" -shift +ACTION="${1:-getlast}" +if [[ -n ${1:-} ]] ; then + shift +fi # We get the parameters while getopts "v:s:ofp" opt; do @@ -87,13 +105,11 @@ while getopts "v:s:ofp" opt; do p) prefix="" ;; - \?) - echo "Invalid option: -$OPTARG" >&2 - exit 1 - ;; :) - echo "Option -$OPTARG requires an argument." >&2 - exit 1 + exiterr "Option -$OPTARG requires an argument." + ;; + *) + exiterr "Invalid option: -$OPTARG" ;; esac done @@ -103,11 +119,13 @@ done function get_default_branch { local __result=$1 - local __remotes=$(git remote) + local __remotes + __remotes=$(git remote) if [[ -n $__remotes ]]; then for __remote in $__remotes; do - local __default_branch_ref=$(git symbolic-ref --quiet refs/remotes/${__remote}/HEAD || true) - local __default_branch=${__default_branch_ref#refs/remotes/${__remote}/} + local __default_branch_ref + __defautlt_branch_ref=$(git symbolic-ref --quiet refs/remotes/"${__remote}"/HEAD || true) + local __default_branch="${__default_branch_ref#refs/remotes/"${__remote}"/}" if [[ -n ${__default_branch} ]]; then break fi @@ -147,16 +165,19 @@ function explode_version { function compare_versions { local __first local __second - explode_version $1 __first - explode_version $2 __second + explode_version "$1" __first + explode_version "$2" __second local lv=$3 # Compares MAJOR, MINOR and PATCH for i in 0 1 2; do local __numberfirst=${__first[$i]} local __numbersecond=${__second[$i]} - case $(($__numberfirst - $__numbersecond)) in + local __comparison=$((__numberfirst - __numbersecond)) + case $__comparison in 0) + eval "$lv=0" + return 0 ;; -[0-9]*) eval "$lv=-1" @@ -166,6 +187,9 @@ function compare_versions { eval "$lv=1" return 0 ;; + *) + exiterr "Unknown result: $__comparison" + ;; esac done @@ -223,7 +247,7 @@ function get_latest_of_two { local __result local __latest=$3 - compare_versions $__first $__second __result + compare_versions "$__first" "$__second" __result case $__result in 0) eval "$__latest=$__second" @@ -234,6 +258,9 @@ function get_latest_of_two { 1) eval "$__latest=$__first" ;; + *) + exiterr "Unknown result: $__result" + ;; esac } @@ -268,14 +295,14 @@ function compare_identifiers { local __result=$3 local partresult local arraylengths - if [[ -n "$__first" ]] && [[ -n "$__second" ]]; then + if [[ -n $__first ]] && [[ -n $__second ]]; then explode_identifier "${__first}" explodedidentifierfirst explode_identifier "${__second}" explodedidentifiersecond firstsize=${#explodedidentifierfirst[@]} secondsize=${#explodedidentifiersecond[@]} - minlength=$(( $firstsize<$secondsize ? $firstsize : $secondsize )) - for (( i = 0 ; i < $minlength ; i++ )); do + minlength=$(( firstsize /dev/null - if [ $? -eq 0 ]; then - echo "$__version pushed to $__remote" - else - echo "Error pushing the tag $__version to $__remote" - exit 1 - fi + git push "$__remote" "$__version" > /dev/null + echo "$__version pushed to $__remote" done else echo "$__version" @@ -538,14 +570,14 @@ function check_git_dirty_status { local __repostatus= get_work_tree_status __repostatus - if [ "$__repostatus" == "uncommitted" ]; then - echo "ERROR: You have uncommitted changes" + if [[ $__repostatus == "uncommitted" ]]; then + echoerr "ERROR: You have uncommitted changes" git status --porcelain exit 1 fi - if [ "$__repostatus" == "unstaged" ]; then - echo "ERROR: You have unstaged changes" + if [[ $__repostatus == "unstaged" ]]; then + echoerr "ERROR: You have unstaged changes" git status --porcelain exit 1 fi @@ -553,18 +585,21 @@ function check_git_dirty_status { # Get the total amount of lines of code in the repo function get_total_lines { - local __empty_id="$(git hash-object -t tree /dev/null)" - local __changes="$(git diff --numstat $__empty_id | cat)" - local __added_deleted=$1 - get_changed_lines "$__changes" $__added_deleted + local __empty_id + __empty_id="$(git hash-object -t tree /dev/null)" + local __changes + __changes="$(git diff --numstat "$__empty_id" | cat)" + local __added_deleted="$1" + get_changed_lines "$__changes" "$__added_deleted" } # Get the total amount of lines of code since the provided tag function get_sincetag_lines { local __sincetag=$1 - local __changes="$(git diff --numstat $__sincetag | cat)" + local __changes + __changes="$(git diff --numstat "$__sincetag" | cat)" local __added_deleted=$2 - get_changed_lines "$__changes" $__added_deleted + get_changed_lines "$__changes" "$__added_deleted" } function get_changed_lines { @@ -580,8 +615,8 @@ function get_changed_lines { if [[ $i =~ $__diff_regex ]] ; then local __added=${BASH_REMATCH[1]} local __deleted=${BASH_REMATCH[2]} - __total_added=$(( $__total_added+$__added )) - __total_deleted=$(( $__total_deleted+$__deleted )) + __total_added=$(( __total_added+__added )) + __total_deleted=$(( __total_deleted+__deleted )) fi done eval "$2=( $__total_added $__total_deleted )" @@ -594,12 +629,12 @@ function get_scope_auto { local __scope= get_total_lines __total - get_sincetag_lines $finalversion __since + get_sincetag_lines "$finalversion" __since local __percentage=0 - if [ "$__total" != "0" ]; then - local __percentage=$(( 100*$__since/$__total )) - if [ $__percentage -gt "10" ]; then + if [[ $__total != "0" ]]; then + local __percentage=$(( 100*__since/__total )) + if [[ $__percentage -gt "10" ]]; then __scope="minor" else __scope="patch" @@ -630,40 +665,43 @@ function get_work_tree_status { } function get_current { - if [ "$hasversiontag" == "true" ]; then - local __commitcount="$(git rev-list $lastversion.. --count)" + local __commitcount + if [[ $hasversiontag == "true" ]]; then + __commitcount="$(git rev-list "$lastversion.." --count)" else - local __commitcount="$(git rev-list --count HEAD)" + __commitcount="$(git rev-list --count HEAD)" fi local __status= get_work_tree_status __status - if [ "$__commitcount" == "0" ] && [ -z "$__status" ]; then + if [[ $__commitcount == "0" ]] && [[ -z $__status ]]; then eval "$1=$lastversion" else - local __buildinfo="$(git rev-parse --short HEAD)" - local __currentbranch="$(git rev-parse --abbrev-ref HEAD | cut -c1-$MAX_BRANCH_LENGTH)" + local __buildinfo + __buildinfo="$(git rev-parse --short HEAD)" + local __currentbranch + __currentbranch="$(git rev-parse --abbrev-ref HEAD | cut -c1-"$MAX_BRANCH_LENGTH")" get_default_branch default_branch - if [ "$__currentbranch" != "master" ]; then + if [[ $__currentbranch != "master" ]]; then __buildinfo="$__currentbranch.$__buildinfo" fi local __suffix= - if [ "$__commitcount" != "0" ]; then - if [ -n "$__suffix" ]; then + if [[ $__commitcount != "0" ]]; then + if [[ -n $__suffix ]]; then __suffix="$__suffix." fi __suffix="$__suffix$__commitcount" fi - if [ -n "$__status" ]; then - if [ -n "$__suffix" ]; then + if [[ -n $__status ]]; then + if [[ -n $__suffix ]]; then __suffix="$__suffix." fi __suffix="$__suffix$__status" fi __suffix="$__suffix+$__buildinfo" - if [ "$lastversion" == "$finalversion" ]; then + if [[ $lastversion == "$finalversion" ]]; then scope="patch" identifier= local __bumped= @@ -677,9 +715,9 @@ function get_current { function init { TAGS="$(git tag --merged)" - IFS=$'\n' read -rd '' -a TAG_ARRAY <<<"$TAGS" + mapfile -t TAG_ARRAY <<<"$TAGS" - get_latest ${TAG_ARRAY[@]} + get_latest "${TAG_ARRAY[@]}" currentbranch="$(git rev-parse --abbrev-ref HEAD)" } @@ -694,10 +732,9 @@ case $ACTION in init get_default_branch default_branch diff=$(git diff $default_branch | cat) - if [ "$forcetag" == "false" ]; then - if [ -n "$diff" ]; then - echo "ERROR: Branch must be updated with $default_branch for final versions" - exit 1 + if [[ $forcetag == "false" ]]; then + if [[ -n $diff ]]; then + exiterr "ERROR: Branch must be updated with $default_branch for final versions" fi fi increase_version @@ -731,6 +768,6 @@ case $ACTION in echo "Last tagged version: $lastversion" ;; *) - echo "'$ACTION' is not a valid command, see --help for available commands." + echoerr "'$ACTION' is not a valid command, see --help for available commands." ;; esac