Skip to content

Commit

Permalink
Silence/fix all shellcheck errors/warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbowes committed Mar 11, 2019
1 parent 3a91cde commit 0bee7ba
Showing 1 changed file with 55 additions and 47 deletions.
102 changes: 55 additions & 47 deletions semtag
Original file line number Diff line number Diff line change
Expand Up @@ -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]*)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -211,16 +211,17 @@ function get_latest {
local __taglist=("$@")
local __tagsnumber=${#__taglist[@]}
local __current
local ver
case $__tagsnumber in
0)
finalversion=$FIRST_VERSION
lastversion=$FIRST_VERSION
;;
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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -296,23 +297,23 @@ 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]}"
if [[ -n "$identifier" ]]; then
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
Expand All @@ -321,15 +322,15 @@ 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

# 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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 )"
Expand All @@ -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
Expand Down Expand Up @@ -518,19 +524,22 @@ 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

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -621,4 +630,3 @@ case $ACTION in
echo "'$ACTION' is not a valid command, see --help for available commands."
;;
esac

0 comments on commit 0bee7ba

Please sign in to comment.