refactor: one owner order for cited repositories

This commit is contained in:
Abdessamad Derraz committed 2026-11-24 00:20:03 +01:00
1 parent b0f7d6c661
commit 409cc843f9
5 files changed
+116 -19

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
"""Which declared repository a cited part belongs to, by its role word."""
from __future__ import annotations
from collections.abc import Callable, Iterable
from typing import TypeVar
T = TypeVar("T")
def field_role(field: str) -> str:
"""source or upstream: the profile field a repository comes from."""
return "upstream" if field == "upstream" else "source"
def answers(field: str, mode: str, role: str) -> bool:
"""Whether a part written under *role* means this repository: the field
it comes from, or the build mode a keyed source declares it under."""
return bool(role) and role in (field_role(field), mode)
def owner_order(
repositories: Iterable[T], role: str, declared: Callable[[T], tuple[str, str]]
) -> list[T]:
"""Repositories in the order a cited part claims them.
Those its role names come first, then the rest, each group in
declaration order: between two trees that carry the cited path, the
one declared first owns a part that names none. `declared` gives the
(field, build mode) a repository was declared under.
"""
items = list(repositories)
if not role:
return items
return sorted(items, key=lambda item: not answers(*declared(item), role))
+27 -8
View File
@@ -5,7 +5,7 @@ from __future__ import annotations
import itertools
import posixpath
import subprocess
from collections.abc import Iterable
from collections.abc import Iterable, Mapping
from dataclasses import dataclass, field
from pathlib import Path
@@ -14,6 +14,7 @@ import yaml
from core import upstream
from model.sourceref import RefPart
from profiling.anchoring import anchor_block
from profiling.attribution import owner_order
from profiling.citations import Citation, citation_key, collect_citations, is_external_citation
from profiling.proseedit import apply_prose_edits, run_after_moves
from model.profilerepos import declared_repositories
@@ -173,6 +174,7 @@ def _realign_part(
cache_dir: str,
offline: bool,
pinned: Iterable[tuple[upstream.Repo, str]] = (),
declared: Mapping[upstream.Repo, tuple[str, str]] | None = None,
) -> tuple[str, tuple[int, int] | str] | None:
"""One range, anchored from its writing revision to the current pin.
@@ -180,16 +182,28 @@ def _realign_part(
repository carries but an unmoved one does lives where nothing moved:
it needs no move, and calling it absent made two readings of the same
history disagree on every run (nethersx2's AetherSX2 citations).
`declared` gives the (field, build mode) of each repository, which the
part's role word is matched against.
"""
if is_external_citation(part.path):
return None
# The path as written decides first, as attribution does: a prefix is
# The role word decides first, as attribution does: retrodream cites
# `(upstream retro_host.c:196-198)` beside the fork's retro_host.c, and
# both trees carry the file.
fields = declared or {}
def claim(item: tuple) -> tuple[str, str]:
return fields.get(item[0], ("source", ""))
pairs = owner_order(pairs, part.role, claim)
pinned = owner_order(pinned, part.role, claim)
# Then the path as written, as attribution reads it: a prefix is
# stripped only when no declared repository carries the path itself.
# hatarib's hatari/src/tos.c lives in hatariB, which did not move, and
# stripping the prefix read it as hatari/hatari's src/tos.c instead.
# Between two repositories that both carry the path, the one declared
# first owns it, as in attribution: freej2me-plus and hex007/freej2me
# both hold src/libretro/, and the citations describe the fork.
# Between two repositories that both carry the path, owner_order puts
# first the one declared first: freej2me-plus and hex007/freej2me both
# hold src/libretro/, and the citations describe the fork.
literal = [
(repo, written, current) for repo, written, current in pairs
if upstream.fetch_file(repo, written, part.path, cache_dir, offline) is not None
@@ -289,6 +303,7 @@ def _realign_citation(
cache_dir: str,
offline: bool,
pinned: Iterable[tuple[upstream.Repo, str]] = (),
declared: Mapping[upstream.Repo, tuple[str, str]] | None = None,
) -> tuple[dict[int, tuple[int, int, str | None]], list[str]]:
"""The moves one prose run needs, and what blocks them.
@@ -304,7 +319,7 @@ def _realign_citation(
outcomes = {
repr(result): result
for result in (
_realign_part(part, pairs, cache_dir, offline, pinned) if pairs else None
_realign_part(part, pairs, cache_dir, offline, pinned, declared) if pairs else None
for pairs in readings
)
}
@@ -335,6 +350,8 @@ class _ProseRepos:
served: dict[upstream.Repo, upstream.Repo]
# Every declared (repository, current pin).
pinned: list[tuple[upstream.Repo, str]]
# The (field, build mode) each repository read is declared under.
roles: dict[upstream.Repo, tuple[str, str]]
def _prose_repos(document: dict) -> _ProseRepos | None:
@@ -366,7 +383,8 @@ def _prose_repos(document: dict) -> _ProseRepos | None:
for field, mode, repo in repos
if (pin := _pin_of(document, f"{field}_commit", mode))
]
return _ProseRepos(repos, served, pinned)
roles = {served.get(repo, repo): (field, mode) for field, mode, repo in repos}
return _ProseRepos(repos, served, pinned, roles)
@dataclass
@@ -463,7 +481,8 @@ def _prose_jobs(
if not any(readings):
continue
moves, blocked = _realign_citation(
citation, readings, ambiguous, cache_dir, offline, declared.pinned
citation, readings, ambiguous, cache_dir, offline, declared.pinned,
declared.roles,
)
if blocked:
# Half a run must not move: the untouched ranges would read as
+11 -11
View File
@@ -27,6 +27,7 @@ from profiling.anchoring import (
verify_at_pin,
worst_status,
)
from profiling.attribution import answers, field_role, owner_order
from profiling.citations import (
Citation,
anchor_tokens,
@@ -541,15 +542,12 @@ class _Resolver:
candidates = self._candidate_paths(path)
# A part that names its repository is looked up there first. In a
# profile keyed by build mode, a part naming none belongs to the
# first build declared, as _realign_part decides a shared path: the
# libretro tree of xenia-edge carries emulator.cc offset, and its
# better-scored line hid a standalone ref that had moved.
# first build declared: the libretro tree of xenia-edge carries
# emulator.cc offset, and its better-scored line hid a standalone
# ref that had moved. owner_order is the rule realign follows too.
if not wanted and self.primary.mode:
wanted = self.primary.mode
ordered = (
sorted(self.views, key=lambda v: not _answers(v, wanted)) if wanted
else self.views
)
ordered = owner_order(self.views, wanted, _declared)
best: tuple[int, RepoView, str] | None = None
for candidate in candidates:
found, best = self._try_candidate(candidate, ordered, start, tokens, wanted, best)
@@ -936,13 +934,15 @@ def build_report(
def _view_role(view: RepoView) -> str:
"""source or upstream: the profile field a repository comes from."""
return "upstream" if view.field == "upstream" else "source"
return field_role(view.field)
def _answers(view: RepoView, role: str) -> bool:
"""Whether a part written under *role* means this repository: the field
it comes from, or the build mode a keyed source declares it under."""
return bool(role) and role in (_view_role(view), view.mode)
return answers(view.field, view.mode, role)
def _declared(view: RepoView) -> tuple[str, str]:
return view.field, view.mode
NO_SOURCE_REF = "no source_ref"
+1
View File
@@ -181,6 +181,7 @@ COMMANDS = {
"profiling/quota.py", "profiling/sourcepaths.py", "profiling/realignpass.py",
"profiling/versioncheck.py", "profiling/selection.py", "profiling/flagchecks.py",
"model/citations.py",
"profiling/attribution.py",
),
"generate_truth.py": ("resolve/truth.py",),
"diff_truth.py": ("exporter/truthdiff.py", "exporter/diffreport.py"),
+42
View File
@@ -59,6 +59,7 @@ from profiling.yamledit import (
from profile_sync import build_parser
from profiling.selection import select_profiles
from profiling.resolver import build_report
from tests.sources import script_source
from profiling.bump import bump_commit
from profiling.citations import collect_citations, collect_tokens
from profiling.detection import (
@@ -5927,6 +5928,47 @@ class RealignGivesASharedPathToTheFirstRepository(unittest.TestCase):
self.assertIsNone(result)
class RealignObeysTheRoleWord(unittest.TestCase):
"""retrodream cites `(upstream retro_host.c:196-198)` beside the fork's
retro_host.c. With both pins moved and both trees carrying the file,
realign read the upstream citation in the fork, declared first."""
def _realign(self, role):
from unittest import mock
fork = upstream.make_repo("github.com", "f", "retrodream")
up = upstream.make_repo("github.com", "o", "dreamcast")
body = [f"line {n}" for n in range(1, 41)]
trees = {
(fork, "f-old"): body, (fork, "f-new"): ["pad"] * 5 + body,
(up, "u-old"): body, (up, "u-new"): body,
}
def fetch(repo, sha, path, *a, **k):
return trees.get((repo, sha)) if path == "retro_host.c" else None
part = sourceref.RefPart("retro_host.c", 20, 22, "20-22", role)
with mock.patch.object(upstream, "fetch_file", side_effect=fetch):
return realign._realign_part(
part, [(fork, "f-old", "f-new"), (up, "u-old", "u-new")], "cache", True,
pinned=[(fork, "f-new"), (up, "u-new")],
declared={fork: ("source", ""), up: ("upstream", "")},
)
def test_an_upstream_part_is_read_in_upstream(self):
self.assertIsNone(self._realign("upstream"))
def test_a_part_naming_no_repository_stays_with_the_first_declared(self):
self.assertEqual(self._realign(""), ("ok", (25, 27)))
def test_attribution_and_realign_share_the_order(self):
"""The resolver and realign order repositories by one function."""
source = script_source("profile_sync.py")
self.assertEqual(source.count("def owner_order("), 1)
self.assertIn("owner_order(self.views", source)
self.assertIn("owner_order(pairs, part.role", source)
class UpstreamForkedByThePort(unittest.TestCase):
"""snes9x, kronos and np2kai pinned upstream_commit on the merge base of
upstream with the fork. Compared to upstream's own HEAD, a bump moved it