From b59fef3d0cb4e53c9c7234df753a20a81acc869c Mon Sep 17 00:00:00 2001 From: Dragos Dumitrache Date: Tue, 2 Jan 2024 11:36:39 +0200 Subject: [PATCH] Make code error reliant This should make it easier to use versioner on repos with no tags --- .github/workflows/test-and-version.yml | 4 ++-- version.sh | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test-and-version.yml b/.github/workflows/test-and-version.yml index 032ee24..81ec0b6 100644 --- a/.github/workflows/test-and-version.yml +++ b/.github/workflows/test-and-version.yml @@ -21,10 +21,10 @@ jobs: run: | ./run_tests.sh - uses: ./versioner/ - if: github.ref == 'ref/heads/master' + if: ${{ github.ref == 'refs/heads/master' }} id: versioner - name: publish tag - if: github.ref == 'ref/heads/master' + if: ${{ github.ref == 'refs/heads/master' }} run: | git config --global user.email ${{ secrets.EMAIL }} git config --global user.name ${{ secrets.NAME }} diff --git a/version.sh b/version.sh index a313fae..31daa6d 100755 --- a/version.sh +++ b/version.sh @@ -65,12 +65,18 @@ function semver { git_branch=$(git branch --show-current) # Get the latest tag - latest_tag=$(git describe --tags $(git rev-list --tags --max-count=1)) - major_minor=$(echo $latest_tag | cut -d '.' -f -2) - patch=$(echo $latest_tag | cut -d '.' -f 3) - # Get the list of commits since the latest tag - commits=$(git rev-list --count $latest_tag..HEAD) - new_patch=$(($commits + $patch)) + latest_tag=$(git tag -l --sort=-creatordate | head -n 1) + if [ $latest_tag ]; then + + major_minor=$(echo "$latest_tag" | cut -d '.' -f -2) + patch=$(echo "$latest_tag" | cut -d '.' -f 3) + # Get the list of commits since the latest tag + commits=$(git rev-list --count "$latest_tag"..HEAD) + # shellcheck disable=SC2004 + new_patch=$(($commits + $patch)) + else + new_patch=$patch + fi git_generated_version="${major_minor}.${new_patch}" future_major=$(cat version.json | jq ".major" --raw-output) future_minor=$(cat version.json | jq ".minor" --raw-output)