diff --git a/semtag b/semtag index 7143c8a..640f4b4 100755 --- a/semtag +++ b/semtag @@ -119,15 +119,15 @@ function explode_version { function compare_versions { local __first local __second - explode_version $1 __first - explode_version $2 __second - local lv=$3 + 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 + case $((__numberfirst - __numbersecond)) in 0) ;; -[0-9]*) @@ -172,7 +172,7 @@ function get_latest_of_two { local __second=$2 local __result local __latest=$3 - compare_versions $__first $__second __result + compare_versions "$__first" "$__second" __result case $__result in 0) eval "$__latest=$__second" @@ -211,6 +211,7 @@ function get_latest { local __taglist=("$@") local __tagsnumber=${#__taglist[@]} local __current + local ver case $__tagsnumber in 0) finalversion=$FIRST_VERSION @@ -218,9 +219,9 @@ function get_latest { ;; 1) __current=${__taglist[0]} - explode_version $__current ver - if [ -n "$ver" ]; then - if [ -n "${ver[3]}" ]; then + explode_version "$__current" ver + if [[ -n $ver ]]; then + if [[ -n ${ver[3]} ]]; then finalversion=$FIRST_VERSION else finalversion=$__current @@ -232,17 +233,17 @@ function get_latest { fi ;; *) - local __lastpos=$(($__tagsnumber-1)) + local __lastpos=$((__tagsnumber-1)) for i in $(seq 0 $__lastpos) do __current=${__taglist[i]} - explode_version ${__taglist[i]} ver + explode_version "${__taglist[i]}" ver if [ -n "$ver" ]; then if [ -z "${ver[3]}" ]; then - get_latest_of_two $finalversion $__current finalversion + get_latest_of_two $finalversion "$__current" finalversion get_latest_of_two $lastversion $finalversion lastversion else - get_latest_of_two $lastversion $__current lastversion + get_latest_of_two $lastversion "$__current" lastversion fi fi done @@ -265,19 +266,19 @@ function get_next_version { local __fromversion=$1 local __scope=$2 local __result=$3 - explode_version $__fromversion __exploded + explode_version "$__fromversion" __exploded case $__scope in major) - __exploded[0]=$((${__exploded[0]}+1)) + __exploded[0]=$((__exploded[0]+1)) __exploded[1]=0 __exploded[2]=0 ;; minor) - __exploded[1]=$((${__exploded[1]}+1)) + __exploded[1]=$((__exploded[1]+1)) __exploded[2]=0 ;; patch) - __exploded[2]=$((${__exploded[2]}+1)) + __exploded[2]=$((__exploded[2]+1)) ;; esac @@ -296,7 +297,7 @@ function bump_version { 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="v${__explodedlast[0]}.${__explodedlast[1]}.${__explodedlast[2]}" @@ -304,15 +305,15 @@ function bump_version { local __nextid="$identifier.1" 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 + compare_versions "$__candidatefromlast" $lastversion __comparedwithlast if [ "$__comparedwithlast" == -1 ]; then - get_next_version $__candidatefromlast $scope __candidatefromlast + get_next_version "$__candidatefromlast" "$scope" __candidatefromlast __candidatefromlast="$__candidatefromlast-$__nextid" fi fi @@ -321,7 +322,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 @@ -329,7 +330,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 @@ -348,12 +349,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 @@ -392,24 +393,26 @@ function increase_version { $__commitlist" # We check we have info on the user - local __username=$(git config user.name) + 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) + 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 __remote=$(git remote) + local __remote + __remote=$(git remote) if [[ -n $__remote ]]; then - git push $__remote $__version > /dev/null - if [ $? -eq 0 ]; then + if git push "$__remote" "$__version" > /dev/null ; then echo "$__version" else echo "Error pushing the tag $__version to $__remote" @@ -441,18 +444,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 __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_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 { @@ -468,8 +474,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 )" @@ -486,7 +492,7 @@ function get_scope_auto { local __percentage=0 if [ "$__total" != "0" ]; then - local __percentage=$(( 100*$__since/$__total )) + local __percentage=$(( 100*__since/__total )) if [ $__percentage -gt "10" ]; then __scope="minor" else @@ -518,10 +524,11 @@ function get_work_tree_status { } function get_current { + local __commitcount if [ "$hasversiontag" == "true" ]; then - local __commitcount="$(git rev-list $lastversion.. --count)" + __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 @@ -529,8 +536,10 @@ function get_current { 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)" + local __buildinfo + __buildinfo="$(git rev-parse --short HEAD)" + local __currentbranch + __currentbranch="$(git rev-parse --abbrev-ref HEAD)" if [ "$__currentbranch" != "master" ]; then __buildinfo="$__currentbranch.$__buildinfo" fi @@ -567,8 +576,8 @@ function init { TAGS="$(git tag)" IFS=$'\n' read -rd '' -a TAG_ARRAY <<<"$TAGS" - get_latest ${TAG_ARRAY[@]} - currentbranch="$(git rev-parse --abbrev-ref HEAD)" + get_latest "${TAG_ARRAY[@]}" + current="$(git rev-parse --abbrev-ref HEAD)" } case $ACTION in @@ -621,4 +630,3 @@ case $ACTION in echo "'$ACTION' is not a valid command, see --help for available commands." ;; esac -