77
88
99def get_version ():
10- # Use git commands to get version from the checked-out ref.
11- # sphinx-multiversion checks out each ref to a temp directory and runs Sphinx there.
12- # Use current working directory to get the version of the checked-out ref.
10+ """Get version from git tags.
11+
12+ This function is used to determine the version for documentation builds.
13+ For the master branch, we use the latest tag (--abbrev=0) instead of
14+ git describe's default behavior which would show a dev version like
15+ "v3.21.2-2-g8d4e41c" when HEAD is ahead of the latest tag.
16+
17+ This ensures that when master and a tag point to the same commit,
18+ the documentation shows the clean version (e.g., "3.22.0") regardless
19+ of whether the tag was created before or after the docs build started.
20+ """
21+ # Try to get exact tag (for tagged commits)
1322 try :
14- # Try to get exact tag (for tagged commits)
1523 result = subprocess .run (
1624 ["git" , "describe" , "--tags" , "--exact-match" ],
1725 capture_output = True ,
@@ -25,18 +33,20 @@ def get_version():
2533 except (subprocess .CalledProcessError , FileNotFoundError ):
2634 pass
2735
28- # Try to get version using git describe (for non-tagged commits)
36+ # Get the latest tag (without commit count suffix)
37+ # This is used for master branch builds to show a clean version
38+ # instead of a dev version like "v3.21.2-2-g8d4e41c"
2939 try :
3040 result = subprocess .run (
31- ["git" , "describe" , "--tags" , "--always " ],
41+ ["git" , "describe" , "--tags" , "--abbrev=0 " ],
3242 capture_output = True ,
3343 text = True ,
3444 check = True ,
3545 )
36- version_str = result .stdout .strip ()
37- if version_str and version_str .startswith ("v" ):
38- return version_str [1 :] # Remove 'v' prefix
39- return version_str
46+ tag = result .stdout .strip ()
47+ if tag and tag .startswith ("v" ):
48+ return tag [1 :] # Remove 'v' prefix
49+ return tag
4050 except (subprocess .CalledProcessError , FileNotFoundError ):
4151 pass
4252
0 commit comments