@@ -50,6 +50,27 @@ def _run_capture(command: list[str], *, cwd: Path | None = None) -> str:
5050 return result .stdout .strip ()
5151
5252
53+ def _resolve_git_ref (repo_root : Path , ref : str ) -> str :
54+ """Return a git ref that exists in the local clone.
55+
56+ Tag-triggered CI checkouts are often detached HEAD with ``origin/main`` but
57+ no local ``main`` branch. Branch-like refs try ``ref`` then ``origin/ref``.
58+ """
59+ candidates = [ref ]
60+ if not ref .startswith ("refs/" ) and "/" not in ref :
61+ candidates .append (f"origin/{ ref } " )
62+ for candidate in candidates :
63+ probe = subprocess .run (
64+ ["git" , "rev-parse" , "--verify" , f"{ candidate } ^{{commit}}" ],
65+ cwd = repo_root ,
66+ capture_output = True ,
67+ text = True ,
68+ )
69+ if probe .returncode == 0 :
70+ return candidate
71+ return ref
72+
73+
5374def _repo_root (docs_dir : Path ) -> Path :
5475 return docs_dir .parent
5576
@@ -123,13 +144,15 @@ def _conf_ref_for(version_name: str) -> str | None:
123144
124145def _release_date_for_ref (repo_root : Path , ref : str ) -> str :
125146 # ISO-style date (YYYY-MM-DD) for deterministic display in docs headers.
126- return _run_capture (["git" , "log" , "-1" , "--format=%cs" , ref ], cwd = repo_root )
147+ resolved = _resolve_git_ref (repo_root , ref )
148+ return _run_capture (["git" , "log" , "-1" , "--format=%cs" , resolved ], cwd = repo_root )
127149
128150
129151def _version_for_ref (repo_root : Path , ref : str , fallback : str ) -> str :
130152 # Resolve display version from the VERSION file in the selected ref.
153+ resolved = _resolve_git_ref (repo_root , ref )
131154 try :
132- raw = _run_capture (["git" , "show" , f"{ ref } :VERSION" ], cwd = repo_root )
155+ raw = _run_capture (["git" , "show" , f"{ resolved } :VERSION" ], cwd = repo_root )
133156 value = raw .splitlines ()[0 ].strip ().lstrip ("v" )
134157 return value or fallback
135158 except Exception :
@@ -178,8 +201,9 @@ def _ensure_worktree(
178201 path = worktrees_root / name
179202 if path .exists ():
180203 return path
204+ resolved = _resolve_git_ref (repo_root , ref )
181205 try :
182- _run (["git" , "worktree" , "add" , "--detach" , str (path ), ref ], cwd = repo_root )
206+ _run (["git" , "worktree" , "add" , "--detach" , str (path ), resolved ], cwd = repo_root )
183207 except subprocess .CalledProcessError :
184208 # Recover from a previous run that deleted the dir without unregistering.
185209 subprocess .run (
@@ -189,7 +213,7 @@ def _ensure_worktree(
189213 capture_output = True ,
190214 )
191215 _run (
192- ["git" , "worktree" , "add" , "--force" , "--detach" , str (path ), ref ],
216+ ["git" , "worktree" , "add" , "--force" , "--detach" , str (path ), resolved ],
193217 cwd = repo_root ,
194218 )
195219 return path
@@ -319,7 +343,8 @@ def main() -> None:
319343 worktrees_root .mkdir (parents = True , exist_ok = True )
320344
321345 tags = _selected_tags (repo_root )
322- versions : list [dict [str , str ]] = [{"name" : "current" , "ref" : "main" }]
346+ main_ref = _resolve_git_ref (repo_root , "main" )
347+ versions : list [dict [str , str ]] = [{"name" : "current" , "ref" : main_ref }]
323348 versions .extend ({"name" : t , "ref" : t } for t in tags )
324349 if args .preview :
325350 preview_branch = _current_branch (repo_root )
@@ -415,7 +440,7 @@ def main() -> None:
415440 (output_root / "index.html" ).write_text (index_html , encoding = "utf-8" )
416441
417442 # Basic crawl artifacts for search engines.
418- sitemap_entries : list [tuple [str , str ]] = [("" , _release_date_for_ref (repo_root , "main" ))]
443+ sitemap_entries : list [tuple [str , str ]] = [("" , _release_date_for_ref (repo_root , main_ref ))]
419444 for item in links :
420445 sitemap_entries .append ((f"{ item ['name' ]} /index.html" , item .get ("release_date" , "" )))
421446
0 commit comments