Files
nvaldez 792beca2c5 daedalus 0.0.0: empty template - one CLI, many small scripts, each with tests
Sanitized open-source starter: renamed to daedalus, Norse-free of
internal references, AI attribution stripped, skill merged into
AGENTS.md, CI converted to Gitea Actions. No registered scripts yet.
2026-11-11 18:55:11 +01:00

65 lines
2.2 KiB
Python

"""Sanity checks on the files changed against the target branch.
Line endings, private keys, and file size on git changes.
"""
import logging
import os
import pathlib
import subprocess
import sys
from ci import checks
logger = logging.getLogger(__name__)
CHANGE_CHECKS = (checks.check_line_ending, checks.check_private_key, checks.check_file_size)
def git(*args: str) -> subprocess.CompletedProcess:
"""Run git in the repository root and capture its output."""
return subprocess.run(
["git", "-c", "core.quotepath=off", *args],
cwd=checks.REPO_ROOT, capture_output=True, text=True, check=False,
)
def base_ref() -> str | None:
"""Return the first ref that exists to diff against, or None."""
target = os.environ.get("SANITY_TARGET_BRANCH") or os.environ.get("GITHUB_BASE_REF")
candidates = [
os.environ.get("SANITY_DIFF_BASE"),
f"origin/{target}" if target else None,
"origin/main",
"origin/master",
]
for ref in candidates:
if ref and git("rev-parse", "--verify", "--quiet", f"{ref}^{{commit}}").returncode == 0:
return ref
return None
def changed_files() -> list[pathlib.Path]:
"""Files changed against base_ref plus untracked; all files when no base exists."""
ref = base_ref()
if ref is None:
logger.info("No base branch found; checking every tracked and untracked file")
names = git("ls-files", "--cached", "--others", "--exclude-standard").stdout.splitlines()
else:
logger.info("Checking changes against %s", ref)
names = git("diff", "--name-only", "--diff-filter=ACMR", ref).stdout.splitlines()
names += git("ls-files", "--others", "--exclude-standard").stdout.splitlines()
paths = {checks.REPO_ROOT / name for name in names if name}
return sorted(path for path in paths if path.is_file())
def main() -> int:
"""Run CHANGE_CHECKS on every changed file and return the error count."""
files = changed_files()
logger.info("Sanity on %d changed file(s)", len(files))
return sum(check(path) for path in files for check in CHANGE_CHECKS)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(levelname)-5s %(message)s")
sys.exit(main())