From 1efb39f5eb7dac9c5eb68d4e07ce7df3f6f87cb4 Mon Sep 17 00:00:00 2001
From: Serge Bazanski <q3k@q3k.org>
Date: Sun, 13 Aug 2023 18:07:43 +0200
Subject: [PATCH] st3m: make version.py aware of CI

---
 components/st3m/host-tools/version.py | 13 +++++++++++--
 tools/dist.sh                         | 19 +++++--------------
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/components/st3m/host-tools/version.py b/components/st3m/host-tools/version.py
index 8cb4992aa9..3ce39f77a3 100644
--- a/components/st3m/host-tools/version.py
+++ b/components/st3m/host-tools/version.py
@@ -10,6 +10,7 @@ and the release process.
 
 import subprocess
 import sys
+import os
 
 
 class Tag:
@@ -50,7 +51,7 @@ def tags_for_commit(release, commit):
     return res
 
 
-def get_version():
+def get_git_based_version():
     commit = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip()
     branches = (
         subprocess.check_output(
@@ -118,7 +119,15 @@ if len(sys.argv) > 1:
     if sys.argv[1] == "-c":
         fmt = "C"
 
-v = get_version()
+v = None
+if os.environ.get('CI') is not None:
+    tag = os.environ.get('CI_COMMIT_TAG')
+    if tag is not None:
+        # If we're building a tag, just use that as a version.
+        v = tag
+if v is None:
+    v = get_git_based_version()
+
 if fmt == "C":
     print('const char *st3m_version = "' + v + '";')
 else:
diff --git a/tools/dist.sh b/tools/dist.sh
index 14c19c12ae..37ea64c58a 100755
--- a/tools/dist.sh
+++ b/tools/dist.sh
@@ -11,21 +11,12 @@ if [ ! -f sdkconfig.defaults ] || [ ! -f recovery/sdkconfig.defaults ]; then
     exit 1
 fi
 
-if [ ! -z "${CI}" ]; then
-    if [ ! -z "${CI_COMMIT_TAG}" ]; then
-        # If we're building a tag, just use that as a version.
-        version="${CI_COMMIT_TAG}"
-    else
-        # Otherwise, let normal version machinery run, but fetch all changes
-        # and check out the correct branch. Otherwise we'll get weird version
-        # strings.
-        git fetch --unshallow origin "$CI_COMMIT_REF_NAME"
-        git checkout "$CI_COMMIT_REF_NAME"
-        version="$(python3 components/st3m/host-tools/version.py)"
-    fi
-else
-    version="$(python3 components/st3m/host-tools/version.py)"
+# Always fetch full history on Gitlab when not building a tag, otherwise we get bogus results.
+if [ ! -z "${CI}" && -z "${CI_COMMIT_TAG}" ]; then
+    git fetch --unshallow origin "$CI_COMMIT_REF_NAME"
+    git checkout "$CI_COMMIT_REF_NAME"
 fi
+version="$(python3 components/st3m/host-tools/version.py)"
 
 rm -rf sdkconfig build
 rm -rf recovery/sdkconfig recovery/build
-- 
GitLab