mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-24 02:27:52 -06:00
refactor: one git runner and main line fallback
core.gitclone gains required_git_lines, which raises GitRefused with git's error, and fallback_lines, the remote lines tried when no remote names the project. profile_sync and validate_pr use both. profile_sync --changed without --base now also finds a clone whose main line is upstream/master, upstream/v2, origin/master or origin/v2, as validate_pr already did, before falling back to HEAD.
This commit is contained in:
1 parent
97af05db9a
commit
2f8f78b6b5
3 files changed
+46
-20
No files matched your search
@@ -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 = (
|
||||
|
||||
+11
-11
@@ -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"
|
||||
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user