diff --git a/src/stewardship/adoption.py b/src/stewardship/adoption.py index ea2dc42..08f80f7 100644 --- a/src/stewardship/adoption.py +++ b/src/stewardship/adoption.py @@ -616,21 +616,50 @@ def apply_plan(root: Path, plan: dict[str, Any], approve_hash: str) -> list[str] return written +def _covers(rel_path: str, entries: set[str]) -> bool: + """True when ``rel_path`` is one of ``entries`` or sits beneath one of them.""" + return rel_path in entries or any(rel_path.startswith(f"{entry}/") for entry in entries) + + def verify(root: Path, plan: dict[str, Any] | None = None) -> dict[str, Any]: validation = validate_repository(root) - baseline = gitutil.head_revision(root) + head = gitutil.head_revision(root) changed_paths: list[str] = [] preserved_paths: list[str] = [] - if plan is not None: + extra: list[tuple[str, str]] = [] + + if plan is None: + # Without the plan there is nothing to check the repository against, and + # empty changedPaths/preservedPaths would otherwise read as "adoption + # touched nothing" rather than "this report verified nothing". + baseline = head + extra.append(("warning", "ADOPT_REPORT_UNSCOPED")) + else: + baseline = plan.get("baselineRevision", head) for change in plan["changes"]: if change["action"] in ("create", "patch-managed-block"): changed_paths.append(change["path"]) elif change["action"] == "preserve": preserved_paths.append(change["path"]) - unresolved = sorted({f.code for f in validation.findings if f.severity in ("warning", "error")}) - if any(f.severity == "error" for f in validation.findings): + dirty = gitutil.dirty_paths(root) + preserved_set, changed_set = set(preserved_paths), set(changed_paths) + if any(_covers(path, preserved_set) for path in dirty): + extra.append(("error", "ADOPT_PRESERVED_MODIFIED")) + if any( + not _covers(path, preserved_set) and not _covers(path, changed_set) for path in dirty + ): + extra.append(("warning", "ADOPT_DIFF_OUTSIDE_PLAN")) + if baseline != head: + extra.append(("warning", "ADOPT_BASELINE_CHANGED")) + + severities = [f.severity for f in validation.findings] + [severity for severity, _ in extra] + unresolved = sorted( + {f.code for f in validation.findings if f.severity in ("warning", "error")} + | {code for _, code in extra} + ) + if "error" in severities: outcome = "fail" elif unresolved: outcome = "pass-with-warnings" diff --git a/src/stewardship/gitutil.py b/src/stewardship/gitutil.py index 4e6ad32..6eaffde 100644 --- a/src/stewardship/gitutil.py +++ b/src/stewardship/gitutil.py @@ -43,8 +43,12 @@ def dirty_paths(root: Path) -> set[str]: so paths containing spaces or non-ASCII are neither quoted nor mangled, and so rename entries split reliably: a rename emits the destination path, then a second NUL-terminated field holding the original path. + + ``-uall`` matters as much as ``-z``: by default Git collapses a wholly + untracked directory to a single ``dir/`` entry, which would never match the + file paths a plan names and so would read as a change outside the plan. """ - result = _run(["status", "--porcelain", "-z"], root) + result = _run(["status", "--porcelain", "-z", "-uall"], root) if result.returncode != 0: return set() fields = [field for field in result.stdout.split("\0") if field]