@@ -190,31 +190,26 @@ def write_version_note(template, out_dir, version, ver_type, is_stable):
190
190
191
191
def get_version ():
192
192
"""
193
- Returns a tuple of (name of branch/tag, type branch/tag, is_stable)
193
+ Returns a tuple of (name of branch/tag/commit-id , type branch/tag/commit , is_stable)
194
194
"""
195
- # Trust what RTD says our version is, if it is set
196
- version = os .environ .get ("READTHEDOCS_VERSION" , None )
197
- if version == "latest" :
198
- return ("master" , "branch" , False )
199
-
200
- # Otherwise, use git to look for a tag
195
+ # Use git to look for a tag
201
196
try :
202
197
tag = subprocess .check_output (["git" , "describe" , "--tags" , "--exact-match" ]).strip ().decode ('utf-8' )
203
198
is_stable = re .match (r"v[0-9\.]+$" , tag ) is not None
204
199
return (tag , "tag" , is_stable )
205
200
except subprocess .CalledProcessError :
206
201
pass
207
202
208
- # No tag, look for a branch
209
- refs = subprocess . check_output ([ "git" , "for-each-ref" , "--points-at" , "HEAD" , "--format" , "%(refname)" ]). decode ( 'utf-8' )
210
- print ( "refs: \n %s" % refs )
211
- refs = refs . split ( " \n " )
212
- # Note: this looks for branches in 'origin' because GitLab CI doesn't check out a local branch
213
- branches = [ r . replace ( "refs/remotes/origin/" , "" ). strip () for r in refs if r . startswith ( "refs/remotes/origin/" )]
214
- if len ( branches ) == 0 :
215
- # last resort, return the commit (may happen on Gitlab CI sometimes, unclear why )
216
- return ( subprocess . check_output ([ "git" , "rev-parse" , "--short" , " HEAD"]). strip (). decode ( 'utf-8' ), "commit" , False )
217
- if "master" in branches :
218
- return ( "master" , "branch" , False )
219
- else :
220
- return (branches [ 0 ] , "branch " , False ) # take whatever the first branch is
203
+ # No tag, look at branch name from CI, this will give the correct branch name even if the ref for the branch we
204
+ # merge into has moved forward before the pipeline runs
205
+ branch = os . environ . get ( "CI_COMMIT_REF_NAME" , None )
206
+ if branch is not None :
207
+ return ( branch , " branch" , False )
208
+
209
+ # Try to find the branch name even if docs are built locally
210
+ branch = subprocess . check_output ([ "git" , "rev-parse" , "--abbrev-ref" , "HEAD" ]). decode ( 'utf-8' )
211
+ if branch != " HEAD":
212
+ return ( branch , "branch" , False )
213
+
214
+ # As a last resort we return commit SHA-1, should never happen in CI/docs that should be published
215
+ return (subprocess . check_output ([ "git" , "rev-parse " , "--short" , "HEAD" ]). strip (). decode ( 'utf-8' ), "commit" , False )
0 commit comments