From 862702441ce70a272efec97b4fc2fadd9893540f 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 | 214 +++++++++++++++++++++++++++++------------------------ 2 files changed, 132 insertions(+), 116 deletions(-) diff --git a/release.sh b/release.sh index 84057eb..db4c9ed 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..880cdfd 100755 --- a/semtag +++ b/semtag @@ -87,14 +87,14 @@ 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 ;; + *) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; esac done @@ -103,11 +103,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,15 +149,16 @@ 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 __result=$((__numberfirst - __numbersecond)) + case $__result in 0) ;; -[0-9]*) @@ -166,6 +169,10 @@ function compare_versions { eval "$lv=1" return 0 ;; + *) + echo "Unknown result: $__result" >&2 + exit 1 + ;; esac done @@ -223,7 +230,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 +241,10 @@ function get_latest_of_two { 1) eval "$__latest=$__first" ;; + *) + echo "Unknown result: $__result" >&2 + exit 1 + ;; esac } @@ -268,14 +279,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&2 + exit 1 + ;; + esac eval "$__result=${prefix}${__exploded[0]}.${__exploded[1]}.${__exploded[2]}" @@ -396,33 +412,33 @@ function get_next_version { function bump_version { ## First we try to get the next version based on the existing last one - if [ "$scope" == "auto" ]; then + if [[ $scope == "auto" ]]; then get_scope_auto scope fi local __candidatefromlast=$FIRST_VERSION local __explodedlast - explode_version $lastversion __explodedlast - if [[ -n "${__explodedlast[3]}" ]]; then + explode_version "$lastversion" __explodedlast + if [[ -n ${__explodedlast[3]} ]]; then # Last version is not final local __idlast - explode_identifier ${__explodedlast[3]} __idlast + explode_identifier "${__explodedlast[3]}" __idlast # We get the last, given the desired id based on the scope __candidatefromlast="${prefix}${__explodedlast[0]}.${__explodedlast[1]}.${__explodedlast[2]}" if [[ -n "$identifier" ]]; then local __nextid="$identifier.1" - if [ "$identifier" == "${__idlast[0]}" ]; then + if [[ $identifier == "${__idlast[0]}" ]]; then # We target the same identifier as the last so we increase one - __nextid="$identifier.$(( ${__idlast[1]}+1 ))" + __nextid="$identifier.$(( __idlast[1]+1 ))" __candidatefromlast="$__candidatefromlast-$__nextid" else # Different identifiers, we make sure we are assigning a higher identifier, if not, we increase the version __candidatefromlast="$__candidatefromlast-$__nextid" local __comparedwithlast - compare_versions $__candidatefromlast $lastversion __comparedwithlast - if [ "$__comparedwithlast" == -1 ]; then - get_next_version $__candidatefromlast $scope __candidatefromlast + compare_versions "$__candidatefromlast" "$lastversion" __comparedwithlast + if [[ $__comparedwithlast == -1 ]]; then + get_next_version "$__candidatefromlast" "$scope" __candidatefromlast __candidatefromlast="$__candidatefromlast-$__nextid" fi fi @@ -431,7 +447,7 @@ function bump_version { # Then we try to get the version based on the latest final one local __candidatefromfinal=$FIRST_VERSION - get_next_version $finalversion $scope __candidatefromfinal + get_next_version "$finalversion" "$scope" __candidatefromfinal if [[ -n "$identifier" ]]; then __candidatefromfinal="$__candidatefromfinal-$identifier.1" fi @@ -439,7 +455,7 @@ function bump_version { # Finally we compare both candidates local __resultversion local __result - compare_versions $__candidatefromlast $__candidatefromfinal __result + compare_versions "$__candidatefromlast" "$__candidatefromfinal" __result case $__result in 0) __resultversion=$__candidatefromlast @@ -450,6 +466,10 @@ function bump_version { 1) __resultversion=$__candidatefromlast ;; + *) + echo "Invalid result version: $__resultversion" >&2 + exit 1 + ;; esac eval "$1=$__resultversion" @@ -458,12 +478,12 @@ function bump_version { function increase_version { local __version= - if [ -z $forcedversion ]; then + if [[ -z $forcedversion ]]; then bump_version __version else if [[ $forcedversion =~ $SEMVER_REGEX ]] ; then - compare_versions $forcedversion $lastversion __result - if [ $__result -le 0 ]; then + compare_versions "$forcedversion" "$lastversion" __result + if [[ $__result -le 0 ]]; then echo "Version can't be lower than last version: $lastversion" exit 1 fi @@ -474,21 +494,21 @@ function increase_version { __version=$forcedversion fi - if [ "$displayonly" == "true" ]; then + if [[ $displayonly == "true" ]]; then echo "$__version" else - if [ "$forcetag" == "false" ]; then + if [[ $forcetag == "false" ]]; then check_git_dirty_status fi local __commitlist - if [ "$finalversion" == "$FIRST_VERSION" ] || [ "$hasversiontag" != "true" ]; then + if [[ $finalversion == "$FIRST_VERSION" ]] || [[ $hasversiontag != "true" ]]; then __commitlist="$(git log --pretty=oneline | cat)" else - __commitlist="$(git log --pretty=oneline $finalversion... | cat)" + __commitlist="$(git log --pretty=oneline "$finalversion..." | cat)" fi # If we are forcing a bump, we add bump to the commit list - if [[ -z $__commitlist && "$forcetag" == "true" ]]; then + if [[ -z $__commitlist && $forcetag == "true" ]]; then __commitlist="bump" fi @@ -502,30 +522,28 @@ function increase_version { $__commitlist" # We check we have info on the user - local __username=$(git config user.name) - if [ -z "$__username" ]; then + local __username + __username=$(git config user.name) + if [[ -z $__username ]]; then __username=$(id -u -n) - git config user.name $__username + git config user.name "$__username" fi - local __useremail=$(git config user.email) - if [ -z "$__useremail" ]; then + local __useremail + __useremail=$(git config user.email) + if [[ -z $__useremail ]]; then __useremail=$(hostname) git config user.email "$__username@$__useremail" fi - git tag -a $__version -m "$__message" + git tag -a "$__version" -m "$__message" # If we have a remote, we push there - local __remotes=$(git remote) + local __remotes + __remotes=$(git remote) if [[ -n $__remotes ]]; then for __remote in $__remotes; do - git push $__remote $__version > /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,13 +556,13 @@ function check_git_dirty_status { local __repostatus= get_work_tree_status __repostatus - if [ "$__repostatus" == "uncommitted" ]; then + if [[ $__repostatus == "uncommitted" ]]; then echo "ERROR: You have uncommitted changes" git status --porcelain exit 1 fi - if [ "$__repostatus" == "unstaged" ]; then + if [[ $__repostatus == "unstaged" ]]; then echo "ERROR: You have unstaged changes" git status --porcelain exit 1 @@ -553,18 +571,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 +601,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 +615,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 +651,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= @@ -679,7 +703,7 @@ function init { TAGS="$(git tag --merged)" IFS=$'\n' read -rd '' -a TAG_ARRAY <<<"$TAGS" - get_latest ${TAG_ARRAY[@]} + get_latest "${TAG_ARRAY[@]}" currentbranch="$(git rev-parse --abbrev-ref HEAD)" } @@ -694,8 +718,8 @@ case $ACTION in init get_default_branch default_branch diff=$(git diff $default_branch | cat) - if [ "$forcetag" == "false" ]; then - if [ -n "$diff" ]; then + if [[ $forcetag == "false" ]]; then + if [[ -n $diff ]]; then echo "ERROR: Branch must be updated with $default_branch for final versions" exit 1 fi