mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-23 01:57:48 -06:00
110 lines
3.9 KiB
Python
110 lines
3.9 KiB
Python
"""The project's own clone: git output lines and the project's main line."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import subprocess
|
|
|
|
|
|
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)
|
|
|
|
|
|
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 = _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 = (
|
|
"https://github.com/Abdess/retrobios",
|
|
"https://github.com/Abdess/retroarch_system",
|
|
)
|
|
|
|
|
|
def _repo_key(url: str) -> str:
|
|
"""owner/name of a clone URL, whatever its scheme."""
|
|
path = url.strip().rstrip("/").removesuffix(".git")
|
|
return "/".join(re.split(r"[/:]", path)[-2:]).casefold()
|
|
|
|
|
|
def project_main_line() -> str | None:
|
|
"""The project's main line, fetched when this clone does not hold it.
|
|
|
|
A fork's clone has origin alone. Once the commit is pushed to the fork's
|
|
main, origin/main is HEAD and the diff against it was empty, so --changed
|
|
found nothing and exited 0. A remote added as CONTRIBUTING says and never
|
|
fetched did the same: upstream/main did not exist and origin/main won.
|
|
None leaves the upstream/ and origin/ lines in charge when the clone has
|
|
no remote at all and only its uncommitted files can be read.
|
|
"""
|
|
remotes = git_lines("remote") or []
|
|
if not remotes:
|
|
return None
|
|
project = {_repo_key(url) for url in PROJECT_URLS}
|
|
source = PROJECT_URLS[0]
|
|
for remote in remotes:
|
|
urls = git_lines("remote", "get-url", "--all", remote) or []
|
|
if any(_repo_key(url) in project for url in urls):
|
|
if git_lines("rev-parse", "--verify", "--quiet", f"{remote}/main"):
|
|
return f"{remote}/main"
|
|
source = remote
|
|
break
|
|
fetched = subprocess.run(
|
|
["git", "fetch", "--quiet", "--no-tags", source, "main"],
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
if fetched.returncode != 0:
|
|
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
|