diff --git a/scripts/sitesources.py b/scripts/sitesources.py index 436877c6..1b4dd1ea 100644 --- a/scripts/sitesources.py +++ b/scripts/sitesources.py @@ -140,17 +140,14 @@ def _source_ref_markdown(profile: dict, value: str | dict | None) -> str: if part.end is not None and part.end != part.start: display += f"-{part.end}" + # The path is linked as written. A first segment equal to a + # repository's name proves nothing: PCSX2 keeps its sources in + # pcsx2/, hatariB vendors Hatari in hatari/, and stripping it + # broke 470 links. Two repositories and no tree to read: no link. selected: tuple[upstream.Repo, str] | None = None real_path = part.path - if path_re.fullmatch(part.path) and candidates: - for repo, pin in candidates: - prefix = f"{repo.name}/" - if part.path.startswith(prefix): - selected = (repo, pin) - real_path = part.path[len(prefix):] - break - if selected is None and len(candidates) == 1: - selected = candidates[0] + if path_re.fullmatch(part.path) and len(candidates) == 1: + selected = candidates[0] if selected is None: rendered.append(f"`{display}`") diff --git a/tests/test_site_exports.py b/tests/test_site_exports.py index 212af212..37d1a3a5 100644 --- a/tests/test_site_exports.py +++ b/tests/test_site_exports.py @@ -162,9 +162,16 @@ class SiteReferenceContracts(unittest.TestCase): self.assertEqual(rendered, "`jg.c:213-214`") self.assertNotIn("http", rendered) - # A repository-name prefix removes the ambiguity, so it links again. + # A first segment equal to a repository's name proves nothing: PCSX2 + # keeps its sources in pcsx2/, hatariB vendors Hatari in hatari/. + # Stripped, 470 links pointed at nothing or at another file. prefixed = _source_ref_markdown(profile, "example/jg.c:213-214") - self.assertIn("gitlab.com/vendor/example/-/blob/" + "b" * 40 + "/jg.c", prefixed) + self.assertEqual(prefixed, "`example/jg.c:213-214`") + + def test_a_single_repository_links_the_path_as_written(self): + profile = {"source": "https://github.com/PCSX2/pcsx2", "source_commit": "c" * 40} + rendered = _source_ref_markdown(profile, "pcsx2/ImGui/FullscreenUI.cpp:755-759") + self.assertIn("/blob/" + "c" * 40 + "/pcsx2/ImGui/FullscreenUI.cpp#L755-L759", rendered) def test_preprocessor_tokens_cannot_become_page_headings(self): rendered = _admonition_body("First paragraph.\n\n#if FEATURE\nEnabled")