From d3d82dd29662d5512bc904c2efa86531e7bf869f Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 04:27:02 +0100 Subject: [PATCH] fix: resolve the --changed base in one place --- scripts/core/cli.py | 3 ++- scripts/core/gitclone.py | 21 +++++++++++++++- scripts/profile_sync.py | 11 ++------ scripts/validate_pr.py | 19 ++++---------- tests/test_refresh_failures.py | 46 ++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 25 deletions(-) diff --git a/scripts/core/cli.py b/scripts/core/cli.py index b5d438d15..c27e9cf56 100644 --- a/scripts/core/cli.py +++ b/scripts/core/cli.py @@ -10,6 +10,7 @@ from collections.abc import Callable from pathlib import Path from core.artifacts import ArtifactDirectoryMissing, ArtifactLockBusy +from core.gitclone import MainLineUnreachable from core.paths import BIOS_DIR, DB_FILE, EMULATORS_DIR, PLATFORMS_DIR @@ -130,7 +131,7 @@ def run_cli(main: Callable[[], int | None]) -> None: code = main() sys.stdout.flush() except (InputMissing, SourceUnreadable, ArtifactLockBusy, - ArtifactDirectoryMissing) as exc: + ArtifactDirectoryMissing, MainLineUnreachable) as exc: print(f"ERROR: {exc}", file=sys.stderr) sys.exit(1) except BrokenPipeError: diff --git a/scripts/core/gitclone.py b/scripts/core/gitclone.py index e4c3d998a..3f640541e 100644 --- a/scripts/core/gitclone.py +++ b/scripts/core/gitclone.py @@ -10,6 +10,10 @@ class GitRefused(RuntimeError): """A git command that exited non-zero, with what it said.""" +class MainLineUnreachable(RuntimeError): + """The clone does not hold the project's main and cannot fetch it.""" + + def _run(args: tuple[str, ...]) -> subprocess.CompletedProcess[str]: return subprocess.run(["git", *args], capture_output=True, text=True, check=False) @@ -83,8 +87,23 @@ def project_main_line() -> str | None: capture_output=True, text=True, check=False, ) if fetched.returncode != 0: - raise RuntimeError( + raise MainLineUnreachable( f"this clone does not hold the main of {PROJECT_URLS[0]}, and " f"fetching its main from {source} failed: {fetched.stderr.strip()}" ) return "FETCH_HEAD" + + +def compare_base() -> str | None: + """The line --changed compares with: the project's main line, else the + first upstream/ or origin/ line the clone holds, None when it holds none + and only its uncommitted files can be read. profile_sync and validate_pr + both read it, so the two compare against the same base.""" + line = project_main_line() + if line: + return line + for candidates in fallback_lines(): + for candidate in candidates: + if git_lines("rev-parse", "--verify", "--quiet", candidate): + return candidate + return None diff --git a/scripts/profile_sync.py b/scripts/profile_sync.py index dc4d3303e..f1a08acbb 100644 --- a/scripts/profile_sync.py +++ b/scripts/profile_sync.py @@ -285,16 +285,9 @@ def project_base() -> str: is HEAD, and --changed --strict found nothing and passed while the PR's CI then failed. validate_pr.py resolves the same line the same way. """ - from core.gitclone import fallback_lines, git_lines, project_main_line + from core.gitclone import compare_base - line = project_main_line() - if line: - return line - for candidates in fallback_lines(): - for candidate in candidates: - if git_lines("rev-parse", "--verify", "--quiet", candidate): - return candidate - return "HEAD" + return compare_base() or "HEAD" def select_profiles(profiles: dict, args: argparse.Namespace) -> dict: diff --git a/scripts/validate_pr.py b/scripts/validate_pr.py index be14a8ba6..b1fd655e6 100644 --- a/scripts/validate_pr.py +++ b/scripts/validate_pr.py @@ -34,7 +34,7 @@ try: import yaml except ImportError: yaml = None -from core.gitclone import fallback_lines, git_lines, project_main_line +from core.gitclone import compare_base, git_lines from resolve.review import ( ValidationResult, load_platform_hashes, @@ -54,19 +54,10 @@ def get_changed_files() -> list[str]: A git that fails outright is an error, never "nothing changed". """ changed: list[str] = [] - compared = False - project_base = project_main_line() - remotes = [[project_base]] if project_base else fallback_lines() - for candidates in remotes: - for line in candidates: - committed = git_lines("diff", "--name-only", f"{line}...HEAD") - if committed is None: - continue - compared = True - changed.extend(committed) - break - if compared and changed: - break + base = compare_base() + committed = git_lines("diff", "--name-only", f"{base}...HEAD") if base else None + compared = committed is not None + changed.extend(committed or []) staged = git_lines("diff", "--cached", "--name-only") local = git_lines("diff", "--name-only") untracked = git_lines("ls-files", "--others", "--exclude-standard", "--", "bios") diff --git a/tests/test_refresh_failures.py b/tests/test_refresh_failures.py index b40827080..d2b9fec7d 100644 --- a/tests/test_refresh_failures.py +++ b/tests/test_refresh_failures.py @@ -65,6 +65,52 @@ class GitThatCannotAnswer(unittest.TestCase): self.assertIn("untracked files only", proc.stderr) +class AProjectMainThatCannotBeFetched(unittest.TestCase): + """profile_sync --changed ended on a RuntimeError traceback where + validate_pr printed one line, in a clone with a non-project remote and + no way to fetch the project's main.""" + + def test_both_tools_print_one_error_line(self): + import contextlib + import io + + import profile_sync + from core import cli, gitclone + + def unreachable() -> None: + raise gitclone.MainLineUnreachable("no main") + + with mock.patch.object(gitclone, "project_main_line", side_effect=unreachable): + err = io.StringIO() + with contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as ended: + cli.run_cli(profile_sync.project_base) + self.assertEqual(ended.exception.code, 1) + self.assertEqual(err.getvalue(), "ERROR: no main\n") + import validate_pr + with self.assertRaises(RuntimeError): + validate_pr.get_changed_files() + + def test_both_tools_compare_against_the_same_line(self): + import profile_sync + import validate_pr + from core import gitclone + + diffs: list[tuple[str, ...]] = [] + + def lines(*args: str) -> list[str] | None: + if args[:3] == ("rev-parse", "--verify", "--quiet"): + return ["x"] if args[3] == "origin/main" else None + diffs.append(args) + return [] + + with mock.patch.object(gitclone, "project_main_line", return_value=None), \ + mock.patch.object(gitclone, "git_lines", side_effect=lines), \ + mock.patch.object(validate_pr, "git_lines", side_effect=lines): + self.assertEqual(profile_sync.project_base(), "origin/main") + validate_pr.get_changed_files() + self.assertIn(("diff", "--name-only", "origin/main...HEAD"), diffs) + + class ChangesNotYetCommittedAreRead(unittest.TestCase): """--changed read only `git diff origin/main...HEAD`: a file copied into bios/ before any commit, as CONTRIBUTING has it, or committed on a