diff mbox series

[2/5] doc/setversions.py: simplify the get_current_version() function

Message ID 20260317-docs-releases-json-v1-2-492d0b256349@bootlin.com
State Accepted, archived
Commit 2f07c60e55996c5f06e1e82c014b9e876c0792cf
Headers show
Series Docs build cleanup and automations | expand

Commit Message

Antonin Godard March 17, 2026, 10:08 a.m. UTC
While this was inspired with yocto-docs' set_versions.py file[1], some
of the logic here can be simplified with early returns.

As a consequence of these early returns, move the print() call showing
the computed version to the conf.py file.

Signed-off-by: Antonin Godard <antonin.godard@bootlin.com>
---
 doc/conf.py        |  1 +
 doc/setversions.py | 90 +++++++++++++++++++++++++-----------------------------
 2 files changed, 42 insertions(+), 49 deletions(-)
diff mbox series

Patch

diff --git a/doc/conf.py b/doc/conf.py
index 40a022eaa00..315fb7d06a7 100644
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -26,6 +26,7 @@  sys.path.insert(0, os.path.abspath('.'))
 import setversions
 
 current_version = setversions.get_current_version()
+print(f"Version calculated to be {current_version}")
 
 # String used in sidebar
 version = 'Version: ' + current_version
diff --git a/doc/setversions.py b/doc/setversions.py
index 2ec68ac9945..4130df67695 100755
--- a/doc/setversions.py
+++ b/doc/setversions.py
@@ -41,8 +41,6 @@  YOCTO_MAPPING = {
 BB_RELEASE_TAG_RE = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+$")
 
 def get_current_version():
-    ourversion = None
-
     # Test tags exist and inform the user to fetch if not
     try:
         subprocess.run(["git", "show", f"{LTSSERIES[0]}.0"],
@@ -50,63 +48,57 @@  def get_current_version():
     except subprocess.CalledProcessError:
         sys.exit("Please run 'git fetch --tags' before building the documentation")
 
-
     # Try and figure out what we are
     tags = subprocess.run(["git", "tag", "--points-at", "HEAD"],
                           stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                           universal_newlines=True).stdout
     for t in tags.split():
         if re.match(BB_RELEASE_TAG_RE, t):
-            ourversion = t
-            break
+            return t
 
-    if ourversion:
-        # We're a tagged release
-        components = ourversion.split(".")
-    else:
-        # We're floating on a branch
-        branch = subprocess.run(["git", "branch", "--show-current"],
-                                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-                                universal_newlines=True).stdout.strip()
-
-        if branch == "" or branch not in list(YOCTO_MAPPING.keys()) + ["master", "master-next"]:
-            # We're not on a known release branch so we have to guess. Compare the
-            # numbers of commits from each release branch and assume the smallest
-            # number of commits is the one we're based off
-            possible_branch = None
-            branch_count = 0
-            for b in itertools.chain(YOCTO_MAPPING.keys(), ["master"]):
-                result = subprocess.run(["git", "log", "--format=oneline", "HEAD..origin/" + b],
-                                        stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-                                        universal_newlines=True)
-                if result.returncode == 0:
-                    count = result.stdout.count('\n')
-                    if not possible_branch or count < branch_count:
-                        print("Branch %s has count %s" % (b, count))
-                        possible_branch = b
-                        branch_count = count
-            if possible_branch:
-                branch = possible_branch
-            else:
-                branch = "master"
-            print("Nearest release branch estimated to be %s" % branch)
+    # We're floating on a branch
+    branch = subprocess.run(["git", "branch", "--show-current"],
+                            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+                            universal_newlines=True).stdout.strip()
 
-        if branch == "master":
-            ourversion = "dev"
-        elif branch == "master-next":
-            ourversion = "next"
+    if branch == "" or branch not in list(YOCTO_MAPPING.keys()) + ["master", "master-next"]:
+        # We're not on a known release branch so we have to guess. Compare the
+        # numbers of commits from each release branch and assume the smallest
+        # number of commits is the one we're based off
+        possible_branch = None
+        branch_count = 0
+        for b in itertools.chain(YOCTO_MAPPING.keys(), ["master"]):
+            result = subprocess.run(["git", "log", "--format=oneline", "HEAD..origin/" + b],
+                                    stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+                                    universal_newlines=True)
+            if result.returncode == 0:
+                count = result.stdout.count('\n')
+                if not possible_branch or count < branch_count:
+                    print("Branch %s has count %s" % (b, count))
+                    possible_branch = b
+                    branch_count = count
+        if possible_branch:
+            branch = possible_branch
         else:
-            ourversion = branch
-            head_commit = subprocess.run(["git", "rev-parse", "--short", "HEAD"],
-                                         stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-                                         universal_newlines=True).stdout.strip()
-            branch_commit = subprocess.run(["git", "rev-parse", "--short", branch],
-                                            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-                                            universal_newlines=True).stdout.strip()
-            if head_commit != branch_commit:
-                ourversion += f" ({head_commit})"
+            branch = "master"
+        print("Nearest release branch estimated to be %s" % branch)
+
+    if branch == "master":
+        return "dev"
+
+    if branch == "master-next":
+        return "next"
+
+    ourversion = branch
+    head_commit = subprocess.run(["git", "rev-parse", "--short", "HEAD"],
+                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+                                 universal_newlines=True).stdout.strip()
+    branch_commit = subprocess.run(["git", "rev-parse", "--short", branch],
+                                   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+                                   universal_newlines=True).stdout.strip()
+    if head_commit != branch_commit:
+        ourversion += f" ({head_commit})"
 
-    print("Version calculated to be %s" % ourversion)
     return ourversion
 
 def write_switchers_js(js_in, js_out, current_version):