diff --git a/scripts/core/gitclone.py b/scripts/core/gitclone.py index c20bcf3aa..e4c3d998a 100644 --- a/scripts/core/gitclone.py +++ b/scripts/core/gitclone.py @@ -6,13 +6,42 @@ import re import subprocess +class GitRefused(RuntimeError): + """A git command that exited non-zero, with what it said.""" + + +def _run(args: tuple[str, ...]) -> subprocess.CompletedProcess[str]: + return subprocess.run(["git", *args], capture_output=True, text=True, check=False) + + +def _lines(output: str) -> list[str]: + return [line for line in output.splitlines() if line] + def git_lines(*args: str) -> list[str] | None: """Output lines of a git command, or None when git refuses it.""" - result = subprocess.run(["git", *args], capture_output=True, text=True, check=False) - if result.returncode != 0: - return None - return [line for line in result.stdout.splitlines() if line] + result = _run(args) + return _lines(result.stdout) if result.returncode == 0 else None + + +def required_git_lines(*args: str) -> list[str]: + """Output lines of a git command, GitRefused naming its error otherwise.""" + result = _run(args) + if result.returncode: + detail = result.stderr.strip() or f"exit {result.returncode}" + raise GitRefused(f"git {' '.join(args)}: {detail}") + return _lines(result.stdout) + + +# Where a clone with no remote naming the project keeps its main line, each +# remote's names in the order tried. +FALLBACK_REMOTES = ("upstream", "origin") +FALLBACK_BRANCHES = ("main", "master", "v2") + + +def fallback_lines() -> list[list[str]]: + """The candidate main lines when project_main_line() finds none, per remote.""" + return [[f"{remote}/{branch}" for branch in FALLBACK_BRANCHES] for remote in FALLBACK_REMOTES] PROJECT_URLS = ( diff --git a/scripts/profile_sync.py b/scripts/profile_sync.py index 9af122678..8bfd6af9c 100644 --- a/scripts/profile_sync.py +++ b/scripts/profile_sync.py @@ -240,13 +240,12 @@ def build_parser() -> argparse.ArgumentParser: def _git_lines(args: list[str]) -> list[str]: - result = subprocess.run( - ["git", *args], capture_output=True, text=True, check=False - ) - if result.returncode: - detail = result.stderr.strip() or f"exit {result.returncode}" - raise SystemExit(f"ERROR: git {' '.join(args)}: {detail}") - return [line for line in result.stdout.splitlines() if line] + from core.gitclone import GitRefused, required_git_lines + + try: + return required_git_lines(*args) + except GitRefused as exc: + raise SystemExit(f"ERROR: {exc}") from exc def branch_profiles(emulators_dir: str, base: str, added_only: bool) -> list[str]: @@ -286,14 +285,15 @@ 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 git_lines as git_lines, project_main_line + from core.gitclone import fallback_lines, git_lines, project_main_line line = project_main_line() if line: return line - for remote in ("upstream", "origin"): - if git_lines("rev-parse", "--verify", "--quiet", f"{remote}/main"): - return f"{remote}/main" + for candidates in fallback_lines(): + for candidate in candidates: + if git_lines("rev-parse", "--verify", "--quiet", candidate): + return candidate return "HEAD" diff --git a/scripts/validate_pr.py b/scripts/validate_pr.py index 75af1eb35..be14a8ba6 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 git_lines, project_main_line +from core.gitclone import fallback_lines, git_lines, project_main_line from resolve.review import ( ValidationResult, load_platform_hashes, @@ -56,10 +56,7 @@ def get_changed_files() -> list[str]: changed: list[str] = [] compared = False project_base = project_main_line() - remotes = [[project_base]] if project_base else [ - [f"{remote}/{base}" for base in ("main", "master", "v2")] - for remote in ("upstream", "origin") - ] + 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")