fix: resolve the --changed base in one place

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:02:11 +01:00
1 parent 30113783c9
commit d3d82dd296
5 files changed
+75 -25

No files matched your search

+2 -1
View File
@@ -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:
+20 -1
View File
@@ -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
+2 -9
View File
@@ -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:
+5 -14
View File
@@ -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")
+46
View File
@@ -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