@@ -248,7 +248,25 @@ def static_relative(path):
248248
249249
250250def get_git_branch ():
251- """Get the git branch this repository is currently on."""
251+ """Get the git branch this repository is currently on.
252+
253+ On Read the Docs the repo is checked out in detached-HEAD mode, so
254+ ``git name-rev`` returns synthetic names like ``remotes/origin/external-1234``
255+ instead of the real branch. A workaround is provided using
256+ ``READTHEDOCS_VERSION_TYPE`` and ``READTHEDOCS_VERSION`` according to the build type:
257+ - ``"branch"`` builds: READTHEDOCS_VERSION is the branch name (e.g. ``"3.6.x"``) → use it.
258+ - ``"tag"`` builds: READTHEDOCS_VERSION is the tag name (e.g. ``"v3.6.0"``) → use it.
259+ - ``"external"`` (PR preview) builds: READTHEDOCS_VERSION is the PR number (e.g. ``"1241"``)
260+ which is not a valid git ref. In this case we return None so resolve_fallback_branch falls
261+ back to its default instead of generating broken GitHub URLs.
262+ - Local builds: READTHEDOCS_VERSION_TYPE is unset → fall back to git name-rev.
263+ """
264+ rtd_type = os .environ .get ("READTHEDOCS_VERSION_TYPE" )
265+ if rtd_type in ("branch" , "tag" ):
266+ return os .environ .get ("READTHEDOCS_VERSION" )
267+ if rtd_type == "external" :
268+ return None
269+
252270 path_to_here = os .path .abspath (os .path .dirname (__file__ ))
253271
254272 # Invoke git to get the current branch which we use to get the theme
@@ -270,12 +288,28 @@ def get_git_branch():
270288 return p .communicate ()[0 ].decode ().rstrip ()
271289
272290 except Exception :
291+ # Local build without git or some error occurred
273292 print ("Could not get the branch" )
274293
275294 # Couldn't figure out the branch probably due to an error
276295 return None
277296
278297
298+ def resolve_fallback_branch (env_var , docs_branch , default = "master" ):
299+ """
300+ Resolve the branch to use for GitHub links.
301+
302+ Priority:
303+ 1. Environment variable ``env_var`` (e.g. FASTDDS_BRANCH)
304+ 2. Current documentation branch (``docs_branch``)
305+ 3. Hard-coded ``default``
306+
307+ This mirrors the checkout logic used in the ReadTheDocs clone block so
308+ that extlinks and the actual checkout always point at the same branch.
309+ """
310+ return os .environ .get (env_var ) or docs_branch or default
311+
312+
279313def configure_doxyfile (
280314 doxyfile_in ,
281315 doxyfile_out ,
@@ -323,6 +357,27 @@ def configure_doxyfile(
323357# Header files
324358input_dir = os .path .abspath ("{}/fastdds/include/fastdds" .format (project_binary_dir ))
325359
360+ # Current branch of the documentation repository — resolved once, used everywhere.
361+ docs_branch = get_git_branch ()
362+ if docs_branch :
363+ print ('Current documentation branch is "{}"' .format (docs_branch ))
364+ else :
365+ print ("Current documentation branch could not be determined; " \
366+ "GitHub links will point to default branches instead of the corresponding branch." )
367+
368+ # Resolve GitHub link branches: env var → current docs branch → default.
369+ # Computed here so they are available both in the ReadTheDocs clone block and in extlinks.
370+ fastdds_fallback_branch = resolve_fallback_branch ("FASTDDS_BRANCH" , docs_branch , "master" )
371+ fastdds_docs_fallback_branch = resolve_fallback_branch ("FASTDDS_DOCS_BRANCH" , docs_branch , "master" )
372+ fastdds_python_fallback_branch = resolve_fallback_branch ("FASTDDS_PYTHON_BRANCH" , docs_branch , "master" )
373+ fastdds_gen_fallback_branch = resolve_fallback_branch ("FASTDDS_GEN_BRANCH" , docs_branch , "master" )
374+
375+ print ("Fallback branches for GitHub links:" )
376+ print (' Fast-DDS: "{}"' .format (fastdds_fallback_branch ))
377+ print (' Fast-DDS-docs: "{}"' .format (fastdds_docs_fallback_branch ))
378+ print (' Fast-DDS-Python: "{}"' .format (fastdds_python_fallback_branch ))
379+ print (' Fast-DDS-Gen: "{}"' .format (fastdds_gen_fallback_branch ))
380+
326381# Check if we're running on Read the Docs' servers
327382read_the_docs_build = os .environ .get ("READTHEDOCS" , None ) == "True"
328383if read_the_docs_build :
@@ -355,24 +410,14 @@ def configure_doxyfile(
355410 fastdds_repo_name ,
356411 )
357412
358- # Documentation repository branch
359- docs_branch = get_git_branch ()
360- print ('Current documentation branch is "{}"' .format (docs_branch ))
361-
362- # User specified Fast DDS branch
363- fastdds_branch = os .environ .get ("FASTDDS_BRANCH" , None )
364-
365- # First try to checkout to ${FASTDDS_BRANCH}
366- # Else try with current documentation branch
367- # Else checkout to master
368- if fastdds_branch and fastdds .refs .__contains__ ("origin/{}" .format (fastdds_branch )):
413+ # Verify the desired branch actually exists in the cloned remote, falling back to master if not.
414+ fastdds_branch = fastdds_fallback_branch
415+ if fastdds .refs .__contains__ ("origin/{}" .format (fastdds_branch )):
369416 fastdds_branch = "origin/{}" .format (fastdds_branch )
370- elif docs_branch and fastdds .refs .__contains__ ("origin/{}" .format (docs_branch )):
371- fastdds_branch = "origin/{}" .format (docs_branch )
372417 else :
373418 print (
374- 'Fast DDS does not have either "{}" or "{}" branches ' .format (
375- fastdds_branch , docs_branch
419+ 'Fast DDS does not have branch "{}"; falling back to master ' .format (
420+ fastdds_branch
376421 )
377422 )
378423 fastdds_branch = "origin/master"
@@ -388,24 +433,14 @@ def configure_doxyfile(
388433 fastdds_python_repo_name ,
389434 )
390435
391- # User specified Fast DDS branch
392- fastdds_python_branch = os .environ .get ("FASTDDS_PYTHON_BRANCH" , None )
393-
394- # First try to checkout to ${FASTDDS_PYTHON_BRANCH}
395- # Else try with current documentation branch
396- # Else checkout to master
397- if fastdds_python_branch and fastdds_python .refs .__contains__ (
398- "origin/{}" .format (fastdds_python_branch )
399- ):
436+ # Verify the desired branch actually exists in the cloned remote, falling back to master if not.
437+ fastdds_python_branch = fastdds_python_fallback_branch
438+ if fastdds_python .refs .__contains__ ("origin/{}" .format (fastdds_python_branch )):
400439 fastdds_python_branch = "origin/{}" .format (fastdds_python_branch )
401- elif docs_branch and fastdds_python .refs .__contains__ (
402- "origin/{}" .format (docs_branch )
403- ):
404- fastdds_python_branch = "origin/{}" .format (docs_branch )
405440 else :
406441 print (
407- 'Fast DDS Python does not have either "{}" or "{}" branches ' .format (
408- fastdds_python_branch , docs_branch
442+ 'Fast DDS Python does not have branch "{}"; falling back to master ' .format (
443+ fastdds_python_branch
409444 )
410445 )
411446 fastdds_python_branch = "origin/master"
@@ -480,10 +515,34 @@ def configure_doxyfile(
480515 "sphinx_copybutton" ,
481516 "sphinx_design" ,
482517 "sphinx.ext.autodoc" , # Document Pydoc documentation from Python bindings.
518+ "sphinx.ext.extlinks" ,
483519 "sphinx_substitution_extensions" ,
484520 "sphinx_toolbox.collapse" ,
485521]
486522
523+ extlinks = {
524+ # Fast-DDS repo (tree = directory, blob = file)
525+ "fastdds-tree" : (
526+ f"https://github.com/eProsima/Fast-DDS/tree/{ fastdds_fallback_branch } /%s" , "%s"
527+ ),
528+ "fastdds-blob" : (
529+ f"https://github.com/eProsima/Fast-DDS/blob/{ fastdds_fallback_branch } /%s" , "%s"
530+ ),
531+ # Fast-DDS-python repo
532+ "fastdds-python-tree" : (
533+ f"https://github.com/eProsima/Fast-DDS-Python/tree/{ fastdds_python_fallback_branch } /%s" , "%s"
534+ ),
535+ # Fast-DDS-docs repo (code examples embedded in the docs repo)
536+ "fastdds-docs-tree" : (
537+ f"https://github.com/eProsima/Fast-DDS-docs/tree/{ fastdds_docs_fallback_branch } /%s" , "%s"
538+ ),
539+ # Fast-DDS-Gen raw files
540+ "fastddsgen-raw" : (
541+ f"https://raw.githubusercontent.com/eProsima/Fast-DDS-Gen/{ fastdds_gen_fallback_branch } /%s" ,
542+ "%s" ,
543+ ),
544+ }
545+
487546sphinx_tabs_disable_css_loading = False
488547sphinx_tabs_disable_tab_closing = True
489548
@@ -684,6 +743,10 @@ def configure_doxyfile(
684743 <span class="sd-badge sd-outline-primary sd-text-primary" title="Exclusive to Fast DDS Pro">Pro</span>
685744
686745.. |ProjectVersion| replace:: { version }
746+
747+ .. |FastDDSBranch| replace:: { fastdds_fallback_branch }
748+
749+ .. |FastDDSPythonBranch| replace:: { fastdds_python_fallback_branch }
687750"""
688751
689752
0 commit comments